|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Test\Module\oidc\unit\Server\RequestRules\Rules; |
| 6 | + |
| 7 | +use PHPUnit\Framework\MockObject\MockObject; |
| 8 | +use PHPUnit\Framework\MockObject\Stub; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Psr\Http\Message\ServerRequestInterface; |
| 11 | +use SimpleSAML\Module\oidc\Entities\Interfaces\ClientEntityInterface; |
| 12 | +use SimpleSAML\Module\oidc\Helpers; |
| 13 | +use SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException; |
| 14 | +use SimpleSAML\Module\oidc\Server\RequestRules\Interfaces\ResultInterface; |
| 15 | +use SimpleSAML\Module\oidc\Server\RequestRules\Result; |
| 16 | +use SimpleSAML\Module\oidc\Server\RequestRules\ResultBag; |
| 17 | +use SimpleSAML\Module\oidc\Server\RequestRules\Rules\ClientAuthenticationRule; |
| 18 | +use SimpleSAML\Module\oidc\Server\RequestRules\Rules\ClientRule; |
| 19 | +use SimpleSAML\Module\oidc\Server\ResponseModes\ResponseModeInterface; |
| 20 | +use SimpleSAML\Module\oidc\Services\LoggerService; |
| 21 | +use SimpleSAML\Module\oidc\Utils\AuthenticatedOAuth2ClientResolver; |
| 22 | +use SimpleSAML\Module\oidc\Utils\RequestParamsResolver; |
| 23 | +use SimpleSAML\Module\oidc\ValueAbstracts\ResolvedClientAuthenticationMethod; |
| 24 | +use SimpleSAML\OpenID\Codebooks\ClientAuthenticationMethodsEnum; |
| 25 | + |
| 26 | +/** |
| 27 | + * @covers \SimpleSAML\Module\oidc\Server\RequestRules\Rules\ClientAuthenticationRule |
| 28 | + */ |
| 29 | +class ClientAuthenticationRuleTest extends TestCase |
| 30 | +{ |
| 31 | + protected ResultBag $resultBag; |
| 32 | + protected Stub $clientStub; |
| 33 | + protected Stub $requestStub; |
| 34 | + protected Stub $loggerServiceStub; |
| 35 | + protected MockObject $requestParamsResolverMock; |
| 36 | + protected Helpers $helpers; |
| 37 | + protected MockObject $authenticatedOAuth2ClientResolverMock; |
| 38 | + protected Stub $responseModeStub; |
| 39 | + |
| 40 | + protected function setUp(): void |
| 41 | + { |
| 42 | + $this->resultBag = new ResultBag(); |
| 43 | + $this->clientStub = $this->createStub(ClientEntityInterface::class); |
| 44 | + $this->requestStub = $this->createStub(ServerRequestInterface::class); |
| 45 | + $this->loggerServiceStub = $this->createStub(LoggerService::class); |
| 46 | + $this->requestParamsResolverMock = $this->createMock(RequestParamsResolver::class); |
| 47 | + $this->helpers = new Helpers(); |
| 48 | + $this->authenticatedOAuth2ClientResolverMock = $this->createMock(AuthenticatedOAuth2ClientResolver::class); |
| 49 | + $this->responseModeStub = $this->createStub(ResponseModeInterface::class); |
| 50 | + } |
| 51 | + |
| 52 | + protected function sut( |
| 53 | + ?RequestParamsResolver $requestParamsResolver = null, |
| 54 | + ?Helpers $helpers = null, |
| 55 | + ?AuthenticatedOAuth2ClientResolver $authenticatedOAuth2ClientResolver = null, |
| 56 | + ): ClientAuthenticationRule { |
| 57 | + $requestParamsResolver ??= $this->requestParamsResolverMock; |
| 58 | + $helpers ??= $this->helpers; |
| 59 | + $authenticatedOAuth2ClientResolver ??= $this->authenticatedOAuth2ClientResolverMock; |
| 60 | + |
| 61 | + return new ClientAuthenticationRule( |
| 62 | + $requestParamsResolver, |
| 63 | + $helpers, |
| 64 | + $authenticatedOAuth2ClientResolver, |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * A client already resolved by an upstream rule (ClientRule) is used as the pre-fetched client, without |
| 70 | + * touching the client_id request parameter. |
| 71 | + * |
| 72 | + * @throws \Throwable |
| 73 | + */ |
| 74 | + public function testUsesPreFetchedClientFromResultBag(): void |
| 75 | + { |
| 76 | + $this->resultBag->add(new Result(ClientRule::class, $this->clientStub)); |
| 77 | + |
| 78 | + $resolved = new ResolvedClientAuthenticationMethod( |
| 79 | + $this->clientStub, |
| 80 | + ClientAuthenticationMethodsEnum::ClientSecretBasic, |
| 81 | + ); |
| 82 | + |
| 83 | + // The client_id param fallback must not be consulted when a client is already available. |
| 84 | + $this->requestParamsResolverMock->expects($this->never()) |
| 85 | + ->method('getAsStringBasedOnAllowedMethods'); |
| 86 | + $this->authenticatedOAuth2ClientResolverMock->expects($this->never()) |
| 87 | + ->method('findActiveClient'); |
| 88 | + |
| 89 | + $this->authenticatedOAuth2ClientResolverMock->expects($this->once()) |
| 90 | + ->method('forAnySupportedMethod') |
| 91 | + ->with($this->identicalTo($this->requestStub), $this->identicalTo($this->clientStub)) |
| 92 | + ->willReturn($resolved); |
| 93 | + |
| 94 | + $result = $this->sut()->checkRule( |
| 95 | + $this->requestStub, |
| 96 | + $this->resultBag, |
| 97 | + $this->loggerServiceStub, |
| 98 | + [], |
| 99 | + $this->responseModeStub, |
| 100 | + ); |
| 101 | + |
| 102 | + $this->assertInstanceOf(ResultInterface::class, $result); |
| 103 | + $this->assertSame($resolved, $result->getValue()); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * When no upstream client is available but a client_id param is present, it is used to pre-fetch the client. |
| 108 | + * |
| 109 | + * @throws \Throwable |
| 110 | + */ |
| 111 | + public function testFallsBackToClientIdParameterWhenPresent(): void |
| 112 | + { |
| 113 | + $this->requestParamsResolverMock->method('getAsStringBasedOnAllowedMethods') |
| 114 | + ->willReturn('client123'); |
| 115 | + |
| 116 | + $this->authenticatedOAuth2ClientResolverMock->expects($this->once()) |
| 117 | + ->method('findActiveClient') |
| 118 | + ->with('client123') |
| 119 | + ->willReturn($this->clientStub); |
| 120 | + |
| 121 | + $resolved = new ResolvedClientAuthenticationMethod( |
| 122 | + $this->clientStub, |
| 123 | + ClientAuthenticationMethodsEnum::ClientSecretPost, |
| 124 | + ); |
| 125 | + |
| 126 | + $this->authenticatedOAuth2ClientResolverMock->expects($this->once()) |
| 127 | + ->method('forAnySupportedMethod') |
| 128 | + ->with($this->identicalTo($this->requestStub), $this->identicalTo($this->clientStub)) |
| 129 | + ->willReturn($resolved); |
| 130 | + |
| 131 | + $result = $this->sut()->checkRule( |
| 132 | + $this->requestStub, |
| 133 | + $this->resultBag, |
| 134 | + $this->loggerServiceStub, |
| 135 | + [], |
| 136 | + $this->responseModeStub, |
| 137 | + ); |
| 138 | + |
| 139 | + $this->assertInstanceOf(ResultInterface::class, $result); |
| 140 | + $this->assertSame($resolved, $result->getValue()); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * The core of the fix: with no upstream client and no client_id parameter (e.g. private_key_jwt, where the |
| 145 | + * identity is conveyed by the assertion), the rule must still authenticate by letting the resolver derive the |
| 146 | + * client from the presented authentication material - it must NOT reject the request for a missing client_id. |
| 147 | + * |
| 148 | + * @throws \Throwable |
| 149 | + */ |
| 150 | + public function testDoesNotRequireClientIdParameter(): void |
| 151 | + { |
| 152 | + $this->requestParamsResolverMock->method('getAsStringBasedOnAllowedMethods') |
| 153 | + ->willReturn(null); |
| 154 | + |
| 155 | + // With no client_id param, no pre-fetch lookup must happen... |
| 156 | + $this->authenticatedOAuth2ClientResolverMock->expects($this->never()) |
| 157 | + ->method('findActiveClient'); |
| 158 | + |
| 159 | + $resolved = new ResolvedClientAuthenticationMethod( |
| 160 | + $this->clientStub, |
| 161 | + ClientAuthenticationMethodsEnum::PrivateKeyJwt, |
| 162 | + ); |
| 163 | + |
| 164 | + // ...and the resolver is invoked with a null pre-fetched client. |
| 165 | + $this->authenticatedOAuth2ClientResolverMock->expects($this->once()) |
| 166 | + ->method('forAnySupportedMethod') |
| 167 | + ->with($this->identicalTo($this->requestStub), null) |
| 168 | + ->willReturn($resolved); |
| 169 | + |
| 170 | + $result = $this->sut()->checkRule( |
| 171 | + $this->requestStub, |
| 172 | + $this->resultBag, |
| 173 | + $this->loggerServiceStub, |
| 174 | + [], |
| 175 | + $this->responseModeStub, |
| 176 | + ); |
| 177 | + |
| 178 | + $this->assertInstanceOf(ResultInterface::class, $result); |
| 179 | + $this->assertSame($resolved, $result->getValue()); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * If the resolver can not authenticate the client by any supported method, the request is denied. |
| 184 | + * |
| 185 | + * @throws \Throwable |
| 186 | + */ |
| 187 | + public function testThrowsWhenNoAuthenticationMethodResolved(): void |
| 188 | + { |
| 189 | + $this->requestParamsResolverMock->method('getAsStringBasedOnAllowedMethods') |
| 190 | + ->willReturn(null); |
| 191 | + |
| 192 | + $this->authenticatedOAuth2ClientResolverMock->method('forAnySupportedMethod') |
| 193 | + ->willReturn(null); |
| 194 | + |
| 195 | + $this->expectException(OidcServerException::class); |
| 196 | + |
| 197 | + $this->sut()->checkRule( |
| 198 | + $this->requestStub, |
| 199 | + $this->resultBag, |
| 200 | + $this->loggerServiceStub, |
| 201 | + [], |
| 202 | + $this->responseModeStub, |
| 203 | + ); |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * A confidential client must not authenticate using the 'none' method. |
| 208 | + * |
| 209 | + * @throws \Throwable |
| 210 | + */ |
| 211 | + public function testThrowsWhenConfidentialClientUsesNone(): void |
| 212 | + { |
| 213 | + $this->resultBag->add(new Result(ClientRule::class, $this->clientStub)); |
| 214 | + $this->clientStub->method('isConfidential')->willReturn(true); |
| 215 | + |
| 216 | + $resolved = new ResolvedClientAuthenticationMethod( |
| 217 | + $this->clientStub, |
| 218 | + ClientAuthenticationMethodsEnum::None, |
| 219 | + ); |
| 220 | + |
| 221 | + $this->authenticatedOAuth2ClientResolverMock->method('forAnySupportedMethod') |
| 222 | + ->willReturn($resolved); |
| 223 | + |
| 224 | + $this->expectException(OidcServerException::class); |
| 225 | + |
| 226 | + $this->sut()->checkRule( |
| 227 | + $this->requestStub, |
| 228 | + $this->resultBag, |
| 229 | + $this->loggerServiceStub, |
| 230 | + [], |
| 231 | + $this->responseModeStub, |
| 232 | + ); |
| 233 | + } |
| 234 | +} |
0 commit comments