Skip to content

Commit 8eeff73

Browse files
committed
Merge main
2 parents a227ace + c1fc22d commit 8eeff73

File tree

3 files changed

+39
-64
lines changed

3 files changed

+39
-64
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"php": "^7.2|^8.0",
3232
"ext-xml": "*",
3333
"johnstevenson/json-works": "~1.1",
34-
"firebase/php-jwt": "^6.0",
34+
"firebase/php-jwt": "^6.11",
3535
"guzzlehttp/guzzle": "~6.0|~7.0",
3636
"ext-json": "*",
3737
"vonage/jwt": "^0.5.1"

src/OpenTok/OpenTok.php

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace OpenTok;
44

55
use DateTimeImmutable;
6+
use Firebase\JWT\JWT;
67
use Firebase\JWT\Key;
78
use Lcobucci\JWT\Configuration;
89
use Lcobucci\JWT\Encoding\ChainedFormatter;
@@ -105,7 +106,7 @@ public function __construct($apiKey, $apiSecret, $options = array())
105106
* @param string $sessionId The session ID corresponding to the session to which the user
106107
* will connect.
107108
*
108-
* @param array $options This array defines options for the token. This array includes the
109+
* @param array $payload This array defines options for the token. This array includes the
109110
* following keys, all of which are optional:
110111
*
111112
* <ul>
@@ -135,59 +136,34 @@ public function __construct($apiKey, $apiSecret, $options = array())
135136
* </ul>
136137
*
137138
* @param bool $legacy By default, OpenTok uses SHA256 JWTs for authentication. Switching
138-
* legacy to true will create a deprecated T1 token for backwards compatibility.
139+
* legacy to true will create a T1 token for backwards compatibility.
139140
*
140141
* Optionally, you can set $vonage to true and it will generate a Vonage Video token if you are using
141142
* the shim behaviour.
142143
*
143144
* @return string The token string.
144145
*/
145-
public function generateToken(
146-
string $sessionId,
147-
array $options = array(),
148-
bool $legacy = false
149-
): string
146+
public function generateToken(string $sessionId, array $payload = array(), bool $legacy = false): string
150147
{
151-
// Note, JWT generation disabled due to a backend bug regarding `exp` claims being mandatory - CRT
152-
// if ($legacy) {
153-
return $this->returnLegacyToken($sessionId, $options);
154-
// }
155-
156-
// $issuedAt = new \DateTimeImmutable('@' . time());
157-
158-
// $defaults = [
159-
// 'session_id' => $sessionId,
160-
// 'role' => Role::PUBLISHER,
161-
// 'expireTime' => null,
162-
// 'initial_layout_list' => [''],
163-
// 'ist' => 'project',
164-
// 'nonce' => mt_rand(),
165-
// 'scope' => 'session.connect'
166-
// ];
167-
168-
// $options = array_merge($defaults, array_intersect_key($options, $defaults));
169-
170-
// $builder = new Builder(new JoseEncoder(), ChainedFormatter::default());
171-
// $builder = $builder->issuedBy($this->apiKey);
172-
173-
// if ($options['expireTime']) {
174-
// $expiry = new \DateTimeImmutable('@' . $options['expireTime']);
175-
// $builder = $builder->expiresAt($expiry);
176-
// }
177-
178-
// unset($options['expireTime']);
148+
if ($legacy) {
149+
return $this->returnLegacyToken($sessionId, $payload);
150+
}
179151

180-
// $builder = $builder->issuedAt($issuedAt);
181-
// $builder = $builder->canOnlyBeUsedAfter($issuedAt);
182-
// $builder = $builder->identifiedBy(bin2hex(random_bytes(16)));
152+
$issuedAt = new \DateTimeImmutable('@' . time());
183153

184-
// foreach ($options as $key => $value) {
185-
// $builder = $builder->withClaim($key, $value);
186-
// }
154+
$defaults = [
155+
'iss' => $this->apiKey,
156+
'iat' => $issuedAt->getTimestamp(),
157+
'session_id' => $sessionId,
158+
'role' => Role::PUBLISHER,
159+
'ist' => 'project',
160+
'nonce' => mt_rand(),
161+
'scope' => 'session.connect'
162+
];
187163

188-
// $token = $builder->getToken(new \Lcobucci\JWT\Signer\Hmac\Sha256(), InMemory::plainText($this->apiSecret));
164+
$payload = array_merge($defaults, array_intersect_key($payload, $defaults));
189165

190-
// return $token->toString();
166+
return JWT::encode($payload, $this->apiSecret, 'HS256');
191167
}
192168

