-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.go
42 lines (36 loc) · 831 Bytes
/
entity.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
package jwtutils
import (
"encoding/base64"
"fmt"
"github.com/dgrijalva/jwt-go"
)
type JWT struct {
secretKey []byte
}
func New(secretKey string) (*JWT, error) {
dec, err := base64.URLEncoding.DecodeString(secretKey)
if err != nil {
return nil, err
}
return &JWT{
secretKey: dec,
}, nil
}
func (j *JWT) SecretKey() string {
return base64.URLEncoding.EncodeToString(j.secretKey)
}
func (j *JWT) CreateToken(claims jwt.Claims) (string, error) {
return createTokenStringHS512(j.secretKey, claims)
}
func (j JWT) ParseToken(token string, claims jwt.Claims) error {
tokenEntity, err := jwt.ParseWithClaims(token, claims, func(token *jwt.Token) (interface{}, error) {
return j.secretKey, nil
})
if err != nil {
return err
}
if !tokenEntity.Valid {
return fmt.Errorf("token valid failed")
}
return nil
}