33 lines
997 B
Go
33 lines
997 B
Go
package cache
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// SaveRefresh 登记刷新令牌(jti 白名单,用于轮换与登出吊销)
|
|
func (r *Redis) SaveRefresh(uid uint64, jti string, ttl time.Duration) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
key := fmt.Sprintf("refresh:%d:%s", uid, jti)
|
|
return r.client.Set(ctx, key, "1", ttl).Err()
|
|
}
|
|
|
|
// ExistsRefresh 校验刷新令牌是否有效
|
|
func (r *Redis) ExistsRefresh(uid uint64, jti string) (bool, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
key := fmt.Sprintf("refresh:%d:%s", uid, jti)
|
|
n, err := r.client.Exists(ctx, key).Result()
|
|
return n == 1, err
|
|
}
|
|
|
|
// DeleteRefresh 吊销刷新令牌
|
|
func (r *Redis) DeleteRefresh(uid uint64, jti string) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
key := fmt.Sprintf("refresh:%d:%s", uid, jti)
|
|
return r.client.Del(ctx, key).Err()
|
|
}
|