Skip to content

Commit

Permalink
token passing
Browse files Browse the repository at this point in the history
  • Loading branch information
battis committed Apr 24, 2024
1 parent 23dcd31 commit e8cb0ed
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
23 changes: 9 additions & 14 deletions packages/appengine-client/src/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ class ClientFactory

private Cache $secrets;
private GoogleSecretsToken $storage;
private BlackbaudSKY $oauth2;

public function __construct(?string $projectId = null)
{
$this->secrets = new Cache($projectId ?? $_ENV['GOOGLE_CLOUD_PROJECT']);
$this->storage = new GoogleSecretsToken($this->secrets);
$this->storage = new GoogleSecretsToken($this->secrets, $this->oauth2);
$this->oauth2 = new BlackbaudSKY([
BlackbaudSKY::ACCESS_KEY => $this->secrets->get(self::ACCESS_KEY),
'clientId' => $this->secrets->get(self::CLIENT_ID),
'clientSecret' => $this->secrets->get(self::CLIENT_SECRET),
'redirectUri' => $this->secrets->get(self::REDIRECT_URL),
]);
}

/**
Expand All @@ -35,18 +42,6 @@ public function __construct(?string $projectId = null)
*/
public function get(string $class): BaseEndpoint
{
return new $class(
new Client(
new BlackbaudSKY([
BlackbaudSKY::ACCESS_KEY => $this->secrets->get(
self::ACCESS_KEY
),
'clientId' => $this->secrets->get(self::CLIENT_ID),
'clientSecret' => $this->secrets->get(self::CLIENT_SECRET),
'redirectUri' => $this->secrets->get(self::REDIRECT_URL),
]),
$this->storage
)
);
return new $class(new Client($this->oauth2, $this->storage));
}
}
9 changes: 7 additions & 2 deletions packages/appengine-client/src/GoogleSecretsToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Battis\LazySecrets\Cache;
use Battis\OpenAPI\Client\TokenStorage;
use GrotonSchool\OAuth2\Client\Provider\BlackbaudSKY;
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Token\AccessTokenInterface;

Expand All @@ -12,23 +13,27 @@ class GoogleSecretsToken extends TokenStorage
private const ACCESS_TOKEN = 'BLACKBAUD_API_TOKEN';

private Cache $secrets;
private BlackbaudSKY $oauth2;

/**
* @param ?string|\Battis\LazySecrets\Cache $projectId
*/
public function __construct(mixed $projectId)
public function __construct(mixed $projectId, BlackbaudSKY $oauth2)
{
$this->secrets =
$projectId instanceof Cache
? $projectId
: new Cache($projectId ?? $_ENV['GOOGLE_CLOUD_PROJECT']);
$this->oauth2 = $oauth2;
}

public function getToken(): ?AccessTokenInterface
{
$result = $this->secrets->get(self::ACCESS_TOKEN);
if ($result != null) {
return new AccessToken($result);
$token = new AccessToken($result);
$this->oauth2->setAccessToken($token);
return $token;
}
return null;
}
Expand Down
42 changes: 21 additions & 21 deletions packages/oauth2/src/BlackbaudSKY.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
class BlackbaudSKY extends AbstractProvider
{
use ArrayAccessorTrait;
public const ACCESS_KEY = "Bb-Api-Subscription-Key";
public const ACCESS_TOKEN = "access_token";
public const ACCESS_KEY = 'Bb-Api-Subscription-Key';
public const ACCESS_TOKEN = 'access_token';

public const SESSION_STATE = "oauth2_state";
public const SESSION_STATE = 'oauth2_state';

public const ARG_AUTH_CODE = "authorization_code";
public const ARG_AUTH_CODE = 'authorization_code';

public const PARAM_CODE = "code";
public const PARAM_STATE = "state";
public const PARAM_CODE = 'code';
public const PARAM_STATE = 'state';

public const OPT_PARAMS = "params";
public const OPT_REDIRECT_URI = "redirect_uri";
public const OPT_AUTH_CODE_CALLBACK = "authorization_code_callback";
public const OPT_ACCESS_TOKEN_CALLBACK = "access_token_callback";
public const OPT_ERROR_CALLBACK = "error_callback";
public const OPT_PARAMS = 'params';
public const OPT_REDIRECT_URI = 'redirect_uri';
public const OPT_AUTH_CODE_CALLBACK = 'authorization_code_callback';
public const OPT_ACCESS_TOKEN_CALLBACK = 'access_token_callback';
public const OPT_ERROR_CALLBACK = 'error_callback';

private $accessKey;

Expand All @@ -38,7 +38,7 @@ public function __construct(array $options = [], array $collaborators = [])
parent::__construct($options, $collaborators);

if (empty($options[self::ACCESS_KEY])) {
throw new Exception("Blackbaud access key required");
throw new Exception('Blackbaud access key required');
} else {
$this->accessKey = $options[self::ACCESS_KEY];
}
Expand All @@ -50,17 +50,17 @@ public function __construct(array $options = [], array $collaborators = [])

public function getBaseAuthorizationUrl()
{
return "https://oauth2.sky.blackbaud.com/authorization";
return 'https://oauth2.sky.blackbaud.com/authorization';
}

public function getBaseAccessTokenUrl(array $params)
{
return "https://oauth2.sky.blackbaud.com/token";
return 'https://oauth2.sky.blackbaud.com/token';
}

public function getBaseApiUrl()
{
return "https://api.sky.blackbaud.com";
return 'https://api.sky.blackbaud.com';
}

public function getResourceOwnerDetailsUrl(AccessToken $token)
Expand All @@ -86,20 +86,20 @@ protected function createResourceOwner(array $response, AccessToken $token) {}
protected function getAuthorizationHeaders($token = null)
{
return [
self::ACCESS_KEY => $this->accessKey,
"Authorization" => "Bearer " . $token,
];
self::ACCESS_KEY => $this->accessKey,
'Authorization' => 'Bearer ' . $token,
];
}

public function getAccessToken($grant = "", array $options = [])
public function getAccessToken($grant = '', array $options = [])
{
if (!empty($grant)) {
$this->accessToken = parent::getAccessToken($grant, $options);
return $this->accessToken;
} elseif (!empty($this->accessToken)) {
return $this->accessToken->getToken();
} else {
throw new Exception("Stored access token or grant type required");
throw new Exception('Stored access token or grant type required');
}
}

Expand All @@ -117,7 +117,7 @@ public function endpoint(
if ($this->accessToken) {
$token = $this->accessToken;
} else {
throw new Exception("No access token provided or cached");
throw new Exception('No access token provided or cached');
}
}
return new SkyAPIEndpoint($this, $path, $token);
Expand Down

0 comments on commit e8cb0ed

Please sign in to comment.