|
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,51 +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 | 124 | if ($legacy) { |
124 | | - return $this->returnLegacyToken($sessionId, $options); |
| 125 | + return $this->returnLegacyToken($sessionId, $payload); |
125 | 126 | } |
126 | 127 |
|
127 | 128 | $issuedAt = new \DateTimeImmutable('@' . time()); |
128 | 129 |
|
129 | 130 | $defaults = [ |
| 131 | + 'iss' => $this->apiKey, |
| 132 | + 'iat' => $issuedAt->getTimestamp(), |
130 | 133 | 'session_id' => $sessionId, |
131 | 134 | 'role' => Role::PUBLISHER, |
132 | | - 'expireTime' => null, |
133 | | - 'initial_layout_list' => [''], |
134 | 135 | 'ist' => 'project', |
135 | 136 | 'nonce' => mt_rand(), |
136 | 137 | 'scope' => 'session.connect' |
137 | 138 | ]; |
138 | 139 |
|
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)); |
| 140 | + $payload = array_merge($defaults, array_intersect_key($payload, $defaults)); |
160 | 141 |
|
161 | | - return $token->toString(); |
| 142 | + return JWT::encode($payload, $this->apiSecret, 'HS256'); |
162 | 143 | } |
163 | 144 |
|
164 | 145 | private function returnLegacyToken(string $sessionId, array $options = []): string |
|
0 commit comments