feat usn sid 改成string
This commit is contained in:
14
utils/jwt.go
14
utils/jwt.go
@@ -5,16 +5,15 @@ import (
|
||||
"errors"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Claims struct {
|
||||
USN int64 `json:"usn"`
|
||||
USN string `json:"usn"`
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
func GenToken(usn int64, secret string, expires time.Duration) (string, error) {
|
||||
func GenToken(usn string, secret string, expires time.Duration) (string, error) {
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, Claims{
|
||||
USN: usn,
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
@@ -41,16 +40,13 @@ func ParseToken(tokenString string, secret string) (*Claims, error) {
|
||||
return claims, nil
|
||||
}
|
||||
|
||||
func ShouldBindUsn(ctx context.Context, usn *int64) bool {
|
||||
func ShouldBindUsn(ctx context.Context, usn *string) bool {
|
||||
if md, ok := metadata.FromIncomingContext(ctx); ok {
|
||||
usnArr := md.Get("X-Usn")
|
||||
if len(usnArr) == 0 || usnArr[0] == "" {
|
||||
return false
|
||||
}
|
||||
s, _ := strconv.Atoi(usnArr[0])
|
||||
if s > 0 {
|
||||
*usn = int64(s)
|
||||
}
|
||||
*usn = usnArr[0]
|
||||
}
|
||||
return *usn > 0
|
||||
return *usn != ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user