Skip to content

Commit 1ec5687

Browse files
Merge pull request #365 from opentok/fix-auth-shim
fix: Wrapped Vonage auth validators in try/catch
2 parents 387ee26 + 7d869e3 commit 1ec5687

3 files changed

Lines changed: 54 additions & 5 deletions

File tree

src/OpenTok/OpenTok.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,12 @@ public function __construct($apiKey, $apiSecret, $options = [])
5252
{
5353
$apiUrl = 'https://api.opentok.com';
5454

55-
if (Validators::isVonageKeypair($apiKey, $apiSecret)) {
56-
$apiUrl = 'https://video.api.vonage.com';
55+
try {
56+
if (Validators::isVonageKeypair($apiKey, $apiSecret)) {
57+
$apiUrl = 'https://video.api.vonage.com';
58+
}
59+
} catch (InvalidArgumentException) {
60+
// Not a Vonage Keypair, continue
5761
}
5862

5963
// unpack optional arguments (merging with default values) into named variables
@@ -1405,4 +1409,9 @@ private function signString(string $string, $secret): string
14051409
{
14061410
return hash_hmac("sha1", $string, (string) $secret);
14071411
}
1412+
1413+
public function getClient(): Client
1414+
{
1415+
return $this->client;
1416+
}
14081417
}

src/OpenTok/Util/Client.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use GuzzleHttp\Exception\ServerException;
2424
use OpenTok\Exception\BroadcastException;
2525
use GuzzleHttp\Exception\RequestException;
26+
use InvalidArgumentException;
2627
use OpenTok\Exception\ArchiveDomainException;
2728
use OpenTok\Exception\AuthenticationException;
2829
use OpenTok\Exception\BroadcastDomainException;
@@ -123,9 +124,18 @@ public function isConfigured()
123124

124125
private function createAuthHeader(): string
125126
{
126-
if (Validators::isVonageKeypair($this->apiKey, $this->apiSecret)) {
127-
$tokenGenerator = new TokenGenerator($this->apiKey, file_get_contents($this->apiSecret));
128-
return $tokenGenerator->generate();
127+
try {
128+
if (Validators::isVonageKeypair($this->apiKey, $this->apiSecret)) {
129+
$secret = $this->apiSecret;
130+
if (file_exists($this->apiSecret)) {
131+
$secret = file_get_contents($this->apiSecret);
132+
}
133+
134+
$tokenGenerator = new TokenGenerator($this->apiKey, $secret);
135+
return $tokenGenerator->generate();
136+
}
137+
} catch (InvalidArgumentException) {
138+
// Do nothing, fall back to legacy token generation
129139
}
130140

131141
$token = [
@@ -1084,4 +1094,14 @@ private function isDebug(): bool
10841094
{
10851095
return defined('OPENTOK_DEBUG');
10861096
}
1097+
1098+
public function getApiKey(): string
1099+
{
1100+
return $this->apiKey;
1101+
}
1102+
1103+
public function getApiSecret(): string
1104+
{
1105+
return $this->apiSecret;
1106+
}
10871107
}

tests/OpenTokTest/OpenTokTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3419,4 +3419,24 @@ public function testConnectAudioStreamWithInvalidBidirectionalThrows(): void
34193419

34203420
$this->opentok->connectAudio('9999', 'wrwetg', $badPayload);
34213421
}
3422+
3423+
public function testCanCreateClientWithOpenTokCreds(): void
3424+
{
3425+
$client = new OpenTok('12345678', 'b60d0b2568f3ea9731bd9d3f71be263ce19f802f');
3426+
$this->assertInstanceOf(OpenTok::class, $client);
3427+
}
3428+
3429+
public function testCanCreateClientWithOpenTokCredsWithIntKey(): void
3430+
{
3431+
$client = new OpenTok(12345678, 'b60d0b2568f3ea9731bd9d3f71be263ce19f802f');
3432+
$this->assertInstanceOf(OpenTok::class, $client);
3433+
}
3434+
3435+
public function testCanCreateClientWithVonageCreds(): void
3436+
{
3437+
$key = file_get_contents(__DIR__ . '/test.key');
3438+
$client = new OpenTok('1ab38a10-ed9d-4e2b-8b14-95e52d76a13c', $key);
3439+
$this->assertInstanceOf(OpenTok::class, $client);
3440+
$this->assertEquals($client->getClient()->getApiSecret(), $key);
3441+
}
34223442
}

0 commit comments

Comments
 (0)