13
13
# under the License.
14
14
from typing import Any , Dict , List , Union , TypeVar , Callable , Optional
15
15
16
+ from supertokens_python .exceptions import SuperTokensError
17
+ from supertokens_python .framework .request import BaseRequest
16
18
from supertokens_python .recipe .openid .interfaces import (
17
19
GetOpenIdDiscoveryConfigurationResult ,
18
20
)
26
28
ClaimsValidationResult ,
27
29
JSONObject ,
28
30
GetClaimValueOkResult ,
31
+ GetSessionUnauthorizedResponse ,
32
+ GetSessionTryRefreshTokenErrorResponse ,
33
+ GetSessionClaimValidationErrorResponse ,
34
+ CreateNewSessionResult ,
35
+ GetSessionOkResponse ,
36
+ RefreshSessionOkResponse ,
37
+ RefreshSessionUnauthorizedResponse ,
38
+ RefreshSessionTokenTheftErrorResponse ,
29
39
)
30
40
from supertokens_python .recipe .session .recipe import SessionRecipe
41
+ from supertokens_python .recipe .session .session_request_functions import (
42
+ get_session_from_request ,
43
+ create_new_session_in_request ,
44
+ refresh_session_in_request ,
45
+ )
31
46
from supertokens_python .types import MaybeAwaitable
32
47
from supertokens_python .utils import FRAMEWORKS , resolve , deprecated_warn
48
+ from ..exceptions import InvalidClaimsError
33
49
from ..utils import get_required_claim_validators
34
50
from ...jwt .interfaces import (
35
51
CreateJwtOkResult ,
41
57
42
58
43
59
async def create_new_session (
44
- request : Any ,
60
+ request : BaseRequest ,
61
+ # response: BaseResponse,
45
62
user_id : str ,
46
63
access_token_payload : Union [Dict [str , Any ], None ] = None ,
47
64
session_data_in_database : Union [Dict [str , Any ], None ] = None ,
@@ -54,6 +71,37 @@ async def create_new_session(
54
71
if access_token_payload is None :
55
72
access_token_payload = {}
56
73
74
+ recipe_instance = SessionRecipe .get_instance ()
75
+ config = recipe_instance .config
76
+ app_info = recipe_instance .app_info
77
+
78
+ return await create_new_session_in_request (
79
+ request ,
80
+ # response,
81
+ user_context ,
82
+ recipe_instance ,
83
+ access_token_payload ,
84
+ user_id ,
85
+ config ,
86
+ app_info ,
87
+ session_data_in_database ,
88
+ )
89
+
90
+
91
+ async def create_new_session_without_request_response (
92
+ user_id : str ,
93
+ access_token_payload : Union [Dict [str , Any ], None ] = None ,
94
+ session_data_in_database : Union [Dict [str , Any ], None ] = None ,
95
+ disable_anti_csrf : bool = False ,
96
+ user_context : Union [None , Dict [str , Any ]] = None ,
97
+ ) -> CreateNewSessionResult :
98
+ if user_context is None :
99
+ user_context = {}
100
+ if session_data_in_database is None :
101
+ session_data_in_database = {}
102
+ if access_token_payload is None :
103
+ access_token_payload = {}
104
+
57
105
claims_added_by_other_recipes = (
58
106
SessionRecipe .get_instance ().get_claims_added_by_other_recipes ()
59
107
)
@@ -63,16 +111,11 @@ async def create_new_session(
63
111
update = await claim .build (user_id , user_context )
64
112
final_access_token_payload = {** final_access_token_payload , ** update }
65
113
66
- if not hasattr (request , "wrapper_used" ) or not request .wrapper_used :
67
- request = FRAMEWORKS [
68
- SessionRecipe .get_instance ().app_info .framework
69
- ].wrap_request (request )
70
-
71
114
return await SessionRecipe .get_instance ().recipe_implementation .create_new_session (
72
- request ,
73
115
user_id ,
74
116
final_access_token_payload ,
75
117
session_data_in_database ,
118
+ disable_anti_csrf ,
76
119
user_context = user_context ,
77
120
)
78
121
@@ -236,9 +279,11 @@ async def remove_claim(
236
279
237
280
238
281
async def get_session (
239
- request : Any ,
240
- anti_csrf_check : Union [bool , None ] = None ,
241
- session_required : bool = True ,
282
+ request : BaseRequest ,
283
+ # response: BaseResponse,
284
+ session_required : Optional [bool ] = None ,
285
+ anti_csrf_check : Optional [bool ] = None ,
286
+ check_database : Optional [bool ] = None ,
242
287
override_global_claim_validators : Optional [
243
288
Callable [
244
289
[List [SessionClaimValidator ], SessionContainer , Dict [str , Any ]],
@@ -249,40 +294,114 @@ async def get_session(
249
294
) -> Union [SessionContainer , None ]:
250
295
if user_context is None :
251
296
user_context = {}
252
- if not hasattr (request , "wrapper_used" ) or not request .wrapper_used :
253
- request = FRAMEWORKS [
254
- SessionRecipe .get_instance ().app_info .framework
255
- ].wrap_request (request )
256
297
257
- session_recipe_impl = SessionRecipe .get_instance ().recipe_implementation
258
- session = await session_recipe_impl .get_session (
298
+ recipe_instance = SessionRecipe .get_instance ()
299
+ recipe_interface_impl = recipe_instance .recipe_implementation
300
+ config = recipe_instance .config
301
+
302
+ return await get_session_from_request (
259
303
request ,
304
+ # response,
305
+ config ,
306
+ recipe_interface_impl ,
307
+ session_required = session_required ,
308
+ anti_csrf_check = anti_csrf_check ,
309
+ check_database = check_database ,
310
+ override_global_claim_validators = override_global_claim_validators ,
311
+ user_context = user_context ,
312
+ )
313
+
314
+
315
+ # TODO: Add comments
316
+ async def get_session_without_request_response (
317
+ access_token : str ,
318
+ anti_csrf_token : Optional [str ] = None ,
319
+ anti_csrf_check : Optional [bool ] = None ,
320
+ check_database : Optional [bool ] = None ,
321
+ override_global_claim_validators : Optional [
322
+ Callable [
323
+ [List [SessionClaimValidator ], SessionContainer , Dict [str , Any ]],
324
+ MaybeAwaitable [List [SessionClaimValidator ]],
325
+ ]
326
+ ] = None ,
327
+ user_context : Union [None , Dict [str , Any ]] = None ,
328
+ ) -> Union [
329
+ GetSessionOkResponse ,
330
+ GetSessionUnauthorizedResponse ,
331
+ GetSessionTryRefreshTokenErrorResponse ,
332
+ GetSessionClaimValidationErrorResponse ,
333
+ ]:
334
+ if user_context is None :
335
+ user_context = {}
336
+
337
+ recipe_interface_impl = SessionRecipe .get_instance ().recipe_implementation
338
+
339
+ res = await recipe_interface_impl .get_session (
340
+ access_token ,
341
+ anti_csrf_token ,
260
342
anti_csrf_check ,
261
- session_required ,
343
+ check_database ,
344
+ override_global_claim_validators ,
262
345
user_context ,
263
346
)
264
347
265
- if session is not None :
348
+ if isinstance ( res , GetSessionOkResponse ) :
266
349
claim_validators = await get_required_claim_validators (
267
- session , override_global_claim_validators , user_context
350
+ res . session , override_global_claim_validators , user_context
268
351
)
269
- await session .assert_claims (claim_validators , user_context )
352
+ try :
353
+ await res .session .assert_claims (claim_validators , user_context )
354
+ except SuperTokensError as e :
355
+ if isinstance (e , InvalidClaimsError ):
356
+ return GetSessionClaimValidationErrorResponse (e ) # FIXME
357
+ raise e
270
358
271
- return session
359
+ return res
272
360
273
361
274
362
async def refresh_session (
275
- request : Any , user_context : Union [None , Dict [str , Any ]] = None
363
+ request : Any ,
364
+ # response: BaseResponse,
365
+ user_context : Union [None , Dict [str , Any ]] = None ,
276
366
) -> SessionContainer :
277
367
if user_context is None :
278
368
user_context = {}
369
+
279
370
if not hasattr (request , "wrapper_used" ) or not request .wrapper_used :
280
371
request = FRAMEWORKS [
281
372
SessionRecipe .get_instance ().app_info .framework
282
373
].wrap_request (request )
283
374
375
+ # TODO: wrap response if required
376
+
377
+ recipe_instance = SessionRecipe .get_instance ()
378
+ config = recipe_instance .config
379
+ recipe_interface_impl = recipe_instance .recipe_implementation
380
+
381
+ return await refresh_session_in_request (
382
+ request ,
383
+ # response,
384
+ user_context ,
385
+ config ,
386
+ recipe_interface_impl ,
387
+ )
388
+
389
+
390
+ async def refresh_session_without_request_response (
391
+ refresh_token : str ,
392
+ disable_anti_csrf : bool = False ,
393
+ anti_csrf_token : Optional [str ] = None ,
394
+ user_context : Optional [Dict [str , Any ]] = None ,
395
+ ) -> Union [
396
+ RefreshSessionOkResponse ,
397
+ RefreshSessionUnauthorizedResponse ,
398
+ RefreshSessionTokenTheftErrorResponse ,
399
+ ]:
400
+ if user_context is None :
401
+ user_context = {}
402
+
284
403
return await SessionRecipe .get_instance ().recipe_implementation .refresh_session (
285
- request , user_context
404
+ refresh_token , anti_csrf_token , disable_anti_csrf , user_context
286
405
)
287
406
288
407
0 commit comments