Skip to content

Commit fb967af

Browse files
committed
Fixed bug where exp was not set on JWT Tokens when specified
1 parent a78b906 commit fb967af

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/OpenTok/OpenTok.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public function generateToken(string $sessionId, array $payload = array(), bool
153153
'nonce' => mt_rand(),
154154
'scope' => 'session.connect',
155155
'data' => null,
156+
'expireTime' => null,
156157
'initialLayoutClassList' => [],
157158
];
158159

@@ -168,6 +169,11 @@ public function generateToken(string $sessionId, array $payload = array(), bool
168169
unset($payload['initialLayoutClassList']);
169170
}
170171

172+
if (isset($payload['expireTime'])) {
173+
$payload['exp'] = $payload['expireTime'];
174+
unset($payload['expireTime']);
175+
}
176+
171177
return JWT::encode($payload, $this->apiSecret, 'HS256');
172178
}
173179

tests/OpenTokTest/OpenTokTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use OpenTok\Util\Client;
2222
use GuzzleHttp\Middleware;
2323
use GuzzleHttp\HandlerStack;
24-
use OpenTok\Util\Validators;
2524
use PHPUnit\Framework\TestCase;
2625
use GuzzleHttp\Handler\MockHandler;
2726
use OpenTok\Exception\InvalidArgumentException;
@@ -880,6 +879,31 @@ public function testWillGenerateJWTWithClassLayout(): void
880879
$this->assertEquals('focus+main', $decodedArray['initial_layout_class_list']);
881880
}
882881

882+
/**
883+
* Tests that the JWT Token can be generated with the correct expiration time
884+
*/
885+
public function testWillGenerateJWTWithCorrectExpirationTime(): void
886+
{
887+
$openTok = new OpenTok('12345678', 'b60d0b2568f3ea9731bd9d3f71be263ce19f802f');
888+
$expireTime = time() + 3600; // expires in one hour
889+
$token = $openTok->generateToken(
890+
'1_MX4xMjM0NTY3OH4-VGh1IEZlYiAyNyAwNDozODozMSBQU1QgMjAxNH4wLjI0NDgyMjI',
891+
['expireTime' => $expireTime] // expires in one hour
892+
);
893+
894+
$this->assertNotEquals('T1', substr($token, 0, 2));
895+
896+
$decoded = JWT::decode($token, new Key('b60d0b2568f3ea9731bd9d3f71be263ce19f802f', 'HS256'));
897+
$decodedArray = (array) $decoded;
898+
899+
$this->assertEquals('12345678', $decodedArray['iss']);
900+
$this->assertEquals('1_MX4xMjM0NTY3OH4-VGh1IEZlYiAyNyAwNDozODozMSBQU1QgMjAxNH4wLjI0NDgyMjI', $decodedArray['session_id']);
901+
$this->assertEquals('project', $decodedArray['ist']);
902+
$this->assertEquals('session.connect', $decodedArray['scope']);
903+
$this->assertEquals('publisher', $decodedArray['role']);
904+
$this->assertEquals($expireTime, $decodedArray['exp']);
905+
}
906+
883907
public function testStartsArchive(): void
884908
{
885909
// Arrange

0 commit comments

Comments
 (0)