|
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; |
@@ -84,7 +85,7 @@ public function __construct($apiKey, $apiSecret, $options = array()) |
84 | 85 | * @param string $sessionId The session ID corresponding to the session to which the user |
85 | 86 | * will connect. |
86 | 87 | * |
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 |
88 | 89 | * following keys, all of which are optional: |
89 | 90 | * |
90 | 91 | * <ul> |
@@ -114,52 +115,31 @@ public function __construct($apiKey, $apiSecret, $options = array()) |
114 | 115 | * </ul> |
115 | 116 | * |
116 | 117 | * @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. |
118 | 119 | * |
119 | 120 | * @return string The token string. |
120 | 121 | */ |
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 |
122 | 123 | { |
123 | | - // Note, JWT generation disabled due to a backend bug regarding `exp` claims being mandatory - CRT |
124 | | - // if ($legacy) { |
125 | | - return $this->returnLegacyToken($sessionId, $options); |
126 | | - // } |
127 | | - |
128 | | - // $issuedAt = new \DateTimeImmutable('@' . time()); |
129 | | - |
130 | | - // $defaults = [ |
131 | | - // 'session_id' => $sessionId, |
132 | | - // 'role' => Role::PUBLISHER, |
133 | | - // 'expireTime' => null, |
134 | | - // 'initial_layout_list' => [''], |
135 | | - // 'ist' => 'project', |
136 | | - // 'nonce' => mt_rand(), |
137 | | - // 'scope' => 'session.connect' |
138 | | - // ]; |
139 | | - |
140 | | - // $options = array_merge($defaults, array_intersect_key($options, $defaults)); |
141 | | - |
142 | | - // $builder = new Builder(new JoseEncoder(), ChainedFormatter::default()); |
143 | | - // $builder = $builder->issuedBy($this->apiKey); |
144 | | - |
145 | | - // if ($options['expireTime']) { |
146 | | - // $expiry = new \DateTimeImmutable('@' . $options['expireTime']); |
147 | | - // $builder = $builder->expiresAt($expiry); |
148 | | - // } |
149 | | - |
150 | | - // unset($options['expireTime']); |
| 124 | + if ($legacy) { |
| 125 | + return $this->returnLegacyToken($sessionId, $payload); |
| 126 | + } |
151 | 127 |
|
152 | | - // $builder = $builder->issuedAt($issuedAt); |
153 | | - // $builder = $builder->canOnlyBeUsedAfter($issuedAt); |
154 | | - // $builder = $builder->identifiedBy(bin2hex(random_bytes(16))); |
| 128 | + $issuedAt = new \DateTimeImmutable('@' . time()); |
155 | 129 |
|
156 | | - // foreach ($options as $key => $value) { |
157 | | - // $builder = $builder->withClaim($key, $value); |
158 | | - // } |
| 130 | + $defaults = [ |
| 131 | + 'iss' => $this->apiKey, |
| 132 | + 'iat' => $issuedAt->getTimestamp(), |
| 133 | + 'session_id' => $sessionId, |
| 134 | + 'role' => Role::PUBLISHER, |
| 135 | + 'ist' => 'project', |
| 136 | + 'nonce' => mt_rand(), |
| 137 | + 'scope' => 'session.connect' |
| 138 | + ]; |
159 | 139 |
|
160 | | - // $token = $builder->getToken(new \Lcobucci\JWT\Signer\Hmac\Sha256(), InMemory::plainText($this->apiSecret)); |
| 140 | + $payload = array_merge($defaults, array_intersect_key($payload, $defaults)); |
161 | 141 |
|
162 | | - // return $token->toString(); |
| 142 | + return JWT::encode($payload, $this->apiSecret, 'HS256'); |
163 | 143 | } |
164 | 144 |
|
165 | 145 | private function returnLegacyToken(string $sessionId, array $options = []): string |
|
0 commit comments