Browse Source

Merge pull request #3 from Suyghur/feature/v0.0.1

Feature/v0.0.1
#Suyghur 3 years ago
parent
commit
ffd163848a

+ 12 - 2
apis/auth/etc/auth.yaml

@@ -1,6 +1,16 @@
 Name: auth.rpc
-ListenOn: 0.0.0.0:10002
+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 - 21
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,26 +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("uname: %s", in.Uname)
-	l.Logger.Infof("password: %s", in.Password)
-
-	// todo 查询用户信息
-	// todo 生成token
-	if data, err := structpb.NewStruct(map[string]interface{}{
-		"token":         "cs_auth",
-		"basic_rpc_url": "https://www.baidu.com",
-	}); 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))
 }

+ 60 - 23
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,29 +30,61 @@ func NewPlayerAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Player
 }
 
 func (l *PlayerAuthLogic) PlayerAuth(in *pb.PlayerAuthReq) (*pb.AuthResp, error) {
+	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
+	}
 
-	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",
-		"has_own_cs":    1,
-		"cs_id":         "cs1231",
-		"basic_rpc_url": "https://www.baidu.com",
-	}); err != nil {
-		return &pb.AuthResp{
-			Code: 1,
-			Msg:  err.Error(),
-			Data: nil,
-		}, err
-	} else {
-		return &pb.AuthResp{
-			Code: 0,
-			Msg:  "success",
-			Data: data,
-		}, nil
+	// 存入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
+		}),
 	}
 }

+ 34 - 34
apis/auth/pb/auth.pb.go

