|
23 | 23 | use SimpleSAML\Module\oidc\Services\IdTokenBuilder; |
24 | 24 | use SimpleSAML\Module\oidc\Services\LoggerService; |
25 | 25 | use SimpleSAML\Module\oidc\Utils\RequestParamsResolver; |
| 26 | +use SimpleSAML\OpenID\Core\IdToken; |
26 | 27 |
|
27 | 28 | #[CoversClass(ImplicitGrant::class)] |
28 | 29 | class ImplicitGrantTest extends TestCase |
@@ -168,6 +169,76 @@ public function testCanCompleteAuthorizationRequest(): void |
168 | 169 | ); |
169 | 170 | } |
170 | 171 |
|
| 172 | + /** |
| 173 | + * For id_token token the AddClaimsToIdTokenRule does not request the claims (the rule only matches the exact |
| 174 | + * "id_token" response type), but a client configured with the administrator-only `add_claims_to_id_token` |
| 175 | + * option still gets the user's claims released in the ID Token. |
| 176 | + */ |
| 177 | + public function testReleasesUserClaimsInIdTokenWhenClientConfiguredTo(): void |
| 178 | + { |
| 179 | + $this->authorizationRequestMock->method('getUser')->willReturn($this->userEntityMock); |
| 180 | + $this->authorizationRequestMock->method('getRedirectUri')->willReturn('redirectUri'); |
| 181 | + $this->authorizationRequestMock->method('isAuthorizationApproved')->willReturn(true); |
| 182 | + $this->authorizationRequestMock->method('getScopes')->willReturn([$this->scopeEntityMock]); |
| 183 | + $this->authorizationRequestMock->method('getClient')->willReturn($this->clientEntityMock); |
| 184 | + // Response-type rule did NOT request the claims (e.g. id_token token), but the client opts in. |
| 185 | + $this->authorizationRequestMock->method('getAddClaimsToIdToken')->willReturn(false); |
| 186 | + $this->clientEntityMock->method('getAddClaimsToIdToken')->willReturn(true); |
| 187 | + $this->scopeRepositoryMock->method('finalizeScopes')->willReturn([$this->scopeEntityMock]); |
| 188 | + |
| 189 | + $idTokenMock = $this->createMock(IdToken::class); |
| 190 | + $idTokenMock->method('getToken')->willReturn('token'); |
| 191 | + $this->idTokenBuilderMock->expects($this->once()) |
| 192 | + ->method('buildFor') |
| 193 | + ->with( |
| 194 | + $this->anything(), |
| 195 | + $this->anything(), |
| 196 | + true, // $addClaimsFromScopes |
| 197 | + $this->anything(), |
| 198 | + $this->anything(), |
| 199 | + $this->anything(), |
| 200 | + $this->anything(), |
| 201 | + $this->anything(), |
| 202 | + ) |
| 203 | + ->willReturn($idTokenMock); |
| 204 | + |
| 205 | + $this->sut()->completeAuthorizationRequest($this->authorizationRequestMock); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * When neither the response type nor the client requests it, the user's claims are not released in the ID |
| 210 | + * Token (they remain available at the UserInfo endpoint via the issued access token). |
| 211 | + */ |
| 212 | + public function testDoesNotReleaseUserClaimsInIdTokenByDefault(): void |
| 213 | + { |
| 214 | + $this->authorizationRequestMock->method('getUser')->willReturn($this->userEntityMock); |
| 215 | + $this->authorizationRequestMock->method('getRedirectUri')->willReturn('redirectUri'); |
| 216 | + $this->authorizationRequestMock->method('isAuthorizationApproved')->willReturn(true); |
| 217 | + $this->authorizationRequestMock->method('getScopes')->willReturn([$this->scopeEntityMock]); |
| 218 | + $this->authorizationRequestMock->method('getClient')->willReturn($this->clientEntityMock); |
| 219 | + $this->authorizationRequestMock->method('getAddClaimsToIdToken')->willReturn(false); |
| 220 | + $this->clientEntityMock->method('getAddClaimsToIdToken')->willReturn(false); |
| 221 | + $this->scopeRepositoryMock->method('finalizeScopes')->willReturn([$this->scopeEntityMock]); |
| 222 | + |
| 223 | + $idTokenMock = $this->createMock(IdToken::class); |
| 224 | + $idTokenMock->method('getToken')->willReturn('token'); |
| 225 | + $this->idTokenBuilderMock->expects($this->once()) |
| 226 | + ->method('buildFor') |
| 227 | + ->with( |
| 228 | + $this->anything(), |
| 229 | + $this->anything(), |
| 230 | + false, // $addClaimsFromScopes |
| 231 | + $this->anything(), |
| 232 | + $this->anything(), |
| 233 | + $this->anything(), |
| 234 | + $this->anything(), |
| 235 | + $this->anything(), |
| 236 | + ) |
| 237 | + ->willReturn($idTokenMock); |
| 238 | + |
| 239 | + $this->sut()->completeAuthorizationRequest($this->authorizationRequestMock); |
| 240 | + } |
| 241 | + |
171 | 242 | public function testCanValidateAuthorizationRequestWithRequestRules(): void |
172 | 243 | { |
173 | 244 | $this->markTestIncomplete('RequestRulesManager needs to be refactored so it can be strongly typed.'); |
|
0 commit comments