some tweaks
This commit is contained in:
parent
b25e0b532f
commit
528356000f
2
README
2
README
@ -1,7 +1,7 @@
|
|||||||
= next
|
= next
|
||||||
get web presence in good enough shape to point to from blog post
|
get web presence in good enough shape to point to from blog post
|
||||||
gbe-web
|
gbe-web
|
||||||
staging site, behind basic auth
|
staging basic auth
|
||||||
basic design / brand
|
basic design / brand
|
||||||
favicon
|
favicon
|
||||||
google analytics
|
google analytics
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
type Authenticator func(string, string) bool
|
type Authenticator func(string, string) bool
|
||||||
|
|
||||||
func CheckAuth(r *http.Request, auth Authenticator) bool {
|
func testAuth(r *http.Request, auth Authenticator) bool {
|
||||||
s := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
|
s := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
|
||||||
if len(s) != 2 || s[0] != "Basic" {
|
if len(s) != 2 || s[0] != "Basic" {
|
||||||
return false
|
return false
|
||||||
@ -25,30 +25,31 @@ func CheckAuth(r *http.Request, auth Authenticator) bool {
|
|||||||
return auth(pair[0], pair[1])
|
return auth(pair[0], pair[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
func RequireAuth(w http.ResponseWriter, r *http.Request) {
|
func requireAuth(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("WWW-Authenticate", `Basic realm="private"`)
|
w.Header().Set("WWW-Authenticate", `Basic realm="private"`)
|
||||||
w.WriteHeader(401)
|
w.WriteHeader(401)
|
||||||
w.Write([]byte("401 Unauthorized\n"))
|
w.Write([]byte("401 Unauthorized\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithAuth(h http.HandlerFunc, a Authenticator) http.HandlerFunc {
|
func wrapAuth(h http.HandlerFunc, a Authenticator) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if CheckAuth(r, a) {
|
if testAuth(r, a) {
|
||||||
h(w, r)
|
h(w, r)
|
||||||
} else {
|
} else {
|
||||||
RequireAuth(w, r)
|
requireAuth(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handle(w http.ResponseWriter, r *http.Request) {
|
func hello(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprintln(w, "Hello secret world!")
|
fmt.Fprintln(w, "Hello secret world!")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
authenticator := func(_, password string) bool {
|
checkPassword := func(_, password string) bool {
|
||||||
return password == "supersecret"
|
return password == "supersecret"
|
||||||
}
|
}
|
||||||
http.HandleFunc("/", WithAuth(handle, authenticator))
|
handler1 := http.HanderFunc(hello)
|
||||||
http.ListenAndServe(":5000", nil)
|
handler2 := wrapAuth(handler1, checkPassword)
|
||||||
|
http.ListenAndServe(":5000", handler2)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user