Skip to content

Commit 5d58959

Browse files
authored
Merge pull request #1110 from lordrhodos/authorization-request-interface
Introduce AuthorizationRequestInterface
2 parents ee20fda + f1ac1a2 commit 5d58959

9 files changed

+153
-17
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99
### Added (v9)
1010
- A CryptKeyInterface to allow developers to change the CryptKey implementation with greater ease (PR #1044)
1111
- The authorization server can now finalize scopes when a client uses a refresh token (PR #1094)
12+
- An AuthorizationRequestInterface to make it easier to extend the AuthorizationRequest (PR #1110)
1213

1314
### Added
1415
- Added support for PHP 7.4 (PR #1075)

src/AuthorizationServer.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
1919
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
2020
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
21-
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
21+
use League\OAuth2\Server\RequestTypes\AuthorizationRequestInterface;
2222
use League\OAuth2\Server\ResponseTypes\AbstractResponseType;
2323
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
2424
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
@@ -148,7 +148,7 @@ public function enableGrantType(GrantTypeInterface $grantType, DateInterval $acc
148148
*
149149
* @throws OAuthServerException
150150
*
151-
* @return AuthorizationRequest
151+
* @return AuthorizationRequestInterface
152152
*/
153153
public function validateAuthorizationRequest(ServerRequestInterface $request)
154154
{
@@ -164,13 +164,15 @@ public function validateAuthorizationRequest(ServerRequestInterface $request)
164164
/**
165165
* Complete an authorization request
166166
*
167-
* @param AuthorizationRequest $authRequest
168-
* @param ResponseInterface $response
167+
* @param AuthorizationRequestInterface $authRequest
168+
* @param ResponseInterface $response
169169
*
170170
* @return ResponseInterface
171171
*/
172-
public function completeAuthorizationRequest(AuthorizationRequest $authRequest, ResponseInterface $response)
173-
{
172+
public function completeAuthorizationRequest(
173+
AuthorizationRequestInterface $authRequest,
174+
ResponseInterface $response
175+
) {
174176
return $this->enabledGrantTypes[$authRequest->getGrantTypeId()]
175177
->completeAuthorizationRequest($authRequest)
176178
->generateHttpResponse($response);

src/Grant/AbstractGrant.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
3232
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
3333
use League\OAuth2\Server\RequestEvent;
34-
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
34+
use League\OAuth2\Server\RequestTypes\AuthorizationRequestInterface;
3535
use LogicException;
3636
use Psr\Http\Message\ServerRequestInterface;
3737
use TypeError;
@@ -592,7 +592,7 @@ public function validateAuthorizationRequest(ServerRequestInterface $request)
592592
/**
593593
* {@inheritdoc}
594594
*/
595-
public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest)
595+
public function completeAuthorizationRequest(AuthorizationRequestInterface $authorizationRequest)
596596
{
597597
throw new LogicException('This grant cannot complete an authorization request');
598598
}

src/Grant/AuthCodeGrant.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
2323
use League\OAuth2\Server\RequestEvent;
2424
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
25+
use League\OAuth2\Server\RequestTypes\AuthorizationRequestInterface;
2526
use League\OAuth2\Server\ResponseTypes\RedirectResponse;
2627
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
2728
use LogicException;
@@ -327,7 +328,7 @@ function ($method) {
327328
/**
328329
* {@inheritdoc}
329330
*/
330-
public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest)
331+
public function completeAuthorizationRequest(AuthorizationRequestInterface $authorizationRequest)
331332
{
332333
if ($authorizationRequest->getUser() instanceof UserEntityInterface === false) {
333334
throw new LogicException('An instance of UserEntityInterface should be set on the AuthorizationRequest');
@@ -392,11 +393,11 @@ public function completeAuthorizationRequest(AuthorizationRequest $authorization
392393
/**
393394
* Get the client redirect URI if not set in the request.
394395
*
395-
* @param AuthorizationRequest $authorizationRequest
396+
* @param AuthorizationRequestInterface $authorizationRequest
396397
*
397398
* @return string
398399
*/
399-
private function getClientRedirectUri(AuthorizationRequest $authorizationRequest)
400+
private function getClientRedirectUri(AuthorizationRequestInterface $authorizationRequest)
400401
{
401402
return \is_array($authorizationRequest->getClient()->getRedirectUri())
402403
? $authorizationRequest->getClient()->getRedirectUri()[0]

src/Grant/GrantTypeInterface.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
1919
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
2020
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
21-
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
21+
use League\OAuth2\Server\RequestTypes\AuthorizationRequestInterface;
2222
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
2323
use Psr\Http\Message\ServerRequestInterface;
2424

@@ -74,7 +74,7 @@ public function canRespondToAuthorizationRequest(ServerRequestInterface $request
7474
*
7575
* @param ServerRequestInterface $request
7676
*
77-
* @return AuthorizationRequest
77+
* @return AuthorizationRequestInterface
7878
*/
7979
public function validateAuthorizationRequest(ServerRequestInterface $request);
8080

@@ -83,11 +83,11 @@ public function validateAuthorizationRequest(ServerRequestInterface $request);
8383
* The AuthorizationRequest object's $userId property must be set to the authenticated user and the
8484
* $authorizationApproved property must reflect their desire to authorize or deny the client.
8585
*
86-
* @param AuthorizationRequest $authorizationRequest
86+
* @param AuthorizationRequestInterface $authorizationRequest
8787
*
8888
* @return ResponseTypeInterface
8989
*/
90-
public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest);
90+
public function completeAuthorizationRequest(AuthorizationRequestInterface $authorizationRequest);
9191

9292
/**
9393
* The grant type should return true if it is able to respond to this request.

src/Grant/ImplicitGrant.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
1616
use League\OAuth2\Server\RequestEvent;
1717
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
18+
use League\OAuth2\Server\RequestTypes\AuthorizationRequestInterface;
1819
use League\OAuth2\Server\ResponseTypes\RedirectResponse;
1920
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
2021
use LogicException;
@@ -164,7 +165,7 @@ public function validateAuthorizationRequest(ServerRequestInterface $request)
164165
/**
165166
* {@inheritdoc}
166167
*/
167-
public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest)
168+
public function completeAuthorizationRequest(AuthorizationRequestInterface $authorizationRequest)
168169
{
169170
if ($authorizationRequest->getUser() instanceof UserEntityInterface === false) {
170171
throw new LogicException('An instance of UserEntityInterface should be set on the AuthorizationRequest');

src/RequestTypes/AuthorizationRequest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use League\OAuth2\Server\Entities\ScopeEntityInterface;
1414
use League\OAuth2\Server\Entities\UserEntityInterface;
1515

16-
class AuthorizationRequest
16+
class AuthorizationRequest implements AuthorizationRequestInterface
1717
{
1818
/**
1919
* The grant type identifier
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/**
3+
* @author Patrick Rodacker <[email protected]>
4+
* @copyright Copyright (c) Alex Bilbie
5+
* @license http://mit-license.org/
6+
*
7+
* @link https://github.com/thephpleague/oauth2-server
8+
*/
9+
10+
namespace League\OAuth2\Server\RequestTypes;
11+
12+
use League\OAuth2\Server\Entities\ClientEntityInterface;
13+
use League\OAuth2\Server\Entities\ScopeEntityInterface;
14+
use League\OAuth2\Server\Entities\UserEntityInterface;
15+
16+
interface AuthorizationRequestInterface
17+
{
18+
/**
19+
* @return UserEntityInterface|null
20+
*/
21+
public function getUser();
22+
23+
/**
24+
* @param string $state
25+
*/
26+
public function setState($state);
27+
28+
/**
29+
* @return ClientEntityInterface
30+
*/
31+
public function getClient();
32+
33+
/**
34+
* @param bool $authorizationApproved
35+
*/
36+
public function setAuthorizationApproved($authorizationApproved);
37+
38+
/**
39+
* @param ScopeEntityInterface[] $scopes
40+
*/
41+
public function setScopes(array $scopes);
42+
43+
/**
44+
* @param string|null $redirectUri
45+
*/
46+
public function setRedirectUri($redirectUri);
47+
48+
/**
49+
* @return string|null
50+
*/
51+
public function getRedirectUri();
52+
53+
/**
54+
* @return string
55+
*/
56+
public function getCodeChallengeMethod();
57+
58+
/**
59+
* @param string $grantTypeId
60+
*/
61+
public function setGrantTypeId($grantTypeId);
62+
63+
/**
64+
* @param UserEntityInterface $user
65+
*/
66+
public function setUser(UserEntityInterface $user);
67+
68+
/**
69+
* @param ClientEntityInterface $client
70+
*/
71+
public function setClient(ClientEntityInterface $client);
72+
73+
/**
74+
* @param string $codeChallenge
75+
*/
76+
public function setCodeChallenge($codeChallenge);
77+
78+
/**
79+
* @return bool
80+
*/
81+
public function isAuthorizationApproved();
82+
83+
/**
84+
* @return string|null
85+
*/
86+
public function getState();
87+
88+
/**
89+
* @return string
90+
*/
91+
public function getCodeChallenge();
92+
93+
/**
94+
* @param string $codeChallengeMethod
95+
*/
96+
public function setCodeChallengeMethod($codeChallengeMethod);
97+
98+
/**
99+
* @return ScopeEntityInterface[]
100+
*/
101+
public function getScopes();
102+
103+
/**
104+
* @return string
105+
*/
106+
public function getGrantTypeId();
107+
}

tests/Grant/AuthCodeGrantTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,29 @@ public function testCompleteAuthorizationRequest()
475475
$this->assertInstanceOf(RedirectResponse::class, $grant->completeAuthorizationRequest($authRequest));
476476
}
477477

478+
public function testCompleteAuthorizationRequestWithMultipleRedirectUrisOnClient()
479+
{
480+
$client = new ClientEntity();
481+
$client->setRedirectUri(['uriOne', 'uriTwo']);
482+
$authRequest = new AuthorizationRequest();
483+
$authRequest->setAuthorizationApproved(true);
484+
$authRequest->setClient($client);
485+
$authRequest->setGrantTypeId('authorization_code');
486+
$authRequest->setUser(new UserEntity());
487+
488+
$authCodeRepository = $this->getMockBuilder(AuthCodeRepositoryInterface::class)->getMock();
489+
$authCodeRepository->method('getNewAuthCode')->willReturn(new AuthCodeEntity());
490+
491+
$grant = new AuthCodeGrant(
492+
$authCodeRepository,
493+
$this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock(),
494+
new DateInterval('PT10M')
495+
);
496+
$grant->setEncryptionKey($this->cryptStub->getKey());
497+
498+
$this->assertInstanceOf(RedirectResponse::class, $grant->completeAuthorizationRequest($authRequest));
499+
}
500+
478501
public function testCompleteAuthorizationRequestDenied()
479502
{
480503
$authRequest = new AuthorizationRequest();
@@ -2019,6 +2042,7 @@ public function testPublicClientAuthCodeRequestRejectedWhenCodeChallengeRequired
20192042
'response_type' => 'code',
20202043
'client_id' => 'foo',
20212044
'redirect_uri' => 'http://foo/bar',
2045+
'state' => 'foo',
20222046
]);
20232047

20242048
$this->expectException(OAuthServerException::class);

0 commit comments

Comments
 (0)