git.iko.soy/typeable/oauth2-proxy
on master help sign in
redirect to original path after login (#24)

* redirect to original path after login * tests for new redirect behaviour * fixed comment * added redirect fix to changelog

commit090ff11923f475223e68c2ae83e644e17e9f9b39 authorSteve Arch <sarch@cloudbees.com> dateTue 29 Jan 2019, 12:13 (8 years ago) parent440d2f3
3 files changed+40−1
CHANGELOG.md+2 oauthproxy.go+4−1 oauthproxy_test.go+34
CHANGELOG.md+2
@@ -10,6 +10,8 @@
1010- [#21](https://github.com/pusher/oauth2_proxy/pull/21) Docker Improvement (@yaegashi)
1111 - Move Docker base image from debian to alpine
1212 - Install ca-certificates in docker image
13+- [#24](https://github.com/pusher/oauth2_proxy/pull/24) Redirect fix (@agentgonzo)
14+ - After a successful login, you will be redirected to your original URL rather than /
1315
1416# v3.0.0
1517
oauthproxy.go+4−1
@@ -564,7 +564,10 @@ func (p *OAuthProxy) GetRedirect(req *http.Request) (redirect string, err error)
564564
565565 redirect = req.Form.Get("rd")
566566 if redirect == "" || !strings.HasPrefix(redirect, "/") || strings.HasPrefix(redirect, "//") {
567− redirect = "/"
567+ redirect = req.URL.Path
568+ if strings.HasPrefix(redirect, p.ProxyPrefix) {
569+ redirect = "/"
570+ }
568571 }
569572
570573 return
oauthproxy_test.go+34
@@ -18,6 +18,7 @@ import (
1818 "github.com/mbland/hmacauth"
1919 "github.com/pusher/oauth2_proxy/providers"
2020 "github.com/stretchr/testify/assert"
21+ "github.com/stretchr/testify/require"
2122)
2223
2324func init() {
@@ -837,3 +838,36 @@ func TestRequestSignaturePostRequest(t *testing.T) {
837838 assert.Equal(t, 200, st.rw.Code)
838839 assert.Equal(t, st.rw.Body.String(), "signatures match")
839840}
841+
842+func TestGetRedirect(t *testing.T) {
843+ options := NewOptions()
844+ _ = options.Validate()
845+ require.NotEmpty(t, options.ProxyPrefix)
846+ proxy := NewOAuthProxy(options, func(s string) bool { return false })
847+
848+ tests := []struct {
849+ name string
850+ url string
851+ expectedRedirect string
852+ }{
853+ {
854+ name: "request outside of ProxyPrefix redirects to original URL",
855+ url: "/foo/bar",
856+ expectedRedirect: "/foo/bar",
857+ },
858+ {
859+ name: "request under ProxyPrefix redirects to root",
860+ url: proxy.ProxyPrefix + "/foo/bar",
861+ expectedRedirect: "/",
862+ },
863+ }
864+ for _, tt := range tests {
865+ t.Run(tt.name, func(t *testing.T) {
866+ req, _ := http.NewRequest("GET", tt.url, nil)
867+ redirect, err := proxy.GetRedirect(req)
868+
869+ assert.NoError(t, err)
870+ assert.Equal(t, tt.expectedRedirect, redirect)
871+ })
872+ }
873+}