Skip to content

Commit 56d604f

Browse files
authored
Merge pull request #24 from Nexmo/develop-jwt-claims
Changes JWT Generation Signature:
2 parents ff9cb9b + 6f1f955 commit 56d604f

File tree

2 files changed

+82
-21
lines changed

2 files changed

+82
-21
lines changed

src/Client/Credentials/Keypair.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,41 +35,48 @@ public function __construct($privateKey, $application = null)
3535
$this->signer = new Sha256();
3636
}
3737

38-
public function generateJwt($exp = null, $nfb = null, $jti = null, $iat = null)
38+
public function generateJwt(array $claims = [])
3939
{
40-
if(is_null($exp)){
41-
$exp = time() + 60;
40+
$exp = time() + 60;
41+
$iat = time();
42+
$jti = base64_encode(mt_rand());
43+
44+
if(isset($claims['exp'])){
45+
$exp = $claims['exp'];
46+
unset($claims['exp']);
4247
}
4348

44-
if(is_null($iat)){
45-
$iat = time();
49+
if(isset($claims['iat'])){
50+
$iat = $claims['iat'];
51+
unset($claims['iat']);
4652
}
4753

48-
if(is_null($jti)){
49-
$jti = base64_encode(mt_rand());
54+
if(isset($claims['jti'])){
55+
$jti = $claims['jti'];
56+
unset($claims['jti']);
5057
}
5158

5259
$builder = new Builder();
5360
$builder->setIssuedAt($iat)
54-
->setExpiration($exp);
55-
56-
57-
if(isset($this->credentials['application'])){
58-
$builder->set('application_id', $this->credentials['application']);
59-
}
61+
->setExpiration($exp)
62+
->setId($jti);
6063

61-
if(!is_null($nfb)){
62-
$builder->setNotBefore($nfb);
63-
}
6464

65-
if(!is_null($jti)){
66-
$builder->setId($jti);
65+
if(isset($claims['nbf'])){
66+
$builder->setNotBefore($claims['nbf']);
67+
unset($claims['nbf']);
6768
}
6869

6970
if(isset($this->credentials['application'])){
7071
$builder->set('application_id', $this->credentials['application']);
7172
}
7273

74+
if(!empty($claims)){
75+
foreach($claims as $claim => $value){
76+
$builder->set($claim, $value);
77+
}
78+
}
79+
7380
return $builder->sign($this->signer, $this->key)->getToken();
7481
}
7582
}

test/Client/Credentials/KeypairTest.php

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,64 @@ public function testProperties()
4444
$this->assertEquals($this->application, $credentials->application);
4545
}
4646

47-
public function testGetJWT()
47+
public function testDefaultJWT()
4848
{
4949
$credentials = new Keypair($this->key, $this->application);
50-
$jwt = $credentials->generateJwt();
51-
$this->markTestIncomplete('generated JWT, but not tested as valid');
50+
51+
//could use the JWT object, but hope to remove as a dependency
52+
$jwt = (string) $credentials->generateJwt();
53+
54+
list($header, $payload, $sig) = $this->decodeJWT($jwt);
55+
56+
$this->assertArrayHasKey('typ', $header);
57+
$this->assertArrayHasKey('alg', $header);
58+
59+
$this->assertEquals('JWT', $header['typ']);
60+
$this->assertEquals('RS256', $header['alg']);
61+
62+
$this->assertArrayHasKey('application_id', $payload);
63+
$this->assertArrayHasKey('jti', $payload);
64+
65+
$this->assertEquals($this->application, $payload['application_id']);
66+
}
67+
68+
public function testAdditionalClaims()
69+
{
70+
$credentials = new Keypair($this->key, $this->application);
71+
72+
$claims = [
73+
'arbitrary' => [
74+
'nested' => [
75+
'data' => "something"
76+
]
77+
],
78+
'nbf' => 900
79+
];
80+
81+
$jwt = $credentials->generateJwt($claims);
82+
83+
list($header, $payload, $sig) = $this->decodeJWT($jwt);
84+
85+
$this->assertArrayHasKey('arbitrary', $payload);
86+
$this->assertEquals($claims['arbitrary'], $payload['arbitrary']);
87+
88+
$this->assertArrayHasKey('nbf', $payload);
89+
$this->assertEquals(900, $payload['nbf']);
90+
}
91+
92+
protected function decodeJWT($jwt)
93+
{
94+
$parts = explode('.', $jwt);
95+
$this->assertCount(3, $parts);
96+
97+
$header = json_decode(base64_decode($parts[0]), true);
98+
$payload = json_decode(base64_decode($parts[1]), true);
99+
$sig = $parts[2];
100+
101+
return [
102+
$header,
103+
$payload,
104+
$sig
105+
];
52106
}
53107
}

0 commit comments

Comments
 (0)