-
Notifications
You must be signed in to change notification settings - Fork 33
/
utils.go
393 lines (347 loc) · 14.6 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package session
import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/supertokens/supertokens-golang/recipe/session/claims"
"github.com/supertokens/supertokens-golang/recipe/session/sessionwithjwt"
"github.com/supertokens/supertokens-golang/recipe/session/sessmodels"
"github.com/supertokens/supertokens-golang/supertokens"
"golang.org/x/net/publicsuffix"
)
func validateAndNormaliseUserInput(appInfo supertokens.NormalisedAppinfo, config *sessmodels.TypeInput) (sessmodels.TypeNormalisedInput, error) {
var (
cookieDomain *string = nil
err error
)
if config != nil && config.CookieDomain != nil {
cookieDomain, err = normaliseSessionScopeOrThrowError(*config.CookieDomain)
if err != nil {
return sessmodels.TypeNormalisedInput{}, err
}
}
topLevelAPIDomain, err := GetTopLevelDomainForSameSiteResolution(appInfo.APIDomain.GetAsStringDangerous())
if err != nil {
return sessmodels.TypeNormalisedInput{}, err
}
topLevelWebsiteDomain, err := GetTopLevelDomainForSameSiteResolution(appInfo.WebsiteDomain.GetAsStringDangerous())
if err != nil {
return sessmodels.TypeNormalisedInput{}, err
}
apiDomainScheme, err := GetURLScheme(appInfo.APIDomain.GetAsStringDangerous())
if err != nil {
return sessmodels.TypeNormalisedInput{}, err
}
websiteDomainScheme, err := GetURLScheme(appInfo.WebsiteDomain.GetAsStringDangerous())
if err != nil {
return sessmodels.TypeNormalisedInput{}, err
}
cookieSameSite := cookieSameSite_LAX
if topLevelAPIDomain != topLevelWebsiteDomain {
cookieSameSite = cookieSameSite_NONE
}
if apiDomainScheme != websiteDomainScheme {
cookieSameSite = cookieSameSite_NONE
}
if config != nil && config.CookieSameSite != nil {
cookieSameSite, err = normaliseSameSiteOrThrowError(*config.CookieSameSite)
if err != nil {
return sessmodels.TypeNormalisedInput{}, err
}
}
cookieSecure := false
if config == nil || config.CookieSecure == nil {
cookieSecure = strings.HasPrefix(appInfo.APIDomain.GetAsStringDangerous(), "https")
} else {
cookieSecure = *config.CookieSecure
}
sessionExpiredStatusCode := 401
if config != nil && config.SessionExpiredStatusCode != nil {
sessionExpiredStatusCode = *config.SessionExpiredStatusCode
}
invalidClaimStatusCode := 403
if config != nil && config.InvalidClaimStatusCode != nil {
invalidClaimStatusCode = *config.InvalidClaimStatusCode
}
if sessionExpiredStatusCode == invalidClaimStatusCode {
return sessmodels.TypeNormalisedInput{}, errors.New("SessionExpiredStatusCode and InvalidClaimStatusCode cannot have the same value")
}
if config != nil && config.AntiCsrf != nil {
if *config.AntiCsrf != antiCSRF_NONE && *config.AntiCsrf != antiCSRF_VIA_CUSTOM_HEADER && *config.AntiCsrf != antiCSRF_VIA_TOKEN {
return sessmodels.TypeNormalisedInput{}, errors.New("antiCsrf config must be one of 'NONE' or 'VIA_CUSTOM_HEADER' or 'VIA_TOKEN'")
}
}
antiCsrf := antiCSRF_NONE
if config == nil || config.AntiCsrf == nil {
if cookieSameSite == cookieSameSite_NONE {
antiCsrf = antiCSRF_VIA_CUSTOM_HEADER
} else {
antiCsrf = antiCSRF_NONE
}
} else {
antiCsrf = *config.AntiCsrf
}
errorHandlers := sessmodels.NormalisedErrorHandlers{
OnTokenTheftDetected: func(sessionHandle string, userID string, req *http.Request, res http.ResponseWriter) error {
recipeInstance, err := getRecipeInstanceOrThrowError()
if err != nil {
return err
}
return sendTokenTheftDetectedResponse(*recipeInstance, sessionHandle, userID, req, res)
},
OnTryRefreshToken: func(message string, req *http.Request, res http.ResponseWriter) error {
recipeInstance, err := getRecipeInstanceOrThrowError()
if err != nil {
return err
}
return sendTryRefreshTokenResponse(*recipeInstance, message, req, res)
},
OnUnauthorised: func(message string, req *http.Request, res http.ResponseWriter) error {
recipeInstance, err := getRecipeInstanceOrThrowError()
if err != nil {
return err
}
return sendUnauthorisedResponse(*recipeInstance, message, req, res)
},
OnInvalidClaim: func(validationErrors []claims.ClaimValidationError, req *http.Request, res http.ResponseWriter) error {
recipeInstance, err := getRecipeInstanceOrThrowError()
if err != nil {
return err
}
return sendInvalidClaimResponse(*recipeInstance, validationErrors, req, res)
},
}
if config != nil && config.ErrorHandlers != nil {
if config.ErrorHandlers.OnTokenTheftDetected != nil {
errorHandlers.OnTokenTheftDetected = config.ErrorHandlers.OnTokenTheftDetected
}
if config.ErrorHandlers.OnUnauthorised != nil {
errorHandlers.OnUnauthorised = config.ErrorHandlers.OnUnauthorised
}
if config.ErrorHandlers.OnInvalidClaim != nil {
errorHandlers.OnInvalidClaim = config.ErrorHandlers.OnInvalidClaim
}
}
IsAnIPAPIDomain, err := supertokens.IsAnIPAddress(topLevelAPIDomain)
if err != nil {
return sessmodels.TypeNormalisedInput{}, err
}
IsAnIPWebsiteDomain, err := supertokens.IsAnIPAddress(topLevelWebsiteDomain)
if err != nil {
return sessmodels.TypeNormalisedInput{}, err
}
if cookieSameSite == cookieSameSite_NONE &&
!cookieSecure && !((topLevelAPIDomain == "localhost" || IsAnIPAPIDomain) &&
(topLevelWebsiteDomain == "localhost" || IsAnIPWebsiteDomain)) {
// We can allow insecure cookie when both website & API domain are localhost or an IP
// When either of them is a different domain, API domain needs to have https and a secure cookie to work
return sessmodels.TypeNormalisedInput{}, errors.New("Since your API and website domain are different, for sessions to work, please use https on your apiDomain and dont set cookieSecure to false.")
}
refreshAPIPath, err := supertokens.NewNormalisedURLPath(refreshAPIPath)
if err != nil {
return sessmodels.TypeNormalisedInput{}, err
}
Jwt := sessmodels.JWTNormalisedConfig{Enable: false, PropertyNameInAccessTokenPayload: "jwt"}
if config != nil && config.Jwt != nil {
Jwt.Enable = config.Jwt.Enable
Jwt.Issuer = config.Jwt.Issuer
if config.Jwt.PropertyNameInAccessTokenPayload != nil {
Jwt.PropertyNameInAccessTokenPayload = *config.Jwt.PropertyNameInAccessTokenPayload
}
}
if sessionwithjwt.ACCESS_TOKEN_PAYLOAD_JWT_PROPERTY_NAME_KEY == Jwt.PropertyNameInAccessTokenPayload {
return sessmodels.TypeNormalisedInput{}, errors.New(sessionwithjwt.ACCESS_TOKEN_PAYLOAD_JWT_PROPERTY_NAME_KEY + " is a reserved property name, please use a different key name for the jwt")
}
typeNormalisedInput := sessmodels.TypeNormalisedInput{
RefreshTokenPath: appInfo.APIBasePath.AppendPath(refreshAPIPath),
CookieDomain: cookieDomain,
CookieSameSite: cookieSameSite,
CookieSecure: cookieSecure,
SessionExpiredStatusCode: sessionExpiredStatusCode,
InvalidClaimStatusCode: invalidClaimStatusCode,
AntiCsrf: antiCsrf,
ErrorHandlers: errorHandlers,
Jwt: Jwt,
Override: sessmodels.OverrideStruct{
Functions: func(originalImplementation sessmodels.RecipeInterface) sessmodels.RecipeInterface {
return originalImplementation
}, APIs: func(originalImplementation sessmodels.APIInterface) sessmodels.APIInterface {
return originalImplementation
},
OpenIdFeature: nil},
}
if config != nil && config.Override != nil {
if config.Override.Functions != nil {
typeNormalisedInput.Override.Functions = config.Override.Functions
}
if config.Override.APIs != nil {
typeNormalisedInput.Override.APIs = config.Override.APIs
}
typeNormalisedInput.Override.OpenIdFeature = config.Override.OpenIdFeature
}
return typeNormalisedInput, nil
}
func normaliseSameSiteOrThrowError(sameSite string) (string, error) {
sameSite = strings.TrimSpace(sameSite)
sameSite = strings.ToLower(sameSite)
if sameSite != cookieSameSite_STRICT && sameSite != cookieSameSite_LAX && sameSite != cookieSameSite_NONE {
return "", errors.New(`cookie same site must be one of "strict", "lax", or "none"`)
}
return sameSite, nil
}
func GetTopLevelDomainForSameSiteResolution(URL string) (string, error) {
urlObj, err := url.Parse(URL)
if err != nil {
return "", err
}
hostname := urlObj.Hostname()
isAnIP, err := supertokens.IsAnIPAddress(hostname)
if err != nil {
return "", err
}
if strings.HasPrefix(hostname, "localhost") || strings.HasPrefix(hostname, "localhost.org") || isAnIP {
return "localhost", nil
}
parsedURL, err := publicsuffix.EffectiveTLDPlusOne(hostname)
if err != nil {
return "", errors.New("Please make sure that the apiDomain and websiteDomain have correct values")
}
return parsedURL, nil
}
func GetURLScheme(URL string) (string, error) {
urlObj, err := url.Parse(URL)
if err != nil {
return "", err
}
return urlObj.Scheme, nil
}
func normaliseSessionScopeOrThrowError(sessionScope string) (*string, error) {
sessionScope = strings.TrimSpace(sessionScope)
sessionScope = strings.ToLower(sessionScope)
sessionScope = strings.TrimPrefix(sessionScope, ".")
if !strings.HasPrefix(sessionScope, "http://") && !strings.HasPrefix(sessionScope, "https://") {
sessionScope = "http://" + sessionScope
}
urlObj, err := url.Parse(sessionScope)
if err != nil {
return nil, errors.New("Please provide a valid sessionScope")
}
sessionScope = urlObj.Hostname()
sessionScope = strings.TrimPrefix(sessionScope, ".")
noDotNormalised := sessionScope
isAnIP, err := supertokens.IsAnIPAddress(sessionScope)
if err != nil {
return nil, err
}
if sessionScope == "localhost" || isAnIP {
noDotNormalised = sessionScope
}
if strings.HasPrefix(sessionScope, ".") {
noDotNormalised = "." + sessionScope
}
return &noDotNormalised, nil
}
func getCurrTimeInMS() uint64 {
return uint64(time.Now().UnixNano() / 1000000)
}
func attachCreateOrRefreshSessionResponseToRes(config sessmodels.TypeNormalisedInput, res http.ResponseWriter, response sessmodels.CreateOrRefreshAPIResponse) {
accessToken := response.AccessToken
refreshToken := response.RefreshToken
idRefreshToken := response.IDRefreshToken
setFrontTokenInHeaders(res, response.Session.UserID, response.AccessToken.Expiry, response.Session.UserDataInAccessToken)
attachAccessTokenToCookie(config, res, accessToken.Token, accessToken.Expiry)
attachRefreshTokenToCookie(config, res, refreshToken.Token, refreshToken.Expiry)
setIDRefreshTokenInHeaderAndCookie(config, res, idRefreshToken.Token, idRefreshToken.Expiry)
if response.AntiCsrfToken != nil {
setAntiCsrfTokenInHeaders(res, *response.AntiCsrfToken)
}
}
func sendTryRefreshTokenResponse(recipeInstance Recipe, _ string, _ *http.Request, response http.ResponseWriter) error {
return supertokens.SendNon200ResponseWithMessage(response, "try refresh token", recipeInstance.Config.SessionExpiredStatusCode)
}
func sendUnauthorisedResponse(recipeInstance Recipe, _ string, _ *http.Request, response http.ResponseWriter) error {
return supertokens.SendNon200ResponseWithMessage(response, "unauthorised", recipeInstance.Config.SessionExpiredStatusCode)
}
func sendInvalidClaimResponse(recipeInstance Recipe, claimValidationErrors []claims.ClaimValidationError, _ *http.Request, response http.ResponseWriter) error {
return supertokens.SendNon200Response(response, recipeInstance.Config.InvalidClaimStatusCode, map[string]interface{}{
"message": "invalid claim",
"claimValidationErrors": claimValidationErrors,
})
}
func sendTokenTheftDetectedResponse(recipeInstance Recipe, sessionHandle string, _ string, _ *http.Request, response http.ResponseWriter) error {
_, err := (*recipeInstance.RecipeImpl.RevokeSession)(sessionHandle, &map[string]interface{}{})
if err != nil {
return err
}
return supertokens.SendNon200ResponseWithMessage(response, "token theft detected", recipeInstance.Config.SessionExpiredStatusCode)
}
func frontendHasInterceptor(req *http.Request) bool {
return getRidFromHeader(req) != nil
}
func getKeyInfoFromJson(response map[string]interface{}) []sessmodels.KeyInfo {
keyList := []sessmodels.KeyInfo{}
_, ok := response["jwtSigningPublicKeyList"]
if ok {
for _, k := range response["jwtSigningPublicKeyList"].([]interface{}) {
keyList = append(keyList, sessmodels.KeyInfo{
PublicKey: (k.((map[string]interface{})))["publicKey"].(string),
ExpiryTime: uint64((k.((map[string]interface{})))["expiryTime"].(float64)),
CreatedAt: uint64((k.((map[string]interface{})))["createdAt"].(float64)),
})
}
}
return keyList
}
func validateClaimsInPayload(claimValidators []claims.SessionClaimValidator, newAccessTokenPayload map[string]interface{}, userContext supertokens.UserContext) []claims.ClaimValidationError {
validationErrors := []claims.ClaimValidationError{}
for _, validator := range claimValidators {
claimValidationResult := validator.Validate(newAccessTokenPayload, userContext)
supertokens.LogDebugMessage(fmt.Sprint("validateClaimsInPayload ", validator.ID, " validation res ", claimValidationResult))
if !claimValidationResult.IsValid {
validationErrors = append(validationErrors, claims.ClaimValidationError{
ID: validator.ID,
Reason: claimValidationResult.Reason,
})
}
}
return validationErrors
}
func getRequiredClaimValidators(
sessionContainer sessmodels.SessionContainer,
overrideGlobalClaimValidators func(globalClaimValidators []claims.SessionClaimValidator, sessionContainer sessmodels.SessionContainer, userContext supertokens.UserContext) ([]claims.SessionClaimValidator, error),
userContext supertokens.UserContext,
) ([]claims.SessionClaimValidator, error) {
instance, err := getRecipeInstanceOrThrowError()
if err != nil {
return nil, err
}
claimValidatorsAddedByOtherRecipes := instance.getClaimValidatorsAddedByOtherRecipes()
globalClaimValidators, err := (*instance.RecipeImpl.GetGlobalClaimValidators)(sessionContainer.GetUserID(), claimValidatorsAddedByOtherRecipes, userContext)
if err != nil {
return nil, err
}
if overrideGlobalClaimValidators != nil {
globalClaimValidators, err = overrideGlobalClaimValidators(globalClaimValidators, sessionContainer, userContext)
if err != nil {
return nil, err
}
}
return globalClaimValidators, nil
}