Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds support for additional claims in JWT tokens #171

Merged
merged 2 commits into from Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Middleware/ScopedAccessTokenMiddleware.php
Expand Up @@ -113,7 +113,7 @@ public function __construct(
* $client = new Client([
* 'handler' => $stack,
* 'base_url' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/',
* 'auth' => 'google_auth' // authorize all requests
* 'auth' => 'scoped' // authorize all requests
* ]);
*
* $res = $client->get('myproject/taskqueues/myqueue');
Expand Down
29 changes: 29 additions & 0 deletions src/OAuth2.php
Expand Up @@ -238,6 +238,12 @@ class OAuth2 implements FetchAuthTokenInterface
*/
private $extensionParams;

/**
* When using the toJwt function, these claims will be added to the JWT
* payload.
*/
private $additionalClaims;

/**
* Create a new OAuthCredentials.
*
Expand Down Expand Up @@ -322,6 +328,7 @@ public function __construct(array $config)
'signingKey' => null,
'signingAlgorithm' => null,
'scope' => null,
'additionalClaims' => [],
], $config);

$this->setAuthorizationUri($opts['authorizationUri']);
Expand All @@ -340,6 +347,7 @@ public function __construct(array $config)
$this->setSigningAlgorithm($opts['signingAlgorithm']);
$this->setScope($opts['scope']);
$this->setExtensionParams($opts['extensionParams']);
$this->setAdditionalClaims($opts['additionalClaims']);
$this->updateToken($opts);
}

Expand Down Expand Up @@ -413,6 +421,7 @@ public function toJwt(array $config = [])
if (!(is_null($this->getSub()))) {
$assertion['sub'] = $this->getSub();
}
$assertion += $this->getAdditionalClaims();

return $this->jwtEncode($assertion, $this->getSigningKey(),
$this->getSigningAlgorithm());
Expand Down Expand Up @@ -1212,6 +1221,26 @@ public function setRefreshToken($refreshToken)
$this->refreshToken = $refreshToken;
}

/**
* Sets additional claims to be included in the JWT token
*
* @param array $additionalClaims
*/
public function setAdditionalClaims(array $additionalClaims)
{
$this->additionalClaims = $additionalClaims;
}

/**
* Gets the additional claims to be included in the JWT token.
*
* @return array
*/
public function getAdditionalClaims()
{
return $this->additionalClaims;
}

/**
* The expiration of the last received token.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/OAuth2Test.php
Expand Up @@ -454,6 +454,21 @@ public function testCanRS256EncodeAValidPayload()
$this->assertEquals($roundTrip->scope, $testConfig['scope']);
}

public function testCanHaveAdditionalClaims()
{
$publicKey = file_get_contents(__DIR__ . '/fixtures' . '/public.pem');
$privateKey = file_get_contents(__DIR__ . '/fixtures' . '/private.pem');
$testConfig = $this->signingMinimal;
$targetAud = '123@456.com';
$testConfig['additionalClaims'] = ['target_audience' => $targetAud];
$o = new OAuth2($testConfig);
$o->setSigningAlgorithm('RS256');
$o->setSigningKey($privateKey);
$payload = $o->toJwt();
$roundTrip = $this->jwtDecode($payload, $publicKey, array('RS256'));
$this->assertEquals($roundTrip->target_audience, $targetAud);
}

private function jwtDecode()
{
$args = func_get_args();
Expand Down