Skip to content

Commit dab1c4a

Browse files
Merge pull request #681 from GDATASoftwareAG/php/http_api_authenticator_proposal
Refators the Authenntication stuff
2 parents 6d58eb3 + a2826f7 commit dab1c4a

File tree

4 files changed

+23
-76
lines changed

4 files changed

+23
-76
lines changed

php/src/vaas/Authentication/ClientCredentialsGrantAuthenticator.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Amp\Cancellation;
66
use Amp\Http\Client\HttpClient;
7-
use VaasSdk\Options\AuthenticationOptions;
87

98
class ClientCredentialsGrantAuthenticator implements AuthenticatorInterface
109
{
@@ -17,15 +16,9 @@ class ClientCredentialsGrantAuthenticator implements AuthenticatorInterface
1716
* @param string|null $tokenUrl The optional token url. Defaults to 'https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token'
1817
* @param HttpClient|null $httpClient Your optional custom http client.
1918
*/
20-
public function __construct(string $clientId, string $clientSecret, ?string $tokenUrl = null, ?HttpClient $httpClient = null)
19+
public function __construct(public string $clientId, public string $clientSecret, ?string $tokenUrl = null, ?HttpClient $httpClient = null)
2120
{
22-
$options = new AuthenticationOptions(
23-
grantType: GrantType::CLIENT_CREDENTIALS,
24-
clientId: $clientId,
25-
tokenUrl: $tokenUrl ?? 'https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token',
26-
clientSecret: $clientSecret
27-
);
28-
$this->tokenReceiver = new TokenReceiver($options, $httpClient);
21+
$this->tokenReceiver = new TokenReceiver($this, $tokenUrl, $httpClient);
2922
}
3023

3124
/**

php/src/vaas/Authentication/ResourceOwnerPasswordGrantAuthenticator.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Amp\Cancellation;
66
use Amp\Http\Client\HttpClient;
7-
use VaasSdk\Options\AuthenticationOptions;
87

98
class ResourceOwnerPasswordGrantAuthenticator implements AuthenticatorInterface
109
{
@@ -19,16 +18,9 @@ class ResourceOwnerPasswordGrantAuthenticator implements AuthenticatorInterface
1918
* @param string|null $tokenUrl The optional token url. Defaults to 'https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token'
2019
* @param HttpClient|null $httpClient Your optional custom http client.
2120
*/
22-
public function __construct(string $clientId, string $userName, string $password, ?string $tokenUrl = null, ?HttpClient $httpClient = null)
21+
public function __construct(public string $clientId, public string $userName, public string $password, ?string $tokenUrl = null, ?HttpClient $httpClient = null)
2322
{
24-
$options = new AuthenticationOptions(
25-
grantType: GrantType::PASSWORD,
26-
clientId: $clientId,
27-
tokenUrl: $tokenUrl ?? 'https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token',
28-
userName: $userName,
29-
password: $password
30-
);
31-
$this->tokenReceiver = new TokenReceiver($options, $httpClient);
23+
$this->tokenReceiver = new TokenReceiver($this, $tokenUrl, $httpClient);
3224
}
3325

3426
/**

php/src/vaas/Authentication/TokenReceiver.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@
1212
use Exception;
1313
use InvalidArgumentException;
1414
use VaasSdk\Exceptions\VaasAuthenticationException;
15-
use VaasSdk\Options\AuthenticationOptions;
1615
use function Amp\async;
1716
use function Amp\delay;
1817

1918
class TokenReceiver
2019
{
21-
private HttpClient $httpClient;
22-
private AuthenticationOptions $authenticationOptions;
2320
private Mutex $mutex;
2421
private ?TokenResponse $lastTokenResponse = null;
2522
private int $validTo = 0;
2623
private ?int $lastRequestTime = null;
2724

28-
public function __construct(AuthenticationOptions $options, ?HttpClient $httpClient = null)
25+
public function __construct(
26+
public AuthenticatorInterface $authenticator,
27+
public ?string $tokenUrl = null,
28+
public ?HttpClient $httpClient = null)
2929
{
30-
$this->authenticationOptions = $options;
30+
$this->tokenUrl = $tokenUrl ?? 'https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token';
3131
$this->httpClient = $httpClient ?? HttpClientBuilder::buildDefault();
3232
$this->mutex = new LocalMutex();
3333
}
@@ -81,7 +81,7 @@ private function requestTokenAsync(?Cancellation $cancellation = null): Future
8181
{
8282
return async(function () use ($cancellation) {
8383
$form = $this->tokenRequestToForm();
84-
$request = new Request($this->authenticationOptions->tokenUrl, 'POST');
84+
$request = new Request($this->tokenUrl, 'POST');
8585
$request->setBody($form);
8686
$request->setHeader('Content-Type', 'application/x-www-form-urlencoded');
8787

@@ -118,19 +118,20 @@ private function requestTokenAsync(?Cancellation $cancellation = null): Future
118118
*/
119119
private function tokenRequestToForm(): string
120120
{
121-
if ($this->authenticationOptions->grantType == GrantType::CLIENT_CREDENTIALS) {
122-
return http_build_query([
123-
'client_id' => $this->authenticationOptions->clientId,
124-
'client_secret' => $this->authenticationOptions->clientSecret ?? throw new InvalidArgumentException(),
121+
return match (true)
122+
{
123+
$this->authenticator instanceof ClientCredentialsGrantAuthenticator => http_build_query([
124+
'client_id' => $this->authenticator->clientId,
125+
'client_secret' => $this->authenticator->clientSecret,
125126
'grant_type' => 'client_credentials',
126-
]);
127-
}
128-
129-
return http_build_query([
130-
'client_id' => $this->authenticationOptions->clientId,
131-
'username' => $this->authenticationOptions->userName ?? throw new InvalidArgumentException(),
132-
'password' => $this->authenticationOptions->password ?? throw new InvalidArgumentException(),
127+
]),
128+
$this->authenticator instanceof ResourceOwnerPasswordGrantAuthenticator => http_build_query([
129+
'client_id' => $this->authenticator->clientId,
130+
'username' => $this->authenticator->userName,
131+
'password' => $this->authenticator->password,
133132
'grant_type' => 'password',
134-
]);
133+
]),
134+
default => throw new InvalidArgumentException("Unknown authenticator type")
135+
};
135136
}
136137
}

php/src/vaas/Options/AuthenticationOptions.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)