|
16 | 16 | * limitations under the License. |
17 | 17 | */ |
18 | 18 |
|
19 | | -use Doctrine\DBAL\Statement; |
| 19 | +use OpenConext\EngineBlock\Authentication\Value\ConsentHashQuery; |
| 20 | +use OpenConext\EngineBlock\Authentication\Value\ConsentStoreParameters; |
| 21 | +use OpenConext\EngineBlock\Authentication\Value\ConsentUpdateParameters; |
| 22 | +use OpenConext\EngineBlock\Authentication\Value\ConsentVersion; |
20 | 23 | use OpenConext\EngineBlock\Metadata\Entity\ServiceProvider; |
21 | 24 | use OpenConext\EngineBlock\Authentication\Value\ConsentType; |
22 | 25 | use OpenConext\EngineBlock\Service\Consent\ConsentHashServiceInterface; |
@@ -84,14 +87,36 @@ public function __construct( |
84 | 87 |
|
85 | 88 | public function explicitConsentWasGivenFor(ServiceProvider $serviceProvider): bool |
86 | 89 | { |
87 | | - return !$this->_consentEnabled || |
88 | | - $this->_hasStoredConsent($serviceProvider, ConsentType::TYPE_EXPLICIT); |
| 90 | + if (!$this->_consentEnabled) { |
| 91 | + return true; |
| 92 | + } |
| 93 | + $consent = $this->_hasStoredConsent($serviceProvider, ConsentType::TYPE_EXPLICIT); |
| 94 | + return $consent->given(); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Although the user has given consent previously we want to upgrade the deprecated unstable consent |
| 99 | + * to the new stable consent type. |
| 100 | + * https://www.pivotaltracker.com/story/show/176513931 |
| 101 | + */ |
| 102 | + public function upgradeAttributeHashFor(ServiceProvider $serviceProvider, string $consentType): void |
| 103 | + { |
| 104 | + if (!$this->_consentEnabled) { |
| 105 | + return; |
| 106 | + } |
| 107 | + $consentVersion = $this->_hasStoredConsent($serviceProvider, $consentType); |
| 108 | + if ($consentVersion->isUnstable()) { |
| 109 | + $this->_updateConsent($serviceProvider, $consentType); |
| 110 | + } |
89 | 111 | } |
90 | 112 |
|
91 | 113 | public function implicitConsentWasGivenFor(ServiceProvider $serviceProvider): bool |
92 | 114 | { |
93 | | - return !$this->_consentEnabled || |
94 | | - $this->_hasStoredConsent($serviceProvider, ConsentType::TYPE_IMPLICIT); |
| 115 | + if (!$this->_consentEnabled) { |
| 116 | + return true; |
| 117 | + } |
| 118 | + $consent = $this->_hasStoredConsent($serviceProvider, ConsentType::TYPE_IMPLICIT); |
| 119 | + return $consent->given(); |
95 | 120 | } |
96 | 121 |
|
97 | 122 | public function giveExplicitConsentFor(ServiceProvider $serviceProvider): bool |
@@ -138,38 +163,48 @@ private function _storeConsent(ServiceProvider $serviceProvider, $consentType): |
138 | 163 | return false; |
139 | 164 | } |
140 | 165 |
|
141 | | - $parameters = array( |
142 | | - sha1($consentUuid), |
143 | | - $serviceProvider->entityId, |
144 | | - $this->_getStableAttributesHash($this->_responseAttributes), |
145 | | - $consentType, |
| 166 | + $parameters = new ConsentStoreParameters( |
| 167 | + hashedUserId: sha1($consentUuid), |
| 168 | + serviceId: $serviceProvider->entityId, |
| 169 | + attributeStableHash: $this->_getStableAttributesHash($this->_responseAttributes), |
| 170 | + consentType: $consentType, |
146 | 171 | ); |
147 | 172 |
|
148 | 173 | return $this->_hashService->storeConsentHash($parameters); |
149 | 174 | } |
150 | 175 |
|
151 | | - private function _hasStoredConsent(ServiceProvider $serviceProvider, $consentType): bool |
| 176 | + private function _updateConsent(ServiceProvider $serviceProvider, $consentType): bool |
152 | 177 | { |
153 | | - $parameters = array( |
154 | | - sha1($this->_getConsentUid()), |
155 | | - $serviceProvider->entityId, |
156 | | - $this->_getAttributesHash($this->_responseAttributes), |
157 | | - $consentType, |
| 178 | + $consentUid = $this->_getConsentUid(); |
| 179 | + if (!is_string($consentUid)) { |
| 180 | + return false; |
| 181 | + } |
| 182 | + |
| 183 | + $parameters = new ConsentUpdateParameters( |
| 184 | + attributeStableHash: $this->_getStableAttributesHash($this->_responseAttributes), |
| 185 | + attributeHash: $this->_getAttributesHash($this->_responseAttributes), |
| 186 | + hashedUserId: sha1($consentUid), |
| 187 | + serviceId: $serviceProvider->entityId, |
| 188 | + consentType: $consentType, |
158 | 189 | ); |
159 | 190 |
|
160 | | - $hasUnstableConsentHash = $this->_hashService->retrieveConsentHash($parameters); |
| 191 | + return $this->_hashService->updateConsentHash($parameters); |
| 192 | + } |
161 | 193 |
|
162 | | - if ($hasUnstableConsentHash) { |
163 | | - return true; |
| 194 | + private function _hasStoredConsent(ServiceProvider $serviceProvider, $consentType): ConsentVersion |
| 195 | + { |
| 196 | + $consentUid = $this->_getConsentUid(); |
| 197 | + if (!is_string($consentUid)) { |
| 198 | + return ConsentVersion::notGiven(); |
164 | 199 | } |
165 | 200 |
|
166 | | - $parameters[2] = array( |
167 | | - sha1($this->_getConsentUid()), |
168 | | - $serviceProvider->entityId, |
169 | | - $this->_getStableAttributesHash($this->_responseAttributes), |
170 | | - $consentType, |
| 201 | + $query = new ConsentHashQuery( |
| 202 | + hashedUserId: sha1($consentUid), |
| 203 | + serviceId: $serviceProvider->entityId, |
| 204 | + attributeHash: $this->_getAttributesHash($this->_responseAttributes), |
| 205 | + attributeStableHash: $this->_getStableAttributesHash($this->_responseAttributes), |
| 206 | + consentType: $consentType, |
171 | 207 | ); |
172 | | - |
173 | | - return $this->_hashService->retrieveConsentHash($parameters); |
| 208 | + return $this->_hashService->retrieveConsentHash($query); |
174 | 209 | } |
175 | 210 | } |
0 commit comments