cleanup error handling

This commit is contained in:
Jehiah Czebotar 2012-12-17 13:15:23 -05:00
parent 4177e94a09
commit 42359333b2
2 changed files with 26 additions and 24 deletions

View File

@ -100,7 +100,6 @@ func apiRequest(req *http.Request) (*simplejson.Json, error) {
} }
func (p *OauthProxy) redeemCode(code string) (string, error) { func (p *OauthProxy) redeemCode(code string) (string, error) {
params := url.Values{} params := url.Values{}
params.Add("redirect_uri", p.redirectUrl.String()) params.Add("redirect_uri", p.redirectUrl.String())
params.Add("client_id", p.clientID) params.Add("client_id", p.clientID)
@ -125,6 +124,7 @@ func (p *OauthProxy) redeemCode(code string) (string, error) {
} }
return access_token, nil return access_token, nil
} }
func (p *OauthProxy) getUserInfo(token string) (string, error) { func (p *OauthProxy) getUserInfo(token string) (string, error) {
params := url.Values{} params := url.Values{}
params.Add("access_token", token) params.Add("access_token", token)
@ -164,29 +164,33 @@ func ClearCookie(rw http.ResponseWriter, req *http.Request, key string) {
http.SetCookie(rw, cookie) http.SetCookie(rw, cookie)
} }
func ErrorPage(rw http.ResponseWriter, code int, title string, message string, signinmessage string) { func (p *OauthProxy) ErrorPage(rw http.ResponseWriter, code int, title string, message string) {
log.Printf("ErrorPage %d %s %s %s", code, title, message, signinmessage) log.Printf("ErrorPage %d %s %s", code, title, message)
rw.WriteHeader(code) rw.WriteHeader(code)
t := getTemplates() templates := getTemplates()
p := struct { t := struct {
Title string Title string
Message string Message string
SignInMessage string
}{ }{
Title: fmt.Sprintf("%d %s", code, title), Title: fmt.Sprintf("%d %s", code, title),
Message: message, Message: message,
SignInMessage: signinmessage,
} }
t.ExecuteTemplate(rw, "error.html", p) templates.ExecuteTemplate(rw, "error.html", t)
}
func (p *OauthProxy) SignInPage(rw http.ResponseWriter, req *http.Request, code int) {
// TODO: capture state for which url to redirect to at the end
rw.WriteHeader(code)
templates := getTemplates()
t := struct{ SignInMessage string }{SignInMessage: p.SignInMessage}
templates.ExecuteTemplate(rw, "sign_in.html", t)
} }
func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) { func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// check if this is a redirect back at the end of oauth // check if this is a redirect back at the end of oauth
if req.URL.Path == signInPath { if req.URL.Path == signInPath {
ClearCookie(rw, req, p.CookieKey) ClearCookie(rw, req, p.CookieKey)
t := getTemplates() p.SignInPage(rw, req, 200)
p := struct{ SignInMessage string }{SignInMessage: p.SignInMessage}
t.ExecuteTemplate(rw, "sign_in.html", p)
return return
} }
if req.URL.Path == oauthStartPath { if req.URL.Path == oauthStartPath {
@ -197,31 +201,31 @@ func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// finish the oauth cycle // finish the oauth cycle
reqParams, err := url.ParseQuery(req.URL.RawQuery) reqParams, err := url.ParseQuery(req.URL.RawQuery)
if err != nil { if err != nil {
ErrorPage(rw, 500, "Internal Error", err.Error(), p.SignInMessage) p.ErrorPage(rw, 500, "Internal Error", err.Error())
return return
} }
errorString, ok := reqParams["error"] errorString, ok := reqParams["error"]
if ok && len(errorString) == 1 { if ok && len(errorString) == 1 {
ErrorPage(rw, 403, "Permission Denied", errorString[0], p.SignInMessage) p.ErrorPage(rw, 403, "Permission Denied", errorString[0])
return return
} }
code, ok := reqParams["code"] code, ok := reqParams["code"]
if !ok || len(code) != 1 { if !ok || len(code) != 1 {
ErrorPage(rw, 500, "Internal Error", "Invalid API response", p.SignInMessage) p.ErrorPage(rw, 500, "Internal Error", "Invalid API response")
return return
} }
token, err := p.redeemCode(code[0]) token, err := p.redeemCode(code[0])
if err != nil { if err != nil {
log.Printf("error redeeming code %s", err.Error()) log.Printf("error redeeming code %s", err.Error())
ErrorPage(rw, 500, "Internal Error", err.Error(), p.SignInMessage) p.ErrorPage(rw, 500, "Internal Error", err.Error())
return return
} }
// validate user // validate user
email, err := p.getUserInfo(token) email, err := p.getUserInfo(token)
if err != nil { if err != nil {
log.Printf("error redeeming code %s", err.Error()) log.Printf("error redeeming code %s", err.Error())
ErrorPage(rw, 500, "Internal Error", err.Error(), p.SignInMessage) p.ErrorPage(rw, 500, "Internal Error", err.Error())
return return
} }
@ -246,10 +250,11 @@ func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
http.Redirect(rw, req, "/", 302) http.Redirect(rw, req, "/", 302)
return return
} else { } else {
ErrorPage(rw, 403, "Permission Denied", "Invalid Account", p.SignInMessage) p.ErrorPage(rw, 403, "Permission Denied", "Invalid Account")
return return
} }
} }
cookie, err := req.Cookie(p.CookieKey) cookie, err := req.Cookie(p.CookieKey)
var ok bool var ok bool
var email string var email string
@ -264,9 +269,8 @@ func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
} }
if !ok { if !ok {
log.Printf("invalid cookie. redirecting to sign in") log.Printf("invalid cookie")
// TODO: capture state for which url to redirect to at the end p.SignInPage(rw, req, 403)
http.Redirect(rw, req, "/oauth2/sign_in", 302)
return return
} }

View File

@ -18,16 +18,14 @@ func getTemplates() *template.Template {
if err != nil { if err != nil {
log.Fatalf("failed parsing template %s", err.Error()) log.Fatalf("failed parsing template %s", err.Error())
} }
t, err = t.Parse(`{{define "error.html"}} t, err = t.Parse(`{{define "error.html"}}
<html><head><title>{{.Title}}</title></head> <html><head><title>{{.Title}}</title></head>
<body> <body>
<h2>{{.Title}}</h2> <h2>{{.Title}}</h2>
<p>{{.Message}}</p> <p>{{.Message}}</p>
<hr> <hr>
<form method="GET" action="/oauth2/start"> <p><a href="/oauth2/sign_in">Sign In</a></p>
<button type="submit">Sign In w/ Google</button>
{{.SignInMessage}}
</form>
</body> </body>
</html>{{end}}`) </html>{{end}}`)
if err != nil { if err != nil {