14
14
from __future__ import annotations
15
15
16
16
import time
17
- from typing import TYPE_CHECKING , Any , Dict , List , Union
17
+ from typing import TYPE_CHECKING , Any , Dict , List , Union , Optional
18
18
19
19
from supertokens_python .recipe .session .interfaces import SessionInformationResult
20
20
28
28
from supertokens_python .logger import log_debug_message
29
29
from supertokens_python .normalised_url_path import NormalisedURLPath
30
30
from supertokens_python .process_state import AllowedProcessStates , ProcessState
31
+ from supertokens_python .recipe .session .interfaces import TokenInfo
31
32
32
33
from .exceptions import (
33
34
TryRefreshTokenError ,
37
38
)
38
39
39
40
41
+ class CreateOrRefreshAPIResponseSession :
42
+ def __init__ (self , handle : str , userId : str , userDataInJWT : Any ):
43
+ self .handle = handle
44
+ self .userId = userId
45
+ self .userDataInJWT = userDataInJWT
46
+
47
+
48
+ class CreateOrRefreshAPIResponse :
49
+ def __init__ (
50
+ self ,
51
+ session : CreateOrRefreshAPIResponseSession ,
52
+ accessToken : TokenInfo ,
53
+ refreshToken : TokenInfo ,
54
+ antiCsrfToken : Optional [str ],
55
+ ):
56
+ self .session = session
57
+ self .accessToken = accessToken
58
+ self .refreshToken = refreshToken
59
+ self .antiCsrfToken = antiCsrfToken
60
+
61
+
62
+ class GetSessionAPIResponseSession :
63
+ def __init__ (
64
+ self , handle : str , userId : str , userDataInJWT : Dict [str , Any ], expiryTime : int
65
+ ) -> None :
66
+ self .handle = handle
67
+ self .userId = userId
68
+ self .userDataInJWT = userDataInJWT
69
+ self .expiryTime = expiryTime
70
+
71
+
72
+ class GetSessionAPIResponseAccessToken :
73
+ def __init__ (self , token : str , expiry : int , createdTime : int ) -> None :
74
+ self .token = token
75
+ self .expiry = expiry
76
+ self .createdTime = createdTime
77
+
78
+
79
+ class GetSessionAPIResponse :
80
+ def __init__ (
81
+ self ,
82
+ session : GetSessionAPIResponseSession ,
83
+ accessToken : Optional [GetSessionAPIResponseAccessToken ] = None ,
84
+ ) -> None :
85
+ self .session = session
86
+ self .accessToken = accessToken
87
+
88
+
40
89
async def create_new_session (
41
90
recipe_implementation : RecipeImplementation ,
42
91
user_id : str ,
43
92
disable_anti_csrf : bool ,
44
93
access_token_payload : Union [None , Dict [str , Any ]],
45
94
session_data_in_database : Union [None , Dict [str , Any ]],
46
- ) -> Dict [ str , Any ] :
95
+ ) -> CreateOrRefreshAPIResponse :
47
96
if session_data_in_database is None :
48
97
session_data_in_database = {}
49
98
if access_token_payload is None :
@@ -66,7 +115,22 @@ async def create_new_session(
66
115
67
116
response .pop ("status" , None )
68
117
69
- return response # FIXME: type the response
118
+ return CreateOrRefreshAPIResponse (
119
+ CreateOrRefreshAPIResponseSession (
120
+ response ["handle" ], response ["userId" ], response ["userDataInJWT" ]
121
+ ),
122
+ TokenInfo (
123
+ response ["accessToken" ]["token" ],
124
+ response ["accessToken" ]["expiry" ],
125
+ response ["accessToken" ]["createdTime" ],
126
+ ),
127
+ TokenInfo (
128
+ response ["refreshToken" ]["token" ],
129
+ response ["refreshToken" ]["expiry" ],
130
+ response ["refreshToken" ]["createdTime" ],
131
+ ),
132
+ response ["antiCsrfToken" ] if "antiCsrfToken" in response else None ,
133
+ )
70
134
71
135
72
136
async def get_session (
@@ -75,9 +139,9 @@ async def get_session(
75
139
anti_csrf_token : Union [str , None ],
76
140
do_anti_csrf_check : bool ,
77
141
always_check_core : bool ,
78
- ) -> Dict [ str , Any ] :
142
+ ) -> GetSessionAPIResponse :
79
143
config = recipe_implementation .config
80
- access_token_info = None
144
+ access_token_info : Optional [ Dict [ str , Any ]] = None
81
145
82
146
try :
83
147
access_token_info = get_info_from_access_token (
@@ -179,14 +243,14 @@ async def get_session(
179
243
and not always_check_core
180
244
and access_token_info ["parentRefreshTokenHash1" ] is None
181
245
):
182
- return {
183
- "session" : {
184
- "handle" : access_token_info ["sessionHandle" ],
185
- "userId" : access_token_info ["userId" ],
186
- "userDataInJWT" : access_token_info ["userData" ],
187
- "expiryTime" : access_token_info ["expiryTime" ],
188
- }
189
- }
246
+ return GetSessionAPIResponse (
247
+ GetSessionAPIResponseSession (
248
+ access_token_info ["sessionHandle" ],
249
+ access_token_info ["userId" ],
250
+ access_token_info ["userData" ],
251
+ access_token_info ["expiryTime" ],
252
+ )
253
+ )
190
254
191
255
ProcessState .get_instance ().add_state (
192
256
AllowedProcessStates .CALLING_SERVICE_IN_VERIFY
@@ -206,7 +270,19 @@ async def get_session(
206
270
)
207
271
if response ["status" ] == "OK" :
208
272
response .pop ("status" , None )
209
- return response # FIXME: type the response
273
+ return GetSessionAPIResponse (
274
+ GetSessionAPIResponseSession (
275
+ response ["session" ]["handle" ],
276
+ response ["session" ]["userId" ],
277
+ response ["session" ]["userData" ],
278
+ response ["session" ]["expiresAt" ],
279
+ ),
280
+ GetSessionAPIResponseAccessToken (
281
+ response ["accessToken" ]["token" ],
282
+ response ["accessToken" ]["expiry" ],
283
+ response ["accessToken" ]["createdTime" ],
284
+ ),
285
+ )
210
286
if response ["status" ] == "UNAUTHORISED" :
211
287
log_debug_message ("getSession: Returning UNAUTHORISED because of core response" )
212
288
raise_unauthorised_exception (response ["message" ])
@@ -222,7 +298,7 @@ async def refresh_session(
222
298
refresh_token : str ,
223
299
anti_csrf_token : Union [str , None ],
224
300
disable_anti_csrf : bool ,
225
- ) -> Dict [ str , Any ] :
301
+ ) -> CreateOrRefreshAPIResponse :
226
302
data = {
227
303
"refreshToken" : refresh_token ,
228
304
"enableAntiCsrf" : (
@@ -249,7 +325,22 @@ async def refresh_session(
249
325
)
250
326
if response ["status" ] == "OK" :
251
327
response .pop ("status" , None )
252
- return response # FIXME: type the response
328
+ return CreateOrRefreshAPIResponse (
329
+ CreateOrRefreshAPIResponseSession (
330
+ response ["handle" ], response ["userId" ], response ["userDataInJWT" ]
331
+ ),
332
+ TokenInfo (
333
+ response ["accessToken" ]["token" ],
334
+ response ["accessToken" ]["expiry" ],
335
+ response ["accessToken" ]["createdTime" ],
336
+ ),
337
+ TokenInfo (
338
+ response ["refreshToken" ]["token" ],
339
+ response ["refreshToken" ]["expiry" ],
340
+ response ["refreshToken" ]["createdTime" ],
341
+ ),
342
+ response ["antiCsrfToken" ] if "antiCsrfToken" in response else None ,
343
+ )
253
344
if response ["status" ] == "UNAUTHORISED" :
254
345
log_debug_message (
255
346
"refreshSession: Returning UNAUTHORISED because of core response"
0 commit comments