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

Fix Passport Compatibility for V9 #1402

Merged
merged 19 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
2 changes: 2 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<file>tests</file>
<file>examples</file>

<exclude-pattern>examples/vendor/*</exclude-pattern>

<rule ref="PSR12">
<exclude name="Generic.Files.LineLength.TooLong" />
</rule>
Expand Down
4 changes: 2 additions & 2 deletions src/Entities/ClientEntityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ interface ClientEntityInterface
/**
* Get the client's identifier.
*
* @return non-empty-string
* @return int|non-empty-string
*/
public function getIdentifier(): string;
public function getIdentifier(): int|string;

/**
* Get the client's name.
Expand Down
8 changes: 6 additions & 2 deletions src/Entities/RefreshTokenEntityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ interface RefreshTokenEntityInterface
{
/**
* Get the token's identifier.
*
* @return int|non-empty-string
*/
public function getIdentifier(): string;
public function getIdentifier(): int|string;

/**
* Set the token's identifier.
*
* @param int|non-empty-string $identifier
*/
public function setIdentifier(mixed $identifier): void;
public function setIdentifier(int|string $identifier): void;

/**
* Get the token's expiry date time.
Expand Down
4 changes: 3 additions & 1 deletion src/Entities/ScopeEntityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ interface ScopeEntityInterface extends JsonSerializable
{
/**
* Get the scope's identifier.
*
* @return int|non-empty-string
*/
public function getIdentifier(): string;
public function getIdentifier(): int|string;
}
14 changes: 10 additions & 4 deletions src/Entities/TokenInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ interface TokenInterface
{
/**
* Get the token's identifier.
*
* @return int|non-empty-string
*/
public function getIdentifier(): string;
public function getIdentifier(): int|string;

/**
* Set the token's identifier.
*
* @param int|non-empty-string $identifier
*/
public function setIdentifier(mixed $identifier): void;
public function setIdentifier(int|string $identifier): void;

/**
* Get the token's expiry date time.
Expand All @@ -39,12 +43,14 @@ public function setExpiryDateTime(DateTimeImmutable $dateTime): void;
/**
* Set the identifier of the user associated with the token.
*
* @param non-empty-string $identifier
* @param non-empty-string|int $identifier
*/
public function setUserIdentifier(string $identifier): void;
public function setUserIdentifier(string|int $identifier): void;

/**
* Get the token user's identifier.
*
* @return non-empty-string|int|null
*/
public function getUserIdentifier(): string|int|null;

Expand Down
18 changes: 9 additions & 9 deletions src/Entities/Traits/AccessTokenTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ private function convertToJWT(): Token
$this->initJwtConfiguration();

return $this->jwtConfiguration->builder()
->permittedFor($this->getClient()->getIdentifier())
->identifiedBy($this->getIdentifier())
->permittedFor((string) $this->getClient()->getIdentifier())
->identifiedBy((string) $this->getIdentifier())
->issuedAt(new DateTimeImmutable())
->canOnlyBeUsedAfter(new DateTimeImmutable())
->expiresAt($this->getExpiryDateTime())
->relatedTo($this->getSubjectIdentifier())
->relatedTo((string) $this->getSubjectIdentifier())
->withClaim('scopes', $this->getScopes())
->getToken($this->jwtConfiguration->signer(), $this->jwtConfiguration->signingKey());
}
Expand All @@ -85,24 +85,24 @@ abstract public function getClient(): ClientEntityInterface;
abstract public function getExpiryDateTime(): DateTimeImmutable;

/**
* @return non-empty-string|null
* @return non-empty-string|int|null
*/
abstract public function getUserIdentifier(): string|null;
abstract public function getUserIdentifier(): string|int|null;

/**
* @return ScopeEntityInterface[]
*/
abstract public function getScopes(): array;

/**
* @return non-empty-string
* @return int|non-empty-string
*/
abstract public function getIdentifier(): string;
abstract public function getIdentifier(): int|string;

/**
* @return non-empty-string
* @return int|non-empty-string
*/
private function getSubjectIdentifier(): string
private function getSubjectIdentifier(): int|string
{
return $this->getUserIdentifier() ?? $this->getClient()->getIdentifier();
}
Expand Down
5 changes: 4 additions & 1 deletion src/Entities/Traits/DeviceCodeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ abstract public function getExpiryDateTime(): DateTimeImmutable;
*/
abstract public function getScopes(): array;

abstract public function getIdentifier(): string;
/**
* @return int|non-empty-string
*/
abstract public function getIdentifier(): int|string;

