|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Shlinkio\Shlink\Rest\Authentication\Plugin; |
|
5
|
|
|
|
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
use Shlinkio\Shlink\Rest\Authentication\JWTServiceInterface; |
|
9
|
|
|
use Shlinkio\Shlink\Rest\Exception\VerifyAuthenticationException; |
|
10
|
|
|
use Shlinkio\Shlink\Rest\Util\RestUtils; |
|
11
|
|
|
use Throwable; |
|
12
|
|
|
use Zend\I18n\Translator\TranslatorInterface; |
|
13
|
|
|
use function count; |
|
14
|
|
|
use function explode; |
|
15
|
|
|
use function sprintf; |
|
16
|
|
|
use function strtolower; |
|
17
|
|
|
|
|
18
|
|
|
class AuthorizationHeaderPlugin implements AuthenticationPluginInterface |
|
19
|
|
|
{ |
|
20
|
|
|
public const HEADER_NAME = 'Authorization'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var JWTServiceInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $jwtService; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var TranslatorInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $translator; |
|
30
|
|
|
|
|
31
|
5 |
|
public function __construct(JWTServiceInterface $jwtService, TranslatorInterface $translator) |
|
32
|
|
|
{ |
|
33
|
5 |
|
$this->jwtService = $jwtService; |
|
34
|
5 |
|
$this->translator = $translator; |
|
35
|
5 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @throws VerifyAuthenticationException |
|
39
|
|
|
*/ |
|
40
|
4 |
|
public function verify(ServerRequestInterface $request): void |
|
41
|
|
|
{ |
|
42
|
|
|
// Get token making sure the an authorization type is provided |
|
43
|
4 |
|
$authToken = $request->getHeaderLine(self::HEADER_NAME); |
|
44
|
4 |
|
$authTokenParts = explode(' ', $authToken); |
|
45
|
4 |
|
if (count($authTokenParts) === 1) { |
|
46
|
1 |
|
throw VerifyAuthenticationException::withError( |
|
47
|
1 |
|
RestUtils::INVALID_AUTHORIZATION_ERROR, |
|
48
|
1 |
|
sprintf( |
|
49
|
1 |
|
$this->translator->translate('You need to provide the Bearer type in the %s header.'), |
|
50
|
1 |
|
self::HEADER_NAME |
|
51
|
|
|
) |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// Make sure the authorization type is Bearer |
|
56
|
3 |
|
[$authType, $jwt] = $authTokenParts; |
|
|
|
|
|
|
57
|
3 |
|
if (strtolower($authType) !== 'bearer') { |
|
58
|
1 |
|
throw VerifyAuthenticationException::withError( |
|
59
|
1 |
|
RestUtils::INVALID_AUTHORIZATION_ERROR, |
|
60
|
1 |
|
sprintf($this->translator->translate( |
|
61
|
1 |
|
'Provided authorization type %s is not supported. Use Bearer instead.' |
|
62
|
1 |
|
), $authType) |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
try { |
|
67
|
2 |
|
if (! $this->jwtService->verify($jwt)) { |
|
68
|
2 |
|
throw $this->createInvalidTokenError(); |
|
69
|
|
|
} |
|
70
|
1 |
|
} catch (Throwable $e) { |
|
71
|
1 |
|
throw $this->createInvalidTokenError($e); |
|
72
|
|
|
} |
|
73
|
1 |
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
private function createInvalidTokenError(Throwable $prev = null): VerifyAuthenticationException |
|
76
|
|
|
{ |
|
77
|
1 |
|
return VerifyAuthenticationException::withError( |
|
78
|
1 |
|
RestUtils::INVALID_AUTH_TOKEN_ERROR, |
|
79
|
1 |
|
sprintf($this->translator->translate( |
|
80
|
|
|
'Missing or invalid auth token provided. Perform a new authentication request and send provided ' |
|
81
|
1 |
|
. 'token on every new request on the %s header' |
|
82
|
1 |
|
), self::HEADER_NAME), |
|
83
|
1 |
|
$prev |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
public function update(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface |
|
88
|
|
|
{ |
|
89
|
1 |
|
$authToken = $request->getHeaderLine(self::HEADER_NAME); |
|
90
|
1 |
|
[, $jwt] = explode(' ', $authToken); |
|
|
|
|
|
|
91
|
1 |
|
$jwt = $this->jwtService->refresh($jwt); |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
1 |
|
return $response->withHeader(self::HEADER_NAME, sprintf('Bearer %s', $jwt)); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.