22
33namespace 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 ;
513use OpenTok \Util \Client ;
614use OpenTok \Util \Validators ;
715use OpenTok \Exception \InvalidArgumentException ;
816use 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.
1929*/
2030class 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 ,
0 commit comments