-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.go
More file actions
33 lines (27 loc) · 823 Bytes
/
auth.go
File metadata and controls
33 lines (27 loc) · 823 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main
import (
"net/http"
)
func withBasicAuth(next interface{}) http.Handler {
switch h := next.(type) {
case http.Handler:
return basicAuthHandler(h)
case func(http.ResponseWriter, *http.Request):
return basicAuthHandler(http.HandlerFunc(h))
default:
panic("[Auth] unsupported handler type")
}
}
func basicAuthHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth()
requiredUsername := config.server.authUser
requiredPassword := config.server.authPW
if !ok || username != requiredUsername || password != requiredPassword {
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r)
})
}