Browse Source

v0.0.1开发:auth认证服务开发

#Suyghur 2 năm trước cách đây
mục cha
commit
7acd70f967

+ 11 - 1
apis/auth/etc/auth.yaml

@@ -1,6 +1,16 @@
 Name: auth.rpc
 ListenOn: 0.0.0.0:11000
+
 Etcd:
   Hosts:
-  - 127.0.0.1:2379
+    - 127.0.0.1:2379
   Key: auth.rpc
+
+JwtAuth:
+  AccessSecret: ylink2022
+  AccessExpire: 259200
+
+Redis:
+  Host: 127.0.0.1:6379
+  Type: node
+  Pass: ylink2020

+ 4 - 0
apis/auth/internal/config/config.go

@@ -4,4 +4,8 @@ import "github.com/zeromicro/go-zero/zrpc"
 
 type Config struct {
 	zrpc.RpcServerConf
+	JwtAuth struct {
+		AccessSecret string
+		AccessExpire int64
+	}
 }

+ 21 - 2
apis/auth/internal/logic/checkauthlogic.go

@@ -2,6 +2,10 @@ package logic
 
 import (
 	"context"
+	"errors"
+	"fmt"
+	"google.golang.org/protobuf/types/known/structpb"
+	"ylink/ext/globalkey"
 
 	"ylink/apis/auth/internal/svc"
 	"ylink/apis/auth/pb"
@@ -24,7 +28,22 @@ func NewCheckAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckAu
 }
 
 func (l *CheckAuthLogic) CheckAuth(in *pb.CheckAuthReq) (*pb.AuthResp, error) {
-	// todo: add your logic here and delete this line
+	tokenKey := fmt.Sprintf(globalkey.CacheTokenKey, in.Uid)
+	cacheToken, err := l.svcCtx.RedisClient.GetCtx(l.ctx, tokenKey)
+	if err != nil {
+		return nil, err
+	}
+	if cacheToken != in.Token {
+		return nil, errors.New("CheckToken is invalid")
+	}
 
-	return &pb.AuthResp{}, nil
+	data, err := structpb.NewStruct(map[string]interface{}{})
+	if err != nil {
+		return nil, err
+	}
+	return &pb.AuthResp{
+		Code: 0,
+		Msg:  "success",
+		Data: data,
+	}, nil
 }

+ 59 - 19
apis/auth/internal/logic/csauthlogic.go

