Skip to content

Commit f289f3d

Browse files
Explicitly set the sub if required (#286)
1 parent 195a04f commit f289f3d

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/Client/Credentials/Keypair.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ public function generateJwt(array $claims = []): Token
8888
$builder->withClaim('application_id', $this->credentials['application']);
8989
}
9090

91+
if (isset($claims['sub'])) {
92+
$builder->relatedTo($claims['sub']);
93+
94+
unset($claims['sub']);
95+
}
96+
9197
if (!empty($claims)) {
9298
foreach ($claims as $claim => $value) {
9399
$builder->withClaim($claim, $value);

test/Client/Credentials/KeypairTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,39 @@ public function testAdditionalClaims(): void
9292
$this->assertEquals(900, $payload['nbf']);
9393
}
9494

95+
/**
96+
* @link https://github.com/Vonage/vonage-php-sdk-core/issues/276
97+
*/
98+
public function testExampleConversationJWTWorks()
99+
{
100+
$credentials = new Keypair($this->key, $this->application);
101+
$claims = [
102+
'exp' => strtotime(date('Y-m-d', strtotime('+24 Hours'))),
103+
'sub' => 'apg-cs',
104+
'acl' => [
105+
'paths' => [
106+
'/*/users/**' => (object) [],
107+
'/*/conversations/**' => (object) [],
108+
'/*/sessions/**' => (object) [],
109+
'/*/devices/**' => (object) [],
110+
'/*/image/**' => (object) [],
111+
'/*/media/**' => (object) [],
112+
'/*/applications/**' => (object) [],
113+
'/*/push/**' => (object) [],
114+
'/*/knocking/**' => (object) [],
115+
'/*/legs/**' => (object) [],
116+
]
117+
],
118+
];
119+
120+
$jwt = $credentials->generateJwt($claims);
121+
[, $payload] = $this->decodeJWT($jwt->toString());
122+
123+
$this->assertArrayHasKey('exp', $payload);
124+
$this->assertEquals($claims['exp'], $payload['exp']);
125+
$this->assertEquals($claims['sub'], $payload['sub']);
126+
}
127+
95128
/**
96129
* @param $jwt
97130
*/

0 commit comments

Comments
 (0)