From a5450ca229f6bb18f2d42fa927c33b64749d3307 Mon Sep 17 00:00:00 2001 From: Deepjyoti Barman Date: Wed, 2 Oct 2024 08:36:49 +0530 Subject: [PATCH 1/3] Add support for normalizing connection URI for dashboard recipe --- recipe/dashboard/api/implementation.go | 11 +- recipe/dashboard/dashboardGet_test.go | 162 +++++++++++++++++++++++++ 2 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 recipe/dashboard/dashboardGet_test.go diff --git a/recipe/dashboard/api/implementation.go b/recipe/dashboard/api/implementation.go index 322a565f..f2a5f295 100644 --- a/recipe/dashboard/api/implementation.go +++ b/recipe/dashboard/api/implementation.go @@ -17,6 +17,7 @@ package api import ( "strconv" + "strings" "github.com/supertokens/supertokens-golang/recipe/dashboard/constants" "github.com/supertokens/supertokens-golang/recipe/dashboard/dashboardmodels" @@ -45,7 +46,15 @@ func MakeAPIImplementation() dashboardmodels.APIInterface { if err != nil { return "", err } - connectionURI := stInstance.SuperTokens.ConnectionURI + + connectionURIToNormalize := strings.Split(stInstance.SuperTokens.ConnectionURI, ";")[0] + + var normalizationError error + normalizedConnectionURI, normalizationError := supertokens.NewNormalisedURLDomain(connectionURIToNormalize) + if normalizationError != nil { + return "", normalizationError + } + connectionURI := normalizedConnectionURI.GetAsStringDangerous() normalizedDashboardPath, err := supertokens.NewNormalisedURLPath(constants.DashboardAPI) if err != nil { diff --git a/recipe/dashboard/dashboardGet_test.go b/recipe/dashboard/dashboardGet_test.go new file mode 100644 index 00000000..ef315ffa --- /dev/null +++ b/recipe/dashboard/dashboardGet_test.go @@ -0,0 +1,162 @@ +package dashboard + +import ( + "fmt" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/supertokens/supertokens-golang/recipe/emailpassword" + + "github.com/stretchr/testify/assert" + "github.com/supertokens/supertokens-golang/recipe/dashboard/dashboardmodels" + "github.com/supertokens/supertokens-golang/supertokens" + "github.com/supertokens/supertokens-golang/test/unittesting" +) + +func TestThatDashboardGetNormalizesConnectionURIWithoutHTTP(t *testing.T) { + connectionURI := "http://localhost:8080" + connectionURIWithoutProtocol := strings.Replace(connectionURI, "http://", "", -1) + config := supertokens.TypeInput{ + OnSuperTokensAPIError: func(err error, req *http.Request, res http.ResponseWriter) { + print(err) + }, + Supertokens: &supertokens.ConnectionInfo{ + ConnectionURI: connectionURIWithoutProtocol, + }, + AppInfo: supertokens.AppInfo{ + APIDomain: "api.supertokens.io", + AppName: "SuperTokens", + WebsiteDomain: "supertokens.io", + }, + RecipeList: []supertokens.Recipe{ + emailpassword.Init(nil), + Init(&dashboardmodels.TypeInput{ + ApiKey: "testapikey", + }), + }, + } + + BeforeEach() + unittesting.StartUpST("localhost", "8080") + defer AfterEach() + err := supertokens.Init(config) + if err != nil { + t.Error(err.Error()) + } + + mux := http.NewServeMux() + testServer := httptest.NewServer(supertokens.Middleware(mux)) + defer testServer.Close() + + req, err := http.NewRequest(http.MethodGet, testServer.URL+"/auth/dashboard", strings.NewReader(`{}`)) + req.Header.Set("Authorization", "Bearer testapikey") + res, err := http.DefaultClient.Do(req) + assert.Equal(t, res.StatusCode, 200) + + if err != nil { + t.Error(err.Error()) + } + + body, _ := io.ReadAll(res.Body) + assert.True(t, strings.Contains(string(body), fmt.Sprintf("window.connectionURI = \"%s\"", connectionURI))) +} + +func TestThatDashboardGetNormalizesConnectionURIWithoutHTTPS(t *testing.T) { + connectionURI := "https://try.supertokens.com" + connectionURIWithoutProtocol := strings.Replace(connectionURI, "https://", "", -1) + config := supertokens.TypeInput{ + OnSuperTokensAPIError: func(err error, req *http.Request, res http.ResponseWriter) { + print(err) + }, + Supertokens: &supertokens.ConnectionInfo{ + ConnectionURI: connectionURIWithoutProtocol, + }, + AppInfo: supertokens.AppInfo{ + APIDomain: "api.supertokens.io", + AppName: "SuperTokens", + WebsiteDomain: "supertokens.io", + }, + RecipeList: []supertokens.Recipe{ + emailpassword.Init(nil), + Init(&dashboardmodels.TypeInput{ + ApiKey: "testapikey", + }), + }, + } + + BeforeEach() + unittesting.StartUpST("localhost", "8080") + defer AfterEach() + err := supertokens.Init(config) + if err != nil { + t.Error(err.Error()) + } + + mux := http.NewServeMux() + testServer := httptest.NewServer(supertokens.Middleware(mux)) + defer testServer.Close() + + req, err := http.NewRequest(http.MethodGet, testServer.URL+"/auth/dashboard", strings.NewReader(`{}`)) + req.Header.Set("Authorization", "Bearer testapikey") + res, err := http.DefaultClient.Do(req) + assert.Equal(t, res.StatusCode, 200) + + if err != nil { + t.Error(err.Error()) + } + + body, _ := io.ReadAll(res.Body) + assert.True(t, strings.Contains(string(body), fmt.Sprintf("window.connectionURI = \"%s\"", connectionURI))) +} + +func TestThatDashboardGetReturnsFirstURIWhenMultipleArePassed(t *testing.T) { + firstConnectionURI := "http://localhost:8080" + secondConnectionURI := "https://try.supertokens.com" + multiplConnectionURIs := fmt.Sprintf("%s;%s", firstConnectionURI, secondConnectionURI) + config := supertokens.TypeInput{ + OnSuperTokensAPIError: func(err error, req *http.Request, res http.ResponseWriter) { + print(err) + }, + Supertokens: &supertokens.ConnectionInfo{ + ConnectionURI: multiplConnectionURIs, + }, + AppInfo: supertokens.AppInfo{ + APIDomain: "api.supertokens.io", + AppName: "SuperTokens", + WebsiteDomain: "supertokens.io", + }, + RecipeList: []supertokens.Recipe{ + emailpassword.Init(nil), + Init(&dashboardmodels.TypeInput{ + ApiKey: "testapikey", + }), + }, + } + + BeforeEach() + unittesting.StartUpST("localhost", "8080") + defer AfterEach() + err := supertokens.Init(config) + if err != nil { + t.Error(err.Error()) + } + + mux := http.NewServeMux() + testServer := httptest.NewServer(supertokens.Middleware(mux)) + defer testServer.Close() + + req, err := http.NewRequest(http.MethodGet, testServer.URL+"/auth/dashboard", strings.NewReader(`{}`)) + req.Header.Set("Authorization", "Bearer testapikey") + res, err := http.DefaultClient.Do(req) + assert.Equal(t, res.StatusCode, 200) + + if err != nil { + t.Error(err.Error()) + } + + body, _ := io.ReadAll(res.Body) + assert.True(t, strings.Contains(string(body), fmt.Sprintf("window.connectionURI = \"%s\"", firstConnectionURI))) +} From 81a062f2917136f64136ff41c1dc1485246bd6ac Mon Sep 17 00:00:00 2001 From: Deepjyoti Barman Date: Wed, 2 Oct 2024 08:38:30 +0530 Subject: [PATCH 2/3] Bump version and add changelog with changes --- CHANGELOG.md | 4 ++++ supertokens/constants.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d397b8a3..9c69a038 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [unreleased] +## [0.25.1] - 2024-10-02 + +- Adds support for normalizing the connection URI's before returning them in dashboard GET response. + ## [0.25.0] - 2024-09-25 ### Changes diff --git a/supertokens/constants.go b/supertokens/constants.go index 40e7b7a7..e17ec4e1 100644 --- a/supertokens/constants.go +++ b/supertokens/constants.go @@ -21,7 +21,7 @@ const ( ) // VERSION current version of the lib -const VERSION = "0.25.0" +const VERSION = "0.25.1" var ( cdiSupported = []string{"3.1"} From 4449b3847000bebc71ea9af9b3b44c02abd02971 Mon Sep 17 00:00:00 2001 From: Deepjyoti Barman Date: Wed, 2 Oct 2024 08:42:21 +0530 Subject: [PATCH 3/3] Add some comments on the newly added code --- recipe/dashboard/api/implementation.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/recipe/dashboard/api/implementation.go b/recipe/dashboard/api/implementation.go index f2a5f295..b6c0ea1e 100644 --- a/recipe/dashboard/api/implementation.go +++ b/recipe/dashboard/api/implementation.go @@ -47,11 +47,18 @@ func MakeAPIImplementation() dashboardmodels.APIInterface { return "", err } + // We are splitting the passed URI here so that if multiple URI's are passed + // separated by a colon, the first one is returned. connectionURIToNormalize := strings.Split(stInstance.SuperTokens.ConnectionURI, ";")[0] + // This normalizes the URI to make sure that it has things like protocol etc + // injected into it before it is returned. var normalizationError error normalizedConnectionURI, normalizationError := supertokens.NewNormalisedURLDomain(connectionURIToNormalize) if normalizationError != nil { + // In case of failures, we want to return a 500 here, mainly because that + // is what we return if the connectionURI is invalid which is the case here + // if normalization fails. return "", normalizationError } connectionURI := normalizedConnectionURI.GetAsStringDangerous()