@@ -128,8 +128,7 @@ type CsAuthReq struct {
 	sizeCache     protoimpl.SizeCache
 	unknownFields protoimpl.UnknownFields
 
-	Uname    string `protobuf:"bytes,1,opt,name=uname,proto3" json:"uname,omitempty"`
-	Password string `protobuf:"bytes,2,opt,name=password,proto3" json:"password,omitempty"`
+	CsId string `protobuf:"bytes,1,opt,name=cs_id,json=csId,proto3" json:"cs_id,omitempty"`
 }
 
 func (x *CsAuthReq) Reset() {
@@ -164,16 +163,9 @@ func (*CsAuthReq) Descriptor() ([]byte, []int) {
 	return file_pb_auth_proto_rawDescGZIP(), []int{2}
 }
 
-func (x *CsAuthReq) GetUname() string {
+func (x *CsAuthReq) GetCsId() string {
 	if x != nil {
-		return x.Uname
-	}
-	return ""
-}
-
-func (x *CsAuthReq) GetPassword() string {
-	if x != nil {
-		return x.Password
+		return x.CsId
 	}
 	return ""
 }
@@ -183,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() {
@@ -218,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
@@ -302,28 +302,28 @@ var file_pb_auth_proto_rawDesc = []byte{
 	0x61, 0x79, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x70,
 	0x6c, 0x61, 0x79, 0x65, 0x72, 0x49, 0x64, 0x12, 0x17, 0x0a, 0x07, 0x67, 0x61, 0x6d, 0x65, 0x5f,
 	0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x67, 0x61, 0x6d, 0x65, 0x49, 0x64,
-	0x22, 0x3d, 0x0a, 0x09, 0x43, 0x73, 0x41, 0x75, 0x74, 0x68, 0x52, 0x65, 0x71, 0x12, 0x14, 0x0a,
-	0x05, 0x75, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x75, 0x6e,
-	0x61, 0x6d, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18,
-	0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 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,
+	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, 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 (

+ 3 - 3
apis/auth/pb/auth.proto

@@ -16,12 +16,12 @@ message PlayerAuthReq{
 }
 
 message CsAuthReq{
-  string uname = 1;
-  string password = 2;
+  string cs_id = 1;
 }
 
 message CheckAuthReq{
-  string  token = 1;
+  string uid = 1;
+  string  token = 2;
 }
 
 message AuthResp{

+ 1 - 1
bff/apibff/etc/apibff.yaml

@@ -1,6 +1,6 @@
 Name: apibff
 Host: 0.0.0.0
-Port: 10000
+Port: 10001
 
 AuthRpc:
   Etcd:

+ 31 - 0
bff/authbff/authbff.go

@@ -0,0 +1,31 @@
+package main
+
+import (
+	"flag"
+	"fmt"
+
+	"ylink/bff/authbff/internal/config"
+	"ylink/bff/authbff/internal/handler"
+	"ylink/bff/authbff/internal/svc"
+
+	"github.com/zeromicro/go-zero/core/conf"
+	"github.com/zeromicro/go-zero/rest"
+)
+
+var configFile = flag.String("f", "etc/authbff.yaml", "the config file")
+
+func main() {
+	flag.Parse()
+
+	var c config.Config
+	conf.MustLoad(*configFile, &c)
+
+	ctx := svc.NewServiceContext(c)
+	server := rest.MustNewServer(c.RestConf)
+	defer server.Stop()
+
+	handler.RegisterHandlers(server, ctx)
+
+	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
+	server.Start()
+}

+ 36 - 0
bff/authbff/desc/authbff.api

@@ -0,0 +1,36 @@
+syntax = "v1"
+
+info(
+	title: "api前端服务"
+	desc: "api前端服务 "
+	author: "#Suyghur"
+	version: "v1"
+)
+
+type PlayerAuthReq {
+	PlayerId string `json:"player_id"`
+	GameId   string `json:"game_id"`
+}
+
+type CsAuthReq {
+	CsId string `json:"cs_id"`
+}
+
+type AuthResp {
+	Code int64       `json:"code"`
+	Msg  string      `json:"msg"`
+	Data interface{} `json:"data"`
+}
+
+@server(
+	prefix: api/v1
+)
+service Authbff {
+	@doc "玩家认证"
+	@handler playerAuth
+	post /player/auth (PlayerAuthReq) returns (AuthResp)
+	
+	@doc "客服认证"
+	@handler csAuth
+	post /cs/auth (CsAuthReq) returns (AuthResp)
+}

+ 9 - 0
bff/authbff/etc/authbff.yaml

@@ -0,0 +1,9 @@
+Name: Authbff
+Host: 0.0.0.0
+Port: 10000
+
+AuthRpc:
+  Etcd:
+    Hosts:
+      - 127.0.0.1:2379
+    Key: auth.rpc

+ 11 - 0
bff/authbff/internal/config/config.go

@@ -0,0 +1,11 @@
+package config
+
+import (
+	"github.com/zeromicro/go-zero/rest"
+	"github.com/zeromicro/go-zero/zrpc"
+)
+
+type Config struct {
+	rest.RestConf
+	AuthRpc zrpc.RpcClientConf
+}

+ 35 - 0
bff/authbff/internal/handler/csauthhandler.go

@@ -0,0 +1,35 @@
+package handler
+
+import (
+	"github.com/zeromicro/go-zero/rest/httpx"
+	"net/http"
+	"ylink/bff/authbff/internal/logic"
+	"ylink/bff/authbff/internal/svc"
+	"ylink/bff/authbff/internal/types"
+)
+
+func csAuthHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.CsAuthReq
+		if err := httpx.Parse(r, &req); err != nil {
+			httpx.OkJson(w, &types.AuthResp{
+				Code: 1,
+				Msg:  err.Error(),
+				Data: map[string]interface{}{},
+			})
+			return
+		}
+
+		l := logic.NewCsAuthLogic(r.Context(), svcCtx)
+		resp, err := l.CsAuth(&req)
+		if err != nil {
+			httpx.OkJson(w, &types.AuthResp{
+				Code: 1,
+				Msg:  err.Error(),
+				Data: map[string]interface{}{},
+			})
+		} else {
+			httpx.OkJson(w, resp)
+		}
+	}
+}

+ 28 - 0
bff/authbff/internal/handler/playerauthhandler.go

@@ -0,0 +1,28 @@
+package handler
+
+import (
+	"net/http"
+
+	"github.com/zeromicro/go-zero/rest/httpx"
+	"ylink/bff/authbff/internal/logic"
+	"ylink/bff/authbff/internal/svc"
+	"ylink/bff/authbff/internal/types"
+)
+
+func playerAuthHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.PlayerAuthReq
+		if err := httpx.Parse(r, &req); err != nil {
+			httpx.Error(w, err)
+			return
+		}
+
+		l := logic.NewPlayerAuthLogic(r.Context(), svcCtx)
+		resp, err := l.PlayerAuth(&req)
+		if err != nil {
+			httpx.Error(w, err)
+		} else {
+			httpx.OkJson(w, resp)
+		}
+	}
+}

+ 28 - 0
bff/authbff/internal/handler/routes.go

@@ -0,0 +1,28 @@
+// Code generated by goctl. DO NOT EDIT.
+package handler
+
+import (
+	"net/http"
+
+	"ylink/bff/authbff/internal/svc"
+
+	"github.com/zeromicro/go-zero/rest"
+)
+
+func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
+	server.AddRoutes(
+		[]rest.Route{
+			{
+				Method:  http.MethodPost,
+				Path:    "/player/auth",
+				Handler: playerAuthHandler(serverCtx),
+			},
+			{
+				Method:  http.MethodPost,
+				Path:    "/cs/auth",
+				Handler: csAuthHandler(serverCtx),
+			},
+		},
+		rest.WithPrefix("/api/v1"),
+	)
+}

+ 43 - 0
bff/authbff/internal/logic/csauthlogic.go

@@ -0,0 +1,43 @@
+package logic
+
+import (
+	"context"
+	"ylink/apis/auth/pb"
+
+	"ylink/bff/authbff/internal/svc"
+	"ylink/bff/authbff/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type CsAuthLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewCsAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CsAuthLogic {
+	return &CsAuthLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx,
+	}
+}
+
+func (l *CsAuthLogic) CsAuth(req *types.CsAuthReq) (resp *types.AuthResp, err error) {
+	if authResp, err := l.svcCtx.AuthRpc.CsAuth(l.ctx, &pb.CsAuthReq{
+		CsId: req.CsId,
+	}); err != nil {
+		return &types.AuthResp{
+			Code: authResp.Code,
+			Msg:  authResp.Msg,
+			Data: map[string]interface{}{},
+		}, err
+	} else {
+		return &types.AuthResp{
+			Code: authResp.Code,
+			Msg:  authResp.Msg,
+			Data: authResp.Data,
+		}, nil
+	}
+}

