|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Pdsinterop\Solid\Auth\Repository; |
| 4 | + |
| 5 | +use League\OAuth2\Server\Entities\AccessTokenEntityInterface; |
| 6 | +use League\OAuth2\Server\Entities\ClientEntityInterface; |
| 7 | +use League\OAuth2\Server\Entities\ScopeEntityInterface; |
| 8 | +use League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException; |
| 9 | +use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; |
| 10 | +use Pdsinterop\Solid\Auth\Entity\AccessToken as AccessTokenEntity; |
| 11 | + |
| 12 | +class AccessToken implements AccessTokenRepositoryInterface |
| 13 | +{ |
| 14 | + /** |
| 15 | + * Create a new access token |
| 16 | + * |
| 17 | + * @param ClientEntityInterface $clientEntity |
| 18 | + * @param ScopeEntityInterface[] $scopes |
| 19 | + * @param mixed $userIdentifier |
| 20 | + * |
| 21 | + * @return AccessTokenEntityInterface |
| 22 | + */ |
| 23 | + public function getNewToken( |
| 24 | + ClientEntityInterface $clientEntity, |
| 25 | + array $scopes, |
| 26 | + $userIdentifier = null |
| 27 | + ) : AccessTokenEntityInterface { |
| 28 | + return new AccessTokenEntity($clientEntity); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Persists a new access token to permanent storage. |
| 33 | + * |
| 34 | + * @param AccessTokenEntityInterface $accessTokenEntity |
| 35 | + * |
| 36 | + * @throws UniqueTokenIdentifierConstraintViolationException |
| 37 | + */ |
| 38 | + public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity) : void |
| 39 | + { |
| 40 | + // throw new UniqueTokenIdentifierConstraintViolationException() |
| 41 | + /*/ |
| 42 | + When a new access token is created this method will be called. You don’t have to do anything here but for auditing you probably want to. |
| 43 | +
|
| 44 | + The access token entity passed in has a number of methods you can call which contain data worth saving to a database: |
| 45 | +
|
| 46 | + getIdentifier() : string this is randomly generated unique identifier (of 80+ characters in length) for the access token. |
| 47 | + getExpiryDateTime() : \DateTime the expiry date and time of the access token. |
| 48 | + getUserIdentifier() : string|null the user identifier represented by the access token. |
| 49 | + getScopes() : ScopeEntityInterface[] an array of scope entities |
| 50 | + getClient()->getIdentifier() : string the identifier of the client who requested the access token. |
| 51 | +
|
| 52 | + JWT access tokens contain an expiry date and so will be rejected automatically when used. You can safely clean up expired access tokens from your database. |
| 53 | + /*/ |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Revoke an access token. |
| 58 | + * |
| 59 | + * @param string $tokenId |
| 60 | + */ |
| 61 | + public function revokeAccessToken($tokenId) : void |
| 62 | + { |
| 63 | + /*/ |
| 64 | + This method is called when a refresh token is used to reissue an access token. |
| 65 | +
|
| 66 | + The original access token is revoked a new access token is issued. |
| 67 | + /*/ |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Check if the access token has been revoked. |
| 72 | + * |
| 73 | + * @param string $tokenId |
| 74 | + * |
| 75 | + * @return bool Return true if this token has been revoked |
| 76 | + */ |
| 77 | + public function isAccessTokenRevoked($tokenId) : bool |
| 78 | + { |
| 79 | + /*/ |
| 80 | + This method is called when an access token is validated by the resource server middleware. |
| 81 | +
|
| 82 | + Return true if the access token has been manually revoked before it expired. |
| 83 | +
|
| 84 | + If the token is still valid return false. |
| 85 | + /*/ |
| 86 | + return false; |
| 87 | + } |
| 88 | +} |
0 commit comments