|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Phalcon API. |
| 5 | + * |
| 6 | + * (c) Phalcon Team <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Phalcon\Api\Domain\Services\Auth; |
| 15 | + |
| 16 | +use PayloadInterop\DomainStatus; |
| 17 | +use Phalcon\Api\Domain\ADR\InputTypes; |
| 18 | +use Phalcon\Api\Domain\Components\DataSource\User\UserTypes; |
| 19 | +use Phalcon\Api\Domain\Components\Enums\Common\JWTEnum; |
| 20 | +use Phalcon\Api\Domain\Components\Enums\Http\HttpCodesEnum; |
| 21 | +use Phalcon\Domain\Payload; |
| 22 | + |
| 23 | +/** |
| 24 | + * @phpstan-import-type TUserDbRecord from UserTypes |
| 25 | + * @phpstan-import-type TLogoutInput from InputTypes |
| 26 | + * @phpstan-import-type TValidationErrors from InputTypes |
| 27 | + */ |
| 28 | +final class LogoutPostService extends AbstractAuthService |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @param TLogoutInput $input |
| 32 | + * |
| 33 | + * @return Payload |
| 34 | + */ |
| 35 | + public function __invoke(array $input): Payload |
| 36 | + { |
| 37 | + /** |
| 38 | + * @todo common code with refresh |
| 39 | + */ |
| 40 | + /** |
| 41 | + * Get the token |
| 42 | + */ |
| 43 | + $token = (string)($input['token'] ?? ''); |
| 44 | + $token = $this->filter->string($token); |
| 45 | + |
| 46 | + /** |
| 47 | + * Validation |
| 48 | + * |
| 49 | + * Empty token |
| 50 | + */ |
| 51 | + if (true === empty($token)) { |
| 52 | + return $this->getUnauthorizedPayload( |
| 53 | + [HttpCodesEnum::AppTokenNotPresent->error()] |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @todo catch any exceptions here |
| 59 | + * |
| 60 | + * Is this the refresh token |
| 61 | + */ |
| 62 | + $tokenObject = $this->jwtToken->getObject($token); |
| 63 | + $isRefresh = $tokenObject->getClaims()->get(JWTEnum::Refresh->value); |
| 64 | + if (false === $isRefresh) { |
| 65 | + return $this->getUnauthorizedPayload( |
| 66 | + [HttpCodesEnum::AppTokenNotValid->error()] |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Get the user - if empty return error |
| 72 | + */ |
| 73 | + $user = $this |
| 74 | + ->jwtToken |
| 75 | + ->getUser($this->repository, $tokenObject) |
| 76 | + ; |
| 77 | + if (true === empty($user)) { |
| 78 | + return $this->getUnauthorizedPayload( |
| 79 | + [HttpCodesEnum::AppTokenInvalidUser->error()] |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + $domainUser = $this->transport->newUser($user); |
| 84 | + |
| 85 | + /** @var TValidationErrors $errors */ |
| 86 | + $errors = $this->jwtToken->validate($tokenObject, $domainUser); |
| 87 | + if (true !== empty($errors)) { |
| 88 | + return $this->getUnauthorizedPayload($errors); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Invalidate old tokens |
| 93 | + */ |
| 94 | + $this->cache->invalidateForUser($this->env, $domainUser); |
| 95 | + |
| 96 | + /** |
| 97 | + * Send the payload back |
| 98 | + */ |
| 99 | + return new Payload( |
| 100 | + DomainStatus::SUCCESS, |
| 101 | + [ |
| 102 | + 'data' => [ |
| 103 | + 'authenticated' => false, |
| 104 | + ], |
| 105 | + ] |
| 106 | + ); |
| 107 | + } |
| 108 | +} |
0 commit comments