Skip to content

Commit b34ef94

Browse files
committed
Merge branch 'main' into dev
2 parents e83c4fc + db655f8 commit b34ef94

File tree

4 files changed

+150
-13
lines changed

4 files changed

+150
-13
lines changed

src/OpenTok/OpenTok.php

Lines changed: 25 additions & 6 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;
@@ -170,6 +169,10 @@ public function generateToken($sessionId, $options = array())
170169
* (either automatically or not), you must set the <code>mediaMode</code> key to
171170
* <code>MediaMode::ROUTED</code>.</li>
172171
*
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+
*
173176
* <li><code>'location'</code> (String) &mdash; An IP address that the OpenTok servers
174177
* will use to situate the session in its global network. If you do not set a location hint,
175178
* the OpenTok servers will be based on the first client connecting to the session.</li>
@@ -216,26 +219,41 @@ public function createSession($options = array())
216219
{
217220
if (
218221
array_key_exists('archiveMode', $options) &&
219-
$options['archiveMode'] != ArchiveMode::MANUAL
222+
$options['archiveMode'] !== ArchiveMode::MANUAL
220223
) {
221224
if (
222225
array_key_exists('mediaMode', $options) &&
223-
$options['mediaMode'] != MediaMode::ROUTED
226+
$options['mediaMode'] !== MediaMode::ROUTED
224227
) {
225228
throw new InvalidArgumentException('A session must be routed to be archived.');
226229
} else {
227230
$options['mediaMode'] = MediaMode::ROUTED;
228231
}
229232
}
230233

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+
231247
// unpack optional arguments (merging with default values) into named variables
232248
$defaults = array(
233249
'mediaMode' => MediaMode::RELAYED,
234250
'archiveMode' => ArchiveMode::MANUAL,
235-
'location' => null
251+
'location' => null,
252+
'e2ee' => 'false',
236253
);
254+
237255
$options = array_merge($defaults, array_intersect_key($options, $defaults));
238-
list($mediaMode, $archiveMode, $location) = array_values($options);
256+
list($mediaMode, $archiveMode, $location, $e2ee) = array_values($options);
239257

240258
// validate arguments
241259
Validators::validateMediaMode($mediaMode);
@@ -255,7 +273,8 @@ public function createSession($options = array())
255273
return new Session($this, (string)$sessionId, array(
256274
'location' => $location,
257275
'mediaMode' => $mediaMode,
258-
'archiveMode' => $archiveMode
276+
'archiveMode' => $archiveMode,
277+
'e2ee' => $e2ee
259278
));
260279
}
261280

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
}

tests/OpenTokTest/OpenTokTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PHPUnit\Framework\TestCase;
1919
use GuzzleHttp\Handler\MockHandler;
2020
use OpenTok\Exception\InvalidArgumentException;
21+
use OpenTok\Session;
2122

2223
define('OPENTOK_DEBUG', true);
2324

@@ -221,6 +222,9 @@ public function testCreatesDefaultSession(): void
221222
$p2p_preference = $this->getPostField($request, 'p2p.preference');
222223
$this->assertEquals('enabled', $p2p_preference);
223224

225+
$e2ee = $this->getPostField($request, 'e2ee');
226+
$this->assertEquals('false', $e2ee);
227+
224228
$this->assertInstanceOf('OpenTok\Session', $session);
225229
// NOTE: this is an actual sessionId from the recorded response, this doesn't correspond to
226230
// the API Key and API Secret used to create the session.
@@ -230,6 +234,89 @@ public function testCreatesDefaultSession(): void
230234
);
231235
}
232236