193169
private function returnLegacyToken(string $sessionId, array $options = []): string

tests/OpenTokTest/OpenTokTest.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace OpenTokTest;
44

5+
use Firebase\JWT\JWT;
6+
use Firebase\JWT\Key;
57
use Lcobucci\JWT\Configuration;
68
use Lcobucci\JWT\Token\Plain;
79
use OpenTok\Render;
@@ -753,7 +755,7 @@ public function testWillCreateLegacyT1WhenRequested(): void
753755
public function testWillCreateLegacyT1DirectlyToBypassExpBug(): void
754756
{
755757
$openTok = new OpenTok('12345678', '0123456789abcdef0123456789abcdef0123456789');
756-
$token = $openTok->generateToken('1_MX4xMjM0NTY3OH4-VGh1IEZlYiAyNyAwNDozODozMSBQU1QgMjAxNH4wLjI0NDgyMjI', []);
758+
$token = $openTok->generateToken('1_MX4xMjM0NTY3OH4-VGh1IEZlYiAyNyAwNDozODozMSBQU1QgMjAxNH4wLjI0NDgyMjI', [], true);
757759

758760
$this->assertEquals('T1', substr($token, 0, 2));
759761
}
@@ -796,26 +798,23 @@ public function testWillHitVonageVideoWithVonageJwt(): void
796798
// {
797799
// $openTok = new OpenTok('my-api-key', 'my-super-long-and-cool-api-secret');
798800
// $token = $openTok->generateToken('some-token-value');
801+
public function testWillGenerateSha256Token(): void
802+
{
803+
$openTok = new OpenTok('12345678', '0123456789abcdef0123456789abcdef0123456789');
804+
$token = $openTok->generateToken('1_MX4xMjM0NTY3OH4-VGh1IEZlYiAyNyAwNDozODozMSBQU1QgMjAxNH4wLjI0NDgyMjI');
805+
806+
$this->assertNotEquals('T1', substr($token, 0, 2));
799807

800-
// $config = Configuration::forSymmetricSigner(
801-
// new \Lcobucci\JWT\Signer\Hmac\Sha256(),
802-
// \Lcobucci\JWT\Signer\Key\InMemory::plainText('my-super-long-and-cool-api-secret')
803-
// );
804-
805-
// $token = $config->parser()->parse($token);
806-
// $this->assertInstanceOf(Plain::class, $token);
807-
808-
// $this->assertTrue($config->validator()->validate($token, new \Lcobucci\JWT\Validation\Constraint\SignedWith(
809-
// $config->signer(),
810-
// $config->signingKey()
811-
// )));
812-
813-
// $this->assertEquals('my-api-key', $token->claims()->get('iss'));
814-
// $this->assertEquals('some-token-value', $token->claims()->get('session_id'));
815-
// $this->assertEquals('publisher', $token->claims()->get('role'));
816-
// $this->assertEquals('project', $token->claims()->get('ist'));
817-
// $this->assertEquals('session.connect', $token->claims()->get('scope'));
818-
// }
808+
$decoded = JWT::decode($token, new Key('0123456789abcdef0123456789abcdef0123456789', 'HS256'));
809+
$decodedArray = (array) $decoded;
810+
811+
$this->assertEquals('12345678', $decodedArray['iss']);
812+
$this->assertEquals('1_MX4xMjM0NTY3OH4-VGh1IEZlYiAyNyAwNDozODozMSBQU1QgMjAxNH4wLjI0NDgyMjI', $decodedArray['session_id']);
813+
$this->assertEquals('project', $decodedArray['ist']);
814+
$this->assertEquals('session.connect', $decodedArray['scope']);
815+
$this->assertEquals('publisher', $decodedArray['role']);
816+
817+
}
819818

820819
public function testStartsArchive(): void
821820
{

0 commit comments

Comments
 (0)