@@ -150,7 +150,7 @@ func (p *OauthProxy) getUserInfo(token string) (string, error) {
150
150
151
151
func (p * OauthProxy ) ClearCookie (rw http.ResponseWriter , req * http.Request ) {
152
152
domain := strings .Split (req .Host , ":" )[0 ]
153
- if * cookieDomain != "" {
153
+ if * cookieDomain != "" && strings . HasSuffix ( domain , * cookieDomain ) {
154
154
domain = * cookieDomain
155
155
}
156
156
cookie := & http.Cookie {
@@ -165,9 +165,9 @@ func (p *OauthProxy) ClearCookie(rw http.ResponseWriter, req *http.Request) {
165
165
}
166
166
167
167
func (p * OauthProxy ) SetCookie (rw http.ResponseWriter , req * http.Request , val string ) {
168
-
168
+
169
169
domain := strings .Split (req .Host , ":" )[0 ] // strip the port (if any)
170
- if * cookieDomain != "" {
170
+ if * cookieDomain != "" && strings . HasSuffix ( domain , * cookieDomain ) {
171
171
domain = * cookieDomain
172
172
}
173
173
cookie := & http.Cookie {
@@ -182,7 +182,6 @@ func (p *OauthProxy) SetCookie(rw http.ResponseWriter, req *http.Request, val st
182
182
http .SetCookie (rw , cookie )
183
183
}
184
184
185
-
186
185
func (p * OauthProxy ) ErrorPage (rw http.ResponseWriter , code int , title string , message string ) {
187
186
log .Printf ("ErrorPage %d %s %s" , code , title , message )
188
187
rw .WriteHeader (code )
@@ -202,14 +201,52 @@ func (p *OauthProxy) SignInPage(rw http.ResponseWriter, req *http.Request, code
202
201
p .ClearCookie (rw , req )
203
202
rw .WriteHeader (code )
204
203
templates := getTemplates ()
205
- t := struct { SignInMessage string }{SignInMessage : p .SignInMessage }
204
+
205
+ t := struct {
206
+ SignInMessage string
207
+ Htpasswd bool
208
+ }{
209
+ SignInMessage : p .SignInMessage ,
210
+ Htpasswd : p .HtpasswdFile != nil ,
211
+ }
206
212
templates .ExecuteTemplate (rw , "sign_in.html" , t )
207
213
}
208
214
215
+ func (p * OauthProxy ) ManualSignIn (rw http.ResponseWriter , req * http.Request ) (string , bool ){
216
+ if req .Method != "POST" || p .HtpasswdFile == nil {
217
+ return "" , false
218
+ }
219
+ user := req .FormValue ("username" )
220
+ passwd := req .FormValue ("password" )
221
+ if user == "" {
222
+ return "" , false
223
+ }
224
+ // check auth
225
+ if p .HtpasswdFile .Validate (user , passwd ) {
226
+ log .Printf ("authenticated %s via manual sign in" , user )
227
+ return user , true
228
+ }
229
+ return "" , false
230
+ }
231
+
209
232
func (p * OauthProxy ) ServeHTTP (rw http.ResponseWriter , req * http.Request ) {
210
233
// check if this is a redirect back at the end of oauth
234
+ remoteIP := req .Header .Get ("X-Real-IP" )
235
+ if remoteIP == "" {
236
+ remoteIP = req .RemoteAddr
237
+ }
238
+ log .Printf ("%s %s %s" , remoteIP , req .Method , req .URL .Path )
239
+
240
+ var ok bool
241
+ var user string
211
242
if req .URL .Path == signInPath {
212
- p .SignInPage (rw , req , 200 )
243
+ user , ok = p .ManualSignIn (rw , req )
244
+ if ok {
245
+ p .SetCookie (rw , req , user )
246
+ http .Redirect (rw , req , "/" , 302 )
247
+ } else {
248
+ p .SignInPage (rw , req , 200 )
249
+ }
213
250
return
214
251
}
215
252
if req .URL .Path == oauthStartPath {
@@ -260,20 +297,22 @@ func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
260
297
}
261
298
}
262
299
263
- cookie , err := req . Cookie ( p . CookieKey )
264
- var ok bool
265
- var email string
266
- var user string
267
- if err == nil {
268
- email , ok = validateCookie ( cookie , p . CookieSeed )
269
- user = strings . Split ( email , "@" )[ 0 ]
300
+ if ! ok {
301
+ cookie , err := req . Cookie ( p . CookieKey )
302
+ if err == nil {
303
+ var email string
304
+ email , ok = validateCookie ( cookie , p . CookieSeed )
305
+ user = strings . Split ( email , "@" )[ 0 ]
306
+ }
270
307
}
271
308
272
309
if ! ok {
273
310
user , ok = p .CheckBasicAuth (req )
274
- if ok {
275
- p .SetCookie (rw , req , user )
276
- }
311
+ // if we want to promote basic auth requests to cookie'd requests, we could do that here
312
+ // not sure that would be ideal in all circumstances though
313
+ // if ok {
314
+ // p.SetCookie(rw, req, user)
315
+ // }
277
316
}
278
317
279
318
if ! ok {
@@ -308,6 +347,7 @@ func (p *OauthProxy) CheckBasicAuth(req *http.Request) (string, bool) {
308
347
return "" , false
309
348
}
310
349
if p .HtpasswdFile .Validate (pair [0 ], pair [1 ]) {
350
+ log .Printf ("authenticated %s via basic auth" , pair [0 ])
311
351
return pair [0 ], true
312
352
}
313
353
return "" , false
0 commit comments