Skip to content

Commit d305b4e

Browse files
authored
Merge pull request #348 from opentok/feature/jwt-change
Added legacy transformation, currently no tests for new JWT structure
2 parents be01880 + 37775cb commit d305b4e

File tree

7 files changed

+347
-264
lines changed

7 files changed

+347
-264
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
- ubuntu-latest
77
strategy:
88
matrix:
9-
php: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2']
9+
php: ['8.1', '8.2', '8.3']
1010
steps:
1111
- name: Configure Git
1212
if: ${{ matrix.os == 'windows-latest' }}

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: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22

33
namespace OpenTok;
44

5+
use DateTimeImmutable;
6+
use Firebase\JWT\Key;
7+
use Lcobucci\JWT\Configuration;
8+
use Lcobucci\JWT\Encoding\ChainedFormatter;
9+
use Lcobucci\JWT\Encoding\JoseEncoder;
10+
use Lcobucci\JWT\Signer\Key\InMemory;
11+
use Lcobucci\JWT\Signer\Rsa\Sha256;
12+
use Lcobucci\JWT\Token\Builder;
513
use OpenTok\Util\Client;
614
use OpenTok\Util\Validators;
715
use OpenTok\Exception\InvalidArgumentException;
816
use OpenTok\Exception\UnexpectedValueException;
17+
use Ramsey\Uuid\Uuid;
18+
use Vonage\JWT\TokenGenerator;
919

1020
/**
1121
* Contains methods for creating OpenTok sessions, generating tokens, and working with archives.
@@ -19,7 +29,6 @@
1929
*/
2030
class OpenTok
2131
{
22-
2332
/** @internal */
2433
private $apiKey;
2534
/** @internal */
@@ -104,11 +113,56 @@ public function __construct($apiKey, $apiSecret, $options = array())
104113
*
105114
* </ul>
106115
*
116+
* @param bool $legacy By default, OpenTok uses SHA256 JWTs for authentication. Switching
117+
* legacy to true will create a deprecated T1 token for backwards compatibility.
118+
*
107119
* @return string The token string.
108120
*/
109-
public function generateToken($sessionId, $options = array())
121+
public function generateToken(string $sessionId, array $options = array(), bool $legacy = false): string
122+
{
123+
if ($legacy) {
124+
return $this->returnLegacyToken($sessionId, $options);
125+
}
126+
127+
$issuedAt = new \DateTimeImmutable('@' . time());
128+
129+
$defaults = [
130+
'session_id' => $sessionId,
131+
'role' => Role::PUBLISHER,
132+
'expireTime' => null,
133+
'initial_layout_list' => [''],
134+
'ist' => 'project',
135+
'nonce' => mt_rand(),
136+
'scope' => 'session.connect'
137+
];
138+
139+
$options = array_merge($defaults, array_intersect_key($options, $defaults));
140+
141+
$builder = new Builder(new JoseEncoder(), ChainedFormatter::default());
142+
$builder = $builder->issuedBy($this->apiKey);
143+
144+
if ($options['expireTime']) {
145+
$expiry = new \DateTimeImmutable('@' . $options['expireTime']);
146+
$builder = $builder->expiresAt($expiry);
147+
}
148+
149+
unset($options['expireTime']);
150+
151+
$builder = $builder->issuedAt($issuedAt);
152+
$builder = $builder->canOnlyBeUsedAfter($issuedAt);
153+
$builder = $builder->identifiedBy(bin2hex(random_bytes(16)));
154+
155+
foreach ($options as $key => $value) {
156+
$builder = $builder->withClaim($key, $value);
157+
}
158+
159+
$token = $builder->getToken(new \Lcobucci\JWT\Signer\Hmac\Sha256(), InMemory::plainText($this->apiSecret));
160+
161+
return $token->toString();
162+
}
163+
164+
private function returnLegacyToken(string $sessionId, array $options = []): string
110165
{
111-
// unpack optional arguments (merging with default values) into named variables
112166
$defaults = array(
113167
'role' => Role::PUBLISHER,
114168
'expireTime' => null,
@@ -237,7 +291,6 @@ public function createSession($options = array())
237291
}
238292

239293
if (array_key_exists('e2ee', $options) && $options['e2ee']) {
240-
241294
if (array_key_exists('mediaMode', $options) && $options['mediaMode'] !== MediaMode::ROUTED) {
242295
throw new InvalidArgumentException('MediaMode must be routed in order to enable E2EE');
243296
}
@@ -885,13 +938,13 @@ public function startBroadcast(string $sessionId, array $options = []): Broadcas
885938
Validators::validateResolution($options['resolution']);
886939
}
887940

888-
if (isset($options['outputs']['hls'])) {
889-
Validators::validateBroadcastOutputOptions($options['outputs']['hls']);
890-
}
941+
if (isset($options['outputs']['hls'])) {
942+
Validators::validateBroadcastOutputOptions($options['outputs']['hls']);
943+
}
891944

892-
if (isset($options['outputs']['rtmp'])) {
893-
Validators::validateRtmpStreams($options['outputs']['rtmp']);
894-
}
945+
if (isset($options['outputs']['rtmp'])) {
946+
Validators::validateRtmpStreams($options['outputs']['rtmp']);
947+
}
895948

896949
$defaults = [
897950
'layout' => Layout::getBestFit(),
@@ -900,11 +953,11 @@ public function startBroadcast(string $sessionId, array $options = []): Broadcas
900953
'streamMode' => 'auto',
901954
'resolution' => '640x480',
902955
'maxBitRate' => 2000000,
903-
'outputs' => [
904-
'hls' => [
905-
'dvr' => false,
906-
'lowLatency' => false
907-
]
956+
'outputs' => [
957+
'hls' => [
958+
'dvr' => false,
959+
'lowLatency' => false
960+
]
908961
]
909962
];
910963

@@ -1316,8 +1369,7 @@ public function startCaptions(
13161369
?int $maxDuration = null,
13171370
?bool $partialCaptions = null,
13181371
?string $statusCallbackUrl = null
1319-
): array
1320-
{
1372+
): array {
13211373
return $this->client->startCaptions(
13221374
$sessionId,
13231375
$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
/**

0 commit comments

Comments
 (0)