Skip to content

Commit 3c6e263

Browse files
committed
Add some coverage
1 parent 95d38aa commit 3c6e263

File tree

5 files changed

+172
-16
lines changed

5 files changed

+172
-16
lines changed

src/Server/AuthorizationServer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Psr\Http\Message\ServerRequestInterface;
1717
use SimpleSAML\Error\BadRequest;
1818
use SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException;
19-
use SimpleSAML\Module\oidc\Server\Grants\Interfaces\AuthorizationValidatableWithCheckerResultBagInterface;
19+
use SimpleSAML\Module\oidc\Server\Grants\Interfaces\AuthorizationValidatableWithRequestRules;
2020
use SimpleSAML\Module\oidc\Server\RequestRules\RequestRulesManager;
2121
use SimpleSAML\Module\oidc\Server\RequestRules\Rules\ClientIdRule;
2222
use SimpleSAML\Module\oidc\Server\RequestRules\Rules\IdTokenHintRule;
@@ -103,12 +103,12 @@ public function validateAuthorizationRequest(ServerRequestInterface $request): O
103103

104104
foreach ($this->enabledGrantTypes as $grantType) {
105105
if ($grantType->canRespondToAuthorizationRequest($request)) {
106-
if (! $grantType instanceof AuthorizationValidatableWithCheckerResultBagInterface) {
106+
if (! $grantType instanceof AuthorizationValidatableWithRequestRules) {
107107
throw OidcServerException::serverError('grant type must be validatable with already validated ' .
108108
'result bag');
109109
}
110110

111-
return $grantType->validateAuthorizationRequestWithCheckerResultBag($request, $resultBag);
111+
return $grantType->validateAuthorizationRequestWithRequestRules($request, $resultBag);
112112
}
113113
}
114114

src/Server/Grants/AuthCodeGrant.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
use SimpleSAML\Module\oidc\Repositories\Interfaces\AuthCodeRepositoryInterface;
3232
use SimpleSAML\Module\oidc\Repositories\Interfaces\RefreshTokenRepositoryInterface;
3333
use SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException;
34-
use SimpleSAML\Module\oidc\Server\Grants\Interfaces\AuthorizationValidatableWithCheckerResultBagInterface;
34+
use SimpleSAML\Module\oidc\Server\Grants\Interfaces\AuthorizationValidatableWithRequestRules;
3535
use SimpleSAML\Module\oidc\Server\Grants\Interfaces\OidcCapableGrantTypeInterface;
3636
use SimpleSAML\Module\oidc\Server\Grants\Interfaces\PkceEnabledGrantTypeInterface;
3737
use SimpleSAML\Module\oidc\Server\Grants\Traits\IssueAccessTokenTrait;
@@ -72,7 +72,7 @@ class AuthCodeGrant extends OAuth2AuthCodeGrant implements
7272
// phpcs:ignore
7373
OidcCapableGrantTypeInterface,
7474
// phpcs:ignore
75-
AuthorizationValidatableWithCheckerResultBagInterface
75+
AuthorizationValidatableWithRequestRules
7676
{
7777
use IssueAccessTokenTrait;
7878

@@ -641,7 +641,7 @@ protected function validateAuthorizationCode(
641641
* @inheritDoc
642642
* @throws \Throwable
643643
*/
644-
public function validateAuthorizationRequestWithCheckerResultBag(
644+
public function validateAuthorizationRequestWithRequestRules(
645645
ServerRequestInterface $request,
646646
ResultBagInterface $resultBag,
647647
): OAuth2AuthorizationRequest {

src/Server/Grants/ImplicitGrant.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use SimpleSAML\Module\oidc\Factories\Entities\AccessTokenEntityFactory;
1919
use SimpleSAML\Module\oidc\Repositories\Interfaces\AccessTokenRepositoryInterface;
2020
use SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException;
21-
use SimpleSAML\Module\oidc\Server\Grants\Interfaces\AuthorizationValidatableWithCheckerResultBagInterface;
21+
use SimpleSAML\Module\oidc\Server\Grants\Interfaces\AuthorizationValidatableWithRequestRules;
2222
use SimpleSAML\Module\oidc\Server\Grants\Traits\IssueAccessTokenTrait;
2323
use SimpleSAML\Module\oidc\Server\RequestRules\Interfaces\ResultBagInterface;
2424
use SimpleSAML\Module\oidc\Server\RequestRules\RequestRulesManager;
@@ -43,7 +43,7 @@
4343
/**
4444
* @psalm-suppress PropertyNotSetInConstructor
4545
*/
46-
class ImplicitGrant extends OAuth2ImplicitGrant implements AuthorizationValidatableWithCheckerResultBagInterface
46+
class ImplicitGrant extends OAuth2ImplicitGrant implements AuthorizationValidatableWithRequestRules
4747
{
4848
use IssueAccessTokenTrait;
4949

@@ -118,7 +118,7 @@ public function completeAuthorizationRequest(
118118
* @throws \SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException
119119
* @throws \Throwable
120120
*/
121-
public function validateAuthorizationRequestWithCheckerResultBag(
121+
public function validateAuthorizationRequestWithRequestRules(
122122
ServerRequestInterface $request,
123123
ResultBagInterface $resultBag,
124124
): OAuth2AuthorizationRequest {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
use Psr\Http\Message\ServerRequestInterface;
99
use SimpleSAML\Module\oidc\Server\RequestRules\Interfaces\ResultBagInterface;
1010

11-
interface AuthorizationValidatableWithCheckerResultBagInterface
11+
interface AuthorizationValidatableWithRequestRules
1212
{
1313
/**
1414
* Validate authorization request using an existing ResultBag instance (with already validated checkers).
1515
* This is to evade usage of original validateAuthorizationRequest() method in which it is expected to
1616
* validate client and redirect_uri (which was already validated).
1717
*/
18-
public function validateAuthorizationRequestWithCheckerResultBag(
18+
public function validateAuthorizationRequestWithRequestRules(
1919
ServerRequestInterface $request,
2020
ResultBagInterface $resultBag,
2121
): OAuth2AuthorizationRequest;

tests/unit/src/Server/Grants/ImplicitGrantTest.php

Lines changed: 161 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,171 @@
44

55
namespace SimpleSAML\Test\Module\oidc\unit\Server\Grants;
66

7+
use League\OAuth2\Server\Entities\ScopeEntityInterface;
8+
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
9+
use League\OAuth2\Server\ResponseTypes\RedirectResponse;
10+
use PHPUnit\Framework\Attributes\CoversClass;
11+
use PHPUnit\Framework\MockObject\MockObject;
712
use PHPUnit\Framework\TestCase;
13+
use Psr\Http\Message\ServerRequestInterface;
14+
use SimpleSAML\Module\oidc\Entities\ClientEntity;
15+
use SimpleSAML\Module\oidc\Entities\UserEntity;
16+
use SimpleSAML\Module\oidc\Factories\Entities\AccessTokenEntityFactory;
17+
use SimpleSAML\Module\oidc\Repositories\AccessTokenRepository;
18+
use SimpleSAML\Module\oidc\Repositories\Interfaces\AccessTokenRepositoryInterface;
19+
use SimpleSAML\Module\oidc\Server\Grants\ImplicitGrant;
20+
use SimpleSAML\Module\oidc\Server\RequestRules\Interfaces\ResultBagInterface;
21+
use SimpleSAML\Module\oidc\Server\RequestRules\RequestRulesManager;
22+
use SimpleSAML\Module\oidc\Server\RequestTypes\AuthorizationRequest;
23+
use SimpleSAML\Module\oidc\Services\IdTokenBuilder;
24+
use SimpleSAML\Module\oidc\Utils\RequestParamsResolver;
825

9-
/**
10-
* @covers \SimpleSAML\Module\oidc\Server\Grants\ImplicitGrant
11-
*/
26+
#[CoversClass(ImplicitGrant::class)]
1227
class ImplicitGrantTest extends TestCase
1328
{
14-
public function testIncomplete(): never
29+
protected MockObject $idTokenBuilderMock;
30+
protected \DateInterval $accessTokenTtl1h;
31+
protected MockObject $accessTokenRepositoryMock;
32+
protected MockObject $requestRulesManagerMock;
33+
protected MockObject $requestParamsResolverMock;
34+
protected string $queryDelimiter;
35+
protected MockObject $accessTokenEntityFactoryMock;
36+
protected MockObject $scopeRepositoryMock;
37+
protected MockObject $serverRequestMock;
38+
protected MockObject $authorizationRequestMock;
39+
protected MockObject $userEntityMock;
40+
protected MockObject $scopeEntityMock;
41+
protected MockObject $clientEntityMock;
42+
protected MockObject $resultBagMock;
43+
44+
protected function setUp(): void
45+
{
46+
$this->idTokenBuilderMock = $this->createMock(IdTokenBuilder::class);
47+
$this->accessTokenTtl1h = new \DateInterval('PT1H');
48+
$this->accessTokenRepositoryMock = $this->createMock(AccessTokenRepository::class);
49+
$this->requestRulesManagerMock = $this->createMock(RequestRulesManager::class);
50+
$this->requestParamsResolverMock = $this->createMock(RequestParamsResolver::class);
51+
$this->queryDelimiter = '#';
52+
$this->accessTokenEntityFactoryMock = $this->createMock(AccessTokenEntityFactory::class);
53+
$this->scopeRepositoryMock = $this->createMock(ScopeRepositoryInterface::class);
54+
55+
$this->serverRequestMock = $this->createMock(ServerRequestInterface::class);
56+
$this->authorizationRequestMock = $this->createMock(AuthorizationRequest::class);
57+
$this->userEntityMock = $this->createMock(UserEntity::class);
58+
$this->scopeEntityMock = $this->createMock(ScopeEntityInterface::class);
59+
$this->clientEntityMock = $this->createMock(ClientEntity::class);
60+
$this->resultBagMock = $this->createMock(ResultBagInterface::class);
61+
}
62+
63+
protected function sut(
64+
?IdTokenBuilder $idTokenBuilder = null,
65+
?\DateInterval $accessTokenTtl = null,
66+
?AccessTokenRepositoryInterface $accessTokenRepository = null,
67+
?RequestRulesManager $requestRulesManager = null,
68+
?RequestParamsResolver $requestParamsResolver = null,
69+
?string $queryDelimiter = null,
70+
?AccessTokenEntityFactory $accessTokenEntityFactory = null,
71+
?ScopeRepositoryInterface $scopeRepository = null,
72+
): ImplicitGrant {
73+
$idTokenBuilder ??= $this->idTokenBuilderMock;
74+
$accessTokenTtl ??= $this->accessTokenTtl1h;
75+
$accessTokenRepository ??= $this->accessTokenRepositoryMock;
76+
$requestRulesManager ??= $this->requestRulesManagerMock;
77+
$requestParamsResolver ??= $this->requestParamsResolverMock;
78+
$queryDelimiter ??= $this->queryDelimiter;
79+
$accessTokenEntityFactory ??= $this->accessTokenEntityFactoryMock;
80+
$scopeRepository ??= $this->scopeRepositoryMock;
81+
82+
83+
$implicitGrant = new ImplicitGrant(
84+
$idTokenBuilder,
85+
$accessTokenTtl,
86+
$accessTokenRepository,
87+
$requestRulesManager,
88+
$requestParamsResolver,
89+
$queryDelimiter,
90+
$accessTokenEntityFactory,
91+
);
92+
93+
$implicitGrant->setScopeRepository($scopeRepository);
94+
95+
return $implicitGrant;
96+
}
97+
98+
public function testCanConstruct(): void
99+
{
100+
$this->assertInstanceOf(ImplicitGrant::class, $this->sut());
101+
}
102+
103+
public function testCanRespondToAuthorizationRequestForIdTokenTokenResponseType(): void
104+
{
105+
$this->requestParamsResolverMock->expects($this->once())
106+
->method('getAllBasedOnAllowedMethods')
107+
->willReturn(['client_id' => 'clientId', 'response_type' => 'id_token token']);
108+
109+
$this->assertTrue($this->sut()->canRespondToAuthorizationRequest($this->serverRequestMock));
110+
}
111+
112+
public function testCanRespondToAuthorizationRequestForIdTokenResponseType(): void
113+
{
114+
$this->requestParamsResolverMock->expects($this->once())
115+
->method('getAllBasedOnAllowedMethods')
116+
->willReturn(['client_id' => 'clientId', 'response_type' => 'id_token']);
117+
118+
$this->assertTrue($this->sut()->canRespondToAuthorizationRequest($this->serverRequestMock));
119+
}
120+
121+
public function testCanRespondToAuthorizationRequestReturnsFalseIfNoClientId(): void
122+
{
123+
$this->requestParamsResolverMock->expects($this->once())
124+
->method('getAllBasedOnAllowedMethods')
125+
->willReturn(['response_type' => 'id_token']);
126+
127+
$this->assertFalse($this->sut()->canRespondToAuthorizationRequest($this->serverRequestMock));
128+
}
129+
130+
public function testCanRespondToAuthorizationRequestReturnsFalseForHybridFlow(): void
131+
{
132+
$this->requestParamsResolverMock->expects($this->once())
133+
->method('getAllBasedOnAllowedMethods')
134+
->willReturn(['response_type' => 'code id_token']);
135+
136+
$this->assertFalse($this->sut()->canRespondToAuthorizationRequest($this->serverRequestMock));
137+
}
138+
139+
public function testCompleteAuthorizationRequestThrowsForNonOidcRequests(): void
140+
{
141+
$this->expectException(\Exception::class);
142+
$this->expectExceptionMessage('Unexpected');
143+
144+
$this->sut()->completeAuthorizationRequest($this->createMock(
145+
\League\OAuth2\Server\RequestTypes\AuthorizationRequest::class,
146+
));
147+
}
148+
149+
public function testCanCompleteAuthorizationRequest(): void
150+
{
151+
$this->authorizationRequestMock->expects($this->once())->method('getUser')
152+
->willReturn($this->userEntityMock);
153+
$this->authorizationRequestMock->expects($this->once())->method('getRedirectUri')
154+
->willReturn('redirectUri');
155+
$this->authorizationRequestMock->expects($this->once())->method('isAuthorizationApproved')
156+
->willReturn(true);
157+
$this->authorizationRequestMock->expects($this->once())->method('getScopes')
158+
->willReturn([$this->scopeEntityMock]);
159+
$this->authorizationRequestMock->method('getClient')
160+
->willReturn($this->clientEntityMock);
161+
$this->scopeRepositoryMock->expects($this->once())->method('finalizeScopes')
162+
->willReturn([$this->scopeEntityMock]);
163+
164+
$this->assertInstanceOf(
165+
RedirectResponse::class,
166+
$this->sut()->completeAuthorizationRequest($this->authorizationRequestMock),
167+
);
168+
}
169+
170+
public function testCanValidateAuthorizationRequestWithRequestRules(): void
15171
{
16-
$this->markTestIncomplete();
172+
$this->markTestIncomplete('RequestRulesManager needs to be refactored so it can be strongly typed.');
17173
}
18174
}

0 commit comments

Comments
 (0)