Skip to content

Commit 261699f

Browse files
authored
Merge pull request #12 from district09/feature/oauth
Change BasicAuth to OAuth (oidc)
2 parents 395f23e + c123602 commit 261699f

21 files changed

Lines changed: 548 additions & 158 deletions

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# PHPUnit build artifacts.
22
/build
3-
.phpunit.cache/
3+
.phpunit.cache
44
.phpunit.result.cache
55

66
# Never commit vendors!

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
All Notable changes to `digipolisgent/api-client` package.
44

5+
## [4.0.0]
6+
7+
_Use version 4 of this module to switch over to OAuth authentication.
8+
Key/secret authentication is dropped from the client.
9+
The client and configuration objects have changed parameters._
10+
11+
### Added
12+
13+
* ZALENZOEK-689: Add OAuth2 API authentication.
14+
515
## [3.0.1]
616

717
### Fixed
@@ -82,6 +92,7 @@ This includes:
8292
* Interfaces to create services in client packages.
8393

8494
[Unreleased]: https://github.com/digipolisgent/php_package_dg-api-client/compare/master...develop
95+
[4.0.0]: https://github.com/digipolisgent/php_package_dg-api-client/compare/3.0.1...4.0.0
8596
[3.0.1]: https://github.com/digipolisgent/php_package_dg-api-client/compare/3.0.0...3.0.1
8697
[3.0.0]: https://github.com/digipolisgent/php_package_dg-api-client/compare/2.1.0...3.0.0
8798
[2.1.0]: https://github.com/digipolisgent/php_package_dg-api-client/compare/2.0.0...2.1.0

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# DigipolsigGent API Client
1+
# District09 API Client
22

33
This package provides interfaces and abstract implementations to create an API client.
44

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717
{
1818
"name": "Jelle Sebreghts",
1919
"email": "sebreghts.jelle@district09.gent"
20+
},
21+
{
22+
"name": "Lennart Van Vaerenbergh",
23+
"email": "lennart.vanvaerenbergh@district09.gent"
2024
}
2125
],
2226
"homepage": "https://github.com/digipolisgent/php_package_api-client",
2327
"require": {
2428
"php": "^7.4 || ^8.0",
2529
"ext-json": "*",
2630
"guzzlehttp/guzzle": "^6.5 || ^7.0",
31+
"jumbojett/openid-connect-php": "^1",
2732
"psr/http-message": "^1.0",
2833
"psr/simple-cache": "^1.0"
2934
},

phpunit.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
5+
bootstrap="vendor/autoload.php"
6+
cacheDirectory=".phpunit.cache"
7+
executionOrder="depends,defects"
8+
requireCoverageMetadata="false"
9+
beStrictAboutOutputDuringTests="true"
10+
displayDetailsOnPhpunitDeprecations="true"
11+
displayDetailsOnTestsThatTriggerNotices="true"
12+
displayDetailsOnTestsThatTriggerWarnings="true"
13+
failOnPhpunitDeprecation="true"
14+
failOnRisky="true"
15+
failOnWarning="false"
16+
failOnNotice="false"
17+
>
18+
<source ignoreIndirectDeprecations="true">
19+
<include>
20+
<directory>src</directory>
21+
</include>
22+
</source>
23+
24+
<testsuites>
25+
<testsuite name="Coretalents/Portal">
26+
<directory>tests</directory>
27+
</testsuite>
28+
</testsuites>
29+
30+
<coverage>
31+
<report>
32+
<clover outputFile="build/logs/clover.xml"/>
33+
<html outputDirectory="build/coverage"/>
34+
</report>
35+
</coverage>
36+
</phpunit>

src/Client/AbstractClient.php

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@
88
use DigipolisGent\API\Client\Exception\HandlerNotFound;
99
use DigipolisGent\API\Client\Handler\HandlerInterface;
1010
use DigipolisGent\API\Client\Response\ResponseInterface;
11+
use DigipolisGent\API\Client\Token\OidcTokenProvider;
12+
use DigipolisGent\API\Client\Token\TokenProviderInterface;
1113
use DigipolisGent\API\Logger\LoggableInterface;
1214
use DigipolisGent\API\Logger\LoggableTrait;
1315
use DigipolisGent\API\Logger\RequestLog;
1416
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
1517
use GuzzleHttp\Exception\ClientException;
1618
use Psr\Http\Message\RequestInterface;
19+
use Psr\SimpleCache\CacheInterface;
1720

