- Added Oauth2 authentication. Fixed non-working bearer token also.

This commit is contained in:
Dave Mc Nicoll committed 2024-04-09 19:13:53 -04:00
1 parent f035fdf0a2
commit ce4d6cdf7c
7 files changed
+105 -32

No files matched your search

+50 -7
View File
@@ -2,6 +2,7 @@
namespace Ulmus\Api\Transport;
use League\OAuth2\Client\Provider\GenericProvider;
use Ulmus\Api\Common\AuthenticationEnum;
class CurlAuthorization
@@ -10,31 +11,35 @@ class CurlAuthorization
public array $options = [];
public function fromAuthenticationEnum(AuthenticationEnum $auth, string|false $username, #[\SensitiveParameter] string|false $password, #[\SensitiveParameter] string|false $token,) : static
public function fromAuthenticationEnum(AuthenticationEnum $auth, #[\SensitiveParameter] array $configuration,) : static
{
switch($auth) {
case AuthenticationEnum::Bearer:
$this->bearer($token);
$this->bearer($configuration['token']);
break;
case AuthenticationEnum::Key:
$this->key($token);
$this->key($configuration['token']);
break;
case AuthenticationEnum::Token:
$this->token($token);
$this->token($configuration['token']);
break;
case AuthenticationEnum::Basic:
$this->basic($username, $password);
$this->basic($configuration['username'], $configuration['password']);
break;
case AuthenticationEnum::Ntlm:
$this->ntlm($username, $password);
$this->ntlm($configuration['username'], $configuration['password']);
break;
case AuthenticationEnum::Negotiate:
$this->negotiate($username, $password);
$this->negotiate($configuration['username'], $configuration['password']);
break;
case AuthenticationEnum::OAuth2:
$this->oauth2($configuration['grant'], $configuration['oauth_token_url'], $configuration['redirect_uri'], $configuration['client_id'], $configuration['client_secret'], $configuration['scope']);
break;
}
@@ -43,6 +48,7 @@ class CurlAuthorization
public function bearer(#[\SensitiveParameter] string $token) : void
{
$this->options[CURLOPT_HTTPAUTH] = CURLAUTH_BEARER;
$this->options[CURLOPT_XOAUTH2_BEARER] = $token;
}
@@ -86,6 +92,43 @@ class CurlAuthorization
$this->userpass(CURLAUTH_NEGOTIATE, $username, $password);
}
public function oauth2(Oauth2GrantTypeEnum $grant, string $oauthTokenUrl, string $redirectUri, string $clientId, string $clientSecret, string $scope) : void
{
#return;
# TMP !
if (false === ($_SESSION['grics_token'] ?? false)) {
# IF NO TOKEN OR TIMEOUT
$provider = new GenericProvider([
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'redirectUri' => $redirectUri,
'urlAccessToken' => $oauthTokenUrl,
'urlAuthorize' => 'https://service.example.com/authorize',
'urlResourceOwnerDetails' => 'https://service.example.com/resource',
'scopes' => $scope,
]);
try {
$accessToken = $provider->getAccessToken($grant->value);
$_SESSION['grics_token'] = $accessToken->getToken();
# var_dump($accessToken); die();
# KEEP TOKEN IN APCU !
} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
// Failed to get the access token
exit($e->getMessage());
}
}
$this->bearer($_SESSION['grics_token']);
#var_dump($_SESSION['grics_token']);die();
# SWITCH TO BEARER AUTH TYPE, SET TOKEN FROM APCU IF STILL VALID
}
protected function userpass(int $authType, string $username, #[\SensitiveParameter] $password) : void
{
$this->options[CURLOPT_HTTPAUTH] = $authType;