+ 44 - 0
bff/authbff/internal/logic/playerauthlogic.go

@@ -0,0 +1,44 @@
+package logic
+
+import (
+	"context"
+	"ylink/apis/auth/pb"
+
+	"ylink/bff/authbff/internal/svc"
+	"ylink/bff/authbff/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type PlayerAuthLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewPlayerAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PlayerAuthLogic {
+	return &PlayerAuthLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx,
+	}
+}
+
+func (l *PlayerAuthLogic) PlayerAuth(req *types.PlayerAuthReq) (resp *types.AuthResp, err error) {
+	if authResp, err := l.svcCtx.AuthRpc.PlayerAuth(l.ctx, &pb.PlayerAuthReq{
+		PlayerId: req.PlayerId,
+		GameId:   req.GameId,
+	}); err != nil {
+		return &types.AuthResp{
+			Code: authResp.Code,
+			Msg:  authResp.Msg,
+			Data: map[string]interface{}{},
+		}, err
+	} else {
+		return &types.AuthResp{
+			Code: authResp.Code,
+			Msg:  authResp.Msg,
+			Data: authResp.Data,
+		}, nil
+	}
+}

+ 19 - 0
bff/authbff/internal/svc/servicecontext.go

@@ -0,0 +1,19 @@
+package svc
+
+import (
+	"github.com/zeromicro/go-zero/zrpc"
+	"ylink/apis/auth/auth"
+	"ylink/bff/authbff/internal/config"
+)
+
+type ServiceContext struct {
+	Config  config.Config
+	AuthRpc auth.Auth
+}
+
+func NewServiceContext(c config.Config) *ServiceContext {
+	return &ServiceContext{
+		Config:  c,
+		AuthRpc: auth.NewAuth(zrpc.MustNewClient(c.AuthRpc)),
+	}
+}

+ 17 - 0
bff/authbff/internal/types/types.go

@@ -0,0 +1,17 @@
+// Code generated by goctl. DO NOT EDIT.
+package types
+
+type PlayerAuthReq struct {
+	PlayerId string `json:"player_id"`
+	GameId   string `json:"game_id"`
+}
+
+type CsAuthReq struct {
+	CsId string `json:"cs_id"`
+}
+
+type AuthResp struct {
+	Code int64       `json:"code"`
+	Msg  string      `json:"msg"`
+	Data interface{} `json:"data"`
+}

+ 1 - 1
bff/rpcbff/etc/rpcbff.yaml

@@ -1,5 +1,5 @@
 Name: rpcbff.rpc
-ListenOn: 0.0.0.0:10001
+ListenOn: 0.0.0.0:10002
 Etcd:
   Hosts:
   - 127.0.0.1:2379

+ 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
+}