237+
public function testCreatesE2EESession(): void
238+
{
239+
// Arrange
240+
$this->setupOTWithMocks([[
241+
'code' => 200,
242+
'headers' => [
243+
'Content-Type' => 'text/xml'
244+
],
245+
'path' => 'session/create/relayed'
246+
]]);
247+
248+
$session = $this->opentok->createSession(['e2ee' => true]);
249+
250+
$this->assertCount(1, $this->historyContainer);
251+
252+
$request = $this->historyContainer[0]['request'];
253+
$this->assertEquals('POST', strtoupper($request->getMethod()));
254+
$this->assertEquals('/session/create', $request->getUri()->getPath());
255+
$this->assertEquals('api.opentok.com', $request->getUri()->getHost());
256+
$this->assertEquals('https', $request->getUri()->getScheme());
257+
258+
$authString = $request->getHeaderLine('X-OPENTOK-AUTH');
259+
$this->assertEquals(true, TestHelpers::validateOpenTokAuthHeader($this->API_KEY, $this->API_SECRET, $authString));
260+
261+
$userAgent = $request->getHeaderLine('User-Agent');
262+
$this->assertNotEmpty($userAgent);
263+
$this->assertStringStartsWith('OpenTok-PHP-SDK/4.12.0', $userAgent);
264+
265+
$p2p_preference = $this->getPostField($request, 'p2p.preference');
266+
$this->assertEquals('enabled', $p2p_preference);
267+
268+
$e2ee = $this->getPostField($request, 'e2ee');
269+
$this->assertEquals('true', $e2ee);
270+
271+
$this->assertInstanceOf(Session::class, $session);
272+
$this->assertEquals(
273+
'2_MX4xNzAxMjYzMX4xMjcuMC4wLjF-V2VkIEZlYiAyNiAxODo1NzoyNCBQU1QgMjAxNH4wLjU0MDU4ODc0fg',
274+
$session->getSessionId()
275+
);
276+
}
277+
278+
public function testCannotStartE2EESessionWithWrongMediaMode(): void
279+
{
280+
$this->expectException(InvalidArgumentException::class);
281+
$this->expectErrorMessage('MediaMode must be routed in order to enable E2EE');
282+
283+
$this->setupOTWithMocks([[
284+
'code' => 200,
285+
'headers' => [
286+
'Content-Type' => 'text/xml'
287+
],
288+
'path' => 'session/create/relayed'
289+
]]);
290+
291+
$session = $this->opentok->createSession(
292+
[
293+
'mediaMode' => MediaMode::RELAYED,
294+
'e2ee' => true
295+
]
296+
);
297+
}
298+
299+
public function testCannotStartE2EESessionWithWrongArchiveMode(): void
300+
{
301+
$this->expectException(InvalidArgumentException::class);
302+
$this->expectErrorMessage('ArchiveMode cannot be set to always when using E2EE');
303+
304+
$this->setupOTWithMocks([[
305+
'code' => 200,
306+
'headers' => [
307+
'Content-Type' => 'text/xml'
308+
],
309+
'path' => 'session/create/relayed'
310+
]]);
311+
312+
$session = $this->opentok->createSession(
313+
[
314+
'archiveMode' => ArchiveMode::ALWAYS,
315+
'e2ee' => true
316+
]
317+
);
318+
}
319+
233320
private function getPostField($request, $targetKey)
234321
{
235322
$params = array_map(function ($item) {

tests/OpenTokTest/SessionTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@
22

33
namespace OpenTokTest;
44

5-
use GuzzleHttp\Handler\MockHandler;
6-
use GuzzleHttp\HandlerStack;
7-
use GuzzleHttp\Middleware;
85
use OpenTok\OpenTok;
96
use OpenTok\Session;
107
use OpenTok\MediaMode;
118
use OpenTok\ArchiveMode;
12-
use OpenTok\Util\Client;
139
use PHPUnit\Framework\TestCase;
1410

1511
class SessionTest extends TestCase
@@ -122,6 +118,20 @@ public function badParameterProvider()
122118
);
123119
}
124120

121+
public function testInitialzationWithoutE2ee()
122+
{
123+
$sessionId = 'SESSIONID';
124+
$session = new Session($this->opentok, $sessionId);
125+
$this->assertEquals(false, $session->getE2EE());
126+
}
127+
128+
public function testInitialzationWithE2ee()
129+
{
130+
$sessionId = 'SESSIONID';
131+
$session = new Session($this->opentok, $sessionId, ['e2ee' => true]);
132+
$this->assertEquals(true, $session->getE2EE());
133+
}
134+
125135
public function testInitializationWithExtraneousParams()
126136
{
127137
$sessionId = 'SESSIONID';

0 commit comments

Comments
 (0)