Lines
100.00%
7 / 7
Functions and Methods
100.00%
1 / 1
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| AuthenticationService | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 3 | 100.00% | 1 / 1 | |||
| getAuthenticatedUser | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 3 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace BO\Zmscitizenapi\Services\Core; | |
| 6 | ||
| 7 | use BO\Zmscitizenapi\Exceptions\InvalidAuthTokenException; | |
| 8 | use Psr\Http\Message\RequestInterface; | |
| 9 | use BO\Zmscitizenapi\Models\AuthenticatedUser; | |
| 10 | ||
| 11 | abstract class AuthenticationService | |
| 12 | { | |
| 13 | public static function getAuthenticatedUser(RequestInterface $request): AuthenticatedUser | null | |
| 14 | { | |
| 15 | $authHeader = $request->getHeaderLine('Authorization'); | |
| 16 | if (empty($authHeader)) { | |
| 17 | return null; | |
| 18 | } | |
| 19 | if (!preg_match('/Bearer\s(\S+)/', $authHeader, $matches)) { | |
| 20 | throw new InvalidAuthTokenException('authKeyMismatch', 'Invalid JWT payload.'); | |
| 21 | } | |
| 22 | $token = $matches[1]; | |
| 23 | return AuthenticatedUser::fromJwtPayload($token); | |
| 24 | } | |
| 25 | } |