Skip to content

Commit 6284770

Browse files
committed
Use firebase JWT
1 parent 37775cb commit 6284770

File tree

2 files changed

+10
-29
lines changed

2 files changed

+10
-29
lines changed

composer.json

+1-1
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

+9-28
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;
@@ -84,7 +85,7 @@ public function __construct($apiKey, $apiSecret, $options = array())
8485
* @param string $sessionId The session ID corresponding to the session to which the user
8586
* will connect.
8687
*
87-
* @param array $options This array defines options for the token. This array includes the
88+
* @param array $payload This array defines options for the token. This array includes the
8889
* following keys, all of which are optional:
8990
*
9091
* <ul>
@@ -114,51 +115,31 @@ public function __construct($apiKey, $apiSecret, $options = array())
114115
* </ul>
115116
*
116117
* @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+
* legacy to true will create a T1 token for backwards compatibility.
118119
*
119120
* @return string The token string.
120121
*/
121-
public function generateToken(string $sessionId, array $options = array(), bool $legacy = false): string
122+
public function generateToken(string $sessionId, array $payload = array(), bool $legacy = false): string
122123
{
123124
if ($legacy) {
124-
return $this->returnLegacyToken($sessionId, $options);
125+
return $this->returnLegacyToken($sessionId, $payload);
125126
}
126127

127128
$issuedAt = new \DateTimeImmutable('@' . time());
128129

129130
$defaults = [
131+
'iss' => $this->apiKey,
132+
'iat' => $issuedAt->getTimestamp(),
130133
'session_id' => $sessionId,
131134
'role' => Role::PUBLISHER,
132-
'expireTime' => null,
133-
'initial_layout_list' => [''],
134135
'ist' => 'project',
135136
'nonce' => mt_rand(),
136137
'scope' => 'session.connect'
137138
];
138139

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));
140+
$payload = array_merge($defaults, array_intersect_key($payload, $defaults));
160141

161-
return $token->toString();
142+
return JWT::encode($payload, $this->apiSecret, 'HS256');
162143
}
163144

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

0 commit comments

Comments
 (0)