Skip to content

Commit 0eff984

Browse files
Merge pull request #405 from furkansenharputlu/fix/CreateNewSession-default-token-transfer-method
fix: default to st-auth-mode if getTokenTransferMethod returns any in…
2 parents f581384 + 20cd6b9 commit 0eff984

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [unreleased]
99

10+
- `session.CreateNewSession` now defaults to the value of the `st-auth-mode` header (if available) if the configured `config.GetTokenTransferMethod` returns `any`.
1011

1112
## [0.17.5] - 2024-03-14
1213
- Adds a type uint64 to the `accessTokenCookiesExpiryDurationMillis` local variable in `recipe/session/utils.go`. It also removes the redundant `uint64` type forcing needed because of the untyped variable.

recipe/emailpassword/authMode_test.go

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,43 @@ func TestWithGetTokenTransferMethodProvidedCreateNewSessionWithShouldUseHeaderIf
224224
defer testServer.Close()
225225
setupRoutesForTest(t, mux)
226226

227-
resp := createNewSession(t, testServer.URL, nil, nil, nil, nil)
227+
t.Run("no st-auth-mode", func(t *testing.T) {
228+
resp := createNewSession(t, testServer.URL, nil, nil, nil, nil)
229+
230+
assert.Equal(t, resp["sAccessToken"], "-not-present-")
231+
assert.Equal(t, resp["sRefreshToken"], "-not-present-")
232+
assert.Equal(t, resp["antiCsrf"], "-not-present-")
233+
assert.NotEmpty(t, resp["accessTokenFromHeader"])
234+
assert.NotEqual(t, resp["accessTokenFromHeader"], "-not-present-")
235+
assert.NotEmpty(t, resp["refreshTokenFromHeader"])
236+
assert.NotEqual(t, resp["refreshTokenFromHeader"], "-not-present-")
237+
})
228238

229-
assert.Equal(t, resp["sAccessToken"], "-not-present-")
230-
assert.Equal(t, resp["sRefreshToken"], "-not-present-")
231-
assert.Equal(t, resp["antiCsrf"], "-not-present-")
232-
assert.NotEmpty(t, resp["accessTokenFromHeader"])
233-
assert.NotEqual(t, resp["accessTokenFromHeader"], "-not-present-")
234-
assert.NotEmpty(t, resp["refreshTokenFromHeader"])
235-
assert.NotEqual(t, resp["refreshTokenFromHeader"], "-not-present-")
239+
t.Run("st-auth-mode is cookie", func(t *testing.T) {
240+
authMode := string(sessmodels.CookieTransferMethod)
241+
resp := createNewSession(t, testServer.URL, &authMode, nil, nil, nil)
242+
243+
assert.NotEqual(t, resp["sAccessToken"], "-not-present-")
244+
assert.NotEqual(t, resp["sRefreshToken"], "-not-present-")
245+
assert.NotEqual(t, resp["antiCsrf"], "-not-present-")
246+
assert.NotEmpty(t, resp["accessTokenFromHeader"])
247+
assert.Equal(t, resp["accessTokenFromHeader"], "-not-present-")
248+
assert.NotEmpty(t, resp["refreshTokenFromHeader"])
249+
assert.Equal(t, resp["refreshTokenFromHeader"], "-not-present-")
250+
})
251+
252+
t.Run("st-auth-mode is header", func(t *testing.T) {
253+
authMode := string(sessmodels.HeaderTransferMethod)
254+
resp := createNewSession(t, testServer.URL, &authMode, nil, nil, nil)
255+
256+
assert.Equal(t, resp["sAccessToken"], "-not-present-")
257+
assert.Equal(t, resp["sRefreshToken"], "-not-present-")
258+
assert.Equal(t, resp["antiCsrf"], "-not-present-")
259+
assert.NotEmpty(t, resp["accessTokenFromHeader"])
260+
assert.NotEqual(t, resp["accessTokenFromHeader"], "-not-present-")
261+
assert.NotEmpty(t, resp["refreshTokenFromHeader"])
262+
assert.NotEqual(t, resp["refreshTokenFromHeader"], "-not-present-")
263+
})
236264
}
237265

238266
func TestWithGetTokenTransferMethodProvidedCreateNewSessionWithShouldUseHeaderIfMethodReturnsHeader(t *testing.T) {

recipe/session/sessionRequestFunctions.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ func CreateNewSessionInRequest(req *http.Request, res http.ResponseWriter, tenan
6060

6161
outputTokenTransferMethod := config.GetTokenTransferMethod(req, true, userContext)
6262
if outputTokenTransferMethod == sessmodels.AnyTransferMethod {
63-
outputTokenTransferMethod = sessmodels.HeaderTransferMethod
63+
authMode := GetAuthmodeFromHeader(req)
64+
if authMode != nil && *authMode == sessmodels.CookieTransferMethod {
65+
outputTokenTransferMethod = *authMode
66+
} else {
67+
outputTokenTransferMethod = sessmodels.HeaderTransferMethod
68+
}
6469
}
6570

6671
supertokens.LogDebugMessage(fmt.Sprintf("createNewSession: using transfer method %s", outputTokenTransferMethod))

0 commit comments

Comments
 (0)