From 4bd69baf6704c7458f4ec532ab08ccb85658dff0 Mon Sep 17 00:00:00 2001 From: rishabhpoddar Date: Wed, 22 Nov 2023 20:13:23 +0530 Subject: [PATCH] changes type input - not compiling --- supertokens/models.go | 18 +++++++------- supertokens/utils.go | 55 ++++++++++++++++++++++++++++++------------- 2 files changed, 48 insertions(+), 25 deletions(-) diff --git a/supertokens/models.go b/supertokens/models.go index 2a89cd96..7f84792e 100644 --- a/supertokens/models.go +++ b/supertokens/models.go @@ -20,19 +20,21 @@ import ( ) type NormalisedAppinfo struct { - AppName string - WebsiteDomain NormalisedURLDomain - APIDomain NormalisedURLDomain - TopLevelAPIDomain string - TopLevelWebsiteDomain string - APIBasePath NormalisedURLPath - APIGatewayPath NormalisedURLPath - WebsiteBasePath NormalisedURLPath + AppName string + GetOrigin func(request *http.Request, userContext UserContext) (NormalisedURLDomain, error) + APIDomain NormalisedURLDomain + TopLevelAPIDomain string + GetTopLevelWebsiteDomain func(request *http.Request, userContext UserContext) (string, error) + APIBasePath NormalisedURLPath + APIGatewayPath NormalisedURLPath + WebsiteBasePath NormalisedURLPath } type AppInfo struct { AppName string WebsiteDomain string + Origin string + GetOrigin func(request *http.Request, userContext UserContext) (string, error) APIDomain string WebsiteBasePath *string APIBasePath *string diff --git a/supertokens/utils.go b/supertokens/utils.go index f0e8c027..ef9bf330 100644 --- a/supertokens/utils.go +++ b/supertokens/utils.go @@ -47,9 +47,6 @@ func NormaliseInputAppInfoOrThrowError(appInfo AppInfo) (NormalisedAppinfo, erro if appInfo.AppName == "" { return NormalisedAppinfo{}, errors.New("Please provide your appName inside the appInfo object when calling supertokens.init") } - if appInfo.WebsiteDomain == "" { - return NormalisedAppinfo{}, errors.New("Please provide your websiteDomain inside the appInfo object when calling supertokens.init") - } apiGatewayPath, err := NewNormalisedURLPath("") if err != nil { return NormalisedAppinfo{}, err @@ -60,10 +57,30 @@ func NormaliseInputAppInfoOrThrowError(appInfo AppInfo) (NormalisedAppinfo, erro return NormalisedAppinfo{}, err } } - websiteDomain, err := NewNormalisedURLDomain(appInfo.WebsiteDomain) - if err != nil { - return NormalisedAppinfo{}, err + + if appInfo.Origin == "" && appInfo.WebsiteDomain == "" && appInfo.GetOrigin == nil { + return NormalisedAppinfo{}, errors.New("Please provide either Origin, GetOrigin or WebsiteDomain inside the appInfo object when calling supertokens.init") } + + websiteDomainFunction := func(request *http.Request, userContext UserContext) (NormalisedURLDomain, error) { + origin := appInfo.Origin + if origin == "" { + origin = appInfo.WebsiteDomain + } + + if appInfo.GetOrigin != nil { + originResult, err := appInfo.GetOrigin(request, userContext) + if err != nil { + return NormalisedURLDomain{}, err + } + origin = originResult + } + + return NormalisedURLDomain{ + value: origin, + }, nil + } + apiDomain, err := NewNormalisedURLDomain(appInfo.APIDomain) if err != nil { return NormalisedAppinfo{}, err @@ -73,9 +90,13 @@ func NormaliseInputAppInfoOrThrowError(appInfo AppInfo) (NormalisedAppinfo, erro if err != nil { return NormalisedAppinfo{}, err } - topLevelWebsiteDomain, err := GetTopLevelDomainForSameSiteResolution(websiteDomain.GetAsStringDangerous()) - if err != nil { - return NormalisedAppinfo{}, err + + getTopLevelWebsiteDomain := func(request *http.Request, userContext UserContext) (string, error) { + origin, err := websiteDomainFunction(request, userContext) + if err != nil { + return "", err + } + return GetTopLevelDomainForSameSiteResolution(origin.GetAsStringDangerous()) } APIBasePathStr := "/auth" @@ -97,14 +118,14 @@ func NormaliseInputAppInfoOrThrowError(appInfo AppInfo) (NormalisedAppinfo, erro return NormalisedAppinfo{}, err } return NormalisedAppinfo{ - AppName: appInfo.AppName, - APIGatewayPath: apiGatewayPath, - WebsiteDomain: websiteDomain, - APIDomain: apiDomain, - APIBasePath: apiBasePath, - TopLevelAPIDomain: topLevelAPIDomain, - TopLevelWebsiteDomain: topLevelWebsiteDomain, - WebsiteBasePath: websiteBasePath, + AppName: appInfo.AppName, + APIGatewayPath: apiGatewayPath, + GetOrigin: websiteDomainFunction, + APIDomain: apiDomain, + APIBasePath: apiBasePath, + TopLevelAPIDomain: topLevelAPIDomain, + GetTopLevelWebsiteDomain: getTopLevelWebsiteDomain, + WebsiteBasePath: websiteBasePath, }, nil }