Skip to content

Commit 4118665

Browse files
author
Lennart Van Vaerenbergh
committed
Change BasicAuth to OAuth (oidc)
1 parent 395f23e commit 4118665

21 files changed

Lines changed: 546 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: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
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.
@@ -43,16 +46,37 @@ abstract class AbstractClient implements ClientInterface, LoggableInterface
4346
*/
4447
protected ConfigurationInterface $configuration;
4548

49+
/**
50+
* The OIDC token provider.
51+
*
52+
* @var \DigipolisGent\API\Client\Token\TokenProviderInterface
53+
*/
54+
protected TokenProviderInterface $tokenProvider;
55+
4656
/**
4757
* Client constructor.
4858
*
4959
* @param \GuzzleHttp\ClientInterface $guzzle
60+
* The Guzzle HTTP client.
5061
* @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.
5165
*/
52-
public function __construct(GuzzleClientInterface $guzzle, ConfigurationInterface $configuration)
53-
{
66+
public function __construct(
67+
GuzzleClientInterface $guzzle,
68+
ConfigurationInterface $configuration,
69+
CacheInterface $cache,
70+
) {
5471
$this->guzzle = $guzzle;
5572
$this->configuration = $configuration;
73+
$this->tokenProvider = new OidcTokenProvider(
74+
$configuration->getAuthUri(),
75+
$configuration->getClientId(),
76+
$configuration->getClientSecret(),
77+
$configuration->getScope(),
78+
$cache,
79+
);
5680
}
5781

5882
/**
@@ -90,10 +114,15 @@ public function send(RequestInterface $request): ResponseInterface
90114
*/
91115
protected function injectHeaders(RequestInterface $request): RequestInterface
92116
{
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+
);
97126
}
98127

99128
/**

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)