go-wxhelper/pkg/auth/handle/token_gen.go
2024-01-19 12:06:30 +08:00

79 lines
2.2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package handle
import (
"context"
"encoding/json"
"fmt"
"github.com/go-oauth2/oauth2/v4"
"github.com/google/uuid"
"strings"
"wechat-robot/internal/redis"
)
func NewAccessGenerate() *AccessGenerate {
return &AccessGenerate{}
}
type AccessGenerate struct {
}
// Token
// @description: 手动实现Token生成直接生成UUID替换掉自带的那个憨得一批的长长的字符串
// @receiver ag
// @param ctx context.Context 上下文
// @param data *oauth2.GenerateBasic 生成基础数据
// @param isGenRefresh bool 是否生成RefreshToken
// @return string AccessToken 令牌
// @return string RefreshToken 刷新令牌
// @return error 错误信息
func (ag *AccessGenerate) Token(ctx context.Context, data *oauth2.GenerateBasic, isGenRefresh bool) (string, string, error) {
u, _ := uuid.NewUUID()
access := strings.ReplaceAll(u.String(), "-", "")
refresh := ""
if isGenRefresh {
u, _ = uuid.NewUUID()
refresh = strings.ReplaceAll(u.String(), "-", "")
}
// 生成新的,清理掉旧的
ag.clearOldToken(ctx, data.UserID)
// 返回结果
return access, refresh, nil
}
// clearOldToken
// @description: 清理掉旧的Token和RefreshToken
// @receiver ag
// @param ctx context.Context 上下文
// @param userId string 用户ID
func (ag *AccessGenerate) clearOldToken(ctx context.Context, userId string) {
key := fmt.Sprintf("oauth:token:token:%s", userId)
accessToken, err := redis.Client.Get(context.Background(), key).Result()
if err != nil {
//log.Errorf("获取缓存用户的accessToken失败: %v", err.Error())
return
}
if accessToken != "" {
// 老的Token
var baseKey string
baseKey, err = redis.Client.Get(ctx, "oauth:token:"+accessToken).Result()
if err != nil {
return
}
// 老Token详细数据
var dataStr string
dataStr, err = redis.Client.Get(ctx, "oauth:token:"+baseKey).Result()
if err != nil {
return
}
var m map[string]interface{}
if err = json.Unmarshal([]byte(dataStr), &m); err != nil {
return
}
// 删除AccessToken等信息
redis.Client.Del(ctx, fmt.Sprintf("oauth:token:%v", m["Access"]))
redis.Client.Del(ctx, fmt.Sprintf("oauth:token:%v", m["Refresh"]))
redis.Client.Del(ctx, "oauth:token:"+baseKey)
}
}