Skip to content

Commit c459806

Browse files
committed
promote basic auth to cookie
1 parent 42f5391 commit c459806

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,9 @@ server {
8383
}
8484
}
8585
```
86+
87+
## Documentation
88+
89+
* /oauth2/sign_in - the login page, which also doubles as a sign out page (it clears cookies)
90+
* /oauth2/start - a URL that will redirect to start the oauth cycle
91+
* /oauth2/callback - the URL used at the end of the oauth cycle

oauthproxy.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,13 @@ func (p *OauthProxy) getUserInfo(token string) (string, error) {
148148
return email, nil
149149
}
150150

151-
func ClearCookie(rw http.ResponseWriter, req *http.Request, key string) {
151+
func (p *OauthProxy) ClearCookie(rw http.ResponseWriter, req *http.Request) {
152152
domain := strings.Split(req.Host, ":")[0]
153153
if *cookieDomain != "" {
154154
domain = *cookieDomain
155155
}
156156
cookie := &http.Cookie{
157-
Name: key,
157+
Name: p.CookieKey,
158158
Value: "",
159159
Path: "/",
160160
Domain: domain,
@@ -164,6 +164,25 @@ func ClearCookie(rw http.ResponseWriter, req *http.Request, key string) {
164164
http.SetCookie(rw, cookie)
165165
}
166166

167+
func (p *OauthProxy) SetCookie(rw http.ResponseWriter, req *http.Request, val string) {
168+
169+
domain := strings.Split(req.Host, ":")[0] // strip the port (if any)
170+
if *cookieDomain != "" {
171+
domain = *cookieDomain
172+
}
173+
cookie := &http.Cookie{
174+
Name: p.CookieKey,
175+
Value: signedCookieValue(p.CookieSeed, p.CookieKey, val),
176+
Path: "/",
177+
Domain: domain,
178+
Expires: time.Now().Add(time.Duration(168) * time.Hour), // 7 days
179+
HttpOnly: true,
180+
// Secure: req. ... ? set if X-Scheme: https ?
181+
}
182+
http.SetCookie(rw, cookie)
183+
}
184+
185+
167186
func (p *OauthProxy) ErrorPage(rw http.ResponseWriter, code int, title string, message string) {
168187
log.Printf("ErrorPage %d %s %s", code, title, message)
169188
rw.WriteHeader(code)
@@ -180,6 +199,7 @@ func (p *OauthProxy) ErrorPage(rw http.ResponseWriter, code int, title string, m
180199

181200
func (p *OauthProxy) SignInPage(rw http.ResponseWriter, req *http.Request, code int) {
182201
// TODO: capture state for which url to redirect to at the end
202+
p.ClearCookie(rw, req)
183203
rw.WriteHeader(code)
184204
templates := getTemplates()
185205
t := struct{ SignInMessage string }{SignInMessage: p.SignInMessage}
@@ -189,7 +209,6 @@ func (p *OauthProxy) SignInPage(rw http.ResponseWriter, req *http.Request, code
189209
func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
190210
// check if this is a redirect back at the end of oauth
191211
if req.URL.Path == signInPath {
192-
ClearCookie(rw, req, p.CookieKey)
193212
p.SignInPage(rw, req, 200)
194213
return
195214
}
@@ -232,21 +251,7 @@ func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
232251
// set cookie, or deny
233252
if p.Validator(email) {
234253
log.Printf("authenticating %s completed", email)
235-
domain := strings.Split(req.Host, ":")[0]
236-
if *cookieDomain != "" {
237-
domain = *cookieDomain
238-
}
239-
240-
cookie := &http.Cookie{
241-
Name: p.CookieKey,
242-
Value: signedCookieValue(p.CookieSeed, p.CookieKey, email),
243-
Path: "/",
244-
Domain: domain,
245-
Expires: time.Now().Add(time.Duration(168) * time.Hour), // 7 days
246-
HttpOnly: true,
247-
// Secure: req. ... ? set if X-Scheme: https ?
248-
}
249-
http.SetCookie(rw, cookie)
254+
p.SetCookie(rw, req, email)
250255
http.Redirect(rw, req, "/", 302)
251256
return
252257
} else {
@@ -266,6 +271,9 @@ func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
266271

267272
if !ok {
268273
user, ok = p.CheckBasicAuth(req)
274+
if ok {
275+
p.SetCookie(rw, req, user)
276+
}
269277
}
270278

271279
if !ok {

0 commit comments

Comments
 (0)