Skip to content

Commit 2f4d5d7

Browse files
authored
Merge pull request GetStream#89 from GetStream/task/add-user-session-token-support
Add user-session-token support
2 parents 2db98f6 + b46b50b commit 2f4d5d7

File tree

6 files changed

+35
-2
lines changed

6 files changed

+35
-2
lines changed

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ python:
66
- 3.4
77
- 3.5
88
- 3.6
9-
- 3.7
109

1110
matrix:
1211
fast_finish: true
12+
include:
13+
- python: 3.7
14+
dist: xenial
15+
sudo: true
1316

1417
cache: pip
1518

CHANGELOG

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
Change history
33
================
44

5+
======
6+
2.12.0
7+
======
8+
:release-date: 2018-10-08
9+
:by: Peter van Kampen
10+
11+
Add user-session-token support
12+
513
======
614
2.11.0
715
======

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
if sys.version_info < (2, 7, 0):
3232
install_requires.append('pyOpenSSL<18.0.0')
3333
install_requires.append('pyjwt>=1.3.0,<1.6.0')
34+
install_requires.append('pycparser<2.19')
3435
else:
3536
install_requires.append('pyjwt>=1.3.0,<1.7.0')
3637

stream/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
__copyright__ = 'Copyright 2014, Stream.io, Inc'
66
__credits__ = ['Thierry Schellenbach, mellowmorning.com, @tschellenbach']
77
__license__ = 'BSD-3-Clause'
8-
__version__ = '2.11.0'
8+
__version__ = '2.12.0'
99
__maintainer__ = 'Thierry Schellenbach'
1010
__email__ = 'support@getstream.io'
1111
__status__ = 'Production'

stream/client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,17 @@ def _make_signed_request(self, method_name, relative_url, params=None, data=None
174174
response.url, headers, data)
175175
return self._parse_response(response)
176176

177+
def create_user_session_token(self, user_id, **extra_data):
178+
'''Setup the payload for the given user_id with optional
179+
extra data (key, value pairs) and encode it using jwt
180+
'''
181+
payload = {
182+
'user_id': user_id,
183+
}
184+
for k, v in extra_data.items():
185+
payload[k] = v
186+
return jwt.encode(payload, self.api_secret, algorithm='HS256').decode("utf-8")
187+
177188
def create_jwt_token(self, resource, action, feed_id=None, user_id=None):
178189
'''
179190
Setup the payload for the given resource, action, feed or user

stream/tests/test_client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,16 @@ def test_token_retrieval(self):
372372
self.user1.token
373373
self.user1.get_readonly_token()
374374

375+
def test_user_session_token(self):
376+
client = stream.connect(self.c.api_key, self.c.api_secret)
377+
token = client.create_user_session_token("user")
378+
payload = jwt.decode(token, self.c.api_secret)
379+
self.assertEqual(payload["user_id"], "user")
380+
token = client.create_user_session_token("user", client="python", testing=True)
381+
payload = jwt.decode(token, self.c.api_secret)
382+
self.assertEqual(payload["client"], "python")
383+
self.assertEqual(payload["testing"], True)
384+
375385
def test_add_activity(self):
376386
feed = getfeed("user", "py1")
377387
activity_data = {"actor": 1, "verb": "tweet", "object": 1}

0 commit comments

Comments
 (0)