-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolib.go
35 lines (28 loc) · 808 Bytes
/
golib.go
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
34
35
package golib
import (
// "encoding/json"
// "fmt"
// "github.com/gorilla/mux"
// "io"
// "log"
"net/http"
// "os"
// "strings"
// "strconv"
"crypto/subtle"
// "io/ioutil"
// "regexp"
// "github.com/davecgh/go-spew/spew"
)
func BasicAuth(handler http.HandlerFunc, username, password, realm string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth()
if !ok || subtle.ConstantTimeCompare([]byte(user), []byte(username)) != 1 || subtle.ConstantTimeCompare([]byte(pass), []byte(password)) != 1 {
w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
w.WriteHeader(401)
w.Write([]byte("Unauthorised.\n"))
return
}
handler(w, r)
}
}