Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Types to the Library #1375

Merged
merged 48 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
8618632
Update tests to only run on PHP 7.4, 8.0, and 8.1
Sephster Jan 17, 2022
9f39bf9
Update dependencies for PHP 7.4+
Sephster Jan 17, 2022
ec05e01
Add mock returns for revoke refresh token tests
Sephster Jan 17, 2022
19dc018
re-introduce phpstan
Sephster Jan 24, 2022
6844f4e
Add revokeRefreshTokens to GrantTypeInterface
Sephster Jan 29, 2022
39cca0b
change phpstan.neon.dist name
Sephster Feb 2, 2022
c15f5d9
Update phpstan default config
Sephster Feb 2, 2022
e7b4c4d
Ignore unresolvable phpstan error
Sephster Feb 22, 2022
2e9582d
Add getKeyContents() to CryptKeyInterface
Sephster Feb 22, 2022
e29a40c
Ensure UnencryptedToken is returned when parsing token
Sephster Feb 22, 2022
d32367b
Ignore unresolvable phpstan error
Sephster Feb 22, 2022
ece2df1
Revert accidental commit
Sephster Feb 22, 2022
9275867
Update phpstan ignore parameters
Sephster Feb 22, 2022
285d90c
Fix name of variable from allowedRedirectUri to allowedRedirectUris
Sephster Feb 22, 2022
89c89a9
update PHPStan level
Sephster May 4, 2022
14f5133
Fix condition that always evaluates to false
Sephster May 4, 2022
492b531
Revert type check on scopes as needlessly complicated
Sephster May 4, 2022
b5c67af
Remove always false check on state parameter
Sephster May 4, 2022
157c48a
Remove exception catches that aren't thrown
Sephster May 4, 2022
2f214a4
Add ReturnTypeWillChange to jsonSerialize
Sephster Nov 14, 2022
f4f2972
merge main into this branch
Sephster Nov 14, 2022
b3d8a74
Fix tests by reverting phpstan changes
Sephster Nov 15, 2022
f04eef8
Fix types
Sephster Nov 15, 2022
0e5d3dd
Fix PHPStan errors
Sephster Nov 15, 2022
c4f493f
Support PHPStan level 5
Sephster Nov 15, 2022
f67cfb6
Fix return types for CryptKeyTest and CryptTraitTest
Sephster Nov 15, 2022
b2e680a
Fix return types for StubResponseType
Sephster Nov 15, 2022
2d11620
Add return types for stubs/GrantType
Sephster Nov 15, 2022
b5ebfd6
Fix types for stubs/CryptTraitStub
Sephster Nov 15, 2022
840d350
Set types in stubs/ClientEntity
Sephster Nov 15, 2022
4244acc
Fix array type for PHPStan
Sephster Nov 15, 2022
ddd6a30
Add return types for tests
Sephster Nov 15, 2022
bf8b73a
Add return types for tests
Sephster Nov 15, 2022
f3007f9
Add return types for tests
Sephster Nov 15, 2022
bca5d67
Add types to satisfy phpstan
Sephster Dec 2, 2022
cc599af
More PHPStan fixes
Sephster Jul 19, 2023
cfcdf28
Fix PHP stan issues to level 8
Sephster Jul 25, 2023
9433aea
Fix deprecation in PHPUnit
Sephster Sep 5, 2023
068a13e
Fix tests
Sephster Sep 27, 2023
8582e35
Fix tests
Sephster Sep 28, 2023
df850f8
Fix tests
Sephster Oct 1, 2023
9533169
PHP CS fixes
Sephster Oct 2, 2023
6720d77
Remove empty comments
Sephster Oct 2, 2023
abfa828
Fix styling
Sephster Oct 13, 2023
fac8a9b
Merge in v9 branch
Sephster Oct 17, 2023
d780f13
Update minimum PHPUnit version
Sephster Oct 17, 2023
b0528f7
Tidy up ternary operator
Sephster Oct 25, 2023
a116856
Update changelog with PR number
Sephster Oct 25, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]
### Added
- GrantTypeInterface has a new function, `revokeRefreshTokens()` for enabling or disabling refresh tokens after use (PR #1375)
- A CryptKeyInterface to allow developers to change the CryptKey implementation with greater ease (PR #1044)
- The authorization server can now finalize scopes when a client uses a refresh token (PR #1094)
- An AuthorizationRequestInterface to make it easier to extend the AuthorizationRequest (PR #1110)
- Added function `getKeyContents()` to the `CryptKeyInterface` (PR #1375)

### Fixed
- If a refresh token has expired, been revoked, cannot be decrypted, or does not belong to the correct client, the server will now issue an `invalid_grant` error and a HTTP 400 response. In previous versions the server incorrectly issued an `invalid_request` and HTTP 401 response (PR #1042) (PR #1082)
Expand All @@ -17,6 +19,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Authorization Request objects are now created through the factory method, `createAuthorizationRequest()` (PR #1111)
- Changed parameters for `finalizeScopes()` to allow a reference to an auth code ID (PR #1112)

### Removed
- Removed message property from OAuthException HTTP response. Now just use error_description as per the OAuth 2 spec (PR #1375)

## [8.5.4] - released 2023-08-25
### Added
- Support for league/uri ^7.0 (PR #1367)
Expand Down
23 changes: 18 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@
"league/uri": "^6.7 || ^7.0",
"lcobucci/jwt": "^4.3 || ^5.0",
"psr/http-message": "^1.0.1 || ^2.0",
"defuse/php-encryption": "^2.3",
"defuse/php-encryption": "^2.3.1",
"lcobucci/clock": "^2.2 || ^3.0"
},
"require-dev": {
"phpunit/phpunit": "^9.6.6",
"phpunit/phpunit": "^9.6.11",
"laminas/laminas-diactoros": "^3.0.0",
"phpstan/phpstan": "^0.12.57",
"phpstan/phpstan-phpunit": "^0.12.16",
"roave/security-advisories": "dev-master"
"phpstan/phpstan": "^1.10.26",
"phpstan/phpstan-phpunit": "^1.3.14",
"roave/security-advisories": "dev-master",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan-deprecation-rules": "^1.1",
"phpstan/phpstan-strict-rules": "^1.5",
"slevomat/coding-standard": "^8.13",
"php-parallel-lint/php-parallel-lint": "^1.3",
"squizlabs/php_codesniffer": "^3.7"
},
"repositories": [
{
Expand Down Expand Up @@ -69,5 +75,12 @@
"psr-4": {
"LeagueTests\\": "tests/"
}
},
"config": {
"allow-plugins": {
"ocramius/package-versions": true,
"phpstan/extension-installer": true,
"dealerdirect/phpcodesniffer-composer-installer": false
}
}
}
8 changes: 0 additions & 8 deletions phpstan.neon

This file was deleted.

10 changes: 10 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
parameters:
level: 8
paths:
- src
- tests
ignoreErrors:
-
message: '#Call to an undefined method League\\OAuth2\\Server\\ResponseTypes\\ResponseTypeInterface::getAccessToken\(\)\.#'
path: tests/Grant/ClientCredentialsGrantTest.php
- '#Return type \(League\\Event\\EmitterInterface\|null\) of method LeagueTests\\Stubs\\GrantType::getEmitter\(\) should be covariant with return type \(League\\Event\\EmitterInterface\) of method League\\Event\\EmitterAwareInterface::getEmitter\(\)#'
118 changes: 27 additions & 91 deletions src/AuthorizationServer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @author Alex Bilbie <hello@alexbilbie.com>
* @copyright Copyright (c) Alex Bilbie
Expand All @@ -7,6 +8,8 @@
* @link https://github.com/thephpleague/oauth2-server
*/

declare(strict_types=1);

namespace League\OAuth2\Server;

use DateInterval;
Expand All @@ -32,79 +35,36 @@ class AuthorizationServer implements EmitterAwareInterface
/**
* @var GrantTypeInterface[]
*/
protected $enabledGrantTypes = [];
protected array $enabledGrantTypes = [];

/**
* @var DateInterval[]
*/
protected $grantTypeAccessTokenTTL = [];
protected array $grantTypeAccessTokenTTL = [];

/**
* @var CryptKeyInterface
*/
protected $privateKey;
protected CryptKeyInterface $privateKey;

/**
* @var CryptKeyInterface
*/
protected $publicKey;
protected CryptKeyInterface $publicKey;

/**
* @var ResponseTypeInterface
*/
protected $responseType;
protected ResponseTypeInterface $responseType;

/**
* @var ClientRepositoryInterface
*/
private $clientRepository;
private string|Key $encryptionKey;

/**
* @var AccessTokenRepositoryInterface
*/
private $accessTokenRepository;
private string $defaultScope = '';

/**
* @var ScopeRepositoryInterface
*/
private $scopeRepository;
private bool $revokeRefreshTokens = true;

/**
* @var string|Key
*/
private $encryptionKey;

/**
* @var string
*/
private $defaultScope = '';

/**
* @var bool
*/
private $revokeRefreshTokens = true;

/**
* New server instance.
*
* @param ClientRepositoryInterface $clientRepository
* @param AccessTokenRepositoryInterface $accessTokenRepository
* @param ScopeRepositoryInterface $scopeRepository
* @param CryptKeyInterface|string $privateKey
* @param string|Key $encryptionKey
* @param null|ResponseTypeInterface $responseType
* New server instance
*/
public function __construct(
ClientRepositoryInterface $clientRepository,
AccessTokenRepositoryInterface $accessTokenRepository,
ScopeRepositoryInterface $scopeRepository,
$privateKey,
$encryptionKey,
ResponseTypeInterface $responseType = null
private ClientRepositoryInterface $clientRepository,
private AccessTokenRepositoryInterface $accessTokenRepository,
private ScopeRepositoryInterface $scopeRepository,
CryptKeyInterface|string $privateKey,
Key|string $encryptionKey,
ResponseTypeInterface|null $responseType = null
) {
$this->clientRepository = $clientRepository;
$this->accessTokenRepository = $accessTokenRepository;
$this->scopeRepository = $scopeRepository;

if ($privateKey instanceof CryptKeyInterface === false) {
$privateKey = new CryptKey($privateKey);
Expand All @@ -123,12 +83,9 @@ public function __construct(
}

/**
* Enable a grant type on the server.
*
* @param GrantTypeInterface $grantType
* @param null|DateInterval $accessTokenTTL
* Enable a grant type on the server
*/
public function enableGrantType(GrantTypeInterface $grantType, DateInterval $accessTokenTTL = null)
public function enableGrantType(GrantTypeInterface $grantType, DateInterval|null $accessTokenTTL = null): void
{
if ($accessTokenTTL === null) {
$accessTokenTTL = new DateInterval('PT1H');
Expand All @@ -150,13 +107,9 @@ public function enableGrantType(GrantTypeInterface $grantType, DateInterval $acc
/**
* Validate an authorization request
*
* @param ServerRequestInterface $request
*
* @throws OAuthServerException
*
* @return AuthorizationRequestInterface
*/
public function validateAuthorizationRequest(ServerRequestInterface $request)
public function validateAuthorizationRequest(ServerRequestInterface $request): AuthorizationRequestInterface
{
foreach ($this->enabledGrantTypes as $grantType) {
if ($grantType->canRespondToAuthorizationRequest($request)) {
Expand All @@ -169,16 +122,11 @@ public function validateAuthorizationRequest(ServerRequestInterface $request)

/**
* Complete an authorization request
*
* @param AuthorizationRequestInterface $authRequest
* @param ResponseInterface $response
*
* @return ResponseInterface
*/
public function completeAuthorizationRequest(
AuthorizationRequestInterface $authRequest,
ResponseInterface $response
) {
): ResponseInterface {
return $this->enabledGrantTypes[$authRequest->getGrantTypeId()]
->completeAuthorizationRequest($authRequest)
->generateHttpResponse($response);
Expand All @@ -187,39 +135,31 @@ public function completeAuthorizationRequest(
/**
* Return an access token response.
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
*
* @throws OAuthServerException
*
* @return ResponseInterface
*/
public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseInterface $response)
public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
foreach ($this->enabledGrantTypes as $grantType) {
if (!$grantType->canRespondToAccessTokenRequest($request)) {
continue;
}

$tokenResponse = $grantType->respondToAccessTokenRequest(
$request,
$this->getResponseType(),
$this->grantTypeAccessTokenTTL[$grantType->getIdentifier()]
);

if ($tokenResponse instanceof ResponseTypeInterface) {
return $tokenResponse->generateHttpResponse($response);
}
return $tokenResponse->generateHttpResponse($response);
}

throw OAuthServerException::unsupportedGrantType();
}

/**
* Get the token type that grants will return in the HTTP response.
*
* @return ResponseTypeInterface
*/
protected function getResponseType()
protected function getResponseType(): ResponseTypeInterface
{
$responseType = clone $this->responseType;

Expand All @@ -234,18 +174,14 @@ protected function getResponseType()

/**
* Set the default scope for the authorization server.
*
* @param string $defaultScope
*/
public function setDefaultScope($defaultScope)
public function setDefaultScope(string $defaultScope): void
{
$this->defaultScope = $defaultScope;
}

/**
* Sets whether to revoke refresh tokens or not (for all grant types).
*
* @param bool $revokeRefreshTokens
*/
public function revokeRefreshTokens(bool $revokeRefreshTokens): void
{
Expand Down
13 changes: 6 additions & 7 deletions src/AuthorizationValidators/AuthorizationValidatorInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @author Alex Bilbie <hello@alexbilbie.com>
* @copyright Copyright (c) Alex Bilbie
Expand All @@ -7,19 +8,17 @@
* @link https://github.com/thephpleague/oauth2-server
*/

declare(strict_types=1);

namespace League\OAuth2\Server\AuthorizationValidators;

use Psr\Http\Message\ServerRequestInterface;

interface AuthorizationValidatorInterface
{
/**
* Determine the access token in the authorization header and append OAUth properties to the request
* as attributes.
*
* @param ServerRequestInterface $request
*
* @return ServerRequestInterface
* Determine the access token in the authorization header and append OAUth
* properties to the request as attributes.
*/
public function validateAuthorization(ServerRequestInterface $request);
public function validateAuthorization(ServerRequestInterface $request): ServerRequestInterface;
}
Loading
Loading