Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| RefreshEndpoint | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
5 | |
100.00% |
1 / 1 |
| handle | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Common\Endpoints; |
| 4 | |
| 5 | use Olz\Api\OlzTypedEndpoint; |
| 6 | use Olz\Common\Utils\AuthUtils; |
| 7 | |
| 8 | /** |
| 9 | * @extends OlzTypedEndpoint< |
| 10 | * ?array{}, |
| 11 | * array{ |
| 12 | * status: 'OK'|'KEEP'|'NO', |
| 13 | * } |
| 14 | * > |
| 15 | */ |
| 16 | class RefreshEndpoint extends OlzTypedEndpoint { |
| 17 | protected function handle(mixed $input): mixed { |
| 18 | $auth_utils = $this->authUtils(); |
| 19 | $raw_token = $this->getRequest()->cookies->get(AuthUtils::REFRESH_TOKEN_COOKIE); |
| 20 | $raw_refresh_token = is_string($raw_token) ? $raw_token : null; |
| 21 | try { |
| 22 | $new_raw_token = $auth_utils->refreshSessionFromRefreshToken($raw_refresh_token); |
| 23 | } catch (\Exception $exc) { |
| 24 | if ($raw_refresh_token) { |
| 25 | // The client presented a refresh token, but it could not be used |
| 26 | // (e.g. expired, unknown, or a re-used token). Unlike a request |
| 27 | // without any refresh token (i.e. a guest), this is worth logging. |
| 28 | $this->log()->info("Could not refresh session: {$exc->getMessage()}"); |
| 29 | } |
| 30 | $this->getResponse()->headers->setCookie($auth_utils->createRefreshTokenCookie(null)); |
| 31 | return ['status' => 'NO']; |
| 32 | } |
| 33 | if ($new_raw_token === null) { |
| 34 | return ['status' => 'KEEP']; |
| 35 | } |
| 36 | $this->getResponse()->headers->setCookie( |
| 37 | $auth_utils->createRefreshTokenCookie($new_raw_token) |
| 38 | ); |
| 39 | return ['status' => 'OK']; |
| 40 | } |
| 41 | } |