Quellcode durchsuchen

feat: 增加 Logout 接口

BaiLuoYan vor 1 Woche
Ursprung
Commit
c8f38a1dd2
5 geänderte Dateien mit 155 neuen und 11 gelöschten Zeilen
  1. 5 0
      client.go
  2. 100 11
      pb/perm.pb.go
  3. 7 0
      pb/perm.proto
  4. 38 0
      pb/perm_grpc.pb.go
  5. 5 0
      permlib.go

+ 5 - 0
client.go

@@ -56,3 +56,8 @@ func (c *grpcClient) getUserPerms(ctx context.Context, appKey, appSecret string,
 		ProductCode: productCode,
 	})
 }
+
+func (c *grpcClient) logout(ctx context.Context, accessToken string) error {
+	_, err := c.cli.Logout(ctx, &pb.LogoutReq{AccessToken: accessToken})
+	return err
+}

+ 100 - 11
pb/perm.pb.go

@@ -729,6 +729,86 @@ func (x *GetUserPermsResp) GetMemberType() string {
 	return ""
 }
 
+type LogoutReq struct {
+	state         protoimpl.MessageState `protogen:"open.v1"`
+	AccessToken   string                 `protobuf:"bytes,1,opt,name=accessToken,proto3" json:"accessToken,omitempty"`
+	unknownFields protoimpl.UnknownFields
+	sizeCache     protoimpl.SizeCache
+}
+
+func (x *LogoutReq) Reset() {
+	*x = LogoutReq{}
+	mi := &file_pb_perm_proto_msgTypes[11]
+	ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+	ms.StoreMessageInfo(mi)
+}
+
+func (x *LogoutReq) String() string {
+	return protoimpl.X.MessageStringOf(x)
+}
+
+func (*LogoutReq) ProtoMessage() {}
+
+func (x *LogoutReq) ProtoReflect() protoreflect.Message {
+	mi := &file_pb_perm_proto_msgTypes[11]
+	if x != nil {
+		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+		if ms.LoadMessageInfo() == nil {
+			ms.StoreMessageInfo(mi)
+		}
+		return ms
+	}
+	return mi.MessageOf(x)
+}
+
+// Deprecated: Use LogoutReq.ProtoReflect.Descriptor instead.
+func (*LogoutReq) Descriptor() ([]byte, []int) {
+	return file_pb_perm_proto_rawDescGZIP(), []int{11}
+}
+
+func (x *LogoutReq) GetAccessToken() string {
+	if x != nil {
+		return x.AccessToken
+	}
+	return ""
+}
+
+type LogoutResp struct {
+	state         protoimpl.MessageState `protogen:"open.v1"`
+	unknownFields protoimpl.UnknownFields
+	sizeCache     protoimpl.SizeCache
+}
+
+func (x *LogoutResp) Reset() {
+	*x = LogoutResp{}
+	mi := &file_pb_perm_proto_msgTypes[12]
+	ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+	ms.StoreMessageInfo(mi)
+}
+
+func (x *LogoutResp) String() string {
+	return protoimpl.X.MessageStringOf(x)
+}
+
+func (*LogoutResp) ProtoMessage() {}
+
+func (x *LogoutResp) ProtoReflect() protoreflect.Message {
+	mi := &file_pb_perm_proto_msgTypes[12]
+	if x != nil {
+		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+		if ms.LoadMessageInfo() == nil {
+			ms.StoreMessageInfo(mi)
+		}
+		return ms
+	}
+	return mi.MessageOf(x)
+}
+
+// Deprecated: Use LogoutResp.ProtoReflect.Descriptor instead.
+func (*LogoutResp) Descriptor() ([]byte, []int) {
+	return file_pb_perm_proto_rawDescGZIP(), []int{12}
+}
+
 var File_pb_perm_proto protoreflect.FileDescriptor
 
 const file_pb_perm_proto_rawDesc = "" +
@@ -789,13 +869,18 @@ const file_pb_perm_proto_rawDesc = "" +
 	"\x05perms\x18\x01 \x03(\tR\x05perms\x12\x1e\n" +
 	"\n" +
 	"memberType\x18\x02 \x01(\tR\n" +
-	"memberType2\xa5\x02\n" +
+	"memberType\"-\n" +
+	"\tLogoutReq\x12 \n" +
+	"\vaccessToken\x18\x01 \x01(\tR\vaccessToken\"\f\n" +
+	"\n" +
+	"LogoutResp2\xce\x02\n" +
 	"\vPermService\x12B\n" +
 	"\x0fSyncPermissions\x12\x16.pb.SyncPermissionsReq\x1a\x17.pb.SyncPermissionsResp\x12$\n" +
 	"\x05Login\x12\f.pb.LoginReq\x1a\r.pb.LoginResp\x129\n" +
 	"\fRefreshToken\x12\x13.pb.RefreshTokenReq\x1a\x14.pb.RefreshTokenResp\x126\n" +
 	"\vVerifyToken\x12\x12.pb.VerifyTokenReq\x1a\x13.pb.VerifyTokenResp\x129\n" +
