Skip to content

Commit 9f53bcf

Browse files
committed
Merge branch 'session-error-throwing-fix' of github.com:supertokens/supertokens-golang into session-error-throwing-fix
2 parents 62d29b7 + c61d48f commit 9f53bcf

File tree

4 files changed

+82
-18
lines changed

4 files changed

+82
-18
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- `RegenerateAccessToken` now returns `nil` if the input access token's `sessionHandle` does not exist.
1616
- The session container functions have not changed in behaviour and return errors if `sessionHandle` does not exist. This works on the current session.
1717

18-
### Adds:
18+
### Fixes
19+
- Clears cookies when RevokeSession is called using the session container, even if the session did not exist from before: https://github.com/supertokens/supertokens-node/issues/343
1920

21+
### Adds:
2022
- Adds default userContext for API calls that contains the request object. It can be used in APIs / functions override like so:
2123

2224
```golang

recipe/session/session.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,11 @@ func makeSessionContainerInput(accessToken string, sessionHandle string, userID
4747
func newSessionContainer(config sessmodels.TypeNormalisedInput, session *SessionContainerInput) sessmodels.SessionContainer {
4848

4949
revokeSessionWithContext := func(userContext supertokens.UserContext) error {
50-
success, err := (*session.recipeImpl.RevokeSession)(session.sessionHandle, userContext)
50+
_, err := (*session.recipeImpl.RevokeSession)(session.sessionHandle, userContext)
5151
if err != nil {
5252
return err
5353
}
54-
if success {
55-
clearSessionFromCookie(config, session.res)
56-
}
54+
clearSessionFromCookie(config, session.res)
5755
return nil
5856
}
5957

recipe/session/session_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,3 +956,60 @@ func TestRevokedSessionThrowsErrorWhenCallingGetSessionBySessionHandle(t *testin
956956
assert.Nil(t, sessionInformation)
957957
assert.NoError(t, err)
958958
}
959+
960+
func TestSignoutWorksAfterSessionDeletedOnBackend(t *testing.T) {
961+
sessionHandle := ""
962+
customAntiCsrfVal := "VIA_TOKEN"
963+
configValue := supertokens.TypeInput{
964+
Supertokens: &supertokens.ConnectionInfo{
965+
ConnectionURI: "http://localhost:8080",
966+
},
967+
AppInfo: supertokens.AppInfo{
968+
AppName: "SuperTokens",
969+
WebsiteDomain: "supertokens.io",
970+
APIDomain: "api.supertokens.io",
971+
},
972+
RecipeList: []supertokens.Recipe{
973+
Init(&sessmodels.TypeInput{
974+
AntiCsrf: &customAntiCsrfVal,
975+
}),
976+
},
977+
}
978+
BeforeEach()
979+
unittesting.StartUpST("localhost", "8080")
980+
defer AfterEach()
981+
err := supertokens.Init(configValue)
982+
if err != nil {
983+
t.Error(err.Error())
984+
}
985+
986+
mux := http.NewServeMux()
987+
988+
mux.HandleFunc("/create", func(rw http.ResponseWriter, r *http.Request) {
989+
sess, _ := CreateNewSession(rw, "rope", map[string]interface{}{}, map[string]interface{}{})
990+
sessionHandle = sess.GetHandle()
991+
})
992+
993+
testServer := httptest.NewServer(supertokens.Middleware(mux))
994+
defer func() {
995+
testServer.Close()
996+
}()
997+
req, err := http.NewRequest(http.MethodGet, testServer.URL+"/create", nil)
998+
assert.NoError(t, err)
999+
res, err := http.DefaultClient.Do(req)
1000+
assert.NoError(t, err)
1001+
cookieData := unittesting.ExtractInfoFromResponse(res)
1002+
1003+
RevokeSession(sessionHandle)
1004+
1005+
resp1, err := unittesting.SignoutRequest(testServer.URL, cookieData["sAccessToken"], cookieData["sIdRefreshToken"], cookieData["antiCsrf"])
1006+
cookieData = unittesting.ExtractInfoFromResponse(resp1)
1007+
1008+
assert.Equal(t, cookieData["accessTokenExpiry"], "Thu, 01 Jan 1970 00:00:00 GMT")
1009+
assert.Equal(t, cookieData["refreshTokenExpiry"], "Thu, 01 Jan 1970 00:00:00 GMT")
1010+
assert.Equal(t, cookieData["idRefreshTokenExpiry"], "Thu, 01 Jan 1970 00:00:00 GMT")
1011+
assert.Equal(t, cookieData["accessToken"], "")
1012+
assert.Equal(t, cookieData["refreshToken"], "")
1013+
assert.Equal(t, cookieData["idRefreshTokenFromCookie"], "")
1014+
assert.Equal(t, cookieData["idRefreshTokenFromHeader"], "remove")
1015+
}

test/unittesting/testingutils.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ func ExtractInfoFromResponse(res *http.Response) map[string]string {
190190
var refreshTokenDomain string
191191
var refreshTokenHttpOnly = "false"
192192
var idRefreshTokenFromCookie string
193+
var idRefreshTokenFromHeader string
193194
var idRefreshTokenExpiry string
194195
var idRefreshTokenDomain string
195196
var idRefreshTokenHttpOnly = "false"
@@ -252,20 +253,26 @@ func ExtractInfoFromResponse(res *http.Response) map[string]string {
252253
}
253254
}
254255
}
256+
idRefreshTokenFromHeader = res.Header.Get("id-refresh-token")
257+
antiCsrfVal := ""
258+
if len(antiCsrf) > 0 {
259+
antiCsrfVal = antiCsrf[0]
260+
}
255261
return map[string]string{
256-
"antiCsrf": antiCsrf[0],
257-
"sAccessToken": accessToken,
258-
"sRefreshToken": refreshToken,
259-
"sIdRefreshToken": idRefreshTokenFromCookie,
260-
"refreshTokenExpiry": refreshTokenExpiry,
261-
"refreshTokenDomain": refreshTokenDomain,
262-
"refreshTokenHttpOnly": refreshTokenHttpOnly,
263-
"idRefreshTokenExpiry": idRefreshTokenExpiry,
264-
"idRefreshTokenDomain": idRefreshTokenDomain,
265-
"idRefreshTokenHttpOnly": idRefreshTokenHttpOnly,
266-
"accessTokenExpiry": accessTokenExpiry,
267-
"accessTokenDomain": accessTokenDomain,
268-
"accessTokenHttpOnly": accessTokenHttpOnly,
262+
"antiCsrf": antiCsrfVal,
263+
"sAccessToken": accessToken,
264+
"sRefreshToken": refreshToken,
265+
"sIdRefreshToken": idRefreshTokenFromCookie,
266+
"refreshTokenExpiry": refreshTokenExpiry,
267+
"refreshTokenDomain": refreshTokenDomain,
268+
"refreshTokenHttpOnly": refreshTokenHttpOnly,
269+
"idRefreshTokenExpiry": idRefreshTokenExpiry,
270+
"idRefreshTokenFromHeader": idRefreshTokenFromHeader,
271+
"idRefreshTokenDomain": idRefreshTokenDomain,
272+
"idRefreshTokenHttpOnly": idRefreshTokenHttpOnly,
273+
"accessTokenExpiry": accessTokenExpiry,
274+
"accessTokenDomain": accessTokenDomain,
275+
"accessTokenHttpOnly": accessTokenHttpOnly,
269276
}
270277
}
271278

0 commit comments

Comments
 (0)