Skip to content

Commit 752ba46

Browse files
authored
Merge pull request #78 from Wikia/PLATFORM-7433
PLATFORM-7433 take over return_to param from settings to login flow
2 parents 1066c2a + 014dedc commit 752ba46

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

selfservice/flow/settings/handler.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package settings
22

33
import (
44
"net/http"
5+
"strings"
56
"time"
67

78
"github.com/julienschmidt/httprouter"
@@ -18,6 +19,7 @@ import (
1819
"github.com/ory/kratos/schema"
1920
"github.com/ory/kratos/selfservice/errorx"
2021
"github.com/ory/kratos/selfservice/flow"
22+
"github.com/ory/kratos/selfservice/flow/login"
2123
"github.com/ory/kratos/session"
2224
"github.com/ory/kratos/text"
2325
"github.com/ory/kratos/ui/node"
@@ -85,7 +87,17 @@ func (h *Handler) RegisterPublicRoutes(public *x.RouterPublic) {
8587
if x.IsJSONRequest(r) {
8688
h.d.Writer().WriteError(w, r, session.NewErrNoActiveSessionFound())
8789
} else {
88-
http.Redirect(w, r, h.d.Config(r.Context()).SelfServiceFlowLoginUI().String(), http.StatusSeeOther)
90+
// Fandom-start - take over return_to param from settings to login flow
91+
// upstream PR https://github.com/ory/kratos/pull/2787
92+
// TODO: use url.JoinPath (available in go 1.19)
93+
loginPath := strings.TrimRight(h.d.Config(r.Context()).SelfPublicURL().String(), "/") + login.RouteInitBrowserFlow
94+
redirectUrl, err := x.TakeOverReturnToParameter(r.URL.String(), loginPath)
95+
if err != nil {
96+
http.Redirect(w, r, h.d.Config(r.Context()).SelfServiceFlowLoginUI().String(), http.StatusSeeOther)
97+
} else {
98+
http.Redirect(w, r, redirectUrl, http.StatusSeeOther)
99+
}
100+
// Fandom-end
89101
}
90102
}))
91103

selfservice/flow/settings/handler_test.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,24 @@ func TestHandler(t *testing.T) {
141141
})
142142

143143
t.Run("description=init a flow as browser", func(t *testing.T) {
144-
t.Run("description=without privileges", func(t *testing.T) {
145-
res, body := initSPAFlow(t, new(http.Client))
146-
assert.Equal(t, http.StatusUnauthorized, res.StatusCode, "%s", body)
147-
assert.Equal(t, text.ErrNoActiveSession, gjson.GetBytes(body, "error.id").String(), "%s", body)
144+
t.Run("case=unauthorized users are redirected to login", func(t *testing.T) {
145+
c := testhelpers.NewClientWithCookies(t)
146+
// prevent the redirect
147+
c.CheckRedirect = func(req *http.Request, via []*http.Request) error {
148+
return http.ErrUseLastResponse
149+
}
150+
returnTo := "?return_to=validRedirect"
151+
req, err := http.NewRequest("GET", publicTS.URL+settings.RouteInitBrowserFlow+returnTo, nil)
152+
require.NoError(t, err)
153+
154+
res, err := c.Do(req)
155+
require.NoError(t, err)
156+
// here we check that the redirect status is 303
157+
require.Equal(t, http.StatusSeeOther, res.StatusCode)
158+
location, err := res.Location()
159+
require.NoError(t, err)
160+
require.Equal(t, publicTS.URL+login.RouteInitBrowserFlow+returnTo, location.String())
161+
defer res.Body.Close()
148162
})
149163

150164
t.Run("description=success", func(t *testing.T) {
@@ -179,6 +193,12 @@ func TestHandler(t *testing.T) {
179193
})
180194

181195
t.Run("description=init a flow as SPA", func(t *testing.T) {
196+
t.Run("description=without privileges", func(t *testing.T) {
197+
res, body := initSPAFlow(t, new(http.Client))
198+
assert.Equal(t, http.StatusUnauthorized, res.StatusCode, "%s", body)
199+
assert.Equal(t, text.ErrNoActiveSession, gjson.GetBytes(body, "error.id").String(), "%s", body)
200+
})
201+
182202
t.Run("description=success", func(t *testing.T) {
183203
user1 := testhelpers.NewHTTPClientWithArbitrarySessionToken(t, reg)
184204
res, body := initSPAFlow(t, user1)

0 commit comments

Comments
 (0)