Skip to content

Commit 6fdcb59

Browse files
committed
header based changes for session
1 parent e1650b8 commit 6fdcb59

File tree

4 files changed

+106
-8
lines changed

4 files changed

+106
-8
lines changed

.vscode/settings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
"python.linting.pylintEnabled": true,
33
"python.linting.enabled": true,
44
"editor.codeActionsOnSave": {
5-
"source.organizeImports": true
5+
"source.organizeImports": "explicit"
66
},
77
"python.analysis.typeCheckingMode": "strict",
88
"python.testing.unittestEnabled": false,
99
"python.testing.pytestEnabled": true,
10-
"python.analysis.autoImportCompletions": true
10+
"python.analysis.autoImportCompletions": true,
11+
"circleci.persistedProjectSelection": []
1112
}

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## [unreleased]
1010

11+
## [0.19.0] - 2024-05-06
12+
13+
- `create_new_session` now defaults to the value of the `st-auth-mode` header (if available) if the configured `get_token_transfer_method` returns `any`.
14+
1115
## [0.18.11] - 2024-04-26
1216

1317
- Fixes issues with the propagation of session creation/updates with django-rest-framework because the django-rest-framework wrapped the original request with it's own request object. Updates on that object were not reflecting on the original request object.

supertokens_python/recipe/session/session_request_functions.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
SessionConfig,
5151
TokenTransferMethod,
5252
get_required_claim_validators,
53+
get_auth_mode_from_header,
5354
)
5455
from supertokens_python.types import MaybeAwaitable
5556
from supertokens_python.utils import (
@@ -179,9 +180,11 @@ async def get_session_from_request(
179180
log_debug_message("getSession: Value of antiCsrfToken is: %s", do_anti_csrf_check)
180181

181182
session = await recipe_interface_impl.get_session(
182-
access_token=request_access_token.raw_token_string
183-
if request_access_token is not None
184-
else None,
183+
access_token=(
184+
request_access_token.raw_token_string
185+
if request_access_token is not None
186+
else None
187+
),
185188
anti_csrf_token=anti_csrf_token,
186189
anti_csrf_check=do_anti_csrf_check,
187190
session_required=session_required,
@@ -229,6 +232,7 @@ async def create_new_session_in_request(
229232
) -> SessionContainer:
230233
log_debug_message("createNewSession: Started")
231234

235+
# Handling framework specific request/response wrapping
232236
if not hasattr(request, "wrapper_used") or not request.wrapper_used:
233237
request = FRAMEWORKS[
234238
Supertokens.get_instance().app_info.framework
@@ -238,7 +242,6 @@ async def create_new_session_in_request(
238242
user_context = set_request_in_user_context_if_not_defined(user_context, request)
239243

240244
claims_added_by_other_recipes = recipe_instance.get_claims_added_by_other_recipes()
241-
app_info = recipe_instance.app_info
242245
issuer = (
243246
app_info.api_domain.get_as_string_dangerous()
244247
+ app_info.api_base_path.get_as_string_dangerous()
@@ -252,15 +255,20 @@ async def create_new_session_in_request(
252255

253256
for claim in claims_added_by_other_recipes:
254257
update = await claim.build(user_id, tenant_id, user_context)
255-
final_access_token_payload = {**final_access_token_payload, **update}
258+
final_access_token_payload.update(update)
256259

257260
log_debug_message("createNewSession: Access token payload built")
258261

259262
output_transfer_method = config.get_token_transfer_method(
260263
request, True, user_context
261264
)
262265
if output_transfer_method == "any":
263-
output_transfer_method = "header"
266+
auth_mode_header = get_auth_mode_from_header(request)
267+
if auth_mode_header == "cookie":
268+
output_transfer_method = auth_mode_header
269+
else:
270+
output_transfer_method = "header"
271+
264272
log_debug_message(
265273
"createNewSession: using transfer method %s", output_transfer_method
266274
)

tests/sessions/test_auth_mode.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,91 @@ def check_extracted_info(
162162
assert False, "Invalid expected_transfer_method"
163163

164164

165+
@mark.asyncio
166+
async def test_use_headers_if_get_token_transfer_method_returns_any_and_no_st_auth_mode_header(
167+
app: TestClient,
168+
):
169+
init(
170+
**get_st_init_args(
171+
[
172+
session.init(
173+
anti_csrf="VIA_TOKEN",
174+
get_token_transfer_method=lambda _, __, ___: "any", # Always return "any"
175+
)
176+
]
177+
)
178+
)
179+
start_st()
180+
181+
# Create session without specifying st-auth-mode
182+
res = create_session(app)
183+
184+
# Assert that no tokens are set in the cookies
185+
assert res.get("accessToken") is None
186+
assert res.get("refreshToken") is None
187+
assert res.get("antiCsrf") is None
188+
189+
# Assert that tokens are set in the headers
190+
assert res.get("accessTokenFromHeader") is not None
191+
assert res.get("refreshTokenFromHeader") is not None
192+
193+
194+
@mark.asyncio
195+
async def test_should_use_cookies_if_get_token_transfer_method_returns_any_and_st_auth_mode_is_set_to_cookie(
196+
app: TestClient,
197+
):
198+
init(
199+
**get_st_init_args(
200+
[
201+
session.init(
202+
anti_csrf="VIA_TOKEN",
203+
get_token_transfer_method=lambda _, __, ___: "any", # Always returns "any"
204+
)
205+
]
206+
)
207+
)
208+
start_st()
209+
210+
# Creating session with st-auth-mode set to 'cookie'
211+
res = create_session(app, auth_mode_header="cookie")
212+
213+
# Checking that the tokens are not set in headers
214+
assert res.get("accessToken") is not None
215+
assert res.get("refreshToken") is not None
216+
assert res.get("antiCsrf") is not None
217+
assert res.get("accessTokenFromHeader") is None
218+
assert res.get("refreshTokenFromHeader") is None
219+
220+
221+
@mark.asyncio
222+
async def test_use_headers_if_get_token_transfer_method_returns_any_and_st_auth_mode_is_set_to_header(
223+
app: TestClient,
224+
):
225+
init(
226+
**get_st_init_args(
227+
[
228+
session.init(
229+
anti_csrf="VIA_TOKEN",
230+
get_token_transfer_method=lambda _, __, ___: "any", # Always returns "any"
231+
)
232+
]
233+
)
234+
)
235+
start_st()
236+
237+
# Creating session with st-auth-mode set to 'header'
238+
res = create_session(app, auth_mode_header="header")
239+
240+
# Assert that no tokens are set in the cookies
241+
assert res.get("accessToken") is None
242+
assert res.get("refreshToken") is None
243+
assert res.get("antiCsrf") is None
244+
245+
# Assert that tokens are set in the headers
246+
assert res.get("accessTokenFromHeader") is not None
247+
assert res.get("refreshTokenFromHeader") is not None
248+
249+
165250
@mark.parametrize(
166251
"auth_mode_header, expected_transfer_method",
167252
[

0 commit comments

Comments
 (0)