Skip to content

Commit 5d9b0d1

Browse files
authored
Merge branch 'dev' into feature/audio-only-broadcast
2 parents 13ff02d + d6e93ba commit 5d9b0d1

File tree

7 files changed

+298
-278
lines changed

7 files changed

+298
-278
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
- ubuntu-latest
77
strategy:
88
matrix:
9-
php: ['7.2', '7.3', '7.4', '8.0']
9+
php: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2']
1010
steps:
1111
- name: Configure Git
1212
if: ${{ matrix.os == 'windows-latest' }}

src/OpenTok/OpenTok.php

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace OpenTok;
44

5-
use OpenTok\Layout;
65
use OpenTok\Util\Client;
76
use OpenTok\Util\Validators;
87
use OpenTok\Exception\InvalidArgumentException;
@@ -27,6 +26,8 @@ class OpenTok
2726
private $apiSecret;
2827
/** @internal */
2928
private $client;
29+
/** @internal */
30+
public $options;
3031

3132
/** @internal */
3233
public function __construct($apiKey, $apiSecret, $options = array())
@@ -37,8 +38,10 @@ public function __construct($apiKey, $apiSecret, $options = array())
3738
'client' => null,
3839
'timeout' => null // In the future we should set this to 2
3940
);
40-
$options = array_merge($defaults, array_intersect_key($options, $defaults));
41-
list($apiUrl, $client, $timeout) = array_values($options);
41+
42+
$this->options = array_merge($defaults, array_intersect_key($options, $defaults));
43+
44+
list($apiUrl, $client, $timeout) = array_values($this->options);
4245

4346
// validate arguments
4447
Validators::validateApiKey($apiKey);
@@ -53,7 +56,7 @@ public function __construct($apiKey, $apiSecret, $options = array())
5356
$apiKey,
5457
$apiSecret,
5558
$apiUrl,
56-
['timeout' => $timeout]
59+
array_merge(['timeout' => $timeout], $this->options)
5760
);
5861
}
5962
$this->apiKey = $apiKey;
@@ -166,6 +169,10 @@ public function generateToken($sessionId, $options = array())
166169
* (either automatically or not), you must set the <code>mediaMode</code> key to
167170
* <code>MediaMode::ROUTED</code>.</li>
168171
*
172+
* <li><code>'e2ee'</code> (Boolean) &mdash; Whether to enable
173+
* <a href="https://tokbox.com/developer/guides/end-to-end-encryption">end-to-end encryption</a>
174+
* for a routed session.</li>
175+
*
169176
* <li><code>'location'</code> (String) &mdash; An IP address that the OpenTok servers
170177
* will use to situate the session in its global network. If you do not set a location hint,
171178
* the OpenTok servers will be based on the first client connecting to the session.</li>
@@ -212,26 +219,41 @@ public function createSession($options = array())
212219
{
213220
if (
214221
array_key_exists('archiveMode', $options) &&
215-
$options['archiveMode'] != ArchiveMode::MANUAL
222+
$options['archiveMode'] !== ArchiveMode::MANUAL
216223
) {
217224
if (
218225
array_key_exists('mediaMode', $options) &&
219-
$options['mediaMode'] != MediaMode::ROUTED
226+
$options['mediaMode'] !== MediaMode::ROUTED
220227
) {
221228
throw new InvalidArgumentException('A session must be routed to be archived.');
222229
} else {
223230
$options['mediaMode'] = MediaMode::ROUTED;
224231
}
225232
}
226233

234+
if (array_key_exists('e2ee', $options) && $options['e2ee']) {
235+
236+
if (array_key_exists('mediaMode', $options) && $options['mediaMode'] !== MediaMode::ROUTED) {
237+
throw new InvalidArgumentException('MediaMode must be routed in order to enable E2EE');
238+
}
239+
240+
if (array_key_exists('archiveMode', $options) && $options['archiveMode'] === ArchiveMode::ALWAYS) {
241+
throw new InvalidArgumentException('ArchiveMode cannot be set to always when using E2EE');
242+
}
243+
244+
$options['e2ee'] = 'true';
245+
}
246+
227247
// unpack optional arguments (merging with default values) into named variables
228248
$defaults = array(
229249
'mediaMode' => MediaMode::RELAYED,
230250
'archiveMode' => ArchiveMode::MANUAL,
231-
'location' => null
251+
'location' => null,
252+
'e2ee' => 'false',
232253
);
254+
233255
$options = array_merge($defaults, array_intersect_key($options, $defaults));
234-
list($mediaMode, $archiveMode, $location) = array_values($options);
256+
list($mediaMode, $archiveMode, $location, $e2ee) = array_values($options);
235257

236258
// validate arguments
237259
Validators::validateMediaMode($mediaMode);
@@ -251,7 +273,8 @@ public function createSession($options = array())
251273
return new Session($this, (string)$sessionId, array(
252274
'location' => $location,
253275
'mediaMode' => $mediaMode,
254-
'archiveMode' => $archiveMode
276+
'archiveMode' => $archiveMode,
277+
'e2ee' => $e2ee
255278
));
256279
}
257280

src/OpenTok/Session.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,25 @@ class Session
3232
* @internal
3333
*/
3434
protected $opentok;
35+
/**
36+
* @internal
37+
*/
38+
protected $e2ee;
3539

3640
/**
3741
* @internal
3842
*/
3943
public function __construct($opentok, $sessionId, $properties = array())
4044
{
41-
// unpack arguments
42-
$defaults = array('mediaMode' => MediaMode::ROUTED, 'archiveMode' => ArchiveMode::MANUAL, 'location' => null);
45+
$defaults = [
46+
'mediaMode' => MediaMode::ROUTED,
47+
'archiveMode' => ArchiveMode::MANUAL,
48+
'location' => null,
49+
'e2ee' => false
50+
];
51+
4352
$properties = array_merge($defaults, array_intersect_key($properties, $defaults));
44-
list($mediaMode, $archiveMode, $location) = array_values($properties);
53+
list($mediaMode, $archiveMode, $location, $e2ee) = array_values($properties);
4554

4655
Validators::validateOpenTok($opentok);
4756
Validators::validateSessionId($sessionId);
@@ -54,6 +63,7 @@ public function __construct($opentok, $sessionId, $properties = array())
5463
$this->location = $location;
5564
$this->mediaMode = $mediaMode;
5665
$this->archiveMode = $archiveMode;
66+
$this->e2ee = $e2ee;
5767
}
5868

5969
/**
@@ -148,4 +158,15 @@ public function generateToken($options = array())
148158
{
149159
return $this->opentok->generateToken($this->sessionId, $options);
150160
}
161+
162+
/**
163+
* Whether <a href="https://tokbox.com/developer/guides/end-to-end-encryption">end-to-end encryption</a>
164+
* is set for the session.
165+
*
166+
* @return bool
167+
*/
168+
public function getE2EE(): bool
169+
{
170+
return (bool)$this->e2ee;
171+
}
151172
}

src/OpenTok/Util/Client.php

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace OpenTok\Util;
44

5+
use Composer\InstalledVersions;
56
use Exception as GlobalException;
7+
use GuzzleHttp\Utils;
68
use OpenTok\Layout;
79
use Firebase\JWT\JWT;
810
use OpenTok\MediaMode;
@@ -18,7 +20,6 @@
1820
use GuzzleHttp\Exception\ServerException;
1921
use OpenTok\Exception\BroadcastException;
2022
use GuzzleHttp\Exception\RequestException;
21-
use function GuzzleHttp\default_user_agent;
2223
use OpenTok\Exception\ArchiveDomainException;
2324
use OpenTok\Exception\AuthenticationException;
2425
use OpenTok\Exception\BroadcastDomainException;
@@ -36,17 +37,13 @@
3637
use OpenTok\Exception\ForceDisconnectAuthenticationException;
3738
use OpenTok\Exception\ForceDisconnectUnexpectedValueException;
3839

