-
Notifications
You must be signed in to change notification settings - Fork 0
/
Authorizer.go
79 lines (72 loc) · 2 KB
/
Authorizer.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
76
77
78
79
package middleware
import (
"crypto/sha256"
"crypto/subtle"
"encoding/hex"
"github.com/cockroachdb/errors"
"github.com/dmitry-kostin/go-rest/src/pkg"
"github.com/unrolled/render"
"net/http"
"strings"
)
type Authorizer struct {
renderer *render.Render
config *pkg.Config
logger *pkg.Logger
apiKeys [][]byte
}
func NewAuthorizer(renderer *render.Render, config *pkg.Config, logger *pkg.Logger) *Authorizer {
apiKeys := make([][]byte, 0)
for _, value := range config.AppAPIKeys {
decodedKey, err := hex.DecodeString(value)
if err != nil {
panic("failed to decode API keys")
}
apiKeys = append(apiKeys, decodedKey)
}
return &Authorizer{renderer, config, logger, apiKeys}
}
func (s *Authorizer) isAuthorized(rq *http.Request) bool {
return true
}
func (s *Authorizer) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, rq *http.Request) {
apiKey, err := s.getBearerToken(rq)
if err != nil {
s.logger.Warn().Err(err).Send()
_ = s.renderer.JSON(rw, http.StatusUnauthorized, struct {
Message string `json:"message"`
}{"Permission denied"})
return
}
if !s.isTokenValid(apiKey, s.apiKeys) {
s.logger.Warn().Msgf("invalid authorization attempt with key: %s", apiKey)
_ = s.renderer.JSON(rw, http.StatusUnauthorized, struct {
Message string `json:"message"`
}{"Permission denied"})
return
}
next.ServeHTTP(rw, rq)
})
}
func (s *Authorizer) isTokenValid(rawKey string, availableKeys [][]byte) bool {
hash := sha256.Sum256([]byte(rawKey))
key := hash[:]
for _, value := range availableKeys {
contentEqual := subtle.ConstantTimeCompare(value, key) == 1
if contentEqual {
return true
}
}
return false
}
func (s *Authorizer) getBearerToken(rq *http.Request) (string, error) {
var token string
authHeader := rq.Header.Get("Authorization")
splitToken := strings.Split(authHeader, "Bearer ")
if len(splitToken) != 2 {
return token, errors.New("malformed auth token")
}
token = splitToken[1]
return token, nil
}