|
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; |
9 | 18 | use Vonage\JWT\TokenGenerator;
|
10 | 19 |
|
11 | 20 | /**
|
@@ -109,28 +118,47 @@ public function __construct($apiKey, $apiSecret, $options = array())
|
109 | 118 | *
|
110 | 119 | * @return string The token string.
|
111 | 120 | */
|
112 |
| - public function generateToken($sessionId, $options = array(), $legacy = false) |
| 121 | + public function generateToken(string $sessionId, array $options = array(), bool $legacy = false): string |
113 | 122 | {
|
114 | 123 | if ($legacy) {
|
115 | 124 | return $this->returnLegacyToken($sessionId, $options);
|
116 | 125 | }
|
117 | 126 |
|
| 127 | + $issuedAt = new \DateTimeImmutable('@' . time()); |
| 128 | + |
118 | 129 | $defaults = [
|
119 |
| - 'sessionId' => $sessionId, |
| 130 | + 'session_id' => $sessionId, |
120 | 131 | 'role' => Role::PUBLISHER,
|
121 |
| - 'exp' => null, |
122 |
| - 'data' => null, |
123 |
| - 'initialLayoutClassList' => [''], |
| 132 | + 'expireTime' => null, |
| 133 | + 'initial_layout_list' => [''], |
| 134 | + 'ist' => 'project', |
| 135 | + 'nonce' => mt_rand(), |
| 136 | + 'scope' => 'session.connect' |
124 | 137 | ];
|
125 | 138 |
|
126 | 139 | $options = array_merge($defaults, array_intersect_key($options, $defaults));
|
127 | 140 |
|
128 |
| - $generator = new TokenGenerator($this->apiKey, $this->apiSecret); |
| 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 | + |
129 | 155 | foreach ($options as $key => $value) {
|
130 |
| - $generator->addClaim($key, $value); |
| 156 | + $builder = $builder->withClaim($key, $value); |
131 | 157 | }
|
132 | 158 |
|
133 |
| - return $generator->generate(); |
| 159 | + $token = $builder->getToken(new \Lcobucci\JWT\Signer\Hmac\Sha256(), InMemory::plainText($this->apiSecret)); |
| 160 | + |
| 161 | + return $token->toString(); |
134 | 162 | }
|
135 | 163 |
|
136 | 164 | private function returnLegacyToken(string $sessionId, array $options = []): string
|
|
0 commit comments