Skip to content

Commit

Permalink
Merge pull request #322 from moreonion/multi-org
Browse files Browse the repository at this point in the history
auth: Use simplified app-to-app flow
  • Loading branch information
torotil committed Mar 8, 2023
2 parents 2942515 + ff406e7 commit b5d9fe7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion campaignion_auth/campaignion_auth.variable.inc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function campaignion_auth_variable_info($options) {
'title' => t('Impact-stack organization'),
'description' => t('Machine name of the impact-stack organization owning the data of this installation.'),
'type' => 'string',
'default' => basename(conf_path()),
'default' => 'impact-stack>' . basename(conf_path()),
'localize' => FALSE,
];
return $v;
Expand Down
20 changes: 17 additions & 3 deletions campaignion_auth/src/AuthAppClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,32 @@ public function getToken() : string {
if (($cache = cache_get(static::TOKEN_CID)) && $cache->expire > REQUEST_TIME) {
return $cache->data;
}
$token = $this->post('token/' . urlencode($this->organization), [], $this->key)['token'];
$token = $this->post('token', [], $this->key)['token'];
cache_set(static::TOKEN_CID, $token, 'cache', REQUEST_TIME + $this->tokenLifetime);
return $token;
}

/**
* Get editor token.
* Get token for acting as an editor.
*/
public function getEditorToken() : string {
return $this->getUserToken(['editor']);
}

/**
* Get a token for a set of roles.
*
* @param string[] $roles
* Access to these roles should be granted with the resulting JWT.
*
* @return string
* The JWT from the auth app suitable for using in the Authorization header.
*/
public function getUserToken(array $roles = []) : string {
$token = $this->getToken();
$options['headers']['Authorization'] = "Bearer $token";
$token = $this->post('session', [], ['roles' => ['editor']], $options)['token'];
$session['roles'][$this->organization] = $roles;
$token = $this->post('session', [], $session, $options)['token'];
return $token;
}

Expand Down
2 changes: 1 addition & 1 deletion campaignion_auth/tests/AuthAppClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testRequestingTokenTwiceOnTwoObjects() {
$api = $this->instrumentedApi();
$api->expects($this->once())
->method('send')
->with('token/org1', [], [
->with('token', [], [
'public_key' => 'pk_',
'secret_key' => 'sk_',
], [
Expand Down

0 comments on commit b5d9fe7

Please sign in to comment.