-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
auth.go
89 lines (72 loc) · 2.05 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"errors"
"fmt"
"net/http"
"strings"
"github.com/dgrijalva/jwt-go"
)
func use(h http.HandlerFunc, middleware ...func(http.HandlerFunc) http.HandlerFunc) http.HandlerFunc {
for _, m := range middleware {
h = m(h)
}
return h
}
func auth(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Basic authentication
if exporterConfig.BasicAuth.Active == true {
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
username, password, authOK := r.BasicAuth()
if authOK == false {
http.Error(w, "Not authorized", 401)
return
}
if username != exporterConfig.BasicAuth.Username || password != exporterConfig.BasicAuth.Password {
http.Error(w, "Not authorized", 401)
return
}
}
// Authentication using bearer token
if exporterConfig.BearerAuth.Active == true {
authHeader := r.Header.Get("Authorization")
if authHeader == "" {
http.Error(w, "Not authorized", 401)
return
}
authHeaderParts := strings.Split(authHeader, " ")
if len(authHeaderParts) != 2 || strings.ToLower(authHeaderParts[0]) != "bearer" {
http.Error(w, "Not authorized", 401)
return
}
err := checkJWT(authHeaderParts[1])
if err != nil {
http.Error(w, "Not authorized", 401)
return
}
}
h.ServeHTTP(w, r)
}
}
// checkJWT validates jwt tokens
func checkJWT(jwtToken string) error {
token, err := jwt.Parse(jwtToken, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return []byte(exporterConfig.BearerAuth.SigningKey), nil
})
if err != nil {
return err
}
if _, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
return nil
}
return errors.New("Not authorized")
}
// createJWT creates jwt tokens
func createJWT() (string, error) {
token := jwt.New(jwt.SigningMethodHS256)
tokenString, err := token.SignedString([]byte(exporterConfig.BearerAuth.SigningKey))
return tokenString, err
}