Conflicts: internal/push/sdk/tpns-server-sdk-go/go/auth/auth.go pkg/proto/group/group.prototest-errcode
commit
6eb231aae4
@ -0,0 +1,62 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/hmac"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/base64"
|
||||||
|
b64 "encoding/base64"
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Auther struct {
|
||||||
|
AccessID string
|
||||||
|
SecretKey string
|
||||||
|
}
|
||||||
|
|
||||||
|
var UseSignAuthored = true
|
||||||
|
|
||||||
|
func (a *Auther) Auth(req *http.Request, useSignAuthored bool, auth Auther, reqBody string) {
|
||||||
|
|
||||||
|
if useSignAuthored {
|
||||||
|
now := time.Now()
|
||||||
|
timeStamp := now.Unix()
|
||||||
|
req.Header.Add("AccessId", auth.AccessID)
|
||||||
|
req.Header.Add("TimeStamp", strconv.Itoa(int(timeStamp)))
|
||||||
|
sign := GenSign(uint64(timeStamp), auth.AccessID, auth.SecretKey, reqBody)
|
||||||
|
req.Header.Add("Sign", sign)
|
||||||
|
} else {
|
||||||
|
author := makeAuthHeader(a.AccessID, a.SecretKey)
|
||||||
|
//log.Printf("author string:%v", author)
|
||||||
|
req.Header.Add("Authorization", author)
|
||||||
|
}
|
||||||
|
//req.Header.Add("Content-Type", "application/json")
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeAuthHeader(appID, secretKey string) string {
|
||||||
|
base64Str := base64.StdEncoding.EncodeToString(
|
||||||
|
[]byte(
|
||||||
|
fmt.Sprintf("%s:%s", appID, secretKey),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
return fmt.Sprintf("Basic %s", base64Str)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenSign(timeStamp uint64, accessId string, secretKey, requestBody string) string {
|
||||||
|
signBody := strconv.Itoa(int(timeStamp)) + accessId + requestBody
|
||||||
|
// Create a new HMAC by defining the hash type and the key (as byte array)
|
||||||
|
h := hmac.New(sha256.New, []byte(secretKey))
|
||||||
|
// Write Map to it
|
||||||
|
h.Write([]byte(signBody))
|
||||||
|
|
||||||
|
// Get result and encode as hexadecimal string
|
||||||
|
sha := hex.EncodeToString(h.Sum(nil))
|
||||||
|
//fmt.Println()
|
||||||
|
//fmt.Println("timeStamp: " + strconv.Itoa(int(timeStamp)) + " accessID:" + accessId + " body:" + requestBody)
|
||||||
|
sEnc := b64.StdEncoding.EncodeToString([]byte(sha))
|
||||||
|
//fmt.Println("final Result " + sEnc)
|
||||||
|
return sEnc
|
||||||
|
}
|
@ -0,0 +1,75 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Open_IM/pkg/common/constant"
|
||||||
|
"Open_IM/pkg/common/tokenverify"
|
||||||
|
"Open_IM/pkg/utils"
|
||||||
|
"context"
|
||||||
|
go_redis "github.com/go-redis/redis/v8"
|
||||||
|
"github.com/golang-jwt/jwt/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
uidPidToken = "UID_PID_TOKEN_STATUS:"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Token interface {
|
||||||
|
//结果为空 不返回错误
|
||||||
|
GetTokensWithoutError(ctx context.Context, userID, platform string) (map[string]int, error)
|
||||||
|
//创建token
|
||||||
|
CreateToken(ctx context.Context, userID string, platformID int) (string, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type TokenRedis struct {
|
||||||
|
RedisClient *RedisClient
|
||||||
|
AccessSecret string
|
||||||
|
AccessExpire int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTokenRedis(redisClient *RedisClient, accessSecret string, accessExpire int64) *TokenRedis {
|
||||||
|
return &TokenRedis{redisClient, accessSecret, accessExpire}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 结果为空 不返回错误
|
||||||
|
func (t *TokenRedis) GetTokensWithoutError(ctx context.Context, userID, platform string) (map[string]int, error) {
|
||||||
|
key := uidPidToken + userID + ":" + platform
|
||||||
|
m, err := t.RedisClient.GetClient().HGetAll(context.Background(), key).Result()
|
||||||
|
if err != nil && err == go_redis.Nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
mm := make(map[string]int)
|
||||||
|
for k, v := range m {
|
||||||
|
mm[k] = utils.StringToInt(v)
|
||||||
|
}
|
||||||
|
return mm, utils.Wrap(err, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建token
|
||||||
|
func (t *TokenRedis) CreateToken(ctx context.Context, userID string, platform string) (string, error) {
|
||||||
|
tokens, err := t.GetTokensWithoutError(ctx, userID, platform)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
var deleteTokenKey []string
|
||||||
|
for k, v := range tokens {
|
||||||
|
_, err = tokenverify.GetClaimFromToken(k)
|
||||||
|
if err != nil || v != constant.NormalToken {
|
||||||
|
deleteTokenKey = append(deleteTokenKey, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(deleteTokenKey) != 0 {
|
||||||
|
key := uidPidToken + userID + ":" + platform
|
||||||
|
err := t.RedisClient.GetClient().HDel(context.Background(), key, deleteTokenKey...).Err()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
claims := tokenverify.BuildClaims(userID, platform, t.AccessExpire)
|
||||||
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||||
|
tokenString, err := token.SignedString([]byte(t.AccessSecret))
|
||||||
|
if err != nil {
|
||||||
|
return "", utils.Wrap(err, "")
|
||||||
|
}
|
||||||
|
key := uidPidToken + userID + ":" + platform
|
||||||
|
return "", utils.Wrap(t.RedisClient.GetClient().HSet(context.Background(), key, tokenString, constant.NormalToken).Err(), "")
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Open_IM/pkg/utils"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/dtm-labs/rockscache"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetCache[T any](ctx context.Context, rcClient *rockscache.Client, key string, expire time.Duration, fn func(ctx context.Context) (T, error)) (T, error) {
|
||||||
|
var t T
|
||||||
|
var write bool
|
||||||
|
v, err := rcClient.Fetch(key, expire, func() (s string, err error) {
|
||||||
|
t, err = fn(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
bs, err := json.Marshal(t)
|
||||||
|
if err != nil {
|
||||||
|
return "", utils.Wrap(err, "")
|
||||||
|
}
|
||||||
|
write = true
|
||||||
|
return string(bs), nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return t, err
|
||||||
|
}
|
||||||
|
if write {
|
||||||
|
return t, nil
|
||||||
|
}
|
||||||
|
err = json.Unmarshal([]byte(v), &t)
|
||||||
|
if err != nil {
|
||||||
|
return t, utils.Wrap(err, "")
|
||||||
|
}
|
||||||
|
return t, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetCacheFor[E any, T any](ctx context.Context, rcClient *rockscache.Client, list []E, fn func(ctx context.Context, item E) (T, error)) ([]T, error) {
|
||||||
|
rs := make([]T, 0, len(list))
|
||||||
|
for _, e := range list {
|
||||||
|
r, err := fn(ctx, e)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
rs = append(rs, r)
|
||||||
|
}
|
||||||
|
return rs, nil
|
||||||
|
}
|
@ -1,9 +1,34 @@
|
|||||||
package controller
|
package controller
|
||||||
|
|
||||||
import "context"
|
import (
|
||||||
|
"Open_IM/pkg/common/db/cache"
|
||||||
|
"context"
|
||||||
|
"github.com/go-redis/redis/v8"
|
||||||
|
)
|
||||||
|
|
||||||
type AuthInterface interface {
|
type AuthInterface interface {
|
||||||
GetTokens(ctx context.Context, userID, platform string) (map[string]int, error)
|
//结果为空 不返回错误
|
||||||
DeleteToken(ctx context.Context, userID, platform string) error
|
GetTokensWithoutError(ctx context.Context, userID, platform string) (map[string]int, error)
|
||||||
CreateToken(ctx context.Context, userID string, platformID int, ttl int64) (string, error)
|
|
||||||
|
//创建token
|
||||||
|
CreateToken(ctx context.Context, userID string, platform string) (string, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type AuthController struct {
|
||||||
|
database *cache.TokenRedis
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAuthController(rdb redis.UniversalClient, accessSecret string, accessExpire int64) *AuthController {
|
||||||
|
cache.NewRedisClient(rdb)
|
||||||
|
return &AuthController{database: cache.NewTokenRedis(cache.NewRedisClient(rdb), accessSecret, accessExpire)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 结果为空 不返回错误
|
||||||
|
func (a *AuthController) GetTokensWithoutError(ctx context.Context, userID, platform string) (map[string]int, error) {
|
||||||
|
return a.database.GetTokensWithoutError(ctx, userID, platform)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建token
|
||||||
|
func (a *AuthController) CreateToken(ctx context.Context, userID string, platform string) (string, error) {
|
||||||
|
return a.database.CreateToken(ctx, userID, platform)
|
||||||
}
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue