@@ -148,13 +148,13 @@ func (p *OauthProxy) getUserInfo(token string) (string, error) {
148
148
return email , nil
149
149
}
150
150
151
- func ClearCookie (rw http.ResponseWriter , req * http.Request , key string ) {
151
+ func ( p * OauthProxy ) ClearCookie (rw http.ResponseWriter , req * http.Request ) {
152
152
domain := strings .Split (req .Host , ":" )[0 ]
153
153
if * cookieDomain != "" {
154
154
domain = * cookieDomain
155
155
}
156
156
cookie := & http.Cookie {
157
- Name : key ,
157
+ Name : p . CookieKey ,
158
158
Value : "" ,
159
159
Path : "/" ,
160
160
Domain : domain ,
@@ -164,6 +164,25 @@ func ClearCookie(rw http.ResponseWriter, req *http.Request, key string) {
164
164
http .SetCookie (rw , cookie )
165
165
}
166
166
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
+
167
186
func (p * OauthProxy ) ErrorPage (rw http.ResponseWriter , code int , title string , message string ) {
168
187
log .Printf ("ErrorPage %d %s %s" , code , title , message )
169
188
rw .WriteHeader (code )
@@ -180,6 +199,7 @@ func (p *OauthProxy) ErrorPage(rw http.ResponseWriter, code int, title string, m
180
199
181
200
func (p * OauthProxy ) SignInPage (rw http.ResponseWriter , req * http.Request , code int ) {
182
201
// TODO: capture state for which url to redirect to at the end
202
+ p .ClearCookie (rw , req )
183
203
rw .WriteHeader (code )
184
204
templates := getTemplates ()
185
205
t := struct { SignInMessage string }{SignInMessage : p .SignInMessage }
@@ -189,7 +209,6 @@ func (p *OauthProxy) SignInPage(rw http.ResponseWriter, req *http.Request, code
189
209
func (p * OauthProxy ) ServeHTTP (rw http.ResponseWriter , req * http.Request ) {
190
210
// check if this is a redirect back at the end of oauth
191
211
if req .URL .Path == signInPath {
192
- ClearCookie (rw , req , p .CookieKey )
193
212
p .SignInPage (rw , req , 200 )
194
213
return
195
214
}
@@ -232,21 +251,7 @@ func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
232
251
// set cookie, or deny
233
252
if p .Validator (email ) {
234
253
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 )
250
255
http .Redirect (rw , req , "/" , 302 )
251
256
return
252
257
} else {
@@ -266,6 +271,9 @@ func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
266
271
267
272
if ! ok {
268
273
user , ok = p .CheckBasicAuth (req )
274
+ if ok {
275
+ p .SetCookie (rw , req , user )
276
+ }
269
277
}
270
278
271
279
if ! ok {
0 commit comments