99
1010namespace VonageTest \Client \Credentials ;
1111
12+ use Lcobucci \JWT \Signer \Key \InMemory ;
13+ use Vonage \Client \Exception \Validation ;
1214use VonageTest \VonageTestCase ;
1315use Vonage \Client \Credentials \Keypair ;
1416
1921
2022class KeypairTest extends VonageTestCase
2123{
22- protected $ key ;
23- protected $ application = 'c90ddd99-9a5d-455f-8ade-dde4859e590e ' ;
24+ protected string $ key ;
25+ protected string $ application = 'c90ddd99-9a5d-455f-8ade-dde4859e590e ' ;
2426
2527 public function setUp (): void
2628 {
@@ -36,6 +38,14 @@ public function testAsArray(): void
3638 $ this ->assertEquals ($ this ->application , $ array ['application ' ]);
3739 }
3840
41+ public function testGetKey (): void
42+ {
43+ $ credentials = new Keypair ($ this ->key , $ this ->application );
44+
45+ $ key = $ credentials ->getKey ();
46+ $ this ->assertInstanceOf (InMemory::class, $ key );
47+ }
48+
3949 public function testProperties (): void
4050 {
4151 $ credentials = new Keypair ($ this ->key , $ this ->application );
@@ -48,7 +58,6 @@ public function testDefaultJWT(): void
4858 {
4959 $ credentials = new Keypair ($ this ->key , $ this ->application );
5060
51- //could use the JWT object, but hope to remove as a dependency
5261 $ jwt = (string )$ credentials ->generateJwt ()->toString ();
5362
5463 [$ header , $ payload ] = $ this ->decodeJWT ($ jwt );
@@ -71,27 +80,100 @@ public function testAdditionalClaims(): void
7180 'nested ' => [
7281 'data ' => "something "
7382 ]
74- ],
75- 'nbf ' => 900
83+ ]
84+ ];
85+
86+ $ jwt = $ credentials ->generateJwt ($ claims );
87+ [, $ payload ] = $ this ->decodeJWT ($ jwt ->toString ());
88+
89+ $ this ->assertArrayHasKey ('arbitrary ' , $ payload );
90+ $ this ->assertEquals ($ claims ['arbitrary ' ], $ payload ['arbitrary ' ]);
91+ }
92+
93+ public function testJtiClaim (): void
94+ {
95+ $ credentials = new Keypair ($ this ->key , $ this ->application );
96+
97+ $ claims = [
98+ 'jti ' => '9a1b8ca6-4406-4530-9940-3cde9d41de3f '
99+ ];
100+
101+ $ jwt = $ credentials ->generateJwt ($ claims );
102+ [, $ payload ] = $ this ->decodeJWT ($ jwt ->toString ());
103+
104+ $ this ->assertArrayHasKey ('jti ' , $ payload );
105+ $ this ->assertEquals ($ claims ['jti ' ], $ payload ['jti ' ]);
106+ }
107+
108+ public function testTtlClaim (): void
109+ {
110+ $ credentials = new Keypair ($ this ->key , $ this ->application );
111+
112+ $ claims = [
113+ 'ttl ' => 900
114+ ];
115+
116+ $ jwt = $ credentials ->generateJwt ($ claims );
117+ [, $ payload ] = $ this ->decodeJWT ($ jwt ->toString ());
118+
119+ $ this ->assertArrayHasKey ('exp ' , $ payload );
120+ $ this ->assertEquals (time () + 900 , $ payload ['exp ' ]);
121+ }
122+
123+ public function testNbfNotSupported (): void
124+ {
125+ set_error_handler (static function (int $ errno , string $ errstr ) {
126+ throw new \Exception ($ errstr , $ errno );
127+ }, E_USER_WARNING );
128+
129+ $ this ->expectExceptionMessage ('NotBefore Claim is not supported in Vonage JWT ' );
130+
131+ $ credentials = new Keypair ($ this ->key , $ this ->application );
132+
133+ $ claims = [
134+ 'nbf ' => time () + 900
76135 ];
77136
78137 $ jwt = $ credentials ->generateJwt ($ claims );
79138 [, $ payload ] = $ this ->decodeJWT ($ jwt ->toString ());
80139
81140 $ this ->assertArrayHasKey ('arbitrary ' , $ payload );
82141 $ this ->assertEquals ($ claims ['arbitrary ' ], $ payload ['arbitrary ' ]);
83- $ this ->assertArrayHasKey ('nbf ' , $ payload );
84- $ this ->assertEquals (900 , $ payload ['nbf ' ]);
142+
143+ restore_error_handler ();
144+ }
145+
146+ public function testExpNotSupported (): void
147+ {
148+ set_error_handler (static function (int $ errno , string $ errstr ) {
149+ throw new \Exception ($ errstr , $ errno );
150+ }, E_USER_WARNING );
151+
152+ $ this ->expectExceptionMessage ('Expiry date is automatically generated from now and TTL, so cannot be passed in
153+ as an argument in claims ' );
154+
155+ $ credentials = new Keypair ($ this ->key , $ this ->application );
156+
157+ $ claims = [
158+ 'exp ' => time () + 900
159+ ];
160+
161+ $ jwt = $ credentials ->generateJwt ($ claims );
162+ [, $ payload ] = $ this ->decodeJWT ($ jwt ->toString ());
163+
164+ $ this ->assertArrayHasKey ('arbitrary ' , $ payload );
165+ $ this ->assertEquals ($ claims ['arbitrary ' ], $ payload ['arbitrary ' ]);
166+
167+ restore_error_handler ();
85168 }
86169
87170 /**
88171 * @link https://github.com/Vonage/vonage-php-sdk-core/issues/276
89172 */
90- public function testExampleConversationJWTWorks ()
173+ public function testExampleConversationJWTWorks (): void
91174 {
92175 $ credentials = new Keypair ($ this ->key , $ this ->application );
93176 $ claims = [
94- 'exp ' => strtotime (date ('Y-m-d ' , strtotime ('+24 Hours ' ))),
95177 'sub ' => 'apg-cs ' ,
96178 'acl ' => [
97179 'paths ' => [
@@ -113,7 +195,6 @@ public function testExampleConversationJWTWorks()
113195 [, $ payload ] = $ this ->decodeJWT ($ jwt ->toString ());
114196
115197 $ this ->assertArrayHasKey ('exp ' , $ payload );
116- $ this ->assertEquals ($ claims ['exp ' ], $ payload ['exp ' ]);
117198 $ this ->assertEquals ($ claims ['sub ' ], $ payload ['sub ' ]);
118199 }
119200
0 commit comments