@@ -155,8 +155,8 @@ <h1 class="title">Module <code>supertokens_python.recipe.session.session_request
155155 allowed_transfer_method = config.get_token_transfer_method(
156156 request, False, user_context
157157 )
158- request_transfer_method: TokenTransferMethod
159- request_access_token: Union[ParsedJWTInfo, None]
158+ request_transfer_method: Optional[ TokenTransferMethod] = None
159+ request_access_token: Union[ParsedJWTInfo, None] = None
160160
161161 if (allowed_transfer_method in ("any", "header")) and access_tokens.get(
162162 "header"
@@ -170,25 +170,6 @@ <h1 class="title">Module <code>supertokens_python.recipe.session.session_request
170170 log_debug_message("getSession: using cookie transfer method")
171171 request_transfer_method = "cookie"
172172 request_access_token = access_tokens["cookie"]
173- else:
174- if session_optional:
175- log_debug_message(
176- "getSession: returning None because accessToken is undefined and sessionRequired is false"
177- )
178- # there is no session that exists here, and the user wants session verification
179- # to be optional. So we return None
180- return None
181-
182- log_debug_message(
183- "getSession: UNAUTHORISED because access_token in request is None"
184- )
185- # we do not clear the session here because of a race condition mentioned in:
186- # https://github.com/supertokens/supertokens-node/issues/17
187- raise_unauthorised_exception(
188- "Session does not exist. Are you sending the session tokens in the "
189- "request with the appropriate token transfer method?",
190- clear_tokens=False,
191- )
192173
193174 anti_csrf_token = get_anti_csrf_header(request)
194175 do_anti_csrf_check = anti_csrf_check
@@ -214,7 +195,9 @@ <h1 class="title">Module <code>supertokens_python.recipe.session.session_request
214195 log_debug_message("getSession: Value of antiCsrfToken is: %s", do_anti_csrf_check)
215196
216197 session = await recipe_interface_impl.get_session(
217- access_token=request_access_token.raw_token_string,
198+ access_token=request_access_token.raw_token_string
199+ if request_access_token is not None
200+ else None,
218201 anti_csrf_token=anti_csrf_token,
219202 anti_csrf_check=do_anti_csrf_check,
220203 check_database=check_database,
@@ -228,9 +211,22 @@ <h1 class="title">Module <code>supertokens_python.recipe.session.session_request
228211 )
229212 await session.assert_claims(claim_validators, user_context)
230213
214+ # request_transfer_method can only be None here if the user overriddes get_session
215+ # to load the session by a custom method in that (very niche) case they also need to
216+ # override how the session is attached to the response.
217+ # In that scenario the transferMethod passed to attachToRequestResponse likely doesn't
218+ # matter, still, we follow the general fallback logic
219+
220+ if request_transfer_method is not None:
221+ final_transfer_method = request_transfer_method
222+ elif allowed_transfer_method != "any":
223+ final_transfer_method = allowed_transfer_method
224+ else:
225+ final_transfer_method = "header"
226+
231227 await session.attach_to_request_response(
232228 request,
233- request_transfer_method ,
229+ final_transfer_method ,
234230 )
235231
236232 return session
@@ -665,8 +661,8 @@ <h2 class="section-title" id="header-functions">Functions</h2>
665661 allowed_transfer_method = config.get_token_transfer_method(
666662 request, False, user_context
667663 )
668- request_transfer_method: TokenTransferMethod
669- request_access_token: Union[ParsedJWTInfo, None]
664+ request_transfer_method: Optional[ TokenTransferMethod] = None
665+ request_access_token: Union[ParsedJWTInfo, None] = None
670666
671667 if (allowed_transfer_method in ("any", "header")) and access_tokens.get(
672668 "header"
@@ -680,25 +676,6 @@ <h2 class="section-title" id="header-functions">Functions</h2>
680676 log_debug_message("getSession: using cookie transfer method")
681677 request_transfer_method = "cookie"
682678 request_access_token = access_tokens["cookie"]
683- else:
684- if session_optional:
685- log_debug_message(
686- "getSession: returning None because accessToken is undefined and sessionRequired is false"
687- )
688- # there is no session that exists here, and the user wants session verification
689- # to be optional. So we return None
690- return None
691-
692- log_debug_message(
693- "getSession: UNAUTHORISED because access_token in request is None"
694- )
695- # we do not clear the session here because of a race condition mentioned in:
696- # https://github.com/supertokens/supertokens-node/issues/17
697- raise_unauthorised_exception(
698- "Session does not exist. Are you sending the session tokens in the "
699- "request with the appropriate token transfer method?",
700- clear_tokens=False,
701- )
702679
703680 anti_csrf_token = get_anti_csrf_header(request)
704681 do_anti_csrf_check = anti_csrf_check
@@ -724,7 +701,9 @@ <h2 class="section-title" id="header-functions">Functions</h2>
724701 log_debug_message("getSession: Value of antiCsrfToken is: %s", do_anti_csrf_check)
725702
726703 session = await recipe_interface_impl.get_session(
727- access_token=request_access_token.raw_token_string,
704+ access_token=request_access_token.raw_token_string
705+ if request_access_token is not None
706+ else None,
728707 anti_csrf_token=anti_csrf_token,
729708 anti_csrf_check=do_anti_csrf_check,
730709 check_database=check_database,
@@ -738,9 +717,22 @@ <h2 class="section-title" id="header-functions">Functions</h2>
738717 )
739718 await session.assert_claims(claim_validators, user_context)
740719
720+ # request_transfer_method can only be None here if the user overriddes get_session
721+ # to load the session by a custom method in that (very niche) case they also need to
722+ # override how the session is attached to the response.
723+ # In that scenario the transferMethod passed to attachToRequestResponse likely doesn't
724+ # matter, still, we follow the general fallback logic
725+
726+ if request_transfer_method is not None:
727+ final_transfer_method = request_transfer_method
728+ elif allowed_transfer_method != "any":
729+ final_transfer_method = allowed_transfer_method
730+ else:
731+ final_transfer_method = "header"
732+
741733 await session.attach_to_request_response(
742734 request,
743- request_transfer_method ,
735+ final_transfer_method ,
744736 )
745737
746738 return session</ code > </ pre >
0 commit comments