|
8 | 8 | use DigipolisGent\API\Client\Exception\HandlerNotFound; |
9 | 9 | use DigipolisGent\API\Client\Handler\HandlerInterface; |
10 | 10 | use DigipolisGent\API\Client\Response\ResponseInterface; |
| 11 | +use DigipolisGent\API\Client\Token\OidcTokenProvider; |
| 12 | +use DigipolisGent\API\Client\Token\TokenProviderInterface; |
11 | 13 | use DigipolisGent\API\Logger\LoggableInterface; |
12 | 14 | use DigipolisGent\API\Logger\LoggableTrait; |
13 | 15 | use DigipolisGent\API\Logger\RequestLog; |
14 | 16 | use GuzzleHttp\ClientInterface as GuzzleClientInterface; |
15 | 17 | use GuzzleHttp\Exception\ClientException; |
16 | 18 | use Psr\Http\Message\RequestInterface; |
| 19 | +use Psr\SimpleCache\CacheInterface; |
17 | 20 |
|
18 | 21 | /** |
19 | 22 | * Abstract implementation of the service client. |
@@ -43,16 +46,37 @@ abstract class AbstractClient implements ClientInterface, LoggableInterface |
43 | 46 | */ |
44 | 47 | protected ConfigurationInterface $configuration; |
45 | 48 |
|
| 49 | + /** |
| 50 | + * The OIDC token provider. |
| 51 | + * |
| 52 | + * @var \DigipolisGent\API\Client\Token\TokenProviderInterface |
| 53 | + */ |
| 54 | + protected TokenProviderInterface $tokenProvider; |
| 55 | + |
46 | 56 | /** |
47 | 57 | * Client constructor. |
48 | 58 | * |
49 | 59 | * @param \GuzzleHttp\ClientInterface $guzzle |
| 60 | + * The Guzzle HTTP client. |
50 | 61 | * @param \DigipolisGent\API\Client\Configuration\ConfigurationInterface $configuration |
| 62 | + * The client configuration object. |
| 63 | + * @param \Psr\SimpleCache\CacheInterface $cache |
| 64 | + * Cache used for auth Bearer tokens. Not that this is not for API responses. |
51 | 65 | */ |
52 | | - public function __construct(GuzzleClientInterface $guzzle, ConfigurationInterface $configuration) |
53 | | - { |
| 66 | + public function __construct( |
| 67 | + GuzzleClientInterface $guzzle, |
| 68 | + ConfigurationInterface $configuration, |
| 69 | + CacheInterface $cache, |
| 70 | + ) { |
54 | 71 | $this->guzzle = $guzzle; |
55 | 72 | $this->configuration = $configuration; |
| 73 | + $this->tokenProvider = new OidcTokenProvider( |
| 74 | + $configuration->getAuthUri(), |
| 75 | + $configuration->getClientId(), |
| 76 | + $configuration->getClientSecret(), |
| 77 | + $configuration->getScope(), |
| 78 | + $cache, |
| 79 | + ); |
56 | 80 | } |
57 | 81 |
|
58 | 82 | /** |
@@ -90,10 +114,15 @@ public function send(RequestInterface $request): ResponseInterface |
90 | 114 | */ |
91 | 115 | protected function injectHeaders(RequestInterface $request): RequestInterface |
92 | 116 | { |
93 | | - return $request->withHeader( |
94 | | - 'Content-Length', |
95 | | - (string) strlen((string) $request->getBody()) |
96 | | - ); |
| 117 | + return $request |
| 118 | + ->withHeader( |
| 119 | + 'Content-Length', |
| 120 | + (string) strlen((string) $request->getBody()) |
| 121 | + ) |
| 122 | + ->withHeader( |
| 123 | + 'Authorization', |
| 124 | + 'Bearer ' . $this->tokenProvider->getAccessToken() |
| 125 | + ); |
97 | 126 | } |
98 | 127 |
|
99 | 128 | /** |
|
0 commit comments