Skip to content
This repository was archived by the owner on Jan 24, 2019. It is now read-only.

Redirect to uri even if --skip-provider-button #676

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions oauthproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ func (p *OAuthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
case path == p.SignOutPath:
p.SignOut(rw, req)
case path == p.OAuthStartPath:
p.OAuthStart(rw, req)
p.OAuthStart(rw, req, "")
case path == p.OAuthCallbackPath:
p.OAuthCallback(rw, req)
case path == p.AuthOnlyPath:
Expand All @@ -493,7 +493,7 @@ func (p *OAuthProxy) SignIn(rw http.ResponseWriter, req *http.Request) {
http.Redirect(rw, req, redirect, 302)
} else {
if p.SkipProviderButton {
p.OAuthStart(rw, req)
p.OAuthStart(rw, req, "")
} else {
p.SignInPage(rw, req, http.StatusOK)
}
Expand All @@ -505,17 +505,20 @@ func (p *OAuthProxy) SignOut(rw http.ResponseWriter, req *http.Request) {
http.Redirect(rw, req, "/", 302)
}

func (p *OAuthProxy) OAuthStart(rw http.ResponseWriter, req *http.Request) {
func (p *OAuthProxy) OAuthStart(rw http.ResponseWriter, req *http.Request, redirect string) {
nonce, err := cookie.Nonce()
if err != nil {
p.ErrorPage(rw, 500, "Internal Error", err.Error())
return
}
p.SetCSRFCookie(rw, req, nonce)
redirect, err := p.GetRedirect(req)
if err != nil {
p.ErrorPage(rw, 500, "Internal Error", err.Error())
return
// If not explicitly told where to redirect, try to get it from form parameters.
if redirect == "" {
redirect, err = p.GetRedirect(req)
if err != nil {
p.ErrorPage(rw, 500, "Internal Error", err.Error())
return
}
}
redirectURI := p.GetRedirectURI(req.Host)
http.Redirect(rw, req, p.provider.GetLoginURL(redirectURI, fmt.Sprintf("%v:%v", nonce, redirect)), 302)
Expand Down Expand Up @@ -598,7 +601,8 @@ func (p *OAuthProxy) Proxy(rw http.ResponseWriter, req *http.Request) {
"Internal Error", "Internal Error")
} else if status == http.StatusForbidden {
if p.SkipProviderButton {
p.OAuthStart(rw, req)
// Start OAuth but redirect back to this URI when complete
p.OAuthStart(rw, req, req.URL.RequestURI())
} else {
p.SignInPage(rw, req, http.StatusForbidden)
}
Expand Down
18 changes: 17 additions & 1 deletion oauthproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,19 @@ func TestSignInPageSkipProvider(t *testing.T) {
t.Fatal("Did not find pattern in body: " +
signInSkipProvider + "\nBody:\n" + body)
}

// State is a hex nonce, a colon (encoded as %3A), plus an escaped redirect path.
source_page_re := regexp.MustCompile("state=[0-9a-f]+%3A" + url.PathEscape(endpoint) + `[;"]`)
source_page_match := source_page_re.FindStringSubmatch(body)
if source_page_match == nil {
t.Fatal("Callback state should include redirect to original endpoint: " +
source_page_re.String() + "\nBody:\n" + body)
}
}

func TestSignInPageSkipProviderDirect(t *testing.T) {
sip_test := NewSignInPageTest(true)
const endpoint = "/sign_in"
const endpoint = "/oauth2/sign_in"

code, body := sip_test.GetEndpoint(endpoint)
assert.Equal(t, 302, code)
Expand All @@ -454,6 +462,14 @@ func TestSignInPageSkipProviderDirect(t *testing.T) {
t.Fatal("Did not find pattern in body: " +
signInSkipProvider + "\nBody:\n" + body)
}

// State is a hex nonce, a colon (encoded as %3A), plus an escaped redirect path.
source_page_re := regexp.MustCompile(`state=[0-9a-f]+%3A%2F[;"]`)
source_page_match := source_page_re.FindStringSubmatch(body)
if source_page_match == nil {
t.Fatal("Callback state should include redirect to /: " +
source_page_re.String() + "\nBody:\n" + body)
}
}

type ProcessCookieTest struct {
Expand Down