2
2
3
3
namespace OpenTok ;
4
4
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 ;
5
13
use OpenTok \Util \Client ;
6
14
use OpenTok \Util \Validators ;
7
15
use OpenTok \Exception \InvalidArgumentException ;
8
16
use OpenTok \Exception \UnexpectedValueException ;
17
+ use Ramsey \Uuid \Uuid ;
18
+ use Vonage \JWT \TokenGenerator ;
9
19
10
20
/**
11
21
* Contains methods for creating OpenTok sessions, generating tokens, and working with archives.
19
29
*/
20
30
class OpenTok
21
31
{
22
-
23
32
/** @internal */
24
33
private $ apiKey ;
25
34
/** @internal */
@@ -104,11 +113,56 @@ public function __construct($apiKey, $apiSecret, $options = array())
104
113
*
105
114
* </ul>
106
115
*
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
+ *
107
119
* @return string The token string.
108
120
*/
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
110
165
{
111
- // unpack optional arguments (merging with default values) into named variables
112
166
$ defaults = array (
113
167
'role ' => Role::PUBLISHER ,
114
168
'expireTime ' => null ,
@@ -237,7 +291,6 @@ public function createSession($options = array())
237
291
}
238
292
239
293
if (array_key_exists ('e2ee ' , $ options ) && $ options ['e2ee ' ]) {
240
-
241
294
if (array_key_exists ('mediaMode ' , $ options ) && $ options ['mediaMode ' ] !== MediaMode::ROUTED ) {
242
295
throw new InvalidArgumentException ('MediaMode must be routed in order to enable E2EE ' );
243
296
}
@@ -885,13 +938,13 @@ public function startBroadcast(string $sessionId, array $options = []): Broadcas
885
938
Validators::validateResolution ($ options ['resolution ' ]);
886
939
}
887
940
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
+ }
891
944
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
+ }
895
948
896
949
$ defaults = [
897
950
'layout ' => Layout::getBestFit (),
@@ -900,11 +953,11 @@ public function startBroadcast(string $sessionId, array $options = []): Broadcas
900
953
'streamMode ' => 'auto ' ,
901
954
'resolution ' => '640x480 ' ,
902
955
'maxBitRate ' => 2000000 ,
903
- 'outputs ' => [
904
- 'hls ' => [
905
- 'dvr ' => false ,
906
- 'lowLatency ' => false
907
- ]
956
+ 'outputs ' => [
957
+ 'hls ' => [
958
+ 'dvr ' => false ,
959
+ 'lowLatency ' => false
960
+ ]
908
961
]
909
962
];
910
963
@@ -1316,8 +1369,7 @@ public function startCaptions(
1316
1369
?int $ maxDuration = null ,
1317
1370
?bool $ partialCaptions = null ,
1318
1371
?string $ statusCallbackUrl = null
1319
- ): array
1320
- {
1372
+ ): array {
1321
1373
return $ this ->client ->startCaptions (
1322
1374
$ sessionId ,
1323
1375
$ token ,
0 commit comments