- 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;
+12 -4
View File
@@ -27,7 +27,7 @@ abstract class CurlTransport {
public ContentTypeEnum $contentType = ContentTypeEnum::Json,
# Matching Guzzle's great user-agent syntax
public string $userAgent = "ulmus-api/1.0 curl/{curl} php/{php}",
public bool $debug = false,
public bool $debug = true,
) {
$this->userAgent = str_replace([ '{curl}', '{php}', ], [ curl_version()['version'] ?? "???", phpversion() ], $this->userAgent);
}
@@ -77,9 +77,10 @@ abstract class CurlTransport {
$this->applyMethod($method, $data, $headers, $options);
$headers += $this->authorization->headers;
$options += $this->authorization->options;
if ($this->authorization) {
$headers += HttpHeaderEnum::normalizeHeaderArray($this->authorization->headers);
$options += $this->authorization->options;
}
$options += [
CURLOPT_URL => $url,
@@ -136,6 +137,13 @@ abstract class CurlTransport {
return $response;
}
public function getDebugErrorLines() : array
{
$this->stderr->rewind();
return explode(PHP_EOL, $this->stderr->getContents());
}
protected function applyMethod(MethodEnum $method, mixed &$data, array &$headers, array &$options) : void
{
switch ($method) {
+12
View File
@@ -0,0 +1,12 @@
<?php
namespace Ulmus\Api\Transport;
enum Oauth2GrantTypeEnum : string
{
case AuthorizationCode = "authorization_code";
case PKCE = "pkce";
case ClientCredentials = "client_credentials";
case DeviceCode = "device_code";
case RefreshToken = "refresh_token";
}