Skip to content

Commit

Permalink
Apply review requests
Browse files Browse the repository at this point in the history
  • Loading branch information
lennartdohmann committed Dec 16, 2024
1 parent 550caf5 commit 947a077
Show file tree
Hide file tree
Showing 19 changed files with 458 additions and 204 deletions.
28 changes: 14 additions & 14 deletions php/examples/VaasExample/AuthenticationExamples.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@

namespace VaasExamples;

use VaasSdk\Authentication\Authenticator;
use VaasSdk\Authentication\GrantType;
use VaasSdk\Options\AuthenticationOptions;
use VaasSdk\Authentication\ClientCredentialsGrantAuthenticator;
use VaasSdk\Sha256;
use VaasSdk\Vaas;

// If you got a username and password from us, you can use the ResourceOwnerPasswordAuthenticator like this

// $credentials = new AuthenticationOptions(
// grantType: GrantType::PASSWORD,
// clientId: getenv("VAAS_CLIENT_ID"),
// username: getenv("VAAS_USER_NAME"),
// password: getenv("VAAS_PASSWORD")
// $authenticator = new ResourceOwnerPasswordGrantAuthenticator(
// clientId: getenv("CLIENT_ID"),
// username: getenv("USERNAME"),
// password: getenv("PASSWORD"),
// tokenUrl: getenv("TOKEN_URL")
// );

// You may use self registration and create a new username and password for the
// `Password` authentication method by yourself like the example above on https://vaas.gdata.de/login

// If you got a client id and client secret from us, you can use the `Client Credentials` authentication method like this

$credentials = new AuthenticationOptions(
grantType: GrantType::CLIENT_CREDENTIALS,
$authenticator = new ClientCredentialsGrantAuthenticator(
clientId: getenv("CLIENT_ID"),
clientSecret: getenv("CLIENT_SECRET")
clientSecret: getenv("CLIENT_SECRET"),
tokenUrl: getenv("TOKEN_URL")
);

$authenticator = new Authenticator($credentials);
$vaas = new Vaas($authenticator);
$vaas = (new Vaas())
->withAuthenticator($authenticator)
->build();

// Get verdict for an eicar hash
$vaasVerdict = $vaas->forSha256Async("000005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8")->await();
$vaasVerdict = $vaas->forSha256Async(Sha256::TryFromString("000005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8"))->await();
fwrite(STDOUT, "Verdict for $vaasVerdict->sha256 is $vaasVerdict->verdict->value \n");
16 changes: 8 additions & 8 deletions php/examples/VaasExample/GetVerdictByFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

namespace VaasExamples;

use VaasSdk\Authentication\Authenticator;
use VaasSdk\Authentication\GrantType;
use VaasSdk\Options\AuthenticationOptions;
use VaasSdk\Authentication\ClientCredentialsGrantAuthenticator;
use VaasSdk\Vaas;

include_once("./vendor/autoload.php");

$credentials = new AuthenticationOptions(
grantType: GrantType::CLIENT_CREDENTIALS,

$authenticator = new ClientCredentialsGrantAuthenticator(
clientId: getenv("CLIENT_ID"),
clientSecret: getenv("CLIENT_SECRET")
clientSecret: getenv("CLIENT_SECRET"),
tokenUrl: getenv("TOKEN_URL")
);

$authenticator = new Authenticator($credentials);
$vaas = (new Vaas())
->withAuthenticator($authenticator)
->build();

$vaas = new Vaas($authenticator);

$scanPath = getenv("SCAN_PATH");
$vaasVerdict = $vaas->forFileAsync($scanPath)->await();
Expand Down
25 changes: 14 additions & 11 deletions php/examples/VaasExample/GetVerdictByHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@

namespace VaasExamples;

use VaasSdk\Authentication\Authenticator;
use VaasSdk\Authentication\GrantType;
use VaasSdk\Options\AuthenticationOptions;
use VaasSdk\Authentication\ClientCredentialsGrantAuthenticator;
use VaasSdk\Sha256;
use VaasSdk\Vaas;

include_once("./vendor/autoload.php");

$credentials = new AuthenticationOptions(
grantType: GrantType::CLIENT_CREDENTIALS,

$authenticator = new ClientCredentialsGrantAuthenticator(
clientId: getenv("CLIENT_ID"),
clientSecret: getenv("CLIENT_SECRET")
clientSecret: getenv("CLIENT_SECRET"),
tokenUrl: getenv("TOKEN_URL")
);

$authenticator = new Authenticator($credentials);
$vaas = (new Vaas())
->withAuthenticator($authenticator)
->build();

$vaas = new Vaas($authenticator);

// EICAR
$vaasVerdict = $vaas->forSha256Async("000005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8")->await();
$vaasVerdict = $vaas->forSha256Async(Sha256::TryFromString("000005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8"))->await();
fwrite(STDOUT, "Verdict for $vaasVerdict->sha256 is " . $vaasVerdict->verdict->value . " \n");
// SOMEFILE
$vaasVerdict = $vaas->forSha256Async("70caea443deb0d0a890468f9ac0a9b1187676ba3e66eb60a722b187107eb1ea8")->await();


// Some file
$vaasVerdict = $vaas->forSha256Async(Sha256::TryFromString("70caea443deb0d0a890468f9ac0a9b1187676ba3e66eb60a722b187107eb1ea8"))->await();
fwrite(STDOUT, "Verdict for $vaasVerdict->sha256 is " . $vaasVerdict->verdict->value . " \n");
19 changes: 10 additions & 9 deletions php/examples/VaasExample/GetVerdictByUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@

namespace VaasExamples;

use VaasSdk\Authentication\Authenticator;
use VaasSdk\Authentication\GrantType;
use VaasSdk\Options\AuthenticationOptions;
use VaasSdk\Authentication\ClientCredentialsGrantAuthenticator;
use VaasSdk\Vaas;

include_once("./vendor/autoload.php");

$credentials = new AuthenticationOptions(
grantType: GrantType::CLIENT_CREDENTIALS,

$authenticator = new ClientCredentialsGrantAuthenticator(
clientId: getenv("CLIENT_ID"),
clientSecret: getenv("CLIENT_SECRET")
clientSecret: getenv("CLIENT_SECRET"),
tokenUrl: getenv("TOKEN_URL")
);

$authenticator = new Authenticator($credentials);
$vaas = (new Vaas())
->withAuthenticator($authenticator)
->build();

$vaas = new Vaas($authenticator);

// EICAR
$vaasVerdict = $vaas->forUrlAsync("https://secure.eicar.org/eicar.com")->await();
fwrite(STDOUT, "Verdict for $vaasVerdict->sha256 is " . $vaasVerdict->verdict->value . " \n");
// SOMEFILE

// Some file
$vaasVerdict = $vaas->forUrlAsync("https://www.gdatasoftware.com/oem/verdict-as-a-service")->await();
fwrite(STDOUT, "Verdict for $vaasVerdict->sha256 is " . $vaasVerdict->verdict->value . " \n");
11 changes: 11 additions & 0 deletions php/src/vaas/Authentication/AuthenticatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace VaasSdk\Authentication;

use Amp\Cancellation;
use Amp\Future;

interface AuthenticatorInterface
{
public function getTokenAsync(?Cancellation $cancellation = null): Future;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace VaasSdk\Authentication;

use Amp\Cancellation;
use Amp\Future;
use Amp\Http\Client\HttpClient;
use VaasSdk\Options\AuthenticationOptions;
use function Amp\async;

class ClientCredentialsGrantAuthenticator implements AuthenticatorInterface
{
private TokenReceiver $tokenReceiver;

/**
* 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(string $clientId, string $clientSecret, ?string $tokenUrl = null, ?HttpClient $httpClient = null)
{
$options = new AuthenticationOptions(
grantType: GrantType::CLIENT_CREDENTIALS,
clientId: $clientId,
tokenUrl: $tokenUrl ?? 'https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token',
clientSecret: $clientSecret
);
$this->tokenReceiver = new TokenReceiver($options, $httpClient);
}

/**
* Gets the access token asynchronously.
* If the token is still valid, it will be returned immediately.
* If the token is expired, a new token will be requested.
* @param Cancellation|null $cancellation Cancellation token
* @return Future Future that resolves to the access token string
*/
public function getTokenAsync(?Cancellation $cancellation = null): Future
{
return async(function () use ($cancellation) {
return $this->tokenReceiver->getTokenAsync($cancellation)->await();
});
}
}
6 changes: 3 additions & 3 deletions php/src/vaas/Authentication/GrantType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace VaasSdk\Authentication;

class GrantType
enum GrantType: string
{
const CLIENT_CREDENTIALS = 'client_credentials';
const PASSWORD = 'password';
case CLIENT_CREDENTIALS = 'client_credentials';
case PASSWORD = 'password';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace VaasSdk\Authentication;

use Amp\Cancellation;
use Amp\Future;
use Amp\Http\Client\HttpClient;
use VaasSdk\Options\AuthenticationOptions;
use function Amp\async;

class ResourceOwnerPasswordGrantAuthenticator implements AuthenticatorInterface
{
private TokenReceiver $tokenReceiver;

/**
* The authenticator for the resource owner password grant type if you have a client id, username and password.
* This is the choice if you have registered yourself on https://vaas.gdata.de/login. In this case, the client id is `vaas-customer`.
* @param string $clientId The client id
* @param string $userName Your username or email
* @param string $password Your password
* @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(string $clientId, string $userName, string $password, ?string $tokenUrl = null, ?HttpClient $httpClient = null)
{
$options = new AuthenticationOptions(
grantType: GrantType::PASSWORD,
clientId: $clientId,
tokenUrl: $tokenUrl ?? 'https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token',
userName: $userName,
password: $password
);
$this->tokenReceiver = new TokenReceiver($options, $httpClient);
}

/**
* Gets the access token asynchronously.
* If the token is still valid, it will be returned immediately.
* If the token is expired, a new token will be requested.
* @param Cancellation|null $cancellation Cancellation token
* @return Future Future that resolves to the access token string
*/
public function getTokenAsync(?Cancellation $cancellation = null): Future
{
return async(function () use ($cancellation) {
return $this->tokenReceiver->getTokenAsync($cancellation)->await();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,21 @@
use InvalidArgumentException;
use VaasSdk\Exceptions\VaasAuthenticationException;
use VaasSdk\Options\AuthenticationOptions;
use VaasSdk\Options\VaasOptions;
use function Amp\async;
use function Amp\delay;

class Authenticator
class TokenReceiver
{
private HttpClient $httpClient;
private VaasOptions $options;
private AuthenticationOptions $credentials;
private AuthenticationOptions $authenticationOptions;
private Mutex $mutex;
private ?TokenResponse $lastTokenResponse = null;
private int $validTo = 0;
private ?int $lastRequestTime = null;

public function __construct(AuthenticationOptions $credentials, ?VaasOptions $options = null, ?HttpClient $httpClient = null)
public function __construct(AuthenticationOptions $options, ?HttpClient $httpClient = null)
{
$this->credentials = $credentials;
$this->options = $options ?? new VaasOptions();
$this->authenticationOptions = $options;
$this->httpClient = $httpClient ?? HttpClientBuilder::buildDefault();
$this->mutex = new LocalMutex();
}
Expand Down Expand Up @@ -84,7 +81,7 @@ private function requestTokenAsync(?Cancellation $cancellation = null): Future
{
return async(function () use ($cancellation) {
$form = $this->tokenRequestToForm();
$request = new Request($this->options->tokenUrl, 'POST');
$request = new Request($this->authenticationOptions->tokenUrl, 'POST');
$request->setBody($form);
$request->setHeader('Content-Type', 'application/x-www-form-urlencoded');

Expand Down Expand Up @@ -121,18 +118,18 @@ private function requestTokenAsync(?Cancellation $cancellation = null): Future
*/
private function tokenRequestToForm(): string
{
if ($this->credentials->grantType === GrantType::CLIENT_CREDENTIALS) {
if ($this->authenticationOptions->grantType == GrantType::CLIENT_CREDENTIALS) {
return http_build_query([
'client_id' => $this->credentials->clientId,
'client_secret' => $this->credentials->clientSecret ?? throw new InvalidArgumentException(),
'client_id' => $this->authenticationOptions->clientId,
'client_secret' => $this->authenticationOptions->clientSecret ?? throw new InvalidArgumentException(),
'grant_type' => 'client_credentials',
]);
}

return http_build_query([
'client_id' => $this->credentials->clientId,
'username' => $this->credentials->userName ?? throw new InvalidArgumentException(),
'password' => $this->credentials->password ?? throw new InvalidArgumentException(),
'client_id' => $this->authenticationOptions->clientId,
'username' => $this->authenticationOptions->userName ?? throw new InvalidArgumentException(),
'password' => $this->authenticationOptions->password ?? throw new InvalidArgumentException(),
'grant_type' => 'password',
]);
}
Expand Down
13 changes: 13 additions & 0 deletions php/src/vaas/Exceptions/FileDoesNotExistException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace VaasSdk\Exceptions;

use Exception;

class FileDoesNotExistException extends Exception
{
public function __construct(string $message = 'File does not exist', int $code = 0, Exception $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
13 changes: 13 additions & 0 deletions php/src/vaas/Exceptions/InvalidSha256Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace VaasSdk\Exceptions;

use Exception;

class InvalidSha256Exception extends Exception
{
public function __construct(string $message = 'Invalid SHA256 hash', int $code = 0, Exception $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
Loading

0 comments on commit 947a077

Please sign in to comment.