@@ -2,7 +2,12 @@ package logic
 
 import (
 	"context"
+	"fmt"
+	"github.com/golang-jwt/jwt/v4"
 	"google.golang.org/protobuf/types/known/structpb"
+	"time"
+	"ylink/ext/globalkey"
+	"ylink/ext/jwtdata"
 
 	"ylink/apis/auth/internal/svc"
 	"ylink/apis/auth/pb"
@@ -25,24 +30,59 @@ func NewCsAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CsAuthLogi
 }
 
 func (l *CsAuthLogic) CsAuth(in *pb.CsAuthReq) (*pb.AuthResp, error) {
-	l.Logger.Info("invoke func CsAuth...")
-	l.Logger.Infof("cs_id: %s", in.CsId)
-
-	// todo 查询用户信息
-	// todo 生成token
-	if data, err := structpb.NewStruct(map[string]interface{}{
-		"token": "cs_auth",
-	}); err != nil {
-		return &pb.AuthResp{
-			Code: 1,
-			Msg:  err.Error(),
-			Data: nil,
-		}, err
-	} else {
-		return &pb.AuthResp{
-			Code: 0,
-			Msg:  "success",
-			Data: data,
-		}, nil
+	var token string
+	// 查询redis
+	tokenKey := fmt.Sprintf(globalkey.CacheTokenKey, in.CsId)
+	token, err := l.svcCtx.RedisClient.GetCtx(l.ctx, tokenKey)
+	if err != nil {
+		return nil, err
 	}
+
+	// 生成token
+	if len(token) == 0 {
+		now := time.Now().Unix()
+		token, err = l.generateCsToken(now, in.CsId)
+		if err != nil {
+			return nil, err
+		}
+	}
+
+	data, err := structpb.NewStruct(map[string]interface{}{
+		"token": token,
+	})
+	if err != nil {
+		return nil, err
+	}
+
+	// 存入redis
+	if err := l.svcCtx.RedisClient.SetexCtx(l.ctx, tokenKey, token, int(l.svcCtx.Config.JwtAuth.AccessExpire)); err != nil {
+		return nil, err
+	}
+
+	return &pb.AuthResp{
+		Code: 0,
+		Msg:  "success",
+		Data: data,
+	}, nil
+}
+
+//
+//  generateCsToken
+//  @Description: 客服token签发
+//  @receiver l
+//  @param iat
+//  @param csId
+//  @return string
+//  @return error
+//
+func (l *CsAuthLogic) generateCsToken(iat int64, csId string) (string, error) {
+	secret := l.svcCtx.Config.JwtAuth.AccessSecret
+	expire := l.svcCtx.Config.JwtAuth.AccessExpire
+	claims := make(jwt.MapClaims)
+	claims["iat"] = iat
+	claims["exp"] = iat + expire
+	claims[jwtdata.JwtKeyCsId] = csId
+	token := jwt.New(jwt.SigningMethodHS256)
+	token.Claims = claims
+	return token.SignedString([]byte(secret))
 }

+ 61 - 20
apis/auth/internal/logic/playerauthlogic.go

@@ -2,7 +2,12 @@ package logic
 
 import (
 	"context"
+	"fmt"
+	"github.com/golang-jwt/jwt/v4"
 	"google.golang.org/protobuf/types/known/structpb"
+	"time"
+	"ylink/ext/globalkey"
+	"ylink/ext/jwtdata"
 
 	"ylink/apis/auth/internal/svc"
 	"ylink/apis/auth/pb"
@@ -25,25 +30,61 @@ func NewPlayerAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Player
 }
 
 func (l *PlayerAuthLogic) PlayerAuth(in *pb.PlayerAuthReq) (*pb.AuthResp, error) {
-	l.Logger.Info("invoke func PlayerAuth...")
-	l.Logger.Infof("player_id: %s", in.PlayerId)
-	l.Logger.Infof("game_id: %s", in.GameId)
-
-	// todo 查询用户信息
-	// todo 生成token
-	if data, err := structpb.NewStruct(map[string]interface{}{
-		"token": "player_auth",
-	}); err != nil {
-		return &pb.AuthResp{
-			Code: 1,
-			Msg:  err.Error(),
-			Data: nil,
-		}, err
-	} else {
-		return &pb.AuthResp{
-			Code: 0,
-			Msg:  "success",
-			Data: data,
-		}, nil
+	var token string
+	// 查询redis
+	tokenKey := fmt.Sprintf(globalkey.CacheTokenKey, in.PlayerId)
+	token, err := l.svcCtx.RedisClient.GetCtx(l.ctx, tokenKey)
+	if err != nil {
+		return nil, err
 	}
+
+	// 生成token
+	if len(token) == 0 {
+		now := time.Now().Unix()
+		token, err = l.generatePlayerToken(now, in.PlayerId, in.GameId)
+		if err != nil {
+			return nil, err
+		}
+	}
+
+	data, err := structpb.NewStruct(map[string]interface{}{
+		"token": token,
+	})
+	if err != nil {
+		return nil, err
+	}
+
+	// 存入redis
+	if err := l.svcCtx.RedisClient.SetexCtx(l.ctx, tokenKey, token, int(l.svcCtx.Config.JwtAuth.AccessExpire)); err != nil {
+		return nil, err
+	}
+
+	return &pb.AuthResp{
+		Code: 0,
+		Msg:  "success",
+		Data: data,
+	}, nil
+}
+
+//
+//  generatePlayerToken
+//  @Description: 玩家token签发
+//  @receiver l
+//  @param iat
+//  @param playerId
+//  @param gameId
+//  @return string
+//  @return error
+//
+func (l *PlayerAuthLogic) generatePlayerToken(iat int64, playerId string, gameId string) (string, error) {
+	secret := l.svcCtx.Config.JwtAuth.AccessSecret
+	expire := l.svcCtx.Config.JwtAuth.AccessExpire
+	claims := make(jwt.MapClaims)
+	claims["iat"] = iat
+	claims["exp"] = iat + expire
+	claims[jwtdata.JwtKeyPlayerId] = playerId
+	claims[jwtdata.JwtKeyGameId] = gameId
+	token := jwt.New(jwt.SigningMethodHS256)
+	token.Claims = claims
+	return token.SignedString([]byte(secret))
 }

+ 10 - 2
apis/auth/internal/svc/servicecontext.go

@@ -1,13 +1,21 @@
 package svc
 
-import "ylink/apis/auth/internal/config"
+import (
+	"github.com/zeromicro/go-zero/core/stores/redis"
+	"ylink/apis/auth/internal/config"
+)
 
 type ServiceContext struct {
-	Config config.Config
+	Config      config.Config
+	RedisClient *redis.Redis
 }
 
 func NewServiceContext(c config.Config) *ServiceContext {
 	return &ServiceContext{
 		Config: c,
+		RedisClient: redis.New(c.Redis.Host, func(r *redis.Redis) {
+			r.Type = c.Redis.Type
+			r.Pass = c.Redis.Pass
+		}),
 	}
 }

+ 29 - 20
apis/auth/pb/auth.pb.go

@@ -175,7 +175,8 @@ type CheckAuthReq struct {
 	sizeCache     protoimpl.SizeCache
 	unknownFields protoimpl.UnknownFields
 
-	Token string `protobuf:"bytes,1,opt,name=token,proto3" json:"token,omitempty"`
+	Uid   string `protobuf:"bytes,1,opt,name=uid,proto3" json:"uid,omitempty"`
+	Token string `protobuf:"bytes,2,opt,name=token,proto3" json:"token,omitempty"`
 }
 
 func (x *CheckAuthReq) Reset() {
@@ -210,6 +211,13 @@ func (*CheckAuthReq) Descriptor() ([]byte, []int) {
 	return file_pb_auth_proto_rawDescGZIP(), []int{3}
 }
 
+func (x *CheckAuthReq) GetUid() string {
+	if x != nil {
+		return x.Uid
+	}
+	return ""
+}
+
 func (x *CheckAuthReq) GetToken() string {
 	if x != nil {
 		return x.Token
@@ -296,25 +304,26 @@ var file_pb_auth_proto_rawDesc = []byte{
 	0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x67, 0x61, 0x6d, 0x65, 0x49, 0x64,
 	0x22, 0x20, 0x0a, 0x09, 0x43, 0x73, 0x41, 0x75, 0x74, 0x68, 0x52, 0x65, 0x71, 0x12, 0x13, 0x0a,
 	0x05, 0x63, 0x73, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x73,
-	0x49, 0x64, 0x22, 0x24, 0x0a, 0x0c, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x41, 0x75, 0x74, 0x68, 0x52,
-	0x65, 0x71, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28,
-	0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x22, 0x5d, 0x0a, 0x08, 0x41, 0x75, 0x74, 0x68,
-	0x52, 0x65, 0x73, 0x70, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01,
-	0x28, 0x03, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x6d, 0x73, 0x67, 0x18,
-	0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6d, 0x73, 0x67, 0x12, 0x2b, 0x0a, 0x04, 0x64, 0x61,
-	0x74, 0x61, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x17, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c,
-	0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x53, 0x74, 0x72, 0x75, 0x63,
-	0x74, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x32, 0x89, 0x01, 0x0a, 0x04, 0x41, 0x75, 0x74, 0x68,
-	0x12, 0x2d, 0x0a, 0x0a, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x41, 0x75, 0x74, 0x68, 0x12, 0x11,
-	0x2e, 0x70, 0x62, 0x2e, 0x50, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x41, 0x75, 0x74, 0x68, 0x52, 0x65,
-	0x71, 0x1a, 0x0c, 0x2e, 0x70, 0x62, 0x2e, 0x41, 0x75, 0x74, 0x68, 0x52, 0x65, 0x73, 0x70, 0x12,
-	0x25, 0x0a, 0x06, 0x63, 0x73, 0x41, 0x75, 0x74, 0x68, 0x12, 0x0d, 0x2e, 0x70, 0x62, 0x2e, 0x43,
-	0x73, 0x41, 0x75, 0x74, 0x68, 0x52, 0x65, 0x71, 0x1a, 0x0c, 0x2e, 0x70, 0x62, 0x2e, 0x41, 0x75,
-	0x74, 0x68, 0x52, 0x65, 0x73, 0x70, 0x12, 0x2b, 0x0a, 0x09, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x41,
-	0x75, 0x74, 0x68, 0x12, 0x10, 0x2e, 0x70, 0x62, 0x2e, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x41, 0x75,
-	0x74, 0x68, 0x52, 0x65, 0x71, 0x1a, 0x0c, 0x2e, 0x70, 0x62, 0x2e, 0x41, 0x75, 0x74, 0x68, 0x52,
-	0x65, 0x73, 0x70, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f,
-	0x74, 0x6f, 0x33,
+	0x49, 0x64, 0x22, 0x36, 0x0a, 0x0c, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x41, 0x75, 0x74, 0x68, 0x52,
+	0x65, 0x71, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,
+	0x03, 0x75, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x02, 0x20,
+	0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x22, 0x5d, 0x0a, 0x08, 0x41, 0x75,
+	0x74, 0x68, 0x52, 0x65, 0x73, 0x70, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01,
+	0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x6d, 0x73,
+	0x67, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6d, 0x73, 0x67, 0x12, 0x2b, 0x0a, 0x04,
+	0x64, 0x61, 0x74, 0x61, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x17, 0x2e, 0x67, 0x6f, 0x6f,
+	0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x53, 0x74, 0x72,
+	0x75, 0x63, 0x74, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x32, 0x89, 0x01, 0x0a, 0x04, 0x41, 0x75,
+	0x74, 0x68, 0x12, 0x2d, 0x0a, 0x0a, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x41, 0x75, 0x74, 0x68,
+	0x12, 0x11, 0x2e, 0x70, 0x62, 0x2e, 0x50, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x41, 0x75, 0x74, 0x68,
+	0x52, 0x65, 0x71, 0x1a, 0x0c, 0x2e, 0x70, 0x62, 0x2e, 0x41, 0x75, 0x74, 0x68, 0x52, 0x65, 0x73,
+	0x70, 0x12, 0x25, 0x0a, 0x06, 0x63, 0x73, 0x41, 0x75, 0x74, 0x68, 0x12, 0x0d, 0x2e, 0x70, 0x62,
+	0x2e, 0x43, 0x73, 0x41, 0x75, 0x74, 0x68, 0x52, 0x65, 0x71, 0x1a, 0x0c, 0x2e, 0x70, 0x62, 0x2e,
+	0x41, 0x75, 0x74, 0x68, 0x52, 0x65, 0x73, 0x70, 0x12, 0x2b, 0x0a, 0x09, 0x63, 0x68, 0x65, 0x63,
+	0x6b, 0x41, 0x75, 0x74, 0x68, 0x12, 0x10, 0x2e, 0x70, 0x62, 0x2e, 0x43, 0x68, 0x65, 0x63, 0x6b,
+	0x41, 0x75, 0x74, 0x68, 0x52, 0x65, 0x71, 0x1a, 0x0c, 0x2e, 0x70, 0x62, 0x2e, 0x41, 0x75, 0x74,
+	0x68, 0x52, 0x65, 0x73, 0x70, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70,
+	0x72, 0x6f, 0x74, 0x6f, 0x33,
 }
 
 var (

+ 2 - 1
apis/auth/pb/auth.proto

@@ -20,7 +20,8 @@ message CsAuthReq{
 }
 
 message CheckAuthReq{
-  string  token = 1;
+  string uid = 1;
+  string  token = 2;
 }
 
 message AuthResp{

+ 9 - 0
ext/globalkey/rediskey.go

@@ -0,0 +1,9 @@
+//@File     rediskey.go
+//@Time     2022/04/24
+//@Author   #Suyghur,
+
+package globalkey
+
+const (
+	CacheTokenKey = "token:%s"
+)

+ 28 - 0
ext/jwtdata/jwtdata.go

@@ -0,0 +1,28 @@
+//@File     jwtdata.go
+//@Time     2022/04/24
+//@Author   #Suyghur,
+
+package jwtdata
+
+import "context"
+
+const (
+	JwtKeyPlayerId = "jwt_player_id"
+	JwtKeyGameId   = "jwt_game_id"
+	JwtKeyCsId     = "jwt_cs_id"
+)
+
+func GetPlayerIdFromJwt(ctx context.Context) string {
+	playerId, _ := ctx.Value(JwtKeyPlayerId).(string)
+	return playerId
+}
+
+func GetGameIdFromJwt(ctx context.Context) string {
+	gameId, _ := ctx.Value(JwtKeyGameId).(string)
+	return gameId
+}
+
+func GetCsIdFromJwt(ctx context.Context) string {
+	csId, _ := ctx.Value(JwtKeyCsId).(string)
+	return csId
+}