-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_security.go
More file actions
66 lines (57 loc) · 1.54 KB
/
Copy pathapi_security.go
File metadata and controls
66 lines (57 loc) · 1.54 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"crypto/rand"
"crypto/subtle"
"encoding/hex"
"mime"
"net/http"
"strings"
)
const maxAPIRequestBody = 1 << 20
func newAPIToken() (string, error) {
buf := make([]byte, 32)
if _, err := rand.Read(buf); err != nil {
return "", err
}
return hex.EncodeToString(buf), nil
}
func secureAPI(token string, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.HasPrefix(r.URL.Path, "/api/") {
next.ServeHTTP(w, r)
return
}
if !validBearerToken(r.Header.Get("Authorization"), token) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte("unauthorized\n"))
return
}
if requestHasBody(r) {
mediaType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil || mediaType != "application/json" {
http.Error(w, "content type must be application/json", http.StatusUnsupportedMediaType)
return
}
r.Body = http.MaxBytesReader(w, r.Body, maxAPIRequestBody)
}
next.ServeHTTP(w, r)
})
}
func validBearerToken(header, token string) bool {
const prefix = "Bearer "
if token == "" || !strings.HasPrefix(header, prefix) {
return false
}
provided := strings.TrimPrefix(header, prefix)
if len(provided) != len(token) {
return false
}
return subtle.ConstantTimeCompare([]byte(provided), []byte(token)) == 1
}
func requestHasBody(r *http.Request) bool {
if r.Body == nil || r.Body == http.NoBody {
return false
}
return r.ContentLength != 0
}