-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
81 lines (69 loc) · 2.23 KB
/
jwt.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
package api
import (
"net/http"
"strings"
"github.com/dgrijalva/jwt-go"
"github.com/labstack/echo"
)
// Claims ...
type Claims struct {
Account string `json:"account"`
Role string `json:"role"`
jwt.StandardClaims
}
// JWT ...
type JWT struct {
JWTSecret []byte
}
// AuthRequired is a middleware for account service
func (j *JWT) AuthRequired(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
auth := c.Request().Header.Get("Authorization")
bears := strings.Split(auth, "Bearer ")
if len(bears) < 2 {
return c.JSON(http.StatusUnauthorized, Response{
"error": "no bearing",
})
}
token := bears[1]
// parse and validate token for six things:
// validationErrorMalformed => token is malformed
// validationErrorUnverifiable => token could not be verified because of signing problems
// validationErrorSignatureInvalid => signature validation failed
// validationErrorExpired => exp validation failed
// validationErrorNotValidYet => nbf validation failed
// validationErrorIssuedAt => iat validation failed
tokenClaims, err := jwt.ParseWithClaims(token, &Claims{}, func(token *jwt.Token) (i interface{}, err error) {
return []byte(j.JWTSecret), nil
})
if err != nil {
var message string
if ve, ok := err.(*jwt.ValidationError); ok {
if ve.Errors&jwt.ValidationErrorMalformed != 0 {
message = "token is malformed"
} else if ve.Errors&jwt.ValidationErrorUnverifiable != 0 {
message = "token could not be verified because of signing problems"
} else if ve.Errors&jwt.ValidationErrorSignatureInvalid != 0 {
message = "signature validation failed"
} else if ve.Errors&jwt.ValidationErrorExpired != 0 {
message = "token is expired"
} else if ve.Errors&jwt.ValidationErrorNotValidYet != 0 {
message = "token is not yet valid before sometime"
} else {
message = "can not handle this token"
}
}
return c.JSON(http.StatusUnauthorized, Response{
"error": message,
})
}
if claims, ok := tokenClaims.Claims.(*Claims); ok && tokenClaims.Valid {
c.Set("userID", claims.Subject)
c.Set("role", claims.Role)
return next(c)
}
return c.JSON(http.StatusUnauthorized, Response{
"error": "invalid token",
})
}
}