| Total Complexity | 2 |
| Total Lines | 41 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php declare(strict_types = 1); |
||
| 13 | class GuzzleJWTMiddleware |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * JWT Authentication middleware for Guzzle |
||
| 17 | * |
||
| 18 | * @param string $issuer Add-on key in most cases |
||
| 19 | * @param string $secret Shared secret |
||
| 20 | * |
||
| 21 | * @return callable |
||
| 22 | */ |
||
| 23 | public static function authTokenMiddleware(string $issuer, string $secret): callable |
||
| 24 | { |
||
| 25 | return Middleware::mapRequest( |
||
| 26 | function (RequestInterface $request) use ($issuer, $secret) { |
||
| 27 | return new Request( |
||
| 28 | $request->getMethod(), |
||
| 29 | $request->getUri(), |
||
| 30 | \array_merge($request->getHeaders(), ['Authorization' => 'JWT '.static::createToken($request, $issuer, $secret)]), |
||
| 31 | $request->getBody() |
||
| 32 | ); |
||
| 33 | } |
||
| 34 | ); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Create JWT token used by Atlassian REST API request |
||
| 39 | * |
||
| 40 | * @param RequestInterface $request |
||
| 41 | * @param string $issuer Key of the add-on |
||
| 42 | * @param string $secret Shared secret of the Tenant |
||
| 43 | * |
||
| 44 | * @return string |
||
| 45 | */ |
||
| 46 | private static function createToken(RequestInterface $request, string $issuer, string $secret): string |
||
| 54 | } |
||
| 55 | } |
||
| 56 |