88use DigipolisGent \API \Client \Exception \HandlerNotFound ;
99use DigipolisGent \API \Client \Handler \HandlerInterface ;
1010use DigipolisGent \API \Client \Response \ResponseInterface ;
11+ use DigipolisGent \API \Client \Token \OidcTokenProvider ;
12+ use DigipolisGent \API \Client \Token \TokenProviderInterface ;
1113use DigipolisGent \API \Logger \LoggableInterface ;
1214use DigipolisGent \API \Logger \LoggableTrait ;
1315use DigipolisGent \API \Logger \RequestLog ;
1416use GuzzleHttp \ClientInterface as GuzzleClientInterface ;
1517use GuzzleHttp \Exception \ClientException ;
1618use Psr \Http \Message \RequestInterface ;
19+ use Psr \SimpleCache \CacheInterface ;
1720
1821/**
1922 * Abstract implementation of the service client.
23+ *
24+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2025 */
2126abstract class AbstractClient implements ClientInterface, LoggableInterface
2227{
@@ -43,16 +48,37 @@ abstract class AbstractClient implements ClientInterface, LoggableInterface
4348 */
4449 protected ConfigurationInterface $ configuration ;
4550
51+ /**
52+ * The OIDC token provider.
53+ *
54+ * @var \DigipolisGent\API\Client\Token\TokenProviderInterface
55+ */
56+ protected TokenProviderInterface $ tokenProvider ;
57+
4658 /**
4759 * Client constructor.
4860 *
4961 * @param \GuzzleHttp\ClientInterface $guzzle
62+ * The Guzzle HTTP client.
5063 * @param \DigipolisGent\API\Client\Configuration\ConfigurationInterface $configuration
64+ * The client configuration object.
65+ * @param \Psr\SimpleCache\CacheInterface $cache
66+ * Cache used for auth Bearer tokens. Not that this is not for API responses.
5167 */
52- public function __construct (GuzzleClientInterface $ guzzle , ConfigurationInterface $ configuration )
53- {
68+ public function __construct (
69+ GuzzleClientInterface $ guzzle ,
70+ ConfigurationInterface $ configuration ,
71+ CacheInterface $ cache ,
72+ ) {
5473 $ this ->guzzle = $ guzzle ;
5574 $ this ->configuration = $ configuration ;
75+ $ this ->tokenProvider = new OidcTokenProvider (
76+ $ configuration ->getAuthUri (),
77+ $ configuration ->getClientId (),
78+ $ configuration ->getClientSecret (),
79+ $ configuration ->getScope (),
80+ $ cache ,
81+ );
5682 }
5783
5884 /**
@@ -90,10 +116,15 @@ public function send(RequestInterface $request): ResponseInterface
90116 */
91117 protected function injectHeaders (RequestInterface $ request ): RequestInterface
92118 {
93- return $ request ->withHeader (
94- 'Content-Length ' ,
95- (string ) strlen ((string ) $ request ->getBody ())
96- );
119+ return $ request
120+ ->withHeader (
121+ 'Content-Length ' ,
122+ (string ) strlen ((string ) $ request ->getBody ())
123+ )
124+ ->withHeader (
125+ 'Authorization ' ,
126+ 'Bearer ' . $ this ->tokenProvider ->getAccessToken ()
127+ );
97128 }
98129
99130 /**
0 commit comments