public function getLastPolledAt(): ?DateTimeImmutable
{
Expand Down
11 changes: 7 additions & 4 deletions src/Entities/Traits/EntityTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@
trait EntityTrait
{
/**
* @var non-empty-string
* @var int|non-empty-string
*/
protected string $identifier;
protected int|string $identifier;

/**
* @return non-empty-string
*/
public function getIdentifier(): string
public function getIdentifier(): int|string
{
return $this->identifier;
}

public function setIdentifier(mixed $identifier): void
/**
* @param int|non-empty-string $identifier
*/
public function setIdentifier(int|string $identifier): void
{
$this->identifier = $identifier;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Entities/Traits/TokenEntityTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ trait TokenEntityTrait
protected DateTimeImmutable $expiryDateTime;

/**
* @var non-empty-string|null
* @var non-empty-string|int|null
*/
protected string|null $userIdentifier = null;
protected string|int|null $userIdentifier = null;

protected ClientEntityInterface $client;

Expand Down Expand Up @@ -71,19 +71,19 @@ public function setExpiryDateTime(DateTimeImmutable $dateTime): void
/**
* Set the identifier of the user associated with the token.
*
* @param non-empty-string $identifier The identifier of the user
* @param int|non-empty-string $identifier The identifier of the user
*/
public function setUserIdentifier(string $identifier): void
public function setUserIdentifier(int|string $identifier): void
{
$this->userIdentifier = $identifier;
}

/**
* Get the token user's identifier.
*
* @return non-empty-string|null
* @return non-empty-string|int|null
*/
public function getUserIdentifier(): string|null
public function getUserIdentifier(): string|int|null
{
return $this->userIdentifier;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Exception/OAuthServerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,16 @@ public static function invalidClient(ServerRequestInterface $serverRequest): sta
/**
* Invalid scope error
*/
public static function invalidScope(string $scope, string|null $redirectUri = null): static
public static function invalidScope(int|string $scopeId, string|null $redirectUri = null): static
{
$errorMessage = 'The requested scope is invalid, unknown, or malformed';

if ($scope === '') {
if ($scopeId === '') {
$hint = 'Specify a scope in the request or set a default scope';
} else {
$hint = sprintf(
'Check the `%s` scope',
htmlspecialchars($scope, ENT_QUOTES, 'UTF-8', false)
htmlspecialchars((string) $scopeId, ENT_QUOTES, 'UTF-8', false)
);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Grant/AbstractGrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ protected function validateClient(ServerRequestInterface $request): ClientEntity
* getClientEntity might return null. By contrast, this method will
* always either return a ClientEntityInterface or throw.
*/
protected function getClientEntityOrFail(string $clientId, ServerRequestInterface $request): ClientEntityInterface
protected function getClientEntityOrFail(string|int $clientId, ServerRequestInterface $request): ClientEntityInterface
{
$client = $this->clientRepository->getClientEntity($clientId);

Expand Down Expand Up @@ -473,6 +473,8 @@ protected function issueRefreshToken(AccessTokenEntityInterface $accessToken): ?
/**
* Generate a new unique identifier.
*
* @return non-empty-string
*
* @throws OAuthServerException
*/
protected function generateUniqueIdentifier(int $length = 40): string
Expand Down
3 changes: 2 additions & 1 deletion src/Grant/RefreshTokenGrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function respondToAccessTokenRequest(
// Validate request
$client = $this->validateClient($request);
$oldRefreshToken = $this->validateOldRefreshToken($request, $client->getIdentifier());

$scopes = $this->validateScopes(
$this->getRequestParameter(
'scope',
Expand Down Expand Up @@ -100,7 +101,7 @@ public function respondToAccessTokenRequest(
*
* @return array<string, mixed>
*/
protected function validateOldRefreshToken(ServerRequestInterface $request, string $clientId): array
protected function validateOldRefreshToken(ServerRequestInterface $request, int|string $clientId): array
{
$encryptedRefreshToken = $this->getRequestParameter('refresh_token', $request);
if (!is_string($encryptedRefreshToken)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Repositories/AccessTokenRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface AccessTokenRepositoryInterface extends RepositoryInterface
public function getNewToken(
ClientEntityInterface $clientEntity,
array $scopes,
mixed $userIdentifier = null
string|int|null $userIdentifier = null
): AccessTokenEntityInterface;

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Repositories/ClientRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ interface ClientRepositoryInterface extends RepositoryInterface
/**
* Get a client.
*/
public function getClientEntity(string $clientIdentifier): ?ClientEntityInterface;
public function getClientEntity(string|int $clientIdentifier): ?ClientEntityInterface;

/**
* Validate a client's secret.
*/
public function validateClient(string $clientIdentifier, ?string $clientSecret, ?string $grantType): bool;
public function validateClient(string|int $clientIdentifier, ?string $clientSecret, ?string $grantType): bool;
}
2 changes: 1 addition & 1 deletion src/Repositories/DeviceCodeRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function getDeviceCodeEntityByDeviceCode(
/**
* Revoke a device code.
*/
public function revokeDeviceCode(string $codeId): void;
public function revokeDeviceCode(int|string $codeId): void;

/**
* Check if the device code has been revoked.
Expand Down
2 changes: 1 addition & 1 deletion tests/Stubs/ScopeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ class ScopeEntity implements ScopeEntityInterface
#[ReturnTypeWillChange]
public function jsonSerialize(): string
{
return $this->getIdentifier();
return (string) $this->getIdentifier();
}
}