Skip to content

Commit

Permalink
Testing complete
Browse files Browse the repository at this point in the history
  • Loading branch information
SecondeJK committed Nov 8, 2024
1 parent cc05aed commit ef5a82f
Show file tree
Hide file tree
Showing 2 changed files with 296 additions and 243 deletions.
44 changes: 36 additions & 8 deletions src/OpenTok/OpenTok.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

namespace OpenTok;

use DateTimeImmutable;
use Firebase\JWT\Key;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Encoding\ChainedFormatter;
use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Signer\Rsa\Sha256;
use Lcobucci\JWT\Token\Builder;
use OpenTok\Util\Client;
use OpenTok\Util\Validators;
use OpenTok\Exception\InvalidArgumentException;
use OpenTok\Exception\UnexpectedValueException;
use Ramsey\Uuid\Uuid;
use Vonage\JWT\TokenGenerator;

/**
Expand Down Expand Up @@ -109,28 +118,47 @@ public function __construct($apiKey, $apiSecret, $options = array())
*
* @return string The token string.
*/
public function generateToken($sessionId, $options = array(), $legacy = false)
public function generateToken(string $sessionId, array $options = array(), bool $legacy = false): string
{
if ($legacy) {
return $this->returnLegacyToken($sessionId, $options);
}

$issuedAt = new \DateTimeImmutable('@' . time());

$defaults = [
'sessionId' => $sessionId,
'session_id' => $sessionId,
'role' => Role::PUBLISHER,
'exp' => null,
'data' => null,
'initialLayoutClassList' => [''],
'expireTime' => null,
'initial_layout_list' => [''],
'ist' => 'project',
'nonce' => mt_rand(),
'scope' => 'session.connect'
];

$options = array_merge($defaults, array_intersect_key($options, $defaults));

$generator = new TokenGenerator($this->apiKey, $this->apiSecret);
$builder = new Builder(new JoseEncoder(), ChainedFormatter::default());
$builder = $builder->issuedBy($this->apiKey);

if ($options['expireTime']) {
$expiry = new \DateTimeImmutable('@' . $options['expireTime']);
$builder = $builder->expiresAt($expiry);
}

unset($options['expireTime']);

$builder = $builder->issuedAt($issuedAt);
$builder = $builder->canOnlyBeUsedAfter($issuedAt);
$builder = $builder->identifiedBy(bin2hex(random_bytes(16)));

foreach ($options as $key => $value) {
$generator->addClaim($key, $value);
$builder = $builder->withClaim($key, $value);
}

return $generator->generate();
$token = $builder->getToken(new \Lcobucci\JWT\Signer\Hmac\Sha256(), InMemory::plainText($this->apiSecret));

return $token->toString();
}

private function returnLegacyToken(string $sessionId, array $options = []): string
Expand Down
Loading

0 comments on commit ef5a82f

Please sign in to comment.