Skip to content

Commit

Permalink
Authenticators should extend TokenReceiver.php directly
Browse files Browse the repository at this point in the history
  • Loading branch information
lennartdohmann committed Jan 20, 2025
1 parent f3119a0 commit 466de64
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 51 deletions.
2 changes: 1 addition & 1 deletion php/src/vaas/Authentication/AuthenticatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface AuthenticatorInterface
{
public function getTokenAsync(?Cancellation $cancellation = null): string;
public function getToken(?Cancellation $cancellation = null): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@
use Amp\Cancellation;
use Amp\Http\Client\HttpClient;

class ClientCredentialsGrantAuthenticator implements AuthenticatorInterface
class ClientCredentialsGrantAuthenticator extends TokenReceiver implements AuthenticatorInterface
{
private TokenReceiver $tokenReceiver;

private readonly string $clientId;
private readonly string $clientSecret;

/**
* The authenticator for the client credentials grant type if you have a client id and client secret.
* @param string $clientId The client id
* @param string $clientSecret The client secret
* @param string|null $tokenUrl The optional token url. Defaults to 'https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token'
* @param HttpClient|null $httpClient Your optional custom http client.
*/
public function __construct(public string $clientId, public string $clientSecret, ?string $tokenUrl = null, ?HttpClient $httpClient = null)
public function __construct(string $clientId, string $clientSecret, ?string $tokenUrl = null, ?HttpClient $httpClient = null)
{
$this->tokenReceiver = new ClientCredentialsTokenReceiver($this, $tokenUrl, $httpClient);
parent::__construct($tokenUrl, $httpClient);
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
}

/**
Expand All @@ -28,8 +31,17 @@ public function __construct(public string $clientId, public string $clientSecret
* @param Cancellation|null $cancellation Cancellation token
* @return string The access token string
*/
public function getTokenAsync(?Cancellation $cancellation = null): string
public function getToken(Cancellation $cancellation = null): string
{
return parent::getTokenAsync($cancellation)->await($cancellation);
}

protected function tokenRequestToForm(): string
{
return $this->tokenReceiver->getTokenAsync($cancellation)->await();
return http_build_query([
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'grant_type' => 'client_credentials',
]);
}
}
15 changes: 0 additions & 15 deletions php/src/vaas/Authentication/ClientCredentialsTokenReceiver.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use Amp\Cancellation;
use Amp\Http\Client\HttpClient;

class ResourceOwnerPasswordGrantAuthenticator implements AuthenticatorInterface
class ResourceOwnerPasswordGrantAuthenticator extends TokenReceiver implements AuthenticatorInterface
{
private TokenReceiver $tokenReceiver;
private readonly string $clientId;
private readonly string $userName;
private readonly string $password;

/**
* The authenticator for the resource owner password grant type if you have a client id, username and password.
Expand All @@ -18,9 +20,12 @@ class ResourceOwnerPasswordGrantAuthenticator implements AuthenticatorInterface
* @param string|null $tokenUrl The optional token url. Defaults to 'https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token'
* @param HttpClient|null $httpClient Your optional custom http client.
*/
public function __construct(public string $clientId, public string $userName, public string $password, ?string $tokenUrl = null, ?HttpClient $httpClient = null)
public function __construct(string $clientId, string $userName, string $password, ?string $tokenUrl = null, ?HttpClient $httpClient = null)
{
$this->tokenReceiver = new ResourceOwnerPasswordTokenReceiver($this, $tokenUrl, $httpClient);
parent::__construct($tokenUrl, $httpClient);
$this->clientId = $clientId;
$this->userName = $userName;
$this->password = $password;
}

/**
Expand All @@ -30,8 +35,18 @@ public function __construct(public string $clientId, public string $userName, pu
* @param Cancellation|null $cancellation Cancellation token
* @return string The access token string
*/
public function getTokenAsync(?Cancellation $cancellation = null): string
public function getToken(Cancellation $cancellation = null): string
{
return $this->tokenReceiver->getTokenAsync($cancellation)->await();
return parent::getTokenAsync($cancellation)->await($cancellation);
}

protected function tokenRequestToForm(): string
{
return http_build_query([
'client_id' => $this->clientId,
'username' => $this->userName,
'password' => $this->password,
'grant_type' => 'password',
]);
}
}
16 changes: 0 additions & 16 deletions php/src/vaas/Authentication/ResourceOwnerPasswordTokenReceiver.php

This file was deleted.

1 change: 0 additions & 1 deletion php/src/vaas/Authentication/TokenReceiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ abstract class TokenReceiver
private ?int $lastRequestTime = null;

public function __construct(
public AuthenticatorInterface $authenticator,
public ?string $tokenUrl = null,
public ?HttpClient $httpClient = null)
{
Expand Down
2 changes: 1 addition & 1 deletion php/src/vaas/Vaas.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ private function addRequestHeadersAsync(Request $request, ?string $requestId = '
{
return async(function () use ($request, $requestId) {
$this->logger->debug("Add request headers");
$request->setHeader('Authorization', 'Bearer ' . $this->authenticator->getTokenAsync());
$request->setHeader('Authorization', 'Bearer ' . $this->authenticator->getToken());
$this->logger->debug("Successfully added authorization header with bearer token");
$request->setHeader('User-Agent', sprintf('%s/%s', self::PRODUCT_NAME, self::PRODUCT_VERSION));
if (!empty($requestId)) {
Expand Down
8 changes: 4 additions & 4 deletions php/tests/VaasTesting/AuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function testClientCredentialsGrantAuthenticator_withInvalidCredentials_T
clientSecret: "invalid"
);

$authenticator->getTokenAsync();
$authenticator->getToken();
}

public function testResourceOwnerPasswordGrantAuthenticator_withInvalidCredentials_ThrowsAccessDeniedException(): void
Expand All @@ -68,7 +68,7 @@ public function testResourceOwnerPasswordGrantAuthenticator_withInvalidCredentia
password: "invalid"
);

$authenticator->getTokenAsync();
$authenticator->getToken();
}

public function testClientCredentialsGrantAuthenticator_withValidCredentials_ReturnsToken(): void
Expand All @@ -79,7 +79,7 @@ public function testClientCredentialsGrantAuthenticator_withValidCredentials_Ret
tokenUrl: $_ENV["TOKEN_URL"]
);

$token = $authenticator->getTokenAsync();
$token = $authenticator->getToken();

$this->assertNotNull($token);
}
Expand All @@ -93,7 +93,7 @@ public function testResourceOwnerPasswordGrantAuthenticator_withValidCredentials
tokenUrl: $_ENV["TOKEN_URL"]
);

$token = $authenticator->getTokenAsync();
$token = $authenticator->getToken();

$this->assertNotNull($token);
}
Expand Down

0 comments on commit 466de64

Please sign in to comment.