|
3 | 3 | namespace OpenTok; |
4 | 4 |
|
5 | 5 | use DateTimeImmutable; |
| 6 | +use Firebase\JWT\JWT; |
6 | 7 | use Firebase\JWT\Key; |
7 | 8 | use Lcobucci\JWT\Configuration; |
8 | 9 | use Lcobucci\JWT\Encoding\ChainedFormatter; |
@@ -105,7 +106,7 @@ public function __construct($apiKey, $apiSecret, $options = array()) |
105 | 106 | * @param string $sessionId The session ID corresponding to the session to which the user |
106 | 107 | * will connect. |
107 | 108 | * |
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 |
109 | 110 | * following keys, all of which are optional: |
110 | 111 | * |
111 | 112 | * <ul> |
@@ -135,59 +136,34 @@ public function __construct($apiKey, $apiSecret, $options = array()) |
135 | 136 | * </ul> |
136 | 137 | * |
137 | 138 | * @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. |
139 | 140 | * |
140 | 141 | * Optionally, you can set $vonage to true and it will generate a Vonage Video token if you are using |
141 | 142 | * the shim behaviour. |
142 | 143 | * |
143 | 144 | * @return string The token string. |
144 | 145 | */ |
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 |
150 | 147 | { |
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 | + } |
179 | 151 |
|
180 | | - // $builder = $builder->issuedAt($issuedAt); |
181 | | - // $builder = $builder->canOnlyBeUsedAfter($issuedAt); |
182 | | - // $builder = $builder->identifiedBy(bin2hex(random_bytes(16))); |
| 152 | + $issuedAt = new \DateTimeImmutable('@' . time()); |
183 | 153 |
|
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 | + ]; |
187 | 163 |
|
188 | | - // $token = $builder->getToken(new \Lcobucci\JWT\Signer\Hmac\Sha256(), InMemory::plainText($this->apiSecret)); |
| 164 | + $payload = array_merge($defaults, array_intersect_key($payload, $defaults)); |
189 | 165 |
|
190 | | - // return $token->toString(); |
| 166 | + return JWT::encode($payload, $this->apiSecret, 'HS256'); |
191 | 167 | } |
192 | 168 |
|
193 | 169 | private function returnLegacyToken(string $sessionId, array $options = []): string |
|
0 commit comments