-
Notifications
You must be signed in to change notification settings - Fork 0
/
Middleware_AUTH.go
78 lines (65 loc) · 1.98 KB
/
Middleware_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
package common
import (
"fmt"
"net/http"
"time"
"github.com/dgrijalva/jwt-go/v4"
)
func CreateJWTToken(secret string, expirationDelta time.Duration) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodRS512, jwt.MapClaims{
"exp": time.Now().Add(time.Hour * expirationDelta).Unix(),
"iat": time.Now().Unix(),
})
signedToken, err := token.SignedString([]byte(secret))
if err != nil {
return "", err
}
return signedToken, nil
}
func AUTH(next http.Handler, secret string) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
if req.Method == "OPTIONS" {
next.ServeHTTP(rw, req)
return
}
// Token will be in either header or as a query param
var tokenString string
authHeader := req.Header.Get("x-authorization")
if authHeader == "" {
query := req.URL.Query()
if len(query["x-authorization"]) > 0 {
tokenString = query["x-authorization"][0]
}
} else {
tokenString = authHeader[7:]
}
if len(tokenString) <= 0 {
BadRequestResponse(fmt.Errorf("missing token in x-authorization header")).ServeHTTP(rw, req)
return
}
// parse token
token, err := jwt.ParseWithClaims(tokenString, &jwt.MapClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(secret), nil
})
if err != nil || !token.Valid {
UnauthorisedResponse(fmt.Errorf("expected a valid token in x-authorization header")).ServeHTTP(rw, req)
return
}
// parse permissions from token claim
claims, ok := token.Claims.(*jwt.MapClaims)
if !ok {
UnauthorisedResponse(fmt.Errorf("expected a valid token in x-authorization header")).ServeHTTP(rw, req)
return
}
permissions := parseGroupRoles((*claims)["permissions"].(string))
if permissions == nil {
UnauthorisedResponse(fmt.Errorf("expected a valid token in x-authorization header")).ServeHTTP(rw, req)
return
}
// add permissions to context
SetPermissions(req, permissions)
// continue with middleware chain
next.ServeHTTP(rw, req)
return
})
}