| 1 | <?php |
||
| 8 | use Psr\Http\Message\ServerRequestInterface; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Token extractor to obtain a token from an authorization header. |
||
| 12 | */ |
||
| 13 | final class AuthorizationHeaderExtractor implements TokenExtractorInterface |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * A custom scheme expected in the Authorization header. |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | private $scheme; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Construct a new instance of this extractor |
||
| 24 | * |
||
| 25 | * @param string $scheme A custom scheme expected in the Authorization header. |
||
| 26 | */ |
||
| 27 | public function __construct(string $scheme = 'hmac') |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Extracts the HMAC authentication Token from the given PSR-7 $request. |
||
| 34 | * |
||
| 35 | * @param ServerRequestInterface $request The request containing the HMAC token data. |
||
| 36 | * |
||
| 37 | * @return Token |
||
| 38 | * |
||
| 39 | * @throws AuthenticationException 400 Thrown if any required data is missing. |
||
| 40 | */ |
||
| 41 | public function extract(ServerRequestInterface $request) : Token |
||
| 42 | { |
||
| 43 | $authorizationHeader = $request->getHeaderLine('Authorization'); |
||
| 44 | |||
| 45 | //Authorization: schema PublicKey:Signature:Nonce:Timestamp |
||
| 46 | |||
| 47 | $pattern = "^{$this->scheme}\s(?P<publicKey>[a-zA-z0-9]*):(?P<signature>[a-zA-Z0-9]*):" |
||
| 48 | . '(?P<nonce>[a-zA-Z0-9]*):(?P<timestamp>[0-9]*)$'; |
||
| 49 | $matches = []; |
||
| 50 | $matched = preg_match("/{$pattern}/", $authorizationHeader, $matches); |
||
| 51 | if (!$matched) { |
||
| 52 | throw new AuthenticationException(400, 'Bad Request'); |
||
| 53 | } |
||
| 58 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.