Skip to content

Commit 9708f5c

Browse files
committed
Change JWT and JTI to use a split token
1 parent ea38840 commit 9708f5c

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

driftbase/api/clients.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,23 @@
1212
import http.client as http_client
1313
import json
1414
import logging
15+
1516
import marshmallow as ma
17+
from drift.blueprint import Blueprint, abort
18+
from drift.core.extensions.jwt import current_user, issue_token
19+
from drift.core.extensions.jwt import split_token
20+
from drift.core.extensions.urlregistry import Endpoints
21+
from drift.utils import json_response
1622
from flask import request, url_for, g, current_app
1723
from flask.views import MethodView
18-
from drift.blueprint import Blueprint, abort
1924
from flask_marshmallow.fields import AbsoluteURLFor
2025
from marshmallow_sqlalchemy import SQLAlchemyAutoSchema
2126

22-
from driftbase.richpresence import RichPresenceService
23-
24-
from drift.core.extensions.jwt import current_user, issue_token
25-
from drift.core.extensions.urlregistry import Endpoints
26-
from drift.utils import json_response
2727
from driftbase.config import get_client_heartbeat_config
2828
from driftbase.models.db import (
2929
User, CorePlayer, Client, UserIdentity
3030
)
31+
from driftbase.richpresence import RichPresenceService
3132
from driftbase.utils import url_client
3233

3334
log = logging.getLogger(__name__)
@@ -66,8 +67,8 @@ class Meta:
6667
exclude = ()
6768

6869
client_url = AbsoluteURLFor('clients.entry',
69-
doc="Fully qualified URL of the client resource",
70-
client_id='<client_id>')
70+
doc="Fully qualified URL of the client resource",
71+
client_id='<client_id>')
7172

7273

7374
class ClientPostRequestSchema(ma.Schema):
@@ -206,14 +207,9 @@ def post(self, args):
206207
payload = dict(current_user)
207208
payload["client_id"] = client_id
208209
new_token = issue_token(payload)
209-
210-
jwt = new_token["token"]
211-
jti = new_token['payload']["jti"]
210+
_, token = split_token(new_token["token"])
212211

213212
resource_url = url_client(client_id)
214-
response_header = {
215-
"Location": resource_url,
216-
}
217213
log.info("Client %s for user %s / player %s has been registered",
218214
client_id, user_id, player_id)
219215
heartbeat_period, heartbeat_timeout = get_client_heartbeat_config()
@@ -225,8 +221,8 @@ def post(self, args):
225221
"server_time": utcnow(),
226222
"next_heartbeat_seconds": heartbeat_period,
227223
"heartbeat_timeout": utcnow() + datetime.timedelta(seconds=heartbeat_timeout),
228-
"jti": jti,
229-
"jwt": jwt,
224+
"jti": token,
225+
"jwt": token,
230226
}
231227

232228
message_data = {

driftbase/api/useridentities.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from drift.blueprint import Blueprint, abort
1111
from drift.core.extensions.urlregistry import Endpoints
1212

13-
from drift.core.extensions.jwt import current_user, get_cached_token
13+
from drift.core.extensions.jwt import current_user, get_cached_token_payload
1414

1515
from driftbase.models.db import User, CorePlayer, UserIdentity
1616

@@ -138,7 +138,7 @@ def post(self, args):
138138
abort(http_client.NOT_FOUND, message="User %s is not active" % link_with_user_id)
139139

140140
# Verify that link_with_user_id matches user_id in link_with_user_jti
141-
link_with_user_jti_payload = get_cached_token(link_with_user_jti)
141+
link_with_user_jti_payload = get_cached_token_payload(link_with_user_jti, g.conf)
142142
if link_with_user_jti_payload["user_id"] != link_with_user_id:
143143
log.warning("Request for a user identity switch with user_id %s which does not "
144144
"match user_id %s from JWT",

tests/test_useridentities.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def test_identities_add_gamecenter(self):
8787
self.auth(username="device_%s" % uuid_string())
8888
r = self.get("/").json()
8989
device_user_id = r["current_user"]["user_id"]
90-
device_jti = r["current_user"]["jti"]
90+
device_jti = self.token
9191

9292
# switch to gamecenter user
9393
self.headers = headers_gamecenter

0 commit comments

Comments
 (0)