Skip to content

Commit 39ed62c

Browse files
author
Dave Mun
committed
Fix per review comments
1 parent b7b809c commit 39ed62c

File tree

6 files changed

+45
-130
lines changed

6 files changed

+45
-130
lines changed

opentok/opentok.py

Lines changed: 4 additions & 4 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(self.api_key, self.api_secret)
278+
'X-TB-OPENTOK-AUTH': self._create_jwt_auth_header()
279279
}
280280

281281
def archive_headers(self):
@@ -447,12 +447,12 @@ def get_archives(self, offset=None, count=None):
447447
def _sign_string(self, string, secret):
448448
return hmac.new(secret.encode('utf-8'), string.encode('utf-8'), hashlib.sha1).hexdigest()
449449

450-
def _create_jwt_auth_header(self, api_key, api_secret):
450+
def _create_jwt_auth_header(self):
451451
payload = {
452452
'ist': 'project',
453-
'iss': api_key,
453+
'iss': self.api_key,
454454
'exp': int(time.time()) + (60*5), # 5 minutes
455455
'jti': '{:f}'.format(random.random())
456456
}
457457

458-
return jwt.encode(payload, api_secret, algorithm='HS256')
458+
return jwt.encode(payload, self.api_secret, algorithm='HS256')

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .validate_jwt import validate_jwt_header

tests/test_archive.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
import json
88
import datetime
99
import pytz
10-
from jose import jwt
11-
import time
10+
from .validate_jwt import validate_jwt_header
1211

1312
from opentok import OpenTok, Archive, __version__, OutputModes
1413

@@ -58,12 +57,7 @@ def test_stop_archive(self):
5857

5958
archive.stop()
6059

61-
claims = jwt.decode(httpretty.last_request().headers[u('x-tb-opentok-auth')], self.api_secret, algorithms=[u('HS256')])
62-
expect(claims[u('iss')]).to.equal(self.api_key)
63-
expect(claims[u('ist')]).to.equal(u('project'))
64-
expect(float(claims[u('exp')])).to.be.greater_than(float(time.time()))
65-
expect(float(claims[u('jti')])).to.be.greater_than_or_equal_to(float(0))
66-
expect(float(claims[u('jti')])).to.be.lower_than(float(1))
60+
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
6761
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
6862
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
6963
expect(archive).to.be.an(Archive)
@@ -108,12 +102,7 @@ def test_delete_archive(self):
108102

109103
archive.delete()
110104

111-
claims = jwt.decode(httpretty.last_request().headers[u('x-tb-opentok-auth')], self.api_secret, algorithms=[u('HS256')])
112-
expect(claims[u('iss')]).to.equal(self.api_key)
113-
expect(claims[u('ist')]).to.equal(u('project'))
114-
expect(float(claims[u('exp')])).to.be.greater_than(float(time.time()))
115-
expect(float(claims[u('jti')])).to.be.greater_than_or_equal_to(float(0))
116-
expect(float(claims[u('jti')])).to.be.lower_than(float(1))
105+
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
117106
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
118107
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
119108
# TODO: test that the object is invalidated

tests/test_archive_api.py

Lines changed: 13 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
import json
88
import datetime
99
import pytz
10-
from jose import jwt
11-
import time
10+
from .validate_jwt import validate_jwt_header
1211

1312
from opentok import OpenTok, Archive, ArchiveList, OutputModes, __version__
1413

@@ -43,12 +42,7 @@ def test_start_archive(self):
4342

4443
archive = self.opentok.start_archive(self.session_id)
4544

46-
claims = jwt.decode(httpretty.last_request().headers[u('x-tb-opentok-auth')], self.api_secret, algorithms=[u('HS256')])
47-
expect(claims[u('iss')]).to.equal(self.api_key)
48-
expect(claims[u('ist')]).to.equal(u('project'))
49-
expect(float(claims[u('exp')])).to.be.greater_than(float(time.time()))
50-
expect(float(claims[u('jti')])).to.be.greater_than_or_equal_to(float(0))
51-
expect(float(claims[u('jti')])).to.be.lower_than(float(1))
45+
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
5246
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
5347
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
5448
# non-deterministic json encoding. have to decode to test it properly
@@ -99,12 +93,7 @@ def test_start_archive_with_name(self):
9993

10094
archive = self.opentok.start_archive(self.session_id, name=u('ARCHIVE NAME'))
10195

