Skip to content

Commit

Permalink
Add handling to portal login redirect to preserve original path
Browse files Browse the repository at this point in the history
  • Loading branch information
tsatam committed Sep 15, 2023
1 parent c845f54 commit c2bfd91
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
27 changes: 21 additions & 6 deletions pkg/portal/middleware/aad.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ import (
const (
SessionName = "session"
// Expiration time in unix format
SessionKeyExpires = "expires"
sessionKeyState = "state"
SessionKeyUsername = "user_name"
SessionKeyGroups = "groups"
SessionKeyExpires = "expires"
sessionKeyState = "state"
sessionKeyRedirectUri = "redirect_uri"
SessionKeyUsername = "user_name"
SessionKeyGroups = "groups"
)

// AAD is responsible for ensuring that we have a valid login session with AAD.
Expand Down Expand Up @@ -175,7 +176,11 @@ func (a *aad) CheckAuthentication(h http.Handler) http.Handler {
ctx := r.Context()
if ctx.Value(ContextKeyUsername) == nil {
if r.URL != nil {
http.Redirect(w, r, "/api/login", http.StatusTemporaryRedirect)
redirect := "/api/login"
if r.URL.Path != "" {
redirect += "?" + sessionKeyRedirectUri + "=" + r.URL.Path
}
http.Redirect(w, r, redirect, http.StatusTemporaryRedirect)
return
}
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
Expand Down Expand Up @@ -223,6 +228,10 @@ func (a *aad) redirect(w http.ResponseWriter, r *http.Request) {
sessionKeyState: state,
}

if r.URL.Query().Has(sessionKeyRedirectUri) {
session.Values[sessionKeyRedirectUri] = r.URL.Query().Get(sessionKeyRedirectUri)
}

err = session.Save(r, w)
if err != nil {
a.internalServerError(w, err)
Expand Down Expand Up @@ -308,13 +317,19 @@ func (a *aad) callback(w http.ResponseWriter, r *http.Request) {
session.Values[SessionKeyGroups] = groupsIntersect
session.Values[SessionKeyExpires] = a.now().Add(a.sessionTimeout).Unix()

redirectUri := "/"
if v, ok := session.Values[sessionKeyRedirectUri]; ok {
redirectUri = v.(string)
delete(session.Values, sessionKeyRedirectUri)
}

err = session.Save(r, w)
if err != nil {
a.internalServerError(w, err)
return
}

http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
http.Redirect(w, r, redirectUri, http.StatusTemporaryRedirect)
}

// clientAssertion adds a JWT client assertion according to
Expand Down
11 changes: 11 additions & 0 deletions pkg/portal/middleware/aad_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func TestCheckAuthentication(t *testing.T) {
name string
request func(*aad) (*http.Request, error)
wantStatusCode int
wantRedirectTo string
wantAuthenticated bool
}{
{
Expand All @@ -220,6 +221,7 @@ func TestCheckAuthentication(t *testing.T) {
return http.NewRequestWithContext(ctx, http.MethodGet, "/api/info", nil)
},
wantStatusCode: http.StatusTemporaryRedirect,
wantRedirectTo: "/api/login?redirect_uri=/api/info",
},
{
name: "not authenticated",
Expand All @@ -228,6 +230,7 @@ func TestCheckAuthentication(t *testing.T) {
return http.NewRequestWithContext(ctx, http.MethodGet, "/callback", nil)
},
wantStatusCode: http.StatusTemporaryRedirect,
wantRedirectTo: "/api/login?redirect_uri=/callback",
},
{
name: "invalid cookie",
Expand Down Expand Up @@ -275,6 +278,14 @@ func TestCheckAuthentication(t *testing.T) {
t.Error(w.Code, tt.wantStatusCode)
}

if tt.wantRedirectTo != "" {
redirectLocation := w.Result().Header["Location"]

if redirectLocation == nil || len(redirectLocation) != 1 || redirectLocation[0] != tt.wantRedirectTo {
t.Error(redirectLocation, tt.wantRedirectTo)
}
}

if authenticated != tt.wantAuthenticated {
t.Fatal(authenticated)
}
Expand Down
6 changes: 5 additions & 1 deletion portal/v2/src/Request.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { convertTimeToHours } from "./ClusterDetailListComponents/Statistics/Gra

const OnError = (err: AxiosResponse): AxiosResponse | null => {
if (err.status === 403) {
document.location.href = "/api/login"
var href = "/api/login"
if (document.location.pathname !== "/") {
href += "?redirect_uri=" + document.location.pathname
}
document.location.href = href
return null
} else {
return err
Expand Down

0 comments on commit c2bfd91

Please sign in to comment.