Skip to content

Commit 20b9861

Browse files
committed
Add user-session-token support
1 parent 2db98f6 commit 20b9861

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

‎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).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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,18 @@ 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+
self.assertEqual(token[:59], "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoidXNlci")
379+
payload = jwt.decode(token, self.c.api_secret)
380+
self.assertEqual(payload["user_id"], "user")
381+
token = client.create_user_session_token("user", client="python", testing=True)
382+
self.assertEqual(token[:59], "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoidXNlci")
383+
payload = jwt.decode(token, self.c.api_secret)
384+
self.assertEqual(payload["client"], "python")
385+
self.assertEqual(payload["testing"], True)
386+
375387
def test_add_activity(self):
376388
feed = getfeed("user", "py1")
377389
activity_data = {"actor": 1, "verb": "tweet", "object": 1}

0 commit comments

Comments
 (0)