-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #290 from GDATASoftwareAG/php/include-resource-own…
…er-password-authenticator Php/include resource owner password authenticator
- Loading branch information
Showing
15 changed files
with
161 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace VaasExamples; | ||
|
||
use VaasSdk\ClientCredentialsGrantAuthenticator; | ||
use VaasSdk\Exceptions\InvalidSha256Exception; | ||
use VaasSdk\Exceptions\TimeoutException; | ||
use VaasSdk\Exceptions\VaasAuthenticationException; | ||
use VaasSdk\ResourceOwnerPasswordAuthenticator; | ||
use VaasSdk\Vaas; | ||
|
||
// If you got a username and password from us, you can use the ResourceOwnerPasswordAuthenticator like this | ||
$authenticator = new ResourceOwnerPasswordAuthenticator( | ||
getenv("CLIENT_ID"), | ||
getenv("USER_NAME"), | ||
getenv("PASSWORD"), | ||
getenv("TOKEN_URL") | ||
); | ||
// If you got a client id with a link you may use self registration and create a new username and password for the | ||
// ResourceOwnerPasswordAuthenticator by yourself like the example above. | ||
|
||
// If you got a client id and client secret from us, you can use the ClientCredentialsGrantAuthenticator like this | ||
$authenticator = new ClientCredentialsGrantAuthenticator( | ||
getenv("CLIENT_ID"), | ||
getenv("CLIENT_SECRET"), | ||
getenv("TOKEN_URL") | ||
); | ||
|
||
$vaas = new Vaas( | ||
getenv("VAAS_URL") | ||
); | ||
|
||
try { | ||
$vaas->Connect($authenticator->getToken()); | ||
} catch (VaasAuthenticationException $e) { | ||
fwrite(STDERR, "Authentication failed: " . $e->getMessage() . "\n"); | ||
exit(1); | ||
} | ||
|
||
// Get verdict for an eicar hash | ||
try { | ||
$vaasVerdict = $vaas->ForSha256("000005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8"); | ||
} catch (InvalidSha256Exception $e) { | ||
fwrite(STDERR, "Invalid sha256: " . $e->getMessage() . "\n"); | ||
exit(1); | ||
} catch (TimeoutException $e) { | ||
fwrite(STDERR, "Timeout: " . $e->getMessage() . "\n"); | ||
exit(1); | ||
} | ||
fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is $vaasVerdict->Verdict \n"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace VaasSdk; | ||
|
||
use League\OAuth2\Client\Provider\GenericProvider; | ||
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; | ||
use VaasSdk\Exceptions\VaasAuthenticationException; | ||
|
||
class ResourceOwnerPasswordAuthenticator { | ||
private string $clientId; | ||
private string $userName; | ||
private string $password; | ||
private string $tokenEndpoint; | ||
private $verify; | ||
|
||
public function __construct($clientId, $userName, $password, $tokenEndpoint, $verify=true) { | ||
$this->clientId = $clientId; | ||
$this->userName = $userName; | ||
$this->password = $password; | ||
$this->tokenEndpoint = $tokenEndpoint; | ||
$this->verify = $verify; | ||
} | ||
|
||
/** | ||
* @throws VaasAuthenticationException | ||
*/ | ||
public function getToken() { | ||
$provider = new GenericProvider([ | ||
'clientId' => $this->clientId, | ||
'urlAuthorize' => $this->tokenEndpoint, | ||
'urlAccessToken' => $this->tokenEndpoint, | ||
'urlResourceOwnerDetails' => '', | ||
'verify' => $this->verify, | ||
]); | ||
|
||
try { | ||
$accessToken = $provider->getAccessToken("password", [ | ||
'username' => $this->userName, | ||
'password' => $this->password | ||
]); | ||
return $accessToken->getToken(); | ||
} catch (IdentityProviderException $e) { | ||
throw new VaasAuthenticationException($e->getMessage(), $e->getCode()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters