-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
74 lines (64 loc) · 1.45 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
package auth
import (
"fmt"
"github.com/dgrijalva/jwt-go"
"github.com/madlabx/pkgx/errors"
"github.com/madlabx/pkgx/log"
"github.com/madlabx/pkgx/typex"
)
type JwtConf struct {
Secret []byte
Method jwt.SigningMethod
}
type JwtToken struct {
Method jwt.SigningMethod
Header typex.JsonMap
Claims typex.JsonMap
Signature string
}
type JwtAuth struct {
Conf JwtConf
}
func NewJwtAuth(secret []byte) *JwtAuth {
if len(secret) == 0 {
log.Fatal("The JWT secret must not be empty")
}
return &JwtAuth{
Conf: JwtConf{
Secret: secret,
Method: jwt.SigningMethodHS256,
},
}
}
func (a *JwtAuth) Verify(tokenString string) (*JwtToken, error) {
token, err := jwt.Parse(tokenString, func(*jwt.Token) (interface{}, error) {
return a.Conf.Secret, nil
})
if err != nil {
return nil, err
}
if !token.Valid {
return nil, errors.New("JWT token is not valid")
}
c, ok := token.Claims.(jwt.MapClaims)
if !ok {
return nil, errors.New(fmt.Sprintf("Invalid JWT Claims type: %T", token.Claims))
}
jt := &JwtToken{
Method: token.Method,
Header: typex.JsonMap(token.Header),
Claims: typex.JsonMap(c),
Signature: token.Signature,
}
return jt, nil
}
func (a *JwtAuth) GenToken(claims typex.JsonMap) (string, error) {
var token *jwt.Token
if len(claims) > 0 {
token = jwt.NewWithClaims(a.Conf.Method, jwt.MapClaims(claims))
} else {
token = jwt.New(a.Conf.Method)
}
ts, err := token.SignedString(a.Conf.Secret)
return ts, err
}