-
Notifications
You must be signed in to change notification settings - Fork 159
/
auth.go
75 lines (65 loc) · 1.96 KB
/
auth.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
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
67
68
69
70
71
72
73
74
75
// Copyright (C) 2017 ScyllaDB
package auth
import (
"crypto/subtle"
"encoding/json"
"net/http"
"strings"
"time"
"github.com/scylladb/scylla-operator/pkg/util/httpx"
)
// AddToken sets authorization header. If token is empty it immediately returns
// the next handler.
func AddToken(next http.RoundTripper, token string) http.RoundTripper {
if token == "" {
return next
}
return httpx.RoundTripperFunc(func(req *http.Request) (resp *http.Response, err error) {
r := httpx.CloneRequest(req)
r.Header.Set("Authorization", "Bearer "+token)
return next.RoundTrip(r)
})
}
// ValidateToken is http server middleware that checks if Authorization header
// contains `Bearer token`.
// If not the execution would be held for the penalty duration and then 401
// status code with provided body would be returned.
// If token is empty it immediately returns the next handler.
func ValidateToken(token string, penalty time.Duration,
unauthorizedBody json.RawMessage) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
if token == "" {
return next
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !secureCompare(bearerAuth(r), token) {
if penalty > 0 {
time.Sleep(penalty)
}
w.WriteHeader(http.StatusUnauthorized)
w.Write(unauthorizedBody) // nolint:errcheck
} else {
next.ServeHTTP(w, r)
}
})
}
}
// bearerAuth returns the token provided in the request's Authorization header.
func bearerAuth(r *http.Request) (token string) {
auth := r.Header.Get("Authorization")
if auth == "" {
return
}
return parseBearerAuth(auth)
}
func parseBearerAuth(auth string) (token string) {
const prefix = "Bearer "
// Case insensitive prefix match. See Issue 22736.
if len(auth) < len(prefix) || !strings.EqualFold(auth[:len(prefix)], prefix) {
return ""
}
return auth[len(prefix):]
}
func secureCompare(x, y string) bool {
return subtle.ConstantTimeCompare([]byte(x), []byte(y)) == 1
}