-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.go
More file actions
27 lines (24 loc) · 725 Bytes
/
Copy pathauth.go
File metadata and controls
27 lines (24 loc) · 725 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
package main
import (
"crypto/subtle"
"net/http"
"strings"
)
func (sc *Smithy) RequireWriteAuth(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if sc.WriteToken == "" {
http.Error(w, "write operations are disabled", http.StatusServiceUnavailable)
return
}
provided := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
if _, password, ok := r.BasicAuth(); ok {
provided = password
}
if subtle.ConstantTimeCompare([]byte(provided), []byte(sc.WriteToken)) != 1 {
w.Header().Set("WWW-Authenticate", `Basic realm="smithy"`)
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
next(w, r)
}
}