| 1 | <?php |
||
| 2 | |||
| 3 | namespace Parroauth2\Client\EndPoint\Introspection; |
||
| 4 | |||
| 5 | use Parroauth2\Client\ClientInterface; |
||
| 6 | use Parroauth2\Client\EndPoint\CallableEndPointInterface; |
||
| 7 | use Parroauth2\Client\EndPoint\EndPointParametersTrait; |
||
| 8 | use Parroauth2\Client\EndPoint\EndPointResponseListenerTrait; |
||
| 9 | use Parroauth2\Client\EndPoint\EndPointTransformerInterface; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Handle the token introspection |
||
| 13 | * The introspection permit to get meta-information about the current token |
||
| 14 | * |
||
| 15 | * @see https://tools.ietf.org/html/rfc7662 |
||
| 16 | * |
||
| 17 | * @implements CallableEndPointInterface<IntrospectionResponse> |
||
| 18 | */ |
||
| 19 | class IntrospectionEndPoint implements CallableEndPointInterface |
||
| 20 | { |
||
| 21 | use EndPointParametersTrait; |
||
| 22 | /** @use EndPointResponseListenerTrait<IntrospectionResponse> */ |
||
| 23 | use EndPointResponseListenerTrait; |
||
| 24 | |||
| 25 | public const NAME = 'introspection'; |
||
| 26 | |||
| 27 | public const TYPE_ACCESS_TOKEN = 'access_token'; |
||
| 28 | public const TYPE_REFRESH_TOKEN = 'refresh_token'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var ClientInterface |
||
| 32 | * @readonly |
||
| 33 | */ |
||
| 34 | private $client; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * IntrospectionEndPoint constructor. |
||
| 38 | * |
||
| 39 | * @param ClientInterface $client |
||
| 40 | */ |
||
| 41 | 142 | public function __construct(ClientInterface $client) |
|
| 42 | { |
||
| 43 | 142 | $this->client = $client; |
|
| 44 | 142 | } |
|
| 45 | |||
| 46 | /** |
||
| 47 | * {@inheritdoc} |
||
| 48 | * |
||
| 49 | * @psalm-mutation-free |
||
| 50 | */ |
||
| 51 | 142 | public function name(): string |
|
| 52 | { |
||
| 53 | 142 | return self::NAME; |
|
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * {@inheritdoc} |
||
| 58 | */ |
||
| 59 | 4 | public function apply(EndPointTransformerInterface $transformer): IntrospectionEndPoint |
|
| 60 | { |
||
| 61 | 4 | return $transformer->onIntrospection($this); |
|
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Define the token |
||
| 66 | * |
||
| 67 | * @param string $token Token to revoke. May be an access token or a refresh token |
||
| 68 | * |
||
| 69 | * @return static |
||
| 70 | * |
||
| 71 | * @psalm-mutation-free |
||
| 72 | */ |
||
| 73 | 13 | public function token(string $token): IntrospectionEndPoint |
|
| 74 | { |
||
| 75 | 13 | return $this->set('token', $token); |
|
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Define the token type |
||
| 80 | * The server can ignore this value if not match with the token |
||
| 81 | * |
||
| 82 | * @param string $type the token type |
||
| 83 | * |
||
| 84 | * @return static |
||
| 85 | * |
||
| 86 | * @psalm-mutation-free |
||
| 87 | */ |
||
| 88 | 13 | public function typeHint(string $type): IntrospectionEndPoint |
|
| 89 | { |
||
| 90 | 13 | return $this->set('token_type_hint', $type); |
|
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Request for an access token |
||
| 95 | * |
||
| 96 | * @param string $token The access token |
||
| 97 | * |
||
| 98 | * @return static |
||
| 99 | * |
||
| 100 | * @psalm-mutation-free |
||
| 101 | */ |
||
| 102 | 11 | public function accessToken(string $token): IntrospectionEndPoint |
|
| 103 | { |
||
| 104 | 11 | return $this->token($token)->typeHint(self::TYPE_ACCESS_TOKEN); |
|
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Request for a refresh token |
||
| 109 | * |
||
| 110 | * @param string $token The refresh token |
||
| 111 | * |
||
| 112 | * @return static |
||
| 113 | * |
||
| 114 | * @psalm-mutation-free |
||
| 115 | */ |
||
| 116 | 2 | public function refreshToken(string $token): IntrospectionEndPoint |
|
| 117 | { |
||
| 118 | 2 | return $this->token($token)->typeHint(self::TYPE_REFRESH_TOKEN); |
|
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * {@inheritdoc} |
||
| 123 | */ |
||
| 124 | 6 | public function call(): IntrospectionResponse |
|
| 125 | { |
||
| 126 | 6 | $request = $this->client->endPoints() |
|
| 127 | 6 | ->request('POST', $this) |
|
| 128 | 6 | ->withHeader( |
|
| 129 | 6 | 'Authorization', |
|
| 130 | 6 | 'Basic ' . base64_encode($this->client->clientId() . ':' . $this->client->secret()) |
|
| 131 | ) |
||
| 132 | ; |
||
| 133 | |||
| 134 | 6 | $body = (string) $this->client->provider()->sendRequest($request)->getBody(); |
|
| 135 | 6 | $response = new IntrospectionResponse(json_decode($body, true)); |
|
| 136 | |||
| 137 | 6 | $this->callResponseListeners($response); |
|
| 138 | |||
| 139 | 6 | return $response; |
|
|
0 ignored issues
–
show
|
|||
| 140 | } |
||
| 141 | } |
||
| 142 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: