Skip to content

Commit c10da21

Browse files
unglaublicherdudepstadermannata-no-oneGermanCodingdoxthree
authored
replace guzzle with amphp/http-client and revolt/event-loop (#500)
* SQUASH: ping on upload * replaces guzzle with amphp-http --------- Co-authored-by: Philip Stadermann <philip.stadermann@gdata.de> Co-authored-by: Simonis, Matthias <Matthias.Simonis@gdata.de> Co-authored-by: Version Bot <ata-no-one@gdata.de> Co-authored-by: Max <Maximilian.Froehling@gdata.de> Co-authored-by: doxthree <kevin.heise@tutanota.de>
1 parent af2fbb1 commit c10da21

21 files changed

Lines changed: 423 additions & 268 deletions

.github/workflows/image-retention.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ on:
77
branches:
88
- "*"
99
- "!main" # excludes main
10+
paths:
11+
- "github-actions/cleanup-packages/**"
12+
- ".github/workflows/image-retention.yaml"
1013
workflow_dispatch:
1114

1215
jobs:

php/examples/VaasExample/AuthenticationExamples.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace VaasExamples;
44

5-
use VaasSdk\ClientCredentialsGrantAuthenticator;
5+
use VaasSdk\Authentication\ClientCredentialsGrantAuthenticator;
66
use VaasSdk\Exceptions\InvalidSha256Exception;
77
use VaasSdk\Exceptions\TimeoutException;
88
use VaasSdk\Exceptions\VaasAuthenticationException;

php/examples/VaasExample/GetVerdictByFile.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
namespace VaasExamples;
44

5-
use VaasSdk\ClientCredentialsGrantAuthenticator;
5+
use VaasSdk\Authentication\ClientCredentialsGrantAuthenticator;
66
use VaasSdk\Vaas;
77

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

1010
$authenticator = new ClientCredentialsGrantAuthenticator(
1111
getenv("CLIENT_ID"),
1212
getenv("CLIENT_SECRET"),
13-
getenv("TOKEN_URL") ?? "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token"
13+
getenv("TOKEN_URL") ?: "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token"
1414
);
1515

1616
$vaas = new Vaas(

php/examples/VaasExample/GetVerdictByHash.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
namespace VaasExamples;
44

5-
use VaasSdk\ClientCredentialsGrantAuthenticator;
5+
use VaasSdk\Authentication\ClientCredentialsGrantAuthenticator;
66
use VaasSdk\Vaas;
77

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

1010
$authenticator = new ClientCredentialsGrantAuthenticator(
1111
getenv("CLIENT_ID"),
1212
getenv("CLIENT_SECRET"),
13-
getenv("TOKEN_URL") ?? "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token"
13+
getenv("TOKEN_URL") ?: "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token"
1414
);
1515
$vaas = new Vaas(
16-
getenv("VAAS_URL") ?? "wss://gateway.production.vaas.gdatasecurity.de"
16+
getenv("VAAS_URL") ?: "wss://gateway.production.vaas.gdatasecurity.de"
1717
);
1818
$vaas->Connect($authenticator->getToken());
1919

php/examples/VaasExample/GetVerdictByUrl.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
namespace VaasExamples;
44

5-
use VaasSdk\ClientCredentialsGrantAuthenticator;
5+
use VaasSdk\Authentication\ClientCredentialsGrantAuthenticator;
66
use VaasSdk\Vaas;
77

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

1010
$authenticator = new ClientCredentialsGrantAuthenticator(
1111
getenv("CLIENT_ID"),
1212
getenv("CLIENT_SECRET"),
13-
getenv("TOKEN_URL") ?? "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token"
13+
getenv("TOKEN_URL") ?: "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token"
1414
);
1515
$vaas = new Vaas(
16-
getenv("VAAS_URL") ?? "wss://gateway.production.vaas.gdatasecurity.de"
16+
getenv("VAAS_URL") ?: "wss://gateway.production.vaas.gdatasecurity.de"
1717
);
1818
$vaas->Connect($authenticator->getToken());
1919

php/phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<phpunit>
22
<testsuites>
33
<testsuite name="VaasTesting">
4+
<file>tests/vaas/StreamsInLoopTest.php</file>
45
<file>tests/vaas/Sha256Test.php</file>
56
<file>tests/vaas/VaasTest.php</file>
67
<file>tests/vaas/ProtocolTest.php</file>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace VaasSdk\Authentication;
4+
5+
class ClientCredentialsGrantAuthenticator
6+
{
7+
private OAuth2TokenReceiver $_tokenReceiver;
8+
public function __construct(
9+
string $clientId,
10+
string $clientSecret,
11+
string $tokenEndpoint = "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token"
12+
) {
13+
$this->_tokenReceiver = new OAuth2TokenReceiver($tokenEndpoint, $clientId, $clientSecret);
14+
}
15+
16+
public function getToken(): string
17+
{
18+
return $this->_tokenReceiver->GetToken();
19+
}
20+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace VaasSdk\Authentication;
4+
5+
use Amp\Http\Client\Form;
6+
use Amp\Http\Client\HttpClient;
7+
use Amp\Http\Client\HttpClientBuilder;
8+
use Amp\Http\Client\Request;
9+
use Amp\TimeoutCancellation;
10+
use Exception;
11+
use VaasSdk\Exceptions\VaasAuthenticationException;
12+
13+
class OAuth2TokenReceiver {
14+
private string $_tokenEndpoint;
15+
private string $_clientId;
16+
private string $_clientSecret;
17+
private string $_username;
18+
private string $_password;
19+
private string $_grantType;
20+
private Form $_formParams;
21+
private HttpClient $_browser;
22+
private int $_receiveTokenTimeout = 30;
23+
24+
public function __construct(
25+
string $tokenEndpoint, string $clientId, string $clientSecret = "",
26+
string $username = "", string $password = "")
27+
{
28+
$this->_browser = HttpClientBuilder::buildDefault();
29+
$this->_tokenEndpoint = $tokenEndpoint;
30+
$this->_clientId = $clientId;
31+
$this->_clientSecret = $clientSecret;
32+
$this->_username = $username;
33+
$this->_password = $password;
34+
$this->_grantType = $this->_clientSecret == "" ? "password" : "client_credentials";
35+
36+
$this->_formParams = new Form();
37+
$this->_formParams->addField('client_id', $this->_clientId);
38+
$this->_formParams->addField('grant_type', $this->_grantType);
39+
40+
switch($this->_grantType) {
41+
case "password":
42+
$this->_formParams->addField('username', $this->_username);
43+
$this->_formParams->addField('password', $this->_password);
44+
break;
45+
case "client_credentials":
46+
$this->_formParams->addField('client_secret', $this->_clientSecret);
47+
break;
48+
default:
49+
throw new VaasAuthenticationException("Invalid grant type");
50+
}
51+
}
52+
53+
public function getToken() {
54+
try {
55+
$request = new Request($this->_tokenEndpoint, 'POST');
56+
$request->addHeader('Content-Type', 'application/x-www-form-urlencoded');
57+
$request->setBody($this->_formParams);
58+
$response = $this->_browser->request($request, new TimeoutCancellation($this->_receiveTokenTimeout));
59+
if ($response->getStatus() != 200) {
60+
throw new VaasAuthenticationException($response->getReason(), $response->getStatus());
61+
}
62+
} catch (Exception $e) {
63+
throw new VaasAuthenticationException($e->getMessage(), $e->getCode());
64+
}
65+
$body = $response->getBody()->buffer();
66+
$response_body = json_decode($body);
67+
return $response_body->access_token;
68+
}
69+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace VaasSdk\Authentication;
4+
5+
use VaasSdk\Authentication\OAuth2TokenReceiver;
6+
use VaasSdk\Exceptions\VaasAuthenticationException;
7+
8+
class ResourceOwnerPasswordGrantAuthenticator {
9+
private OAuth2TokenReceiver $_tokenReceiver;
10+
11+
public function __construct($clientId, $userName, $password, $tokenEndpoint) {
12+
$this->_tokenReceiver = new OAuth2TokenReceiver($tokenEndpoint, $clientId, "", $userName, $password);
13+
}
14+
15+
/**
16+
* @throws VaasAuthenticationException
17+
*/
18+
public function getToken() {
19+
return $this->_tokenReceiver->GetToken();
20+
}
21+
}

php/src/vaas/ClientCredentialsGrantAuthenticator.php

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

0 commit comments

Comments
 (0)