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 middleware
import (
"crypto/sha256"
"crypto/subtle"
"net/http"
"nonsense-time/db"
)
func BasicAuth(next http.Handler, authProvider db.AuthProvider) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth()
if !ok || !authProvider.UserExists(username) {
w.Header().Set("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
salt := authProvider.Salt(username)
input := []byte(password)
input = append(input, salt[:]...)
passSaltHash := sha256.Sum256(input)
expectedSaltHash := authProvider.SaltedHash(username)
if subtle.ConstantTimeCompare(expectedSaltHash[:], passSaltHash[:]) != 1 {
w.Header().Set("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r)
})
}
|