Skip to content

Commit 4235933

Browse files
committed
cleanup error handling
1 parent 4177e94 commit 4235933

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

oauthproxy.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ func apiRequest(req *http.Request) (*simplejson.Json, error) {
100100
}
101101

102102
func (p *OauthProxy) redeemCode(code string) (string, error) {
103-
104103
params := url.Values{}
105104
params.Add("redirect_uri", p.redirectUrl.String())
106105
params.Add("client_id", p.clientID)
@@ -125,6 +124,7 @@ func (p *OauthProxy) redeemCode(code string) (string, error) {
125124
}
126125
return access_token, nil
127126
}
127+
128128
func (p *OauthProxy) getUserInfo(token string) (string, error) {
129129
params := url.Values{}
130130
params.Add("access_token", token)
@@ -164,29 +164,33 @@ func ClearCookie(rw http.ResponseWriter, req *http.Request, key string) {
164164
http.SetCookie(rw, cookie)
165165
}
166166

167-
func ErrorPage(rw http.ResponseWriter, code int, title string, message string, signinmessage string) {
168-
log.Printf("ErrorPage %d %s %s %s", code, title, message, signinmessage)
167+
func (p *OauthProxy) ErrorPage(rw http.ResponseWriter, code int, title string, message string) {
168+
log.Printf("ErrorPage %d %s %s", code, title, message)
169169
rw.WriteHeader(code)
170-
t := getTemplates()
171-
p := struct {
170+
templates := getTemplates()
171+
t := struct {
172172
Title string
173173
Message string
174-
SignInMessage string
175174
}{
176175
Title: fmt.Sprintf("%d %s", code, title),
177176
Message: message,
178-
SignInMessage: signinmessage,
179177
}
180-
t.ExecuteTemplate(rw, "error.html", p)
178+
templates.ExecuteTemplate(rw, "error.html", t)
179+
}
180+
181+
func (p *OauthProxy) SignInPage(rw http.ResponseWriter, req *http.Request, code int) {
182+
// TODO: capture state for which url to redirect to at the end
183+
rw.WriteHeader(code)
184+
templates := getTemplates()
185+
t := struct{ SignInMessage string }{SignInMessage: p.SignInMessage}
186+
templates.ExecuteTemplate(rw, "sign_in.html", t)
181187
}
182188

183189
func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
184190
// check if this is a redirect back at the end of oauth
185191
if req.URL.Path == signInPath {
186192
ClearCookie(rw, req, p.CookieKey)
187-
t := getTemplates()
188-
p := struct{ SignInMessage string }{SignInMessage: p.SignInMessage}
189-
t.ExecuteTemplate(rw, "sign_in.html", p)
193+
p.SignInPage(rw, req, 200)
190194
return
191195
}
192196
if req.URL.Path == oauthStartPath {
@@ -197,31 +201,31 @@ func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
197201
// finish the oauth cycle
198202
reqParams, err := url.ParseQuery(req.URL.RawQuery)
199203
if err != nil {
200-
ErrorPage(rw, 500, "Internal Error", err.Error(), p.SignInMessage)
204+
p.ErrorPage(rw, 500, "Internal Error", err.Error())
201205
return
202206
}
203207
errorString, ok := reqParams["error"]
204208
if ok && len(errorString) == 1 {
205-
ErrorPage(rw, 403, "Permission Denied", errorString[0], p.SignInMessage)
209+
p.ErrorPage(rw, 403, "Permission Denied", errorString[0])
206210
return
207211
}
208212
code, ok := reqParams["code"]
209213
if !ok || len(code) != 1 {
210-
ErrorPage(rw, 500, "Internal Error", "Invalid API response", p.SignInMessage)
214+
p.ErrorPage(rw, 500, "Internal Error", "Invalid API response")
211215
return
212216
}
213217

214218
token, err := p.redeemCode(code[0])
215219
if err != nil {
216220
log.Printf("error redeeming code %s", err.Error())
217-
ErrorPage(rw, 500, "Internal Error", err.Error(), p.SignInMessage)
221+
p.ErrorPage(rw, 500, "Internal Error", err.Error())
218222
return
219223
}
220224
// validate user
221225
email, err := p.getUserInfo(token)
222226
if err != nil {
223227
log.Printf("error redeeming code %s", err.Error())
224-
ErrorPage(rw, 500, "Internal Error", err.Error(), p.SignInMessage)
228+
p.ErrorPage(rw, 500, "Internal Error", err.Error())
225229
return
226230
}
227231

@@ -246,10 +250,11 @@ func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
246250
http.Redirect(rw, req, "/", 302)
247251
return
248252
} else {
249-
ErrorPage(rw, 403, "Permission Denied", "Invalid Account", p.SignInMessage)
253+
p.ErrorPage(rw, 403, "Permission Denied", "Invalid Account")
250254
return
251255
}
252256
}
257+
253258
cookie, err := req.Cookie(p.CookieKey)
254259
var ok bool
255260
var email string
@@ -264,9 +269,8 @@ func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
264269
}
265270

266271
if !ok {
267-
log.Printf("invalid cookie. redirecting to sign in")
268-
// TODO: capture state for which url to redirect to at the end
269-
http.Redirect(rw, req, "/oauth2/sign_in", 302)
272+
log.Printf("invalid cookie")
273+
p.SignInPage(rw, req, 403)
270274
return
271275
}
272276

templates.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@ func getTemplates() *template.Template {
1818
if err != nil {
1919
log.Fatalf("failed parsing template %s", err.Error())
2020
}
21+
2122
t, err = t.Parse(`{{define "error.html"}}
2223
<html><head><title>{{.Title}}</title></head>
2324
<body>
2425
<h2>{{.Title}}</h2>
2526
<p>{{.Message}}</p>
2627
<hr>
27-
<form method="GET" action="/oauth2/start">
28-
<button type="submit">Sign In w/ Google</button>
29-
{{.SignInMessage}}
30-
</form>
28+
<p><a href="/oauth2/sign_in">Sign In</a></p>
3129
</body>
3230
</html>{{end}}`)
3331
if err != nil {

0 commit comments

Comments
 (0)