35 lines
623 B
Go
35 lines
623 B
Go
|
|
package auth
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
"github.com/redis/go-redis/v9"
|
||
|
|
"platform/pkg/rds"
|
||
|
|
)
|
||
|
|
|
||
|
|
func find(ctx context.Context, token string) (*Context, error) {
|
||
|
|
|
||
|
|
// 读取认证数据
|
||
|
|
authJSON, err := rds.Client.Get(ctx, accessKey(token)).Result()
|
||
|
|
if err != nil {
|
||
|
|
if errors.Is(err, redis.Nil) {
|
||
|
|
return nil, errors.New("invalid_token")
|
||
|
|
}
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// 反序列化
|
||
|
|
auth := new(Context)
|
||
|
|
if err := json.Unmarshal([]byte(authJSON), auth); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
return auth, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func accessKey(token string) string {
|
||
|
|
return fmt.Sprintf("session:%s", token)
|
||
|
|
}
|