102-
claims = jwt.decode(httpretty.last_request().headers[u('x-tb-opentok-auth')], self.api_secret, algorithms=[u('HS256')])
103-
expect(claims[u('iss')]).to.equal(self.api_key)
104-
expect(claims[u('ist')]).to.equal(u('project'))
105-
expect(float(claims[u('exp')])).to.be.greater_than(float(time.time()))
106-
expect(float(claims[u('jti')])).to.be.greater_than_or_equal_to(float(0))
107-
expect(float(claims[u('jti')])).to.be.lower_than(float(1))
96+
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
10897
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
10998
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
11099
# non-deterministic json encoding. have to decode to test it properly
@@ -153,12 +142,7 @@ def test_start_voice_archive(self):
153142

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

156-
claims = jwt.decode(httpretty.last_request().headers[u('x-tb-opentok-auth')], self.api_secret, algorithms=[u('HS256')])
157-
expect(claims[u('iss')]).to.equal(self.api_key)
158-
expect(claims[u('ist')]).to.equal(u('project'))
159-
expect(float(claims[u('exp')])).to.be.greater_than(float(time.time()))
160-
expect(float(claims[u('jti')])).to.be.greater_than_or_equal_to(float(0))
161-
expect(float(claims[u('jti')])).to.be.lower_than(float(1))
145+
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
162146
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
163147
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
164148
# non-deterministic json encoding. have to decode to test it properly
@@ -209,12 +193,7 @@ def test_start_individual_archive(self):
209193

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

212-
claims = jwt.decode(httpretty.last_request().headers[u('x-tb-opentok-auth')], self.api_secret, algorithms=[u('HS256')])
213-
expect(claims[u('iss')]).to.equal(self.api_key)
214-
expect(claims[u('ist')]).to.equal(u('project'))
215-
expect(float(claims[u('exp')])).to.be.greater_than(float(time.time()))
216-
expect(float(claims[u('jti')])).to.be.greater_than_or_equal_to(float(0))
217-
expect(float(claims[u('jti')])).to.be.lower_than(float(1))
196+
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
218197
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
219198
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
220199
# non-deterministic json encoding. have to decode to test it properly
@@ -266,12 +245,7 @@ def test_start_composed_archive(self):
266245

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

269-
claims = jwt.decode(httpretty.last_request().headers[u('x-tb-opentok-auth')], self.api_secret, algorithms=[u('HS256')])
270-
expect(claims[u('iss')]).to.equal(self.api_key)
271-
expect(claims[u('ist')]).to.equal(u('project'))
272-
expect(float(claims[u('exp')])).to.be.greater_than(float(time.time()))
273-
expect(float(claims[u('jti')])).to.be.greater_than_or_equal_to(float(0))
274-
expect(float(claims[u('jti')])).to.be.lower_than(float(1))
248+
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
275249
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
276250
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
277251
# non-deterministic json encoding. have to decode to test it properly
@@ -324,12 +298,7 @@ def test_stop_archive(self):
324298

325299
archive = self.opentok.stop_archive(archive_id)
326300

327-
claims = jwt.decode(httpretty.last_request().headers[u('x-tb-opentok-auth')], self.api_secret, algorithms=[u('HS256')])
328-
expect(claims[u('iss')]).to.equal(self.api_key)
329-
expect(claims[u('ist')]).to.equal(u('project'))
330-
expect(float(claims[u('exp')])).to.be.greater_than(float(time.time()))
331-
expect(float(claims[u('jti')])).to.be.greater_than_or_equal_to(float(0))
332-
expect(float(claims[u('jti')])).to.be.lower_than(float(1))
301+
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
333302
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
334303
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
335304
expect(archive).to.be.an(Archive)
@@ -356,12 +325,7 @@ def test_delete_archive(self):
356325

357326
self.opentok.delete_archive(archive_id)
358327

359-
claims = jwt.decode(httpretty.last_request().headers[u('x-tb-opentok-auth')], self.api_secret, algorithms=[u('HS256')])
360-
expect(claims[u('iss')]).to.equal(self.api_key)
361-
expect(claims[u('ist')]).to.equal(u('project'))
362-
expect(float(claims[u('exp')])).to.be.greater_than(float(time.time()))
363-
expect(float(claims[u('jti')])).to.be.greater_than_or_equal_to(float(0))
364-
expect(float(claims[u('jti')])).to.be.lower_than(float(1))
328+
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
365329
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
366330
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
367331

