-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from k10r/master
Add GrantType for JSON Web Token (JWT)
- Loading branch information
Showing
7 changed files
with
224 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace CommerceGuys\Guzzle\Oauth2\GrantType; | ||
|
||
use GuzzleHttp\ClientInterface; | ||
use CommerceGuys\Guzzle\Oauth2\GrantType\GrantTypeBase; | ||
use JWT; | ||
use SplFileObject; | ||
use InvalidArgumentException; | ||
|
||
/** | ||
* JSON Web Token (JWT) Bearer Token Profiles for OAuth 2.0 | ||
* | ||
* @link http://tools.ietf.org/html/draft-jones-oauth-jwt-bearer-04 | ||
*/ | ||
class JwtBearer extends GrantTypeBase | ||
{ | ||
protected $grantType = 'urn:ietf:params:oauth:grant-type:jwt-bearer'; | ||
|
||
/** | ||
* @param ClientInterface $client | ||
* @param array $config | ||
*/ | ||
public function __construct(ClientInterface $client, array $config = []) | ||
{ | ||
parent::__construct($client, $config); | ||
|
||
if (!($this->config->get('private_key') instanceof SplFileObject)) { | ||
throw new InvalidArgumentException('private_key needs to be instance of SplFileObject'); | ||
} | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function getRequired() | ||
{ | ||
return array_merge(parent::getRequired(), ['private_key']); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function getAdditionalOptions() | ||
{ | ||
return [ | ||
'body' => [ | ||
'assertion' => $this->computeJwt() | ||
] | ||
]; | ||
} | ||
|
||
/** | ||
* Compute JWT, signing with provided private key | ||
*/ | ||
protected function computeJwt() | ||
{ | ||
$payload = [ | ||
'iss' => $this->config->get('client_id'), | ||
'aud' => sprintf('%s/%s', rtrim($this->client->getBaseUrl(), '/'), ltrim($this->config->get('token_url'), '/')), | ||
'exp' => time() + 60 * 60, | ||
'iat' => time() | ||
]; | ||
|
||
return JWT::encode($payload, $this->readPrivateKey($this->config->get('private_key')), 'RS256'); | ||
} | ||
|
||
/** | ||
* Read private key | ||
* | ||
* @param SplFileObject $privateKey | ||
*/ | ||
protected function readPrivateKey(SplFileObject $privateKey) | ||
{ | ||
$key = ''; | ||
while (!$privateKey->eof()) { | ||
$key .= $privateKey->fgets(); | ||
} | ||
return $key; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace CommerceGuys\Guzzle\Oauth2\Tests\GrantType; | ||
|
||
use CommerceGuys\Guzzle\Oauth2\GrantType\JwtBearer; | ||
use CommerceGuys\Guzzle\Oauth2\Tests\TestBase; | ||
use SplFileObject; | ||
|
||
class JwtBearerTest extends TestBase | ||
{ | ||
public function testMissingConfigException() | ||
{ | ||
$this->setExpectedException('\\InvalidArgumentException', 'Config is missing the following keys: client_id, private_key'); | ||
new JwtBearer($this->getClient()); | ||
} | ||
|
||
public function testPrivateKeyNotSplFileObject() | ||
{ | ||
$this->setExpectedException('\\InvalidArgumentException', 'private_key needs to be instance of SplFileObject'); | ||
$grantType = new JwtBearer($this->getClient(), [ | ||
'client_id' => 'testClient', | ||
'private_key' => 'INVALID' | ||
]); | ||
} | ||
|
||
public function testValidRequestGetsToken() | ||
{ | ||
$grantType = new JwtBearer($this->getClient(), [ | ||
'client_id' => 'testClient', | ||
'private_key' => new SplFileObject(__DIR__ . '/../private.key') | ||
]); | ||
$token = $grantType->getToken(); | ||
$this->assertNotEmpty($token->getToken()); | ||
$this->assertTrue($token->getExpires()->getTimestamp() > time()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
-----BEGIN PRIVATE KEY----- | ||
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCTT//DLGGM1jEJ | ||
kBPc69qjiS07nsYXwsawFChRBGu03MCUcyBr7wz6oq1jkCyJ0ZHAcTmY6bpdCpSf | ||
JM3dt16/GRvz+edmYB/CxnnIGQmlmH+kGAAQkA1wtkZLhYYRbpdb/aDE0Azx6M8h | ||
ibe+dOidlJJQhg1SSwPcVUVWpqs4atelf9QeQtrD92igUZhwpHjQgAE4WCrXPpaO | ||
x/07QiUge0pWHpFBamHwoalu3JLmWEED2uiAunKiY9+VJi6GAJABzIHbsh6i1XU3 | ||
ATF2WNo7qgY2e8XlDanQypvq93qq+erwynclNJh+5yVhDCqD+IX0/knGb2MMdHTP | ||
XiU8pLjPAgMBAAECggEAJMl/h0/X9IGwsUCnlS3Y5anl/9OAiIJ9d48xGjpOY1YV | ||
SX0OhaWmyhhB0HE6jhglm7cquQL1JTL1NmDMgCfAo1wz3NN1c91hURSbaNrHy/Cv | ||
P1029uviT1lVaJqphkTly3Uk5sFF2ktXHnrzxb4QMPnfJ/ix7vEIv8cTj7YDYAz9 | ||
WzdPk3dZ9HyWtt+hVc1fJSNdaqyNQyRzgiUuMCIu37j/MQ5mPWucm4siZcSzF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