| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the league/oauth2-client library |
| 5 |
* |
| 6 |
* For the full copyright and license information, please view the LICENSE |
| 7 |
* file that was distributed with this source code. |
| 8 |
* |
| 9 |
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com> |
| 10 |
* @license http://opensource.org/licenses/MIT MIT |
| 11 |
* @link http://thephpleague.com/oauth2-client/ Documentation |
| 12 |
* @link https://packagist.org/packages/league/oauth2-client Packagist |
| 13 |
* @link https://github.com/thephpleague/oauth2-client GitHub |
| 14 |
*/ |
| 15 |
namespace YoastSEO_Vendor\League\OAuth2\Client\Tool; |
| 16 |
|
| 17 |
use YoastSEO_Vendor\League\OAuth2\Client\Token\AccessToken; |
| 18 |
use YoastSEO_Vendor\League\OAuth2\Client\Token\AccessTokenInterface; |
| 19 |
/** |
| 20 |
* Enables `MAC` header authorization for providers. |
| 21 |
* |
| 22 |
* @link http://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-05 Message Authentication Code (MAC) Tokens |
| 23 |
*/ |
| 24 |
trait MacAuthorizationTrait |
| 25 |
{ |
| 26 |
/** |
| 27 |
* Returns the id of this token for MAC generation. |
| 28 |
* |
| 29 |
* @param AccessToken $token |
| 30 |
* @return string |
| 31 |
*/ |
| 32 |
protected abstract function getTokenId(\YoastSEO_Vendor\League\OAuth2\Client\Token\AccessToken $token); |
| 33 |
/** |
| 34 |
* Returns the MAC signature for the current request. |
| 35 |
* |
| 36 |
* @param string $id |
| 37 |
* @param integer $ts |
| 38 |
* @param string $nonce |
| 39 |
* @return string |
| 40 |
*/ |
| 41 |
protected abstract function getMacSignature($id, $ts, $nonce); |
| 42 |
/** |
| 43 |
* Returns a new random string to use as the state parameter in an |
| 44 |
* authorization flow. |
| 45 |
* |
| 46 |
* @param int $length Length of the random string to be generated. |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
protected abstract function getRandomState($length = 32); |
| 50 |
/** |
| 51 |
* Returns the authorization headers for the 'mac' grant. |
| 52 |
* |
| 53 |
* @param AccessTokenInterface|string|null $token Either a string or an access token instance |
| 54 |
* @return array |
| 55 |
* @codeCoverageIgnore |
| 56 |
* |
| 57 |
* @todo This is currently untested and provided only as an example. If you |
| 58 |
* complete the implementation, please create a pull request for |
| 59 |
* https://github.com/thephpleague/oauth2-client |
| 60 |
*/ |
| 61 |
protected function getAuthorizationHeaders($token = null) |
| 62 |
{ |
| 63 |
if ($token === null) { |
| 64 |
return []; |
| 65 |
} |
| 66 |
$ts = \time(); |
| 67 |
$id = $this->getTokenId($token); |
| 68 |
$nonce = $this->getRandomState(16); |
| 69 |
$mac = $this->getMacSignature($id, $ts, $nonce); |
| 70 |
$parts = []; |
| 71 |
foreach (\compact('id', 'ts', 'nonce', 'mac') as $key => $value) { |
| 72 |
$parts[] = \sprintf('%s="%s"', $key, $value); |
| 73 |
} |
| 74 |
return ['Authorization' => 'MAC ' . \implode(', ', $parts)]; |
| 75 |
} |
| 76 |
} |
| 77 |
|