Browse Source

v0.0.1开发:auth服务token鉴权接口开发

#Suyghur 3 years ago
parent
commit
08e68eb55c

+ 20 - 9
apis/auth/internal/logic/checkauthlogic.go

@@ -3,14 +3,11 @@ package logic
 import (
 	"context"
 	"errors"
-	"fmt"
+	"github.com/golang-jwt/jwt/v4"
+	"github.com/zeromicro/go-zero/core/logx"
 	"google.golang.org/protobuf/types/known/structpb"
-	"ylink/ext/globalkey"
-
 	"ylink/apis/auth/internal/svc"
 	"ylink/apis/auth/pb"
-
-	"github.com/zeromicro/go-zero/core/logx"
 )
 
 type CheckAuthLogic struct {
@@ -28,13 +25,27 @@ func NewCheckAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckAu
 }
 
 func (l *CheckAuthLogic) CheckAuth(in *pb.CheckAuthReq) (*pb.AuthResp, error) {
-	tokenKey := fmt.Sprintf(globalkey.CacheTokenKey, in.Uid)
-	cacheToken, err := l.svcCtx.RedisClient.GetCtx(l.ctx, tokenKey)
+
+	// 解析传入的token
+	// 第二个参数是一个回调函数,作用是判断生成token所用的签名算法是否和传入token的签名算法是否一致。
+	// 算法匹配就返回密钥,用来解析token.
+	token, err := jwt.Parse(in.Token, func(token *jwt.Token) (i interface{}, err error) {
+		if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
+			return nil, errors.New("unexpected signing method")
+		}
+		return []byte(l.svcCtx.Config.JwtAuth.AccessSecret), nil
+	})
+
+	// err不为空,说明token已过期
 	if err != nil {
 		return nil, err
 	}
-	if cacheToken != in.Token {
-		return nil, errors.New("CheckToken is invalid")
+
+	// 将获取的token中的Claims强转为MapClaims
+	_, ok := token.Claims.(jwt.MapClaims)
+	// 判断token是否有效
+	if !(ok && token.Valid) {
+		return nil, errors.New("cannot convert claim to mapClaim")
 	}
 
 	data, err := structpb.NewStruct(map[string]interface{}{})

+ 2 - 22
apis/auth/internal/logic/csauthlogic.go

@@ -2,11 +2,9 @@ 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"
@@ -30,35 +28,17 @@ func NewCsAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CsAuthLogi
 }
 
 func (l *CsAuthLogic) CsAuth(in *pb.CsAuthReq) (*pb.AuthResp, error) {
-	var token string
-	// 查询redis
-	tokenKey := fmt.Sprintf(globalkey.CacheTokenKey, in.CsId)
-	token, err := l.svcCtx.RedisClient.GetCtx(l.ctx, tokenKey)
+	now := time.Now().Unix()
+	token, err := l.generateCsToken(now, in.CsId)
 	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",

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

@@ -2,11 +2,9 @@ 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"
@@ -30,23 +28,12 @@ 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)
+	now := time.Now().Unix()
+	token, err := l.generatePlayerToken(now, in.PlayerId, in.GameId)
 	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,
 	})
@@ -54,11 +41,6 @@ func (l *PlayerAuthLogic) PlayerAuth(in *pb.PlayerAuthReq) (*pb.AuthResp, error)
 		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",

+ 1 - 7
apis/auth/internal/svc/servicecontext.go

@@ -1,21 +1,15 @@
 package svc
 
 import (
-	"github.com/zeromicro/go-zero/core/stores/redis"
 	"ylink/apis/auth/internal/config"
 )
 
 type ServiceContext struct {
-	Config      config.Config
-	RedisClient *redis.Redis
+	Config config.Config
 }
 
 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
-		}),
 	}
 }

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

@@ -175,8 +175,7 @@ type CheckAuthReq struct {
 	sizeCache     protoimpl.SizeCache
 	unknownFields protoimpl.UnknownFields
 
-	Uid   string `protobuf:"bytes,1,opt,name=uid,proto3" json:"uid,omitempty"`
-	Token string `protobuf:"bytes,2,opt,name=token,proto3" json:"token,omitempty"`
+	Token string `protobuf:"bytes,1,opt,name=token,proto3" json:"token,omitempty"`
 }
 
 func (x *CheckAuthReq) Reset() {
@@ -211,13 +210,6 @@ 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
@@ -304,26 +296,25 @@ 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, 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,
+	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,
 }
 
 var (

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

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

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

@@ -16,6 +16,10 @@ type CsAuthReq {
 	CsId string `json:"cs_id"`
 }
 
+type CheckAuthReq {
+	Token string `json:"token"`
+}
+
 type AuthResp {
 	Code int64       `json:"code"`
 	Msg  string      `json:"msg"`
@@ -33,4 +37,8 @@ service Authbff {
 	@doc "客服认证"
 	@handler csAuth
 	post /cs/auth (CsAuthReq) returns (AuthResp)
+	
+	@doc "测试token"
+	@handler checkAuth
+	post /check_auth (CheckAuthReq) returns (AuthResp)
 }

+ 28 - 0
bff/authbff/internal/handler/checkauthhandler.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 checkAuthHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.CheckAuthReq
+		if err := httpx.Parse(r, &req); err != nil {
+			httpx.Error(w, err)
+			return
+		}
+
+		l := logic.NewCheckAuthLogic(r.Context(), svcCtx)
+		resp, err := l.CheckAuth(&req)
+		if err != nil {
+			httpx.Error(w, err)
+		} else {
+			httpx.OkJson(w, resp)
+		}
+	}
+}

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

@@ -22,6 +22,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 				Path:    "/cs/auth",
 				Handler: csAuthHandler(serverCtx),
 			},
+			{
+				Method:  http.MethodPost,
+				Path:    "/check_auth",
+				Handler: checkAuthHandler(serverCtx),
+			},
 		},
 		rest.WithPrefix("/api/v1"),
 	)

+ 43 - 0
bff/authbff/internal/logic/checkauthlogic.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 CheckAuthLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewCheckAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckAuthLogic {
+	return &CheckAuthLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx,
+	}
+}
+
+func (l *CheckAuthLogic) CheckAuth(req *types.CheckAuthReq) (resp *types.AuthResp, err error) {
+	if authResp, err := l.svcCtx.AuthRpc.CheckAuth(l.ctx, &pb.CheckAuthReq{
+		Token: req.Token,
+	}); 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
+	}
+}

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

@@ -10,6 +10,10 @@ type CsAuthReq struct {
 	CsId string `json:"cs_id"`
 }
 
+type CheckAuthReq struct {
+	Token string `json:"token"`
+}
+
 type AuthResp struct {
 	Code int64       `json:"code"`
 	Msg  string      `json:"msg"`