xjg 4 månader sedan
förälder
incheckning
3978804872

+ 5 - 1
Makefile

@@ -20,4 +20,8 @@ base:
 
 .PHONY: health
 health:
-	protoc --proto_path=. --micro_out=. --go_out=:. proto/health.proto
+	protoc --proto_path=. --micro_out=. --go_out=:. proto/health.proto
+
+.PHONY: auth
+auth:
+	protoc --proto_path=. --micro_out=. --go_out=:. proto/authentication.proto

+ 1 - 1
domain/repository/admin_user.go

@@ -56,7 +56,7 @@ func (u *Repository) CreateAdminUser(user *req.AdminUser, adminRoles []int64, ad
 		}
 	}()
 
-	if err := u.db.Create(&user).Error; err != nil {
+	if err := tx.Create(&user).Error; err != nil {
 		tx.Rollback()
 		return err
 	}

+ 13 - 0
handler/authentication.go

@@ -0,0 +1,13 @@
+package handler
+
+import (
+	"context"
+	pb "sghgogs.com/sghblog/authorization-service/proto"
+)
+
+type Authentication struct {
+}
+
+func (svc *Authentication) GetAuthentication(ctx context.Context, auth *pb.AuthenticationRequest, rsp *pb.AuthenticationResponse) error {
+	return nil
+}

+ 1 - 1
install.sh

@@ -1,5 +1,5 @@
 # 测试环境
-export PORT=8081
+export PORT=9001
 export TRACING_ENABLE=true
 export TRACING_JAEGER_URL=http://localhost:14268/api/traces
 #jaeger-collector-service.monitoring.svc.cluster.local:14268

+ 2 - 1
main.go

@@ -41,7 +41,6 @@ func main() {
 		logrus.Fatal(err)
 	}
 	if cfg := config.RedisAddress(); cfg.Enable {
-		fmt.Println(cfg.Enable)
 		authutil.NewJWTAuth(redis.NewClient(&redis.Options{
 			Addr:     cfg.URL,      // Redis 服务器地址
 			Password: cfg.Password, // Redis 密码,如果有的话
@@ -131,6 +130,8 @@ func main() {
 		Service: newService,
 	})
 	authorization_service.RegisterAdminUserServiceHandler(srv.Server(), &handler.AdminUser{Service: newService})
+	// 权限认证使用
+	authorization_service.RegisterAuthenticationServiceHandler(srv.Server(), &handler.Authentication{})
 	// Run service
 	logrus.Info("Run service")
 	if err := srv.Run(); err != nil {

+ 198 - 0
proto/authentication.pb.go

@@ -0,0 +1,198 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+// versions:
+// 	protoc-gen-go v1.31.0
+// 	protoc        v4.25.0
+// source: proto/authentication.proto
+
+package authorization_service
+
+import (
+	protoreflect "google.golang.org/protobuf/reflect/protoreflect"
+	protoimpl "google.golang.org/protobuf/runtime/protoimpl"
+	reflect "reflect"
+	sync "sync"
+)
+
+const (
+	// Verify that this generated code is sufficiently up-to-date.
+	_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
+	// Verify that runtime/protoimpl is sufficiently up-to-date.
+	_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
+)
+
+type AuthenticationRequest struct {
+	state         protoimpl.MessageState
+	sizeCache     protoimpl.SizeCache
+	unknownFields protoimpl.UnknownFields
+}
+
+func (x *AuthenticationRequest) Reset() {
+	*x = AuthenticationRequest{}
+	if protoimpl.UnsafeEnabled {
+		mi := &file_proto_authentication_proto_msgTypes[0]
+		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+		ms.StoreMessageInfo(mi)
+	}
+}
+
+func (x *AuthenticationRequest) String() string {
+	return protoimpl.X.MessageStringOf(x)
+}
+
+func (*AuthenticationRequest) ProtoMessage() {}
+
+func (x *AuthenticationRequest) ProtoReflect() protoreflect.Message {
+	mi := &file_proto_authentication_proto_msgTypes[0]
+	if protoimpl.UnsafeEnabled && x != nil {
+		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+		if ms.LoadMessageInfo() == nil {
+			ms.StoreMessageInfo(mi)
+		}
+		return ms
+	}
+	return mi.MessageOf(x)
+}
+
+// Deprecated: Use AuthenticationRequest.ProtoReflect.Descriptor instead.
+func (*AuthenticationRequest) Descriptor() ([]byte, []int) {
+	return file_proto_authentication_proto_rawDescGZIP(), []int{0}
+}
+
+type AuthenticationResponse struct {
+	state         protoimpl.MessageState
+	sizeCache     protoimpl.SizeCache
+	unknownFields protoimpl.UnknownFields
+}
+
+func (x *AuthenticationResponse) Reset() {
+	*x = AuthenticationResponse{}
+	if protoimpl.UnsafeEnabled {
+		mi := &file_proto_authentication_proto_msgTypes[1]
+		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+		ms.StoreMessageInfo(mi)
+	}
+}
+
+func (x *AuthenticationResponse) String() string {
+	return protoimpl.X.MessageStringOf(x)
+}
+
+func (*AuthenticationResponse) ProtoMessage() {}
+
+func (x *AuthenticationResponse) ProtoReflect() protoreflect.Message {
+	mi := &file_proto_authentication_proto_msgTypes[1]
+	if protoimpl.UnsafeEnabled && x != nil {
+		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+		if ms.LoadMessageInfo() == nil {
+			ms.StoreMessageInfo(mi)
+		}
+		return ms
+	}
+	return mi.MessageOf(x)
+}
+
+// Deprecated: Use AuthenticationResponse.ProtoReflect.Descriptor instead.
+func (*AuthenticationResponse) Descriptor() ([]byte, []int) {
+	return file_proto_authentication_proto_rawDescGZIP(), []int{1}
+}
+
+var File_proto_authentication_proto protoreflect.FileDescriptor
+
+var file_proto_authentication_proto_rawDesc = []byte{
+	0x0a, 0x1a, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x61, 0x75, 0x74, 0x68, 0x65, 0x6e, 0x74, 0x69,
+	0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x16, 0x61, 0x75,
+	0x74, 0x68, 0x65, 0x6e, 0x74, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x73, 0x65, 0x72,
+	0x76, 0x69, 0x63, 0x65, 0x22, 0x17, 0x0a, 0x15, 0x41, 0x75, 0x74, 0x68, 0x65, 0x6e, 0x74, 0x69,
+	0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x18, 0x0a,
+	0x16, 0x41, 0x75, 0x74, 0x68, 0x65, 0x6e, 0x74, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52,
+	0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x32, 0x8b, 0x01, 0x0a, 0x15, 0x41, 0x75, 0x74, 0x68,
+	0x65, 0x6e, 0x74, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63,
+	0x65, 0x12, 0x72, 0x0a, 0x11, 0x47, 0x65, 0x74, 0x41, 0x75, 0x74, 0x68, 0x65, 0x6e, 0x74, 0x69,
+	0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x2d, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x65, 0x6e, 0x74,
+	0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e,
+	0x41, 0x75, 0x74, 0x68, 0x65, 0x6e, 0x74, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65,
+	0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x2e, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x65, 0x6e, 0x74, 0x69,
+	0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x41,
+	0x75, 0x74, 0x68, 0x65, 0x6e, 0x74, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73,
+	0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x1f, 0x5a, 0x1d, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f,
+	0x3b, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x73,
+	0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
+}
+
+var (
+	file_proto_authentication_proto_rawDescOnce sync.Once
+	file_proto_authentication_proto_rawDescData = file_proto_authentication_proto_rawDesc
+)
+
+func file_proto_authentication_proto_rawDescGZIP() []byte {
+	file_proto_authentication_proto_rawDescOnce.Do(func() {
+		file_proto_authentication_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_authentication_proto_rawDescData)
+	})
+	return file_proto_authentication_proto_rawDescData
+}
+
+var file_proto_authentication_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
+var file_proto_authentication_proto_goTypes = []interface{}{
+	(*AuthenticationRequest)(nil),  // 0: authentication_service.AuthenticationRequest
+	(*AuthenticationResponse)(nil), // 1: authentication_service.AuthenticationResponse
+}
+var file_proto_authentication_proto_depIdxs = []int32{
+	0, // 0: authentication_service.AuthenticationService.GetAuthentication:input_type -> authentication_service.AuthenticationRequest
+	1, // 1: authentication_service.AuthenticationService.GetAuthentication:output_type -> authentication_service.AuthenticationResponse
+	1, // [1:2] is the sub-list for method output_type
+	0, // [0:1] is the sub-list for method input_type
+	0, // [0:0] is the sub-list for extension type_name
+	0, // [0:0] is the sub-list for extension extendee
+	0, // [0:0] is the sub-list for field type_name
+}
+
+func init() { file_proto_authentication_proto_init() }
+func file_proto_authentication_proto_init() {
+	if File_proto_authentication_proto != nil {
+		return
+	}
+	if !protoimpl.UnsafeEnabled {
+		file_proto_authentication_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
+			switch v := v.(*AuthenticationRequest); i {
+			case 0:
+				return &v.state
+			case 1:
+				return &v.sizeCache
+			case 2:
+				return &v.unknownFields
+			default:
+				return nil
+			}
+		}
+		file_proto_authentication_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
+			switch v := v.(*AuthenticationResponse); i {
+			case 0:
+				return &v.state
+			case 1:
+				return &v.sizeCache
+			case 2:
+				return &v.unknownFields
+			default:
+				return nil
+			}
+		}
+	}
+	type x struct{}
+	out := protoimpl.TypeBuilder{
+		File: protoimpl.DescBuilder{
+			GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
+			RawDescriptor: file_proto_authentication_proto_rawDesc,
+			NumEnums:      0,
+			NumMessages:   2,
+			NumExtensions: 0,
+			NumServices:   1,
+		},
+		GoTypes:           file_proto_authentication_proto_goTypes,
+		DependencyIndexes: file_proto_authentication_proto_depIdxs,
+		MessageInfos:      file_proto_authentication_proto_msgTypes,
+	}.Build()
+	File_proto_authentication_proto = out.File
+	file_proto_authentication_proto_rawDesc = nil
+	file_proto_authentication_proto_goTypes = nil
+	file_proto_authentication_proto_depIdxs = nil
+}

+ 87 - 0
proto/authentication.pb.micro.go

@@ -0,0 +1,87 @@
+// Code generated by protoc-gen-micro. DO NOT EDIT.
+// source: proto/authentication.proto
+
+package authorization_service
+
+import (
+	fmt "fmt"
+	proto "google.golang.org/protobuf/proto"
+	math "math"
+)
+
+import (
+	context "context"
+	api "go-micro.dev/v4/api"
+	client "go-micro.dev/v4/client"
+	server "go-micro.dev/v4/server"
+)
+
+// Reference imports to suppress errors if they are not otherwise used.
+var _ = proto.Marshal
+var _ = fmt.Errorf
+var _ = math.Inf
+
+// Reference imports to suppress errors if they are not otherwise used.
+var _ api.Endpoint
+var _ context.Context
+var _ client.Option
+var _ server.Option
+
+// Api Endpoints for AuthenticationService service
+
+func NewAuthenticationServiceEndpoints() []*api.Endpoint {
+	return []*api.Endpoint{}
+}
+
+// Client API for AuthenticationService service
+
+type AuthenticationService interface {
+	GetAuthentication(ctx context.Context, in *AuthenticationRequest, opts ...client.CallOption) (*AuthenticationResponse, error)
+}
+
+type authenticationService struct {
+	c    client.Client
+	name string
+}
+
+func NewAuthenticationService(name string, c client.Client) AuthenticationService {
+	return &authenticationService{
+		c:    c,
+		name: name,
+	}
+}
+
+func (c *authenticationService) GetAuthentication(ctx context.Context, in *AuthenticationRequest, opts ...client.CallOption) (*AuthenticationResponse, error) {
+	req := c.c.NewRequest(c.name, "AuthenticationService.GetAuthentication", in)
+	out := new(AuthenticationResponse)
+	err := c.c.Call(ctx, req, out, opts...)
+	if err != nil {
+		return nil, err
+	}
+	return out, nil
+}
+
+// Server API for AuthenticationService service
+
+type AuthenticationServiceHandler interface {
+	GetAuthentication(context.Context, *AuthenticationRequest, *AuthenticationResponse) error
+}
+
+func RegisterAuthenticationServiceHandler(s server.Server, hdlr AuthenticationServiceHandler, opts ...server.HandlerOption) error {
+	type authenticationService interface {
+		GetAuthentication(ctx context.Context, in *AuthenticationRequest, out *AuthenticationResponse) error
+	}
+	type AuthenticationService struct {
+		authenticationService
+	}
+	h := &authenticationServiceHandler{hdlr}
+	return s.Handle(s.NewHandler(&AuthenticationService{h}, opts...))
+}
+
+type authenticationServiceHandler struct {
+	AuthenticationServiceHandler
+}
+
+func (h *authenticationServiceHandler) GetAuthentication(ctx context.Context, in *AuthenticationRequest, out *AuthenticationResponse) error {
+	return h.AuthenticationServiceHandler.GetAuthentication(ctx, in, out)
+}

+ 14 - 0
proto/authentication.proto

@@ -0,0 +1,14 @@
+
+syntax = "proto3";
+
+package authentication_service;
+
+
+
+option go_package = "./proto;authorization_service";
+
+service AuthenticationService {
+  rpc GetAuthentication(AuthenticationRequest) returns(AuthenticationResponse);
+}
+message AuthenticationRequest {}
+message AuthenticationResponse {}

+ 6 - 7
test/admin_role/admin_role_test.go

@@ -37,8 +37,8 @@ func TestAdminLogin(t *testing.T) {
 	newService := service.NewService(repo)
 	t.Run("创建角色", func(t *testing.T) {
 		roleRequest := &pb.CreateAdminRoleRequest{
-			Name:        "super_admin",
-			Description: "超级管理员",
+			Name:        "admin",
+			Description: "管理员",
 		}
 		err = newService.CreateAdminRole(context.Background(), roleRequest)
 		fmt.Println(err)
@@ -92,12 +92,11 @@ func TestAdminLogin(t *testing.T) {
 		// fmt.Println(role)
 	})
 	t.Run("授权角色", func(t *testing.T) {
-		users := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
-		var permissions []int64
+		users := []int64{1}
+		// var permissions []int64
 		err2 := newService.UpdateAdminRole(&pb.UpdateAdminRoleRequest{
-			RoleId:      10,
-			Users:       users,
-			Permissions: permissions,
+			RoleId: 1,
+			Users:  users,
 		})
 		fmt.Println(err2)
 	})

+ 14 - 2
test/adminuser/admin_user_test.go

@@ -68,7 +68,19 @@ func TestAdminLogin(t *testing.T) {
 		// })
 	})
 	t.Run("创建账号", func(t *testing.T) {
-		// password, _ := hashPassword("123456")
+		// Username:    adminUser.Username,
+		// 	Password:    password,
+		// 		PhoneNumber: adminUser.PhoneNumber,
+		// 		Email:       adminUser.Email,
+		// 		Avatar:      adminUser.Avatar,
+
+		err2 := newService.CreateAdminUser(&pb.CreateAdminUserRequest{
+			Username:    "admin",
+			Password:    "123456",
+			PhoneNumber: "13900000000",
+			Email:       "sghblog@163.com",
+			Avatar:      "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
+		})
 		// request := &authorization_service.CreateAdminUserRequest{
 		// 	Username: "admin",
 		// 	Password: password,
@@ -78,7 +90,7 @@ func TestAdminLogin(t *testing.T) {
 		// }
 		// 调用 AdminLogin 方法
 		// err = newService.CreateAdminUser(request)
-		fmt.Println(err)
+		fmt.Println(err2)
 	})
 	t.Run("账号关联角色", func(t *testing.T) {
 		// request := &authorization_service.AssociateAdminUserWithRoleRequest{