Skip to content

Commit cc0e0cf

Browse files
Merge pull request #304 from supertokens/refactor/additional-tests-and-changes
chore: Add additional tests for session verification
2 parents 2917ebc + 817b8b2 commit cc0e0cf

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

CHANGELOG.md

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

88
## [unreleased]
99

10+
- Adds additional tests for session verification
11+
1012
## [0.12.7] - 2023-06-05
1113

1214
### Fixes

recipe/session/session_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,6 +2018,63 @@ func TestThatLockingForJWKSCacheWorksFine(t *testing.T) {
20182018
JWKCacheMaxAgeInMs = originalCacheAge
20192019
}
20202020

2021+
func TestThatGetSessionThrowsWIthDynamicKeysIfSessionWasCreatedWithStaticKeys(t *testing.T) {
2022+
False := false
2023+
configValue := supertokens.TypeInput{
2024+
Supertokens: &supertokens.ConnectionInfo{
2025+
ConnectionURI: "http://localhost:8080",
2026+
},
2027+
AppInfo: supertokens.AppInfo{
2028+
APIDomain: "api.supertokens.io",
2029+
AppName: "SuperTokens",
2030+
WebsiteDomain: "supertokens.io",
2031+
},
2032+
RecipeList: []supertokens.Recipe{
2033+
Init(&sessmodels.TypeInput{
2034+
UseDynamicAccessTokenSigningKey: &False,
2035+
}),
2036+
},
2037+
}
2038+
2039+
BeforeEach()
2040+
unittesting.SetKeyValueInConfig("access_token_dynamic_signing_key_update_interval", "0.0014")
2041+
unittesting.StartUpST("localhost", "8080")
2042+
defer AfterEach()
2043+
err := supertokens.Init(configValue)
2044+
if err != nil {
2045+
t.Error(err.Error())
2046+
}
2047+
2048+
session, err := CreateNewSessionWithoutRequestResponse("testing-user", map[string]interface{}{}, map[string]interface{}{}, nil)
2049+
2050+
resetAll()
2051+
True := true
2052+
configValue = supertokens.TypeInput{
2053+
Supertokens: &supertokens.ConnectionInfo{
2054+
ConnectionURI: "http://localhost:8080",
2055+
},
2056+
AppInfo: supertokens.AppInfo{
2057+
AppName: "SuperTokens",
2058+
WebsiteDomain: "supertokens.io",
2059+
APIDomain: "api.supertokens.io",
2060+
},
2061+
RecipeList: []supertokens.Recipe{
2062+
Init(&sessmodels.TypeInput{
2063+
UseDynamicAccessTokenSigningKey: &True,
2064+
}),
2065+
},
2066+
}
2067+
err = supertokens.Init(configValue)
2068+
if err != nil {
2069+
t.Error(err.Error())
2070+
}
2071+
2072+
sessionTokens := session.GetAllSessionTokensDangerously()
2073+
session, err = GetSessionWithoutRequestResponse(sessionTokens.AccessToken, sessionTokens.AntiCsrfToken, nil)
2074+
assert.Error(t, err)
2075+
assert.Equal(t, err.Error(), "The access token doesn't match the useDynamicAccessTokenSigningKey setting")
2076+
}
2077+
20212078
func jwksLockTestRoutine(t *testing.T, shouldStop *bool, index int, group *sync.WaitGroup, doPost func([]string)) {
20222079
jwks, err := GetCombinedJWKS()
20232080
if err != nil {

recipe/session/verifySession_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,56 @@ func TestThatVerifySessionReturns401IfNoAccessTokenIsSentAndMiddlewareIsNotAdded
925925
assert.Equal(t, res.StatusCode, 401)
926926
}
927927

928+
func TestThatAntiCSRFCheckIsSkippedIfSessionRequiredIsFalseAndNoAccessTokenIsPassed(t *testing.T) {
929+
AntiCsrf := AntiCSRF_VIA_CUSTOM_HEADER
930+
configValue := supertokens.TypeInput{
931+
Supertokens: &supertokens.ConnectionInfo{
932+
ConnectionURI: "http://localhost:8080",
933+
},
934+
AppInfo: supertokens.AppInfo{
935+
AppName: "SuperTokens",
936+
WebsiteDomain: "supertokens.io",
937+
APIDomain: "api.supertokens.io",
938+
},
939+
RecipeList: []supertokens.Recipe{
940+
Init(&sessmodels.TypeInput{
941+
AntiCsrf: &AntiCsrf,
942+
GetTokenTransferMethod: func(req *http.Request, forCreateNewSession bool, userContext supertokens.UserContext) sessmodels.TokenTransferMethod {
943+
return sessmodels.CookieTransferMethod
944+
},
945+
}),
946+
},
947+
}
948+
BeforeEach()
949+
unittesting.StartUpST("localhost", "8080")
950+
defer AfterEach()
951+
err := supertokens.Init(configValue)
952+
if err != nil {
953+
t.Error(err.Error())
954+
}
955+
956+
app := getTestApp([]typeTestEndpoint{})
957+
defer app.Close()
958+
959+
session, err := CreateNewSessionWithoutRequestResponse("test-user", map[string]interface{}{}, map[string]interface{}{}, nil)
960+
assert.NoError(t, err)
961+
962+
sessionTokens := session.GetAllSessionTokensDangerously()
963+
964+
req, err := http.NewRequest(http.MethodGet, app.URL+"/verify", nil)
965+
assert.NoError(t, err)
966+
req.Header.Add("Cookie", "sAccessToken="+*sessionTokens.RefreshToken)
967+
968+
res, err := http.DefaultClient.Do(req)
969+
assert.Equal(t, res.StatusCode, 401)
970+
971+
req, err = http.NewRequest(http.MethodGet, app.URL+"/verify-optional", nil)
972+
assert.NoError(t, err)
973+
974+
res, err = http.DefaultClient.Do(req)
975+
assert.Equal(t, res.StatusCode, 200)
976+
}
977+
928978
type typeTestEndpoint struct {
929979
path string
930980
overrideGlobalClaimValidators func(globalClaimValidators []claims.SessionClaimValidator, sessionContainer sessmodels.SessionContainer, userContext supertokens.UserContext) ([]claims.SessionClaimValidator, error)
@@ -971,6 +1021,19 @@ func getTestApp(endpoints []typeTestEndpoint) *httptest.Server {
9711021
w.Write(respBytes)
9721022
})
9731023

1024+
False := false
1025+
mux.HandleFunc("/verify-optional", VerifySession(&sessmodels.VerifySessionOptions{
1026+
SessionRequired: &False,
1027+
}, func(rw http.ResponseWriter, r *http.Request) {
1028+
GetSession(r, rw, &sessmodels.VerifySessionOptions{
1029+
SessionRequired: &False,
1030+
})
1031+
}))
1032+
1033+
mux.HandleFunc("/verify", VerifySession(&sessmodels.VerifySessionOptions{}, func(rw http.ResponseWriter, r *http.Request) {
1034+
GetSession(r, rw, &sessmodels.VerifySessionOptions{})
1035+
}))
1036+
9741037
mux.HandleFunc("/default-claims", VerifySession(nil, func(w http.ResponseWriter, r *http.Request) {
9751038
sessionContainer := GetSessionFromRequestContext(r.Context())
9761039
resp := map[string]interface{}{

0 commit comments

Comments
 (0)