@@ -390,12 +354,7 @@ def test_find_archive(self):
390354

391355
archive = self.opentok.get_archive(archive_id)
392356

393-
claims = jwt.decode(httpretty.last_request().headers[u('x-tb-opentok-auth')], self.api_secret, algorithms=[u('HS256')])
394-
expect(claims[u('iss')]).to.equal(self.api_key)
395-
expect(claims[u('ist')]).to.equal(u('project'))
396-
expect(float(claims[u('exp')])).to.be.greater_than(float(time.time()))
397-
expect(float(claims[u('jti')])).to.be.greater_than_or_equal_to(float(0))
398-
expect(float(claims[u('jti')])).to.be.lower_than(float(1))
357+
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
399358
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
400359
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
401360
expect(archive).to.be.an(Archive)
@@ -510,12 +469,7 @@ def test_find_archives(self):
510469

511470
archive_list = self.opentok.get_archives()
512471

513-
claims = jwt.decode(httpretty.last_request().headers[u('x-tb-opentok-auth')], self.api_secret, algorithms=[u('HS256')])
514-
expect(claims[u('iss')]).to.equal(self.api_key)
515-
expect(claims[u('ist')]).to.equal(u('project'))
516-
expect(float(claims[u('exp')])).to.be.greater_than(float(time.time()))
517-
expect(float(claims[u('jti')])).to.be.greater_than_or_equal_to(float(0))
518-
expect(float(claims[u('jti')])).to.be.lower_than(float(1))
472+
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
519473
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
520474
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
521475
expect(archive_list).to.be.an(ArchiveList)
@@ -575,12 +529,7 @@ def test_find_archives_with_offset(self):
575529

576530
archive_list = self.opentok.get_archives(offset=3)
577531

578-
claims = jwt.decode(httpretty.last_request().headers[u('x-tb-opentok-auth')], self.api_secret, algorithms=[u('HS256')])
579-
expect(claims[u('iss')]).to.equal(self.api_key)
580-
expect(claims[u('ist')]).to.equal(u('project'))
581-
expect(float(claims[u('exp')])).to.be.greater_than(float(time.time()))
582-
expect(float(claims[u('jti')])).to.be.greater_than_or_equal_to(float(0))
583-
expect(float(claims[u('jti')])).to.be.lower_than(float(1))
532+
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
584533
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
585534
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
586535
expect(httpretty.last_request()).to.have.property("querystring").being.equal({
@@ -630,12 +579,7 @@ def test_find_archives_with_count(self):
630579

631580
archive_list = self.opentok.get_archives(count=2)
632581

633-
claims = jwt.decode(httpretty.last_request().headers[u('x-tb-opentok-auth')], self.api_secret, algorithms=[u('HS256')])
634-
expect(claims[u('iss')]).to.equal(self.api_key)
635-
expect(claims[u('ist')]).to.equal(u('project'))
636-
expect(float(claims[u('exp')])).to.be.greater_than(float(time.time()))
637-
expect(float(claims[u('jti')])).to.be.greater_than_or_equal_to(float(0))
638-
expect(float(claims[u('jti')])).to.be.lower_than(float(1))
582+
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
639583
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
640584
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
641585
expect(httpretty.last_request()).to.have.property("querystring").being.equal({
@@ -711,12 +655,7 @@ def test_find_archives_with_offset_and_count(self):
711655

712656
archive_list = self.opentok.get_archives(count=4, offset=2)
713657

714-
claims = jwt.decode(httpretty.last_request().headers[u('x-tb-opentok-auth')], self.api_secret, algorithms=[u('HS256')])
715-
expect(claims[u('iss')]).to.equal(self.api_key)
716-
expect(claims[u('ist')]).to.equal(u('project'))
717-
expect(float(claims[u('exp')])).to.be.greater_than(float(time.time()))
718-
expect(float(claims[u('jti')])).to.be.greater_than_or_equal_to(float(0))
719-
expect(float(claims[u('jti')])).to.be.lower_than(float(1))
658+
validate_jwt_header(self, httpretty.last_request().headers[u('x-tb-opentok-auth')])
720659
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
721660
expect(httpretty.last_request().headers[u('content-type')]).to.equal(u('application/json'))
722661
expect(httpretty.last_request()).to.have.property("querystring").being.equal({

0 commit comments

Comments
 (0)