@@ -67,13 +67,16 @@ func (p *OauthProxy) SetRedirectUrl(redirectUrl *url.URL) {
67 p.redirectUrl = redirectUrl
67 p.redirectUrl = redirectUrl
70−func (p *OauthProxy) GetLoginURL() string {
70+func (p *OauthProxy) GetLoginURL(redirectUrl string) string {
71 params := url.Values{}
71 params := url.Values{}
72 params.Add("redirect_uri", p.redirectUrl.String())
72 params.Add("redirect_uri", p.redirectUrl.String())
73 params.Add("approval_prompt", "force")
73 params.Add("approval_prompt", "force")
74 params.Add("scope", p.oauthScope)
74 params.Add("scope", p.oauthScope)
75 params.Add("client_id", p.clientID)
75 params.Add("client_id", p.clientID)
76 params.Add("response_type", "code")
76 params.Add("response_type", "code")
77+ if strings.HasPrefix(redirectUrl, "/") {
78+ params.Add("state", redirectUrl)
77 return fmt.Sprintf("%s?%s", p.oauthLoginUrl, params.Encode())
80 return fmt.Sprintf("%s?%s", p.oauthLoginUrl, params.Encode())
@@ -100,6 +103,9 @@ func apiRequest(req *http.Request) (*simplejson.Json, error) {
102func (p *OauthProxy) redeemCode(code string) (string, error) {
105func (p *OauthProxy) redeemCode(code string) (string, error) {
107+ return "", errors.New("missing code")
103 params := url.Values{}
109 params := url.Values{}
104 params.Add("redirect_uri", p.redirectUrl.String())
110 params.Add("redirect_uri", p.redirectUrl.String())
105 params.Add("client_id", p.clientID)
111 params.Add("client_id", p.clientID)
@@ -197,7 +203,6 @@ func (p *OauthProxy) ErrorPage(rw http.ResponseWriter, code int, title string, m
199func (p *OauthProxy) SignInPage(rw http.ResponseWriter, req *http.Request, code int) {
205func (p *OauthProxy) SignInPage(rw http.ResponseWriter, req *http.Request, code int) {
200− // TODO: capture state for which url to redirect to at the end
201 p.ClearCookie(rw, req)
206 p.ClearCookie(rw, req)
202 rw.WriteHeader(code)
207 rw.WriteHeader(code)
203 templates := getTemplates()
208 templates := getTemplates()
@@ -205,9 +210,11 @@ func (p *OauthProxy) SignInPage(rw http.ResponseWriter, req *http.Request, code
205 t := struct {
210 t := struct {
206 SignInMessage string
211 SignInMessage string
207 Htpasswd bool
212 Htpasswd bool
209 SignInMessage: p.SignInMessage,
215 SignInMessage: p.SignInMessage,
210 Htpasswd: p.HtpasswdFile != nil,
216 Htpasswd: p.HtpasswdFile != nil,
217+ Redirect: req.URL.RequestURI(),
212 templates.ExecuteTemplate(rw, "sign_in.html", t)
219 templates.ExecuteTemplate(rw, "sign_in.html", t)
@@ -239,39 +246,46 @@ func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
240 var ok bool
247 var ok bool
241 var user string
248 var user string
249+ redirect := req.FormValue("rd")
242 if req.URL.Path == signInPath {
255 if req.URL.Path == signInPath {
243 user, ok = p.ManualSignIn(rw, req)
256 user, ok = p.ManualSignIn(rw, req)
245 p.SetCookie(rw, req, user)
258 p.SetCookie(rw, req, user)
246− http.Redirect(rw, req, "/", 302)
259+ http.Redirect(rw, req, redirect, 302)
248 p.SignInPage(rw, req, 200)
261 p.SignInPage(rw, req, 200)
252 if req.URL.Path == oauthStartPath {
265 if req.URL.Path == oauthStartPath {
266+ // get the ?rd= value
267+ err := req.ParseForm()
269+ p.ErrorPage(rw, 500, "Internal Error", err.Error())
253− http.Redirect(rw, req, p.GetLoginURL(), 302)
272+ http.Redirect(rw, req, p.GetLoginURL(redirect), 302)
256 if req.URL.Path == oauthCallbackPath {
275 if req.URL.Path == oauthCallbackPath {
257 // finish the oauth cycle
276 // finish the oauth cycle
258− reqParams, err := url.ParseQuery(req.URL.RawQuery)
277+ err := req.ParseForm()
259 if err != nil {
278 if err != nil {
260 p.ErrorPage(rw, 500, "Internal Error", err.Error())
279 p.ErrorPage(rw, 500, "Internal Error", err.Error())
263− errorString, ok := reqParams["error"]
282+ errorString := req.Form.Get("error")
264− if ok && len(errorString) == 1 {
283+ if errorString != "" {
265− p.ErrorPage(rw, 403, "Permission Denied", errorString[0])
284+ p.ErrorPage(rw, 403, "Permission Denied", errorString)
268− code, ok := reqParams["code"]
269− if !ok || len(code) != 1 {
270− p.ErrorPage(rw, 500, "Internal Error", "Invalid API response")
274− token, err := p.redeemCode(code[0])
288+ token, err := p.redeemCode(req.Form.Get("code"))
275 if err != nil {
289 if err != nil {
276 log.Printf("error redeeming code %s", err.Error())
290 log.Printf("error redeeming code %s", err.Error())
277 p.ErrorPage(rw, 500, "Internal Error", err.Error())
291 p.ErrorPage(rw, 500, "Internal Error", err.Error())
@@ -285,11 +299,16 @@ func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
302+ redirect := req.Form.Get("state")
288 // set cookie, or deny
307 // set cookie, or deny
289 if p.Validator(email) {
308 if p.Validator(email) {
290 log.Printf("authenticating %s completed", email)
309 log.Printf("authenticating %s completed", email)
291 p.SetCookie(rw, req, email)
310 p.SetCookie(rw, req, email)
292− http.Redirect(rw, req, "/", 302)
311+ http.Redirect(rw, req, redirect, 302)
295 p.ErrorPage(rw, 403, "Permission Denied", "Invalid Account")
314 p.ErrorPage(rw, 403, "Permission Denied", "Invalid Account")