-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
46 lines (40 loc) · 1.1 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
package jwt
import (
"context"
"errors"
"github.com/go-kratos/kratos/v2/middleware/selector"
jwtV4 "github.com/golang-jwt/jwt/v4"
"github.com/iwinder/qyblog/internal/pkg/qycms_common/auth"
"strings"
)
type SecurityUser struct {
auth.ASecurityUser
ID uint64
NickName string
AuthorityName string
RoleIds string
jwtV4.RegisteredClaims
}
// CreateToken generate token
func CreateToken(c SecurityUser, key string) (string, error) {
claims := jwtV4.NewWithClaims(jwtV4.SigningMethodHS256, c)
signedString, err := claims.SignedString([]byte(key))
if err != nil {
return "", errors.New("generate token failed" + err.Error())
}
return signedString, nil
}
func NewWhiteListMatcher() selector.MatchFunc {
whiteList := make(map[string]bool)
whiteList["/api.qycms_bff.admin.v1.QyAdminLogin/Login"] = true
whiteList["/api.qycms_bff.admin.v1.QyAdminUser/CreateUser"] = true
return func(ctx context.Context, operation string) bool {
if _, ok := whiteList[operation]; ok {
return false
}
if strings.Contains(operation, "/api.qycms_bff.web.") {
return false
}
return true
}
}