Skip to content

Commit b95cd89

Browse files
Merge pull request #323 from supertokens/feat/overridable-req-access-token
feat: Make loading access token from the request overrideable
2 parents 5a58f79 + f8958ee commit b95cd89

File tree

6 files changed

+48
-29
lines changed

6 files changed

+48
-29
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88

9+
## [unreleased]
10+
11+
### Changes
12+
- Made the access token string optional in the overrideable `get_session` function
13+
- Moved checking if the access token is defined into the overrideable `get_session` function
14+
915
## [0.13.0] - 2023-05-04
1016
### Breaking changes
1117

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070

7171
setup(
7272
name="supertokens_python",
73-
version="0.13.0",
73+
version="0.13.1",
7474
author="SuperTokens",
7575
license="Apache 2.0",
7676
author_email="[email protected]",

supertokens_python/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from __future__ import annotations
1515

1616
SUPPORTED_CDI_VERSIONS = ["2.21"]
17-
VERSION = "0.13.0"
17+
VERSION = "0.13.1"
1818
TELEMETRY = "/telemetry"
1919
USER_COUNT = "/users/count"
2020
USER_DELETE = "/user/remove"

supertokens_python/recipe/session/interfaces.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ def get_global_claim_validators(
156156
@abstractmethod
157157
async def get_session(
158158
self,
159-
access_token: str,
160-
anti_csrf_token: Optional[str],
159+
access_token: Optional[str],
160+
anti_csrf_token: Optional[str] = None,
161161
anti_csrf_check: Optional[bool] = None,
162162
session_required: Optional[bool] = None,
163163
check_database: Optional[bool] = None,

supertokens_python/recipe/session/recipe_implementation.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ async def validate_claims_in_jwt_payload(
171171

172172
async def get_session(
173173
self,
174-
access_token: str,
175-
anti_csrf_token: Optional[str],
174+
access_token: Optional[str],
175+
anti_csrf_token: Optional[str] = None,
176176
anti_csrf_check: Optional[bool] = None,
177177
session_required: Optional[bool] = None,
178178
check_database: Optional[bool] = None,
@@ -194,6 +194,23 @@ async def get_session(
194194

195195
log_debug_message("getSession: Started")
196196

197+
if access_token is None:
198+
if session_required is False:
199+
log_debug_message(
200+
"getSession: returning None because access_token is undefined and session_required is False"
201+
)
202+
# there is no session that exists here, and the user wants session verification to be optional. So we return None
203+
return None
204+
205+
log_debug_message(
206+
"getSession: UNAUTHORISED because accessToken in request is undefined"
207+
)
208+
# we do not clear the session here because of a race condition mentioned in https://github.com/supertokens/supertokens-node/issues/17
209+
raise UnauthorisedError(
210+
"Session does not exist. Are you sending the session tokens in the request with the appropriate token transfer method?",
211+
clear_tokens=False,
212+
)
213+
197214
access_token_obj: Optional[ParsedJWTInfo] = None
198215
try:
199216
access_token_obj = parse_jwt_without_signature_verification(access_token)

supertokens_python/recipe/session/session_request_functions.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ async def get_session_from_request(
127127
allowed_transfer_method = config.get_token_transfer_method(
128128
request, False, user_context
129129
)
130-
request_transfer_method: TokenTransferMethod
131-
request_access_token: Union[ParsedJWTInfo, None]
130+
request_transfer_method: Optional[TokenTransferMethod] = None
131+
request_access_token: Union[ParsedJWTInfo, None] = None
132132

133133
if (allowed_transfer_method in ("any", "header")) and access_tokens.get(
134134
"header"
@@ -142,25 +142,6 @@ async def get_session_from_request(
142142
log_debug_message("getSession: using cookie transfer method")
143143
request_transfer_method = "cookie"
144144
request_access_token = access_tokens["cookie"]
145-
else:
146-
if session_optional:
147-
log_debug_message(
148-
"getSession: returning None because accessToken is undefined and sessionRequired is false"
149-
)
150-
# there is no session that exists here, and the user wants session verification
151-
# to be optional. So we return None
152-
return None
153-
154-
log_debug_message(
155-
"getSession: UNAUTHORISED because access_token in request is None"
156-
)
157-
# we do not clear the session here because of a race condition mentioned in:
158-
# https://github.com/supertokens/supertokens-node/issues/17
159-
raise_unauthorised_exception(
160-
"Session does not exist. Are you sending the session tokens in the "
161-
"request with the appropriate token transfer method?",
162-
clear_tokens=False,
163-
)
164145

165146
anti_csrf_token = get_anti_csrf_header(request)
166147
do_anti_csrf_check = anti_csrf_check
@@ -186,7 +167,9 @@ async def get_session_from_request(
186167
log_debug_message("getSession: Value of antiCsrfToken is: %s", do_anti_csrf_check)
187168

188169
session = await recipe_interface_impl.get_session(
189-
access_token=request_access_token.raw_token_string,
170+
access_token=request_access_token.raw_token_string
171+
if request_access_token is not None
172+
else None,
190173
anti_csrf_token=anti_csrf_token,
191174
anti_csrf_check=do_anti_csrf_check,
192175
check_database=check_database,
@@ -200,9 +183,22 @@ async def get_session_from_request(
200183
)
201184
await session.assert_claims(claim_validators, user_context)
202185

186+
# request_transfer_method can only be None here if the user overriddes get_session
187+
# to load the session by a custom method in that (very niche) case they also need to
188+
# override how the session is attached to the response.
189+
# In that scenario the transferMethod passed to attachToRequestResponse likely doesn't
190+
# matter, still, we follow the general fallback logic
191+
192+
if request_transfer_method is not None:
193+
final_transfer_method = request_transfer_method
194+
elif allowed_transfer_method != "any":
195+
final_transfer_method = allowed_transfer_method
196+
else:
197+
final_transfer_method = "header"
198+
203199
await session.attach_to_request_response(
204200
request,
205-
request_transfer_method,
201+
final_transfer_method,
206202
)
207203

208204
return session

0 commit comments

Comments
 (0)