-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt.go
55 lines (43 loc) · 1.26 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
package mon
import (
"errors"
"time"
"github.com/dgrijalva/jwt-go"
)
type IJwt interface {
// Encrypt returns encrypted json web token
Encrypt(data jwt.MapClaims) (string, error)
// Decrypt returns map-claim after successfully parses and validate json web token
// based on secret and signing method
Decrypt(token string) (jwt.MapClaims, error)
}
type Jwt struct {
Secret string
SigningMethod string
}
func (j *Jwt) Encrypt(data jwt.MapClaims) (string, error) {
data["exp"] = time.Now().Add(time.Hour * 24).Unix()
secret := []byte(j.Secret)
return jwt.NewWithClaims(jwt.GetSigningMethod(j.SigningMethod), data).SignedString(secret)
}
func (j *Jwt) Decrypt(tokenStr string) (jwt.MapClaims, error) {
token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) {
if token.Method != jwt.GetSigningMethod(j.SigningMethod) {
return nil, errors.New("invalid signing method")
}
return []byte(j.Secret), nil
})
if err != nil {
return nil, err
}
if _, ok := token.Claims.(jwt.MapClaims); !ok && !token.Valid {
return nil, errors.New("invalid token")
}
return token.Claims.(jwt.MapClaims), nil
}
func NewJwt(secret string, signingMethod string) IJwt {
return &Jwt{
Secret: secret,
SigningMethod: signingMethod,
}
}