Skip to content

Commit 8116ca2

Browse files
authored
Merge pull request #89 from davemun/jwt-auth-iat-claim
ADD JWT IAT claim, update to X-OPENTOK-AUTH header
2 parents 613e536 + 96a745a commit 8116ca2

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed

opentok/opentok.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def headers(self):
275275
"""For internal use."""
276276
return {
277277
'User-Agent': 'OpenTok-Python-SDK/' + __version__ + ' ' + platform.python_version(),
278-
'X-TB-OPENTOK-AUTH': self._create_jwt_auth_header()
278+
'X-OPENTOK-AUTH': self._create_jwt_auth_header()
279279
}
280280

281281
def archive_headers(self):
@@ -451,7 +451,8 @@ def _create_jwt_auth_header(self):
451451
payload = {
452452
'ist': 'project',
453453
'iss': self.api_key,
454-
'exp': int(time.time()) + (60*5), # 5 minutes
454+
'iat': int(time.time()), # current time in unix time (seconds)
455+
'exp': int(time.time()) + (60*3), # 3 minutes in the future (seconds)
455456
'jti': '{:f}'.format(random.random())
456457
}
457458

tests/test_archive.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def test_stop_archive(self):
5757

5858
archive.stop()
5959

60-
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
60+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
6161
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
6262
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
6363
expect(archive).to.be.an(Archive)
@@ -102,7 +102,7 @@ def test_delete_archive(self):
102102

103103
archive.delete()
104104

105-
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
105+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
106106
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
107107
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
108108
# TODO: test that the object is invalidated

tests/test_archive_api.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_start_archive(self):
4242

4343
archive = self.opentok.start_archive(self.session_id)
4444

45-
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
45+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
4646
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
4747
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
4848
# non-deterministic json encoding. have to decode to test it properly
@@ -93,7 +93,7 @@ def test_start_archive_with_name(self):
9393

9494
archive = self.opentok.start_archive(self.session_id, name=u('ARCHIVE NAME'))
9595

96-
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
96+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
9797
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
9898
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
9999
# non-deterministic json encoding. have to decode to test it properly
@@ -142,7 +142,7 @@ def test_start_voice_archive(self):
142142

143143
archive = self.opentok.start_archive(self.session_id, name=u('ARCHIVE NAME'), has_video=False)
144144

145-
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
145+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
146146
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
147147
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
148148
# non-deterministic json encoding. have to decode to test it properly
@@ -193,7 +193,7 @@ def test_start_individual_archive(self):
193193

194194
archive = self.opentok.start_archive(self.session_id, name=u('ARCHIVE NAME'), output_mode=OutputModes.individual)
195195

196-
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
196+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
197197
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
198198
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
199199
# non-deterministic json encoding. have to decode to test it properly
@@ -245,7 +245,7 @@ def test_start_composed_archive(self):
245245

246246
archive = self.opentok.start_archive(self.session_id, name=u('ARCHIVE NAME'), output_mode=OutputModes.composed)
247247

248-
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
248+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
249249
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
250250
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
251251
# non-deterministic json encoding. have to decode to test it properly
@@ -298,7 +298,7 @@ def test_stop_archive(self):
298298

299299
archive = self.opentok.stop_archive(archive_id)
300300

301-
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
301+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
302302
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
303303
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
304304
expect(archive).to.be.an(Archive)
@@ -325,7 +325,7 @@ def test_delete_archive(self):
325325

326326
self.opentok.delete_archive(archive_id)
327327

328-
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
328+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
329329
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
330330
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
331331

@@ -354,7 +354,7 @@ def test_find_archive(self):
354354

355355
archive = self.opentok.get_archive(archive_id)
356356

357-
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
357+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
358358
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
359359
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
360360
expect(archive).to.be.an(Archive)
@@ -469,7 +469,7 @@ def test_find_archives(self):
469469

470470
archive_list = self.opentok.get_archives()
471471

472-
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
472+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
473473
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
474474
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
475475
expect(archive_list).to.be.an(ArchiveList)
@@ -529,7 +529,7 @@ def test_find_archives_with_offset(self):
529529

530530
archive_list = self.opentok.get_archives(offset=3)
531531

532-
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
532+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
533533
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
534534
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
535535
expect(httpretty.last_request()).to.have.property("querystring").being.equal({
@@ -579,7 +579,7 @@ def test_find_archives_with_count(self):
579579

580580
archive_list = self.opentok.get_archives(count=2)
581581

582-
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
582+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
583583
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
584584
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
585585
expect(httpretty.last_request()).to.have.property("querystring").being.equal({
@@ -655,7 +655,7 @@ def test_find_archives_with_offset_and_count(self):
655655

656656
archive_list = self.opentok.get_archives(count=4, offset=2)
657657

658-
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
658+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
659659
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
660660
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
661661
expect(httpretty.last_request()).to.have.property("querystring").being.equal({

tests/test_session_creation.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_create_default_session(self):
2323

2424
session = self.opentok.create_session()
2525

26-
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
26+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
2727
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
2828
body = parse_qs(httpretty.last_request().body)
2929
expect(body).to.have.key(b('p2p.preference')).being.equal([b('enabled')])
@@ -42,7 +42,7 @@ def test_create_routed_session(self):
4242

4343
session = self.opentok.create_session(media_mode=MediaModes.routed)
4444

45-
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
45+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
4646
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
4747
body = parse_qs(httpretty.last_request().body)
4848
expect(body).to.have.key(b('p2p.preference')).being.equal([b('disabled')])
@@ -61,7 +61,7 @@ def test_create_session_with_location_hint(self):
6161

6262
session = self.opentok.create_session(location='12.34.56.78')
6363

64-
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
64+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
6565
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
6666
# ordering of keys is non-deterministic, must parse the body to see if it is correct
6767
body = parse_qs(httpretty.last_request().body)
@@ -81,7 +81,7 @@ def test_create_routed_session_with_location_hint(self):
8181

8282
session = self.opentok.create_session(location='12.34.56.78', media_mode=MediaModes.routed)
8383

84-
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
84+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
8585
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
8686
# ordering of keys is non-deterministic, must parse the body to see if it is correct
8787
body = parse_qs(httpretty.last_request().body)
@@ -101,7 +101,7 @@ def test_create_manual_archive_mode_session(self):
101101

102102
session = self.opentok.create_session(media_mode=MediaModes.routed, archive_mode=ArchiveModes.manual)
103103

104-
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
104+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
105105
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
106106
body = parse_qs(httpretty.last_request().body)
107107
expect(body).to.have.key(b('p2p.preference')).being.equal([b('disabled')])
@@ -120,7 +120,7 @@ def test_create_always_archive_mode_session(self):
120120

121121
session = self.opentok.create_session(media_mode=MediaModes.routed, archive_mode=ArchiveModes.always)
122122

123-
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
123+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
124124
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
125125
body = parse_qs(httpretty.last_request().body)
126126
expect(body).to.have.key(b('p2p.preference')).being.equal([b('disabled')])

0 commit comments

Comments
 (0)