39-
// TODO: build this dynamically
40-
/** @internal */
41-
define('OPENTOK_SDK_VERSION', '4.12.0');
42-
/** @internal */
43-
define('OPENTOK_SDK_USER_AGENT', 'OpenTok-PHP-SDK/' . OPENTOK_SDK_VERSION);
44-
4540
/**
46-
* @internal
47-
*/
41+
* @internal
42+
*/
4843
class Client
4944
{
45+
public const OPENTOK_SDK_USER_AGENT_IDENTIFIER = 'OpenTok-PHP-SDK/';
46+
5047
protected $apiKey;
5148
protected $apiSecret;
5249
protected $configured = false;
@@ -56,18 +53,24 @@ class Client
5653
*/
5754
protected $client;
5855

56+
/**
57+
* @var array|mixed
58+
*/
59+
public $options;
60+
5961
public function configure($apiKey, $apiSecret, $apiUrl, $options = array())
6062
{
63+
$this->options = $options;
6164
$this->apiKey = $apiKey;
6265
$this->apiSecret = $apiSecret;
6366

64-
if (isset($options['client'])) {
67+
if (isset($this->options['client'])) {
6568
$this->client = $options['client'];
6669
} else {
6770
$clientOptions = [
6871
'base_uri' => $apiUrl,
6972
'headers' => [
70-
'User-Agent' => OPENTOK_SDK_USER_AGENT . ' ' . default_user_agent(),
73+
'User-Agent' => $this->buildUserAgentString()
7174
],
7275
];
7376

@@ -94,6 +97,26 @@ public function configure($apiKey, $apiSecret, $apiUrl, $options = array())
9497
$this->configured = true;
9598
}
9699

100+
private function buildUserAgentString(): string
101+
{
102+
$userAgent = [];
103+
104+
$userAgent[] = self::OPENTOK_SDK_USER_AGENT_IDENTIFIER
105+
. InstalledVersions::getVersion('opentok/opentok');
106+
107+
$userAgent[] = 'php/' . PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION;
108+
109+
if (isset($this->options['app'])) {
110+
$app = $this->options['app'];
111+
if (isset($app['name'], $app['version'])) {
112+
// You must use both of these for custom agent strings
113+
$userAgent[] = $app['name'] . '/' . $app['version'];
114+
}
115+
}
116+
117+
return implode(' ', $userAgent);
118+
}
119+
97120
public function isConfigured()
98121
{
99122
return $this->configured;
@@ -939,7 +962,7 @@ private function handleSignalingException(ClientException $e)
939962
throw new SignalConnectionException($message, $responseCode);
940963
case 413:
941964
$message = 'The type string exceeds the maximum length (128 bytes),'
942-
. ' or the data string exceeds the maximum size (8 kB).';
965+
. ' or the data string exceeds the maximum size (8 kB).';
943966
throw new SignalUnexpectedValueException($message, $responseCode);
944967
default:
945968
break;

tests/OpenTokTest/ArchiveTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,6 @@ public function testStopsArchive()
160160
$authString = $request->getHeaderLine('X-OPENTOK-AUTH');
161161
$this->assertEquals(true, TestHelpers::validateOpenTokAuthHeader($this->API_KEY, $this->API_SECRET, $authString));
162162

163-
// TODO: test the dynamically built User Agent string
164-
$userAgent = $request->getHeaderLine('User-Agent');
165-
$this->assertNotEmpty($userAgent);
166-
$this->assertStringStartsWith('OpenTok-PHP-SDK/4.12.0', $userAgent);
167-
168163
// TODO: test the properties of the actual archive object
169164
$this->assertEquals('stopped', $this->archive->status);
170165

@@ -277,11 +272,6 @@ public function testDeletesArchive()
277272
$authString = $request->getHeaderLine('X-OPENTOK-AUTH');
278273
$this->assertEquals(true, TestHelpers::validateOpenTokAuthHeader($this->API_KEY, $this->API_SECRET, $authString));
279274

280-
// TODO: test the dynamically built User Agent string
281-
$userAgent = $request->getHeaderLine('User-Agent');
282-
$this->assertNotEmpty($userAgent);
283-
$this->assertStringStartsWith('OpenTok-PHP-SDK/4.12.0', $userAgent);
284-
285275
$this->assertTrue($success);
286276
// TODO: assert that all properties of the archive object were cleared
287277
}

0 commit comments

Comments
 (0)