Skip to content

Commit 4da3b60

Browse files
committed
Added legacy transformation, currently no tests for new JWT structure
1 parent ef2e91c commit 4da3b60

File tree

6 files changed

+58
-28
lines changed

6 files changed

+58
-28
lines changed

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"johnstevenson/json-works": "~1.1",
3434
"firebase/php-jwt": "^6.0",
3535
"guzzlehttp/guzzle": "~6.0|~7.0",
36-
"ext-json": "*"
36+
"ext-json": "*",
37+
"vonage/jwt": "^0.5.1"
3738
},
3839
"require-dev": {
3940
"phpunit/phpunit": "^7.4|^8.0",
@@ -55,5 +56,10 @@
5556
"OpenTok\\": "src/OpenTok",
5657
"OpenTokTest\\": "tests/OpenTokTest"
5758
}
59+
},
60+
"config": {
61+
"allow-plugins": {
62+
"php-http/discovery": true
63+
}
5864
}
5965
}

sample/Archiving/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ $app->get('/host', function () use ($app, $sessionId) {
5656

5757
$token = $app->opentok->generateToken($sessionId, array(
5858
'role' => Role::MODERATOR
59-
));
59+
), true);
6060

6161
$app->render('host.html', array(
6262
'apiKey' => $app->apiKey,

src/OpenTok/OpenTok.php

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use OpenTok\Util\Validators;
77
use OpenTok\Exception\InvalidArgumentException;
88
use OpenTok\Exception\UnexpectedValueException;
9+
use Vonage\JWT\TokenGenerator;
910

1011
/**
1112
* Contains methods for creating OpenTok sessions, generating tokens, and working with archives.
@@ -19,7 +20,6 @@
1920
*/
2021
class OpenTok
2122
{
22-
2323
/** @internal */
2424
private $apiKey;
2525
/** @internal */
@@ -104,11 +104,37 @@ public function __construct($apiKey, $apiSecret, $options = array())
104104
*
105105
* </ul>
106106
*
107+
* @param bool $legacy By default, OpenTok uses SHA256 JWTs for authentication. Switching
108+
* legacy to true will create a deprecated T1 token for backwards compatibility.
109+
*
107110
* @return string The token string.
108111
*/
109-
public function generateToken($sessionId, $options = array())
112+
public function generateToken($sessionId, $options = array(), $legacy = false)
113+
{
114+
if ($legacy) {
115+
return $this->returnLegacyToken($sessionId, $options);
116+
}
117+
118+
$defaults = [
119+
'sessionId' => $sessionId,
120+
'role' => Role::PUBLISHER,
121+
'exp' => null,
122+
'data' => null,
123+
'initialLayoutClassList' => [''],
124+
];
125+
126+
$options = array_merge($defaults, array_intersect_key($options, $defaults));
127+
128+
$generator = new TokenGenerator($this->apiKey, $this->apiSecret);
129+
foreach ($options as $key => $value) {
130+
$generator->addClaim($key, $value);
131+
}
132+
133+
return $generator->generate();
134+
}
135+
136+
private function returnLegacyToken(string $sessionId, array $options = []): string
110137
{
111-
// unpack optional arguments (merging with default values) into named variables
112138
$defaults = array(
113139
'role' => Role::PUBLISHER,
114140
'expireTime' => null,
@@ -237,7 +263,6 @@ public function createSession($options = array())
237263
}
238264

239265
if (array_key_exists('e2ee', $options) && $options['e2ee']) {
240-
241266
if (array_key_exists('mediaMode', $options) && $options['mediaMode'] !== MediaMode::ROUTED) {
242267
throw new InvalidArgumentException('MediaMode must be routed in order to enable E2EE');
243268
}
@@ -875,13 +900,13 @@ public function startBroadcast(string $sessionId, array $options = []): Broadcas
875900
Validators::validateResolution($options['resolution']);
876901
}
877902

878-
if (isset($options['outputs']['hls'])) {
879-
Validators::validateBroadcastOutputOptions($options['outputs']['hls']);
880-
}
903+
if (isset($options['outputs']['hls'])) {
904+
Validators::validateBroadcastOutputOptions($options['outputs']['hls']);
905+
}
881906

882-
if (isset($options['outputs']['rtmp'])) {
883-
Validators::validateRtmpStreams($options['outputs']['rtmp']);
884-
}
907+
if (isset($options['outputs']['rtmp'])) {
908+
Validators::validateRtmpStreams($options['outputs']['rtmp']);
909+
}
885910

886911
$defaults = [
887912
'layout' => Layout::getBestFit(),
@@ -890,11 +915,11 @@ public function startBroadcast(string $sessionId, array $options = []): Broadcas
890915
'streamMode' => 'auto',
891916
'resolution' => '640x480',
892917
'maxBitRate' => 2000000,
893-
'outputs' => [
894-
'hls' => [
895-
'dvr' => false,
896-
'lowLatency' => false
897-
]
918+
'outputs' => [
919+
'hls' => [
920+
'dvr' => false,
921+
'lowLatency' => false
922+
]
898923
]
899924
];
900925

@@ -1306,8 +1331,7 @@ public function startCaptions(
13061331
?int $maxDuration = null,
13071332
?bool $partialCaptions = null,
13081333
?string $statusCallbackUrl = null
1309-
): array
1310-
{
1334+
): array {
13111335
return $this->client->startCaptions(
13121336
$sessionId,
13131337
$token,

src/OpenTok/Session.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ public function __toString()
154154
*
155155
* @return string The token string.
156156
*/
157-
public function generateToken($options = array())
157+
public function generateToken($options = array(), bool $legacy = false)
158158
{
159-
return $this->opentok->generateToken($this->sessionId, $options);
159+
return $this->opentok->generateToken($this->sessionId, $options, $legacy);
160160
}
161161

162162
/**

tests/OpenTokTest/OpenTokTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ public function testGeneratesToken(): void
582582
$opentok = new OpenTok($bogusApiKey, $bogusApiSecret);
583583

584584
// Act
585-
$token = $opentok->generateToken($sessionId);
585+
$token = $opentok->generateToken($sessionId, [], true);
586586

587587
// Assert
588588
$this->assertIsString($token);
@@ -613,7 +613,7 @@ public function testGeneratesTokenWithRole(): void
613613
$opentok = new OpenTok($bogusApiKey, $bogusApiSecret);
614614

615615
// Act
616-
$token = $opentok->generateToken($sessionId, array('role' => Role::MODERATOR));
616+
$token = $opentok->generateToken($sessionId, array('role' => Role::MODERATOR), true);
617617

618618
// Assert
619619
$this->assertIsString($token);
@@ -645,7 +645,7 @@ public function testGeneratesTokenWithExpireTime(): void
645645
// Act
646646
// expires in one hour (60 seconds * 60 minutes)
647647
$inOneHour = time() + (60 * 60);
648-
$token = $opentok->generateToken($sessionId, array('expireTime' => $inOneHour ));
648+
$token = $opentok->generateToken($sessionId, array('expireTime' => $inOneHour ), true);
649649

650650
// Assert
651651
$this->assertIsString($token);
@@ -675,7 +675,7 @@ public function testGeneratesTokenWithData(): void
675675

676676
// Act
677677
$userStatus = '{nick:"johnny",status:"hey there fellas!"}';
678-
$token = $opentok->generateToken($sessionId, array('data' => $userStatus ));
678+
$token = $opentok->generateToken($sessionId, array('data' => $userStatus), true);
679679

680680
// Assert
681681
$this->assertIsString($token);
@@ -712,7 +712,7 @@ public function testGeneratesTokenWithInitialLayoutClassList(): void
712712
// Act
713713
$token = $opentok->generateToken($sessionId, array(
714714
'initialLayoutClassList' => $initialLayouClassList
715-
));
715+
), true);
716716

717717
// Assert
718718
$this->assertIsString($token);
@@ -737,7 +737,7 @@ public function testFailsWhenGeneratingTokenUsingInvalidRole(): void
737737
{
738738
$this->expectException('InvalidArgumentException');
739739
$this->setupOT();
740-
$token = $this->opentok->generateToken('SESSIONID', array('role' => 'notarole'));
740+
$token = $this->opentok->generateToken('SESSIONID', array('role' => 'notarole'), true);
741741
}
742742

743743
public function testStartsArchive(): void

tests/OpenTokTest/SessionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function testGeneratesToken()
156156
$opentok = new OpenTok($bogusApiKey, $bogusApiSecret);
157157
$session = new Session($opentok, $sessionId);
158158

159-
$token = $session->generateToken();
159+
$token = $session->generateToken([], true);
160160

161161
$this->assertIsString($token);
162162
$decodedToken = TestHelpers::decodeToken($token);

0 commit comments

Comments
 (0)