1821
/**
1922
* Abstract implementation of the service client.
23+
*
24+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2025
*/
2126
abstract 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
/**

src/Client/Configuration/Configuration.php

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,34 @@ class Configuration implements ConfigurationInterface
1616
*/
1717
protected string $endpointUri;
1818

19+
/**
20+
* Endpoint URI for the authentication.
21+
*
22+
* @var string
23+
*/
24+
protected string $authEndpointUri;
25+
26+
/**
27+
* Client ID.
28+
*
29+
* @var string
30+
*/
31+
protected string $clientId;
32+
33+
/**
34+
* Client secret.
35+
*
36+
* @var string
37+
*/
38+
protected string $clientSecret;
39+
40+
/**
41+
* Auth scope.
42+
*
43+
* @var string
44+
*/
45+
protected string $scope;
46+
1947
/**
2048
* The configuration options.
2149
*
@@ -28,10 +56,33 @@ class Configuration implements ConfigurationInterface
2856

2957
/**
3058
* Create new configuration.
59+
*
60+
* @param string $endpointUri
61+
* The endpoint URI.
62+
* @param string $authEndpointUri
63+
* The authentication endpoint URI.
64+
* @param string $clientId
65+
* The client ID.
66+
* @param string $clientSecret
67+
* The client secret.
68+
* @param string $scope
69+
* The authentication scope.
70+
* @param array $options
71+
* The client extra options.
3172
*/
32-
public function __construct(string $endpointUri, array $options = [])
33-
{
73+
public function __construct(
74+
string $endpointUri,
75+
string $authEndpointUri,
76+
string $clientId,
77+
string $clientSecret,
78+
string $scope,
79+
array $options = [],
80+
) {
3481
$this->endpointUri = $endpointUri;
82+
$this->authEndpointUri = $authEndpointUri;
83+
$this->clientId = $clientId;
84+
$this->clientSecret = $clientSecret;
85+
$this->scope = $scope;
3586

3687
foreach ($options as $key => $value) {
3788
if (!array_key_exists($key, $this->options)) {
@@ -50,6 +101,38 @@ public function getUri(): string
50101
return $this->endpointUri;
51102
}
52103

104+
/**
105+
* @inheritDoc
106+
*/
107+
public function getAuthUri(): string
108+
{
109+
return $this->authEndpointUri;
110+
}
111+
112+
/**
113+
* @inheritDoc
114+
*/
115+
public function getClientId(): string
116+
{
117+
return $this->clientId;
118+
}
119+
120+
/**
121+
* @inheritDoc
122+
*/
123+
public function getClientSecret(): string
124+
{
125+
return $this->clientSecret;
126+
}
127+
128+
/**
129+
* @inheritDoc
130+
*/
131+
public function getScope(): string
132+
{
133+
return $this->scope;
134+
}
135+
53136
/**
54137
* @inheritDoc
55138
*/

src/Client/Configuration/ConfigurationInterface.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,34 @@ interface ConfigurationInterface
1616
*/
1717
public function getUri(): string;
1818

19+
/**
20+
* Get the authentication URI.
21+
*
22+
* @return string
23+
*/
24+
public function getAuthUri(): string;
25+
26+
/**
27+
* Get the client ID.
28+
*
29+
* @return string
30+
*/
31+
public function getClientId(): string;
32+
33+
/**
34+
* Get the client secret.
35+
*
36+
* @return string
37+
*/
38+
public function getClientSecret(): string;
39+
40+
/**
41+
* Get the scope.
42+
*
43+
* @return string
44+
*/
45+
public function getScope(): string;
46+
1947
/**
2048
* Get the service version number to use.
2149
*

0 commit comments

Comments
 (0)