-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
51 lines (45 loc) · 1.21 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
package utils
import (
"errors"
"github.com/astaxie/beego"
"github.com/dgrijalva/jwt-go"
"time"
)
type WhaleClaims struct {
Uid int `json: "uid"`
Uname string `json: "uname"`
jwt.StandardClaims
}
func ParaseToken(authorization string) (*WhaleClaims, bool, error) {
token, _ := jwt.ParseWithClaims(authorization, &WhaleClaims{}, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, nil
}
return []byte(beego.AppConfig.String("authKey")), nil
})
if claims, ok := token.Claims.(*WhaleClaims); ok && token.Valid {
//fmt.Println("claims:", claims)
return claims, true, nil
}
return nil, false, errors.New("token invalid")
}
func GenToken(uid int, uname string) (string, error) {
expireToken := time.Now().Add(time.Hour * 24).Unix()
claims := WhaleClaims{
uid,
uname,
jwt.StandardClaims{
ExpiresAt: expireToken,
Issuer: "www.whale4cloud.com",
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString([]byte(beego.AppConfig.String("authKey")))
}
func GenerateRefreshJwtWithToken(token string) (string, error) {
return "", nil
}
//获取Id
func GetId() int {
return int(time.Now().Unix())
}