Skip to content

Commit 86e591b

Browse files
committed
WIP, close to working test but no end to end test
1 parent 8eeff73 commit 86e591b

File tree

13 files changed

+145
-189
lines changed

13 files changed

+145
-189
lines changed

sample/Archiving/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Next, input your own API Key and API Secret into the `run-demo` script file:
1919

2020
```
2121
export API_KEY=0000000
22-
export API_SECRET=abcdef1234567890abcdef01234567890abcdef
22+
export API_SECRET=b60d0b2568f3ea9731bd9d3f71be263ce19f802f
2323
```
2424

2525
Finally, start the PHP CLI development server (requires PHP >= 5.4) using the `run-demo` script

src/OpenTok/Archive.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,6 @@ public function __construct($archiveData, $options = array())
144144

145145
$this->client = isset($client) ? $client : new Client();
146146
if (!$this->client->isConfigured()) {
147-
Validators::validateApiKey($apiKey);
148-
Validators::validateApiSecret($apiSecret);
149147
Validators::validateApiUrl($apiUrl);
150148

151149
$this->client->configure($apiKey, $apiSecret, $apiUrl);

src/OpenTok/ArchiveList.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ public function __construct($archiveListData, $options = array())
4949

5050
$this->client = isset($client) ? $client : new Client();
5151
if (!$this->client->isConfigured()) {
52-
Validators::validateApiKey($apiKey);
53-
Validators::validateApiSecret($apiSecret);
5452
Validators::validateApiUrl($apiUrl);
5553

5654
$this->client->configure($apiKey, $apiSecret, $apiUrl);

src/OpenTok/Broadcast.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ public function __construct($broadcastData, $options = array())
147147
$this->client = $options['client'] ?? new Client();
148148

149149
if (!$this->client->isConfigured()) {
150-
Validators::validateApiKey($options['apiKey']);
151-
Validators::validateApiSecret($options['apiSecret']);
152150
Validators::validateApiUrl($options['apiUrl']);
153151

154152
$this->client->configure($options['apiKey'], $options['apiSecret'], $options['apiUrl']);

src/OpenTok/OpenTok.php

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
* and the API secret for your <a href="https://tokbox.com/account">OpenTok Video API account</a>. Do not
2626
* publicly share your API secret. You will use it with the OpenTok() constructor (only on your web
2727
* server) to create OpenTok sessions.
28+
*
29+
* If you set api_key to a VONAGE_APPLICATION_ID and api_secret to a VONAGE_PRIVATE_KEY_PATH, the SDK
30+
* will hit Vonage Video with Vonage Auth instead.
2831
* <p>
2932
* Be sure to include the entire OpenTok server SDK on your web server.
3033
*/
@@ -37,24 +40,25 @@ class OpenTok
3740
/** @internal */
3841
private $client;
3942

43+
/**
44+
* @var bool
45+
* Override to determine whether to hit Vonage servers with Vonage Auth in requests
46+
*/
47+
private $vonage = false;
48+
4049
/**
4150
* @var array
4251
* @internal
43-
* Options that can override the defaults. Additionally, you can set the keys
44-
* application_id & private_key_path to strings that will then override the default
45-
* OpenTok Client behaviour when making requests to the Vonage Video API.
4652
*/
4753
public $options;
4854

4955
/** @internal */
5056
public function __construct($apiKey, $apiSecret, $options = array())
5157
{
52-
$validateKeys = true;
53-
Validators::validateVonageJwtArguments($options);
5458
$apiUrl = 'https://api.opentok.com';
5559

56-
if (array_key_exists('application_id', $options) && array_key_exists('private_key_path', $options)) {
57-
$validateKeys = false;
60+
if (Validators::isVonageKeypair($apiKey, $apiSecret)) {
61+
$this->vonage = true;
5862
$apiUrl = 'https://video.api.vonage.com';
5963
}
6064

@@ -63,20 +67,12 @@ public function __construct($apiKey, $apiSecret, $options = array())
6367
'apiUrl' => $apiUrl,
6468
'client' => null,
6569
'timeout' => null, // In the future we should set this to 2
66-
'application_id' => null,
67-
'private_key_path' => null,
6870
);
6971

7072
$this->options = array_merge($defaults, array_intersect_key($options, $defaults));
7173

7274
list($apiUrl, $client, $timeout) = array_values($this->options);
7375

74-
// validate arguments
75-
if ($validateKeys) {
76-
Validators::validateApiKey($apiKey);
77-
Validators::validateApiSecret($apiSecret);
78-
}
79-
8076
Validators::validateApiUrl($apiUrl);
8177
Validators::validateClient($client);
8278
Validators::validateDefaultTimeout($timeout);
@@ -138,9 +134,6 @@ public function __construct($apiKey, $apiSecret, $options = array())
138134
* @param bool $legacy By default, OpenTok uses SHA256 JWTs for authentication. Switching
139135
* legacy to true will create a T1 token for backwards compatibility.
140136
*
141-
* Optionally, you can set $vonage to true and it will generate a Vonage Video token if you are using
142-
* the shim behaviour.
143-
*
144137
* @return string The token string.
145138
*/
146139
public function generateToken(string $sessionId, array $payload = array(), bool $legacy = false): string

src/OpenTok/Util/Client.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ class Client
4747

4848
protected $apiKey;
4949
protected $apiSecret;
50-
protected $applicationId = null;
51-
protected $privateKeyPath = null;
5250
protected $configured = false;
5351

5452
/**
@@ -67,14 +65,6 @@ public function configure($apiKey, $apiSecret, $apiUrl, $options = array())
6765
$this->apiKey = $apiKey;
6866
$this->apiSecret = $apiSecret;
6967

70-
if (array_key_exists('application_id', $options) || array_key_exists('private_key_path', $options)) {
71-
if (!is_null($options['application_id']) && !is_null($options['private_key_path'])) {
72-
$this->applicationId = $options['application_id'];
73-
$this->privateKeyPath = $options['private_key_path'];
74-
$apiUrl = 'https://video.api.vonage.com';
75-
}
76-
}
77-
7868
if (isset($this->options['client'])) {
7969
$this->client = $options['client'];
8070
} else {
@@ -135,11 +125,8 @@ public function isConfigured()
135125

136126
private function createAuthHeader()
137127
{
138-
if (!is_null($this->applicationId) && !is_null($this->privateKeyPath)) {
139-
$projectRoot = dirname(__DIR__, 3); // Adjust the number of dirname() calls if necessary to match your
140-
// project structure.
141-
$privateKeyFullPath = $projectRoot . DIRECTORY_SEPARATOR . $this->privateKeyPath;
142-
$tokenGenerator = new TokenGenerator($this->applicationId, file_get_contents($privateKeyFullPath));
128+
if (Validators::isVonageKeypair($this->apiKey, $this->apiSecret)) {
129+
$tokenGenerator = new TokenGenerator($this->apiKey, file_get_contents($this->apiSecret));
143130
return $tokenGenerator->generate();
144131
}
145132

src/OpenTok/Util/Validators.php

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use OpenTok\Exception\InvalidArgumentException;
1414
use JohnStevenson\JsonWorks\Document;
1515
use JohnStevenson\JsonWorks\Utils as JsonUtils;
16+
use RuntimeException;
1617

1718
/**
1819
* @internal
@@ -25,31 +26,44 @@ class Validators
2526

2627
public const STREAM_MODES = ['auto', 'manual'];
2728

28-
public static function validateApiKey($apiKey)
29+
public static function isVonageKeypair($apiKey, $apiSecret): bool
2930
{
30-
if (!(is_string($apiKey) || is_int($apiKey))) {
31-
throw new InvalidArgumentException(
32-
'The apiKey was not a string nor an integer: ' . print_r($apiKey, true)
33-
);
31+
if (!is_string($apiKey) || !is_string($apiSecret)) {
32+
throw new InvalidArgumentException("API Key and API Secret must be strings.");
33+
}
34+
35+
$isOpenTokKey = preg_match('/^\d+$/', $apiKey);
36+
$isOpenTokSecret = preg_match('/^[a-f0-9]{40}$/i', $apiSecret);
37+
38+
if ($isOpenTokKey && $isOpenTokSecret) {
39+
return false;
40+
}
41+
42+
$isVonageApplicationId = preg_match('/^[a-f0-9\-]{36}$/i', $apiKey);
43+
$isVonagePrivateKey = self::isValidPrivateKey($apiSecret);
44+
45+
if ($isVonageApplicationId && $isVonagePrivateKey) {
46+
return true;
3447
}
48+
49+
// Mixed formats or invalid formats - throw an exception
50+
throw new InvalidArgumentException("Invalid Vonage Keypair credentials provided.");
3551
}
3652

37-
public static function validateVonageJwtArguments(array $options)
53+
private static function isValidPrivateKey(string $filePath): bool
3854
{
39-
if (!isset($data['application_id']) && !isset($data['private_key_path'])) {
40-
return;
55+
if (!file_exists($filePath) || !is_readable($filePath)) {
56+
throw new InvalidArgumentException("Private key file does not exist or is not readable.");
4157
}
4258

43-
if (isset($data['application_id']) && isset($data['private_key_path'])) {
44-
if (is_string($data['application_id']) && is_string($data['private_key_path'])) {
45-
return;
46-
};
59+
$keyContents = file_get_contents($filePath);
60+
61+
if ($keyContents === false) {
62+
throw new RuntimeException("Failed to read private key file.");
4763
}
4864

49-
// If one key is present but not the other, validation fails
50-
throw new InvalidArgumentException(
51-
'You are attempting to use the Vonage Video API. Both application_id and private key paths must be in options and both strings.'
52-
);
65+
// Check if it contains a valid private RSA key header
66+
return (bool) preg_match('/^-----BEGIN PRIVATE KEY-----[\s\S]+-----END PRIVATE KEY-----$/m', trim($keyContents));
5367
}
5468

5569
public static function validateForceMuteAllOptions(array $options)
@@ -74,13 +88,6 @@ public static function validateForceMuteAllOptions(array $options)
7488
}
7589
}
7690

77-
public static function validateApiSecret($apiSecret)
78-
{
79-
if (!(is_string($apiSecret))) {
80-
throw new InvalidArgumentException('The apiSecret was not a string: ' . print_r($apiSecret, true));
81-
}
82-
}
83-
8491
public static function validateApiUrl($apiUrl)
8592
{
8693
if (!(is_string($apiUrl) && filter_var($apiUrl, FILTER_VALIDATE_URL))) {

tests/OpenTokTest/ArchiveTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function setupArchives($streamMode)
6262
private function setupOTWithMocks($mocks)
6363
{
6464
$this->API_KEY = defined('API_KEY') ? API_KEY : '12345678';
65-
$this->API_SECRET = defined('API_SECRET') ? API_SECRET : '0123456789abcdef0123456789abcdef0123456789';
65+
$this->API_SECRET = defined('API_SECRET') ? API_SECRET : 'b60d0b2568f3ea9731bd9d3f71be263ce19f802f';
6666

6767
if (is_array($mocks)) {
6868
$responses = TestHelpers::mocksToResponses($mocks, self::$mockBasePath);

tests/OpenTokTest/BroadcastTest.php

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function setupBroadcasts($streamMode)
6969
private function setupOTWithMocks($mocks)
7070
{
7171
$this->API_KEY = defined('API_KEY') ? API_KEY : '12345678';
72-
$this->API_SECRET = defined('API_SECRET') ? API_SECRET : '0123456789abcdef0123456789abcdef0123456789';
72+
$this->API_SECRET = defined('API_SECRET') ? API_SECRET : 'b60d0b2568f3ea9731bd9d3f71be263ce19f802f';
7373

7474
if (is_array($mocks)) {
7575
$responses = TestHelpers::mocksToResponses($mocks, self::$mockBasePath);
@@ -98,39 +98,6 @@ private function setupOTWithMocks($mocks)
9898
$handlerStack->push($history);
9999
}
100100

101-
public function testCannotCreateBroadcastWithAddInvalidApiKey(): void
102-
{
103-
$this->expectException(InvalidArgumentException::class);
104-
$this->expectExceptionMessage('The apiKey was not a string nor an integer: ');
105-
106-
$broadcastObject = new Broadcast($this->broadcastData, [
107-
'apiKey' => new Client()
108-
]);
109-
}
110-
111-
public function testCannotCreateBroadcastWithInvalidApiSecret(): void
112-
{
113-
$this->expectException(InvalidArgumentException::class);
114-
$this->expectExceptionMessage('The apiSecret was not a string: OpenTok\Util\Client Object');
115-
116-
$broadcastObject = new Broadcast($this->broadcastData, [
117-
'apiKey' => 'test',
118-
'apiSecret' => new Client()
119-
]);
120-
}
121-
122-
public function testCannotCreateBroadcastWithInvalidApiUrl(): void
123-
{
124-
$this->expectException(InvalidArgumentException::class);
125-
$this->expectExceptionMessage('The optional apiUrl was not a string: ');
126-
127-
$broadcastObject = new Broadcast($this->broadcastData, [
128-
'apiKey' => 'validKey',
129-
'apiSecret' => 'validSecret',
130-
'apiUrl' => 'test'
131-
]);
132-
}
133-
134101
private function setupOT()
135102
{
136103
return $this->setupOTWithMocks([]);

0 commit comments

Comments
 (0)