-	"\fGetUserPerms\x12\x13.pb.GetUserPermsReq\x1a\x14.pb.GetUserPermsRespB\x1eZ\x1cgithub.com/xiaozi/permlib/pbb\x06proto3"
+	"\fGetUserPerms\x12\x13.pb.GetUserPermsReq\x1a\x14.pb.GetUserPermsResp\x12'\n" +
+	"\x06Logout\x12\r.pb.LogoutReq\x1a\x0e.pb.LogoutRespB\x1eZ\x1cgithub.com/xiaozi/permlib/pbb\x06proto3"
 
 var (
 	file_pb_perm_proto_rawDescOnce sync.Once
@@ -809,7 +894,7 @@ func file_pb_perm_proto_rawDescGZIP() []byte {
 	return file_pb_perm_proto_rawDescData
 }
 
-var file_pb_perm_proto_msgTypes = make([]protoimpl.MessageInfo, 11)
+var file_pb_perm_proto_msgTypes = make([]protoimpl.MessageInfo, 13)
 var file_pb_perm_proto_goTypes = []any{
 	(*PermItem)(nil),            // 0: pb.PermItem
 	(*SyncPermissionsReq)(nil),  // 1: pb.SyncPermissionsReq
@@ -822,6 +907,8 @@ var file_pb_perm_proto_goTypes = []any{
 	(*VerifyTokenResp)(nil),     // 8: pb.VerifyTokenResp
 	(*GetUserPermsReq)(nil),     // 9: pb.GetUserPermsReq
 	(*GetUserPermsResp)(nil),    // 10: pb.GetUserPermsResp
+	(*LogoutReq)(nil),           // 11: pb.LogoutReq
+	(*LogoutResp)(nil),          // 12: pb.LogoutResp
 }
 var file_pb_perm_proto_depIdxs = []int32{
 	0,  // 0: pb.SyncPermissionsReq.perms:type_name -> pb.PermItem
@@ -830,13 +917,15 @@ var file_pb_perm_proto_depIdxs = []int32{
 	5,  // 3: pb.PermService.RefreshToken:input_type -> pb.RefreshTokenReq
 	7,  // 4: pb.PermService.VerifyToken:input_type -> pb.VerifyTokenReq
 	9,  // 5: pb.PermService.GetUserPerms:input_type -> pb.GetUserPermsReq
-	2,  // 6: pb.PermService.SyncPermissions:output_type -> pb.SyncPermissionsResp
-	4,  // 7: pb.PermService.Login:output_type -> pb.LoginResp
-	6,  // 8: pb.PermService.RefreshToken:output_type -> pb.RefreshTokenResp
-	8,  // 9: pb.PermService.VerifyToken:output_type -> pb.VerifyTokenResp
-	10, // 10: pb.PermService.GetUserPerms:output_type -> pb.GetUserPermsResp
-	6,  // [6:11] is the sub-list for method output_type
-	1,  // [1:6] is the sub-list for method input_type
+	11, // 6: pb.PermService.Logout:input_type -> pb.LogoutReq
+	2,  // 7: pb.PermService.SyncPermissions:output_type -> pb.SyncPermissionsResp
+	4,  // 8: pb.PermService.Login:output_type -> pb.LoginResp
+	6,  // 9: pb.PermService.RefreshToken:output_type -> pb.RefreshTokenResp
+	8,  // 10: pb.PermService.VerifyToken:output_type -> pb.VerifyTokenResp
+	10, // 11: pb.PermService.GetUserPerms:output_type -> pb.GetUserPermsResp
+	12, // 12: pb.PermService.Logout:output_type -> pb.LogoutResp
+	7,  // [7:13] is the sub-list for method output_type
+	1,  // [1:7] is the sub-list for method input_type
 	1,  // [1:1] is the sub-list for extension type_name
 	1,  // [1:1] is the sub-list for extension extendee
 	0,  // [0:1] is the sub-list for field type_name
@@ -853,7 +942,7 @@ func file_pb_perm_proto_init() {
 			GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
 			RawDescriptor: unsafe.Slice(unsafe.StringData(file_pb_perm_proto_rawDesc), len(file_pb_perm_proto_rawDesc)),
 			NumEnums:      0,
-			NumMessages:   11,
+			NumMessages:   13,
 			NumExtensions: 0,
 			NumServices:   1,
 		},

+ 7 - 0
pb/perm.proto

@@ -10,6 +10,7 @@ service PermService {
   rpc RefreshToken(RefreshTokenReq) returns (RefreshTokenResp);
   rpc VerifyToken(VerifyTokenReq) returns (VerifyTokenResp);
   rpc GetUserPerms(GetUserPermsReq) returns (GetUserPermsResp);
+  rpc Logout(LogoutReq) returns (LogoutResp);
 }
 
 message PermItem {
@@ -83,3 +84,9 @@ message GetUserPermsResp {
   repeated string perms = 1;
   string memberType = 2;
 }
+
+message LogoutReq {
+  string accessToken = 1;
+}
+
+message LogoutResp {}

+ 38 - 0
pb/perm_grpc.pb.go

@@ -24,6 +24,7 @@ const (
 	PermService_RefreshToken_FullMethodName    = "/pb.PermService/RefreshToken"
 	PermService_VerifyToken_FullMethodName     = "/pb.PermService/VerifyToken"
 	PermService_GetUserPerms_FullMethodName    = "/pb.PermService/GetUserPerms"
+	PermService_Logout_FullMethodName          = "/pb.PermService/Logout"
 )
 
 // PermServiceClient is the client API for PermService service.
@@ -35,6 +36,7 @@ type PermServiceClient interface {
 	RefreshToken(ctx context.Context, in *RefreshTokenReq, opts ...grpc.CallOption) (*RefreshTokenResp, error)
 	VerifyToken(ctx context.Context, in *VerifyTokenReq, opts ...grpc.CallOption) (*VerifyTokenResp, error)
 	GetUserPerms(ctx context.Context, in *GetUserPermsReq, opts ...grpc.CallOption) (*GetUserPermsResp, error)
+	Logout(ctx context.Context, in *LogoutReq, opts ...grpc.CallOption) (*LogoutResp, error)
 }
 
 type permServiceClient struct {
@@ -95,6 +97,16 @@ func (c *permServiceClient) GetUserPerms(ctx context.Context, in *GetUserPermsRe
 	return out, nil
 }
 
+func (c *permServiceClient) Logout(ctx context.Context, in *LogoutReq, opts ...grpc.CallOption) (*LogoutResp, error) {
+	cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
+	out := new(LogoutResp)
+	err := c.cc.Invoke(ctx, PermService_Logout_FullMethodName, in, out, cOpts...)
+	if err != nil {
+		return nil, err
+	}
+	return out, nil
+}
+
 // PermServiceServer is the server API for PermService service.
 // All implementations must embed UnimplementedPermServiceServer
 // for forward compatibility.
@@ -104,6 +116,7 @@ type PermServiceServer interface {
 	RefreshToken(context.Context, *RefreshTokenReq) (*RefreshTokenResp, error)
 	VerifyToken(context.Context, *VerifyTokenReq) (*VerifyTokenResp, error)
 	GetUserPerms(context.Context, *GetUserPermsReq) (*GetUserPermsResp, error)
+	Logout(context.Context, *LogoutReq) (*LogoutResp, error)
 	mustEmbedUnimplementedPermServiceServer()
 }
 
@@ -129,6 +142,9 @@ func (UnimplementedPermServiceServer) VerifyToken(context.Context, *VerifyTokenR
 func (UnimplementedPermServiceServer) GetUserPerms(context.Context, *GetUserPermsReq) (*GetUserPermsResp, error) {
 	return nil, status.Error(codes.Unimplemented, "method GetUserPerms not implemented")
 }
+func (UnimplementedPermServiceServer) Logout(context.Context, *LogoutReq) (*LogoutResp, error) {
+	return nil, status.Error(codes.Unimplemented, "method Logout not implemented")
+}
 func (UnimplementedPermServiceServer) mustEmbedUnimplementedPermServiceServer() {}
 func (UnimplementedPermServiceServer) testEmbeddedByValue()                     {}
 
@@ -240,6 +256,24 @@ func _PermService_GetUserPerms_Handler(srv interface{}, ctx context.Context, dec
 	return interceptor(ctx, in, info, handler)
 }
 
+func _PermService_Logout_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
+	in := new(LogoutReq)
+	if err := dec(in); err != nil {
+		return nil, err
+	}
+	if interceptor == nil {
+		return srv.(PermServiceServer).Logout(ctx, in)
+	}
+	info := &grpc.UnaryServerInfo{
+		Server:     srv,
+		FullMethod: PermService_Logout_FullMethodName,
+	}
+	handler := func(ctx context.Context, req interface{}) (interface{}, error) {
+		return srv.(PermServiceServer).Logout(ctx, req.(*LogoutReq))
+	}
+	return interceptor(ctx, in, info, handler)
+}
+
 // PermService_ServiceDesc is the grpc.ServiceDesc for PermService service.
 // It's only intended for direct use with grpc.RegisterService,
 // and not to be introspected or modified (even as a copy)
@@ -267,6 +301,10 @@ var PermService_ServiceDesc = grpc.ServiceDesc{
 			MethodName: "GetUserPerms",
 			Handler:    _PermService_GetUserPerms_Handler,
 		},
+		{
+			MethodName: "Logout",
+			Handler:    _PermService_Logout_Handler,
+		},
 	},
 	Streams:  []grpc.StreamDesc{},
 	Metadata: "pb/perm.proto",

+ 5 - 0
permlib.go

@@ -159,6 +159,11 @@ func (e *Engine) GetUserPerms(ctx context.Context, userId int64) (*UserPermsResu
 	}, nil
 }
 
+// Logout 调用权限系统 gRPC Logout 接口,使 accessToken 对应用户的所有令牌立即失效
+func (e *Engine) Logout(ctx context.Context, accessToken string) error {
+	return e.client.logout(ctx, accessToken)
+}
+
 // AuthMiddleware 返回鉴权 handler,供产品端中间件直接调用
 func (e *Engine) AuthMiddleware(next http.HandlerFunc) http.HandlerFunc {
 	return e.authMiddleware(next)