forked from printfcoder/stack-rpc-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_token.go
90 lines (73 loc) · 2.25 KB
/
access_token.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
82
83
84
85
86
87
88
89
90
package access
import (
"fmt"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/micro-in-cn/tutorials/microservice-in-micro/part2/basic/config"
"github.com/micro/go-micro/broker"
"github.com/micro/go-micro/util/log"
)
var (
// tokenExpiredDate app token过期日期 30天
tokenExpiredDate = 3600 * 24 * 30 * time.Second
// tokenIDKeyPrefix tokenID 前缀
tokenIDKeyPrefix = "token:auth:id:"
tokenExpiredTopic = "mu.micro.book.topic.auth.tokenExpired"
)
// Subject token 持有者
type Subject struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
}
// MakeAccessToken 生成token并保存到redis
func (s *service) MakeAccessToken(subject *Subject) (ret string, err error) {
m, err := s.createTokenClaims(subject)
if err != nil {
return "", fmt.Errorf("[MakeAccessToken] 创建token Claim 失败,err: %s", err)
}
// 创建
token := jwt.NewWithClaims(jwt.SigningMethodHS256, m)
ret, err = token.SignedString([]byte(config.GetJwtConfig().GetSecretKey()))
if err != nil {
return "", fmt.Errorf("[MakeAccessToken] 创建token失败,err: %s", err)
}
// 保存到redis
err = s.saveTokenToCache(subject, ret)
if err != nil {
return "", fmt.Errorf("[MakeAccessToken] 保存token到缓存失败,err: %s", err)
}
return
}
// GetCachedAccessToken 获取token
func (s *service) GetCachedAccessToken(subject *Subject) (ret string, err error) {
ret, err = s.getTokenFromCache(subject)
if err != nil {
return "", fmt.Errorf("[GetCachedAccessToken] 从缓存获取token失败,err: %s", err)
}
return
}
// DelUserAccessToken 清除用户token
func (s *service) DelUserAccessToken(tk string) (err error) {
// 解析token字符串
claims, err := s.parseToken(tk)
if err != nil {
return fmt.Errorf("[DelUserAccessToken] 错误的token,err: %s", err)
}
// 通过解析到的用户id删除
err = s.delTokenFromCache(&Subject{
ID: claims.Subject,
})
if err != nil {
return fmt.Errorf("[DelUserAccessToken] 清除用户token,err: %s", err)
}
// 广播删除
msg := &broker.Message{
Body: []byte(claims.Subject),
}
if err := broker.Publish(tokenExpiredTopic, msg); err != nil {
log.Logf("[pub] 发布token删除消息失败: %v", err)
} else {
fmt.Println("[pub] 发布token删除消息:", string(msg.Body))
}
return
}