acquia /
http-hmac-php
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Acquia\Hmac; |
||
| 4 | |||
| 5 | use Acquia\Hmac\Exception\MalformedRequestException; |
||
| 6 | use Psr\Http\Message\RequestInterface; |
||
| 7 | |||
| 8 | class AuthorizationHeader implements AuthorizationHeaderInterface |
||
| 9 | { |
||
| 10 | protected $realm = 'Acquia'; |
||
| 11 | protected $id; |
||
| 12 | protected $nonce; |
||
| 13 | protected $version = '2.0'; |
||
| 14 | protected $signature; |
||
| 15 | protected $headers = []; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Initializes the authorization header with the required fields. |
||
| 19 | * |
||
| 20 | * @param string $realm |
||
| 21 | * The realm/provider. |
||
| 22 | * @param string $id |
||
| 23 | * The API key's unique identifier. |
||
| 24 | * @param string $nonce |
||
| 25 | * The nonce, a hex-based v1 or v4 UUID. |
||
| 26 | * @param string $version |
||
| 27 | * The version of the HTTP HMAC spec. |
||
| 28 | * @param string[] $headers |
||
| 29 | * A list of custom headers included in the signature. |
||
| 30 | * @param string $signature |
||
| 31 | * The Base64-encoded signature of the request. |
||
| 32 | */ |
||
| 33 | 25 | public function __construct($realm, $id, $nonce, $version, array $headers, $signature) |
|
| 34 | { |
||
| 35 | 25 | $this->realm = $realm; |
|
| 36 | 25 | $this->id = $id; |
|
| 37 | 25 | $this->nonce = $nonce; |
|
| 38 | 25 | $this->version = $version; |
|
| 39 | 25 | $this->headers = $headers; |
|
| 40 | 25 | $this->signature = $signature; |
|
| 41 | 25 | } |
|
| 42 | |||
| 43 | /** |
||
| 44 | * {@inheritDoc} |
||
| 45 | */ |
||
| 46 | 24 | public static function createFromRequest(RequestInterface $request) |
|
| 47 | { |
||
| 48 | 24 | if (!$request->hasHeader('Authorization')) { |
|
| 49 | 1 | throw new MalformedRequestException('Authorization header is required.', null, 0, $request); |
|
| 50 | } |
||
| 51 | |||
| 52 | 23 | $header = $request->getHeaderLine('Authorization'); |
|
| 53 | |||
| 54 | 23 | $id_match = preg_match('/.*id="(.*?)"/', $header, $id_matches); |
|
| 55 | 23 | $realm_match = preg_match('/.*realm="(.*?)"/', $header, $realm_matches); |
|
| 56 | 23 | $nonce_match = preg_match('/.*nonce="(.*?)"/', $header, $nonce_matches); |
|
| 57 | 23 | $version_match = preg_match('/.*version="(.*?)"/', $header, $version_matches); |
|
| 58 | 23 | $signature_match = preg_match('/.*signature="(.*?)"/', $header, $signature_matches); |
|
| 59 | 23 | $headers_match = preg_match('/.*headers="(.*?)"/', $header, $headers_matches); |
|
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 60 | |||
| 61 | 23 | if (!$id_match || !$realm_match || !$nonce_match || !$version_match || !$signature_match) { |
|
| 62 | 5 | throw new MalformedRequestException( |
|
| 63 | 5 | 'Authorization header requires a realm, id, version, nonce and a signature.', |
|
| 64 | 5 | null, |
|
| 65 | 5 | 0, |
|
| 66 | 5 | $request |
|
| 67 | ); |
||
| 68 | } |
||
| 69 | |||
| 70 | 18 | $customHeaders = !empty($headers_matches[1]) ? explode('%3B', $headers_matches[1]) : []; |
|
| 71 | |||
| 72 | 18 | return new static( |
|
| 73 | 18 | rawurldecode($realm_matches[1]), |
|
| 74 | 18 | $id_matches[1], |
|
| 75 | 18 | $nonce_matches[1], |
|
| 76 | 18 | $version_matches[1], |
|
| 77 | 18 | $customHeaders, |
|
| 78 | 18 | $signature_matches[1] |
|
| 79 | ); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritDoc} |
||
| 84 | */ |
||
| 85 | 8 | public function getRealm() |
|
| 86 | { |
||
| 87 | 8 | return $this->realm; |
|
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritDoc} |
||
| 92 | */ |
||
| 93 | 11 | public function getId() |
|
| 94 | { |
||
| 95 | 11 | return $this->id; |
|
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritDoc} |
||
| 100 | */ |
||
| 101 | 13 | public function getNonce() |
|
| 102 | { |
||
| 103 | 13 | return $this->nonce; |
|
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritDoc} |
||
| 108 | */ |
||
| 109 | 8 | public function getVersion() |
|
| 110 | { |
||
| 111 | 8 | return $this->version; |
|
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritDoc} |
||
| 116 | */ |
||
| 117 | 8 | public function getCustomHeaders() |
|
| 118 | { |
||
| 119 | 8 | return $this->headers; |
|
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritDoc} |
||
| 124 | */ |
||
| 125 | 14 | public function getSignature() |
|
| 126 | { |
||
| 127 | 14 | return $this->signature; |
|
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * {@inheritDoc} |
||
| 132 | */ |
||
| 133 | 15 | public function __toString() |
|
| 134 | { |
||
| 135 | 15 | return 'acquia-http-hmac realm="' . rawurlencode($this->realm) . '",' |
|
| 136 | 15 | . 'id="' . $this->id . '",' |
|
| 137 | 15 | . 'nonce="' . $this->nonce . '",' |
|
| 138 | 15 | . 'version="' . $this->version . '",' |
|
| 139 | 15 | . 'headers="' . implode('%3B', $this->headers) . '",' |
|
| 140 | 15 | . 'signature="' . $this->signature . '"'; |
|
| 141 | } |
||
| 142 | } |
||
| 143 |