diff --git a/pkg/authkit/third_client.go b/pkg/authkit/third_client.go index 3e230e8..7b584e0 100644 --- a/pkg/authkit/third_client.go +++ b/pkg/authkit/third_client.go @@ -17,8 +17,35 @@ func GetThirdClient(nx *nex.Conn, id uint, secret *string) (*models.ThirdClient, } resp, err := proto.NewThirdClientServiceClient(conn). GetThirdClient(context.Background(), &proto.GetThirdClientRequest{ - ClientId: uint64(id), - Secret: secret, + Id: lo.ToPtr(uint64(id)), + Secret: secret, + }) + if err != nil { + return nil, err + } + + return &models.ThirdClient{ + Alias: resp.GetInfo().GetAlias(), + Name: resp.GetInfo().GetName(), + Description: resp.GetInfo().GetDescription(), + IsDraft: resp.GetInfo().GetIsDraft(), + AccountID: lo.TernaryF(resp.GetInfo().AccountId != nil, func() *uint { + return lo.ToPtr(uint(resp.GetInfo().GetAccountId())) + }, func() *uint { + return nil + }), + }, nil +} + +func GetThirdClientByAlias(nx *nex.Conn, alias string, secret *string) (*models.ThirdClient, error) { + conn, err := nx.GetClientGrpcConn(nex.ServiceTypeAuth) + if err != nil { + return nil, fmt.Errorf("failed to get auth service client: %v", err) + } + resp, err := proto.NewThirdClientServiceClient(conn). + GetThirdClient(context.Background(), &proto.GetThirdClientRequest{ + Alias: &alias, + Secret: secret, }) if err != nil { return nil, err diff --git a/pkg/internal/grpc/thrid_client.go b/pkg/internal/grpc/third_client.go similarity index 64% rename from pkg/internal/grpc/thrid_client.go rename to pkg/internal/grpc/third_client.go index 8b70d88..a4cba66 100644 --- a/pkg/internal/grpc/thrid_client.go +++ b/pkg/internal/grpc/third_client.go @@ -2,6 +2,7 @@ package grpc import ( "context" + "git.solsynth.dev/hypernet/passport/pkg/authkit/models" "git.solsynth.dev/hypernet/passport/pkg/internal/database" "git.solsynth.dev/hypernet/passport/pkg/proto" @@ -10,9 +11,19 @@ import ( ) func (v *App) GetThirdClient(ctx context.Context, request *proto.GetThirdClientRequest) (*proto.GetThirdClientResponse, error) { + tx := database.C + if request.Id == nil && request.Alias == nil { + return nil, status.Error(codes.InvalidArgument, "either id or alias must be specified") + } + if request.Id != nil { + tx = tx.Where("id = ?", request.Id) + } else if request.Alias != nil { + tx = tx.Where("alias = ?", request.Alias) + } + var client models.ThirdClient - if err := database.C.Where("id = ?", request.ClientId).First(&client).Error; err != nil { - return nil, status.Errorf(codes.NotFound, "requested client with id %d was not found", request.ClientId) + if err := tx.First(&client).Error; err != nil { + return nil, status.Errorf(codes.NotFound, "requested client was not found") } if request.Secret != nil { diff --git a/pkg/proto/third_client.pb.go b/pkg/proto/third_client.pb.go index df4cc3f..15e31d9 100644 --- a/pkg/proto/third_client.pb.go +++ b/pkg/proto/third_client.pb.go @@ -110,8 +110,9 @@ type GetThirdClientRequest struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - ClientId uint64 `protobuf:"varint,1,opt,name=client_id,json=clientId,proto3" json:"client_id,omitempty"` - Secret *string `protobuf:"bytes,2,opt,name=secret,proto3,oneof" json:"secret,omitempty"` + Id *uint64 `protobuf:"varint,1,opt,name=id,proto3,oneof" json:"id,omitempty"` + Alias *string `protobuf:"bytes,2,opt,name=alias,proto3,oneof" json:"alias,omitempty"` + Secret *string `protobuf:"bytes,3,opt,name=secret,proto3,oneof" json:"secret,omitempty"` } func (x *GetThirdClientRequest) Reset() { @@ -144,13 +145,20 @@ func (*GetThirdClientRequest) Descriptor() ([]byte, []int) { return file_third_client_proto_rawDescGZIP(), []int{1} } -func (x *GetThirdClientRequest) GetClientId() uint64 { - if x != nil { - return x.ClientId +func (x *GetThirdClientRequest) GetId() uint64 { + if x != nil && x.Id != nil { + return *x.Id } return 0 } +func (x *GetThirdClientRequest) GetAlias() string { + if x != nil && x.Alias != nil { + return *x.Alias + } + return "" +} + func (x *GetThirdClientRequest) GetSecret() string { if x != nil && x.Secret != nil { return *x.Secret @@ -219,25 +227,27 @@ var file_third_client_proto_rawDesc = []byte{ 0x73, 0x44, 0x72, 0x61, 0x66, 0x74, 0x12, 0x22, 0x0a, 0x0a, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x5f, 0x69, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x04, 0x48, 0x00, 0x52, 0x09, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x49, 0x64, 0x88, 0x01, 0x01, 0x42, 0x0d, 0x0a, 0x0b, 0x5f, 0x61, - 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x5f, 0x69, 0x64, 0x22, 0x5c, 0x0a, 0x15, 0x47, 0x65, 0x74, - 0x54, 0x68, 0x69, 0x72, 0x64, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, - 0x73, 0x74, 0x12, 0x1b, 0x0a, 0x09, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x69, 0x64, 0x18, - 0x01, 0x20, 0x01, 0x28, 0x04, 0x52, 0x08, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64, 0x12, - 0x1b, 0x0a, 0x06, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x48, - 0x00, 0x52, 0x06, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x88, 0x01, 0x01, 0x42, 0x09, 0x0a, 0x07, - 0x5f, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x22, 0x44, 0x0a, 0x16, 0x47, 0x65, 0x74, 0x54, 0x68, - 0x69, 0x72, 0x64, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, - 0x65, 0x12, 0x2a, 0x0a, 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, - 0x16, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x54, 0x68, 0x69, 0x72, 0x64, 0x43, 0x6c, 0x69, - 0x65, 0x6e, 0x74, 0x49, 0x6e, 0x66, 0x6f, 0x52, 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x32, 0x65, 0x0a, - 0x12, 0x54, 0x68, 0x69, 0x72, 0x64, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x53, 0x65, 0x72, 0x76, - 0x69, 0x63, 0x65, 0x12, 0x4f, 0x0a, 0x0e, 0x47, 0x65, 0x74, 0x54, 0x68, 0x69, 0x72, 0x64, 0x43, - 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x12, 0x1c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x47, 0x65, + 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x5f, 0x69, 0x64, 0x22, 0x80, 0x01, 0x0a, 0x15, 0x47, 0x65, 0x74, 0x54, 0x68, 0x69, 0x72, 0x64, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x71, 0x75, - 0x65, 0x73, 0x74, 0x1a, 0x1d, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x47, 0x65, 0x74, 0x54, - 0x68, 0x69, 0x72, 0x64, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, - 0x73, 0x65, 0x22, 0x00, 0x42, 0x09, 0x5a, 0x07, 0x2e, 0x3b, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, - 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x65, 0x73, 0x74, 0x12, 0x13, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04, 0x48, + 0x00, 0x52, 0x02, 0x69, 0x64, 0x88, 0x01, 0x01, 0x12, 0x19, 0x0a, 0x05, 0x61, 0x6c, 0x69, 0x61, + 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x48, 0x01, 0x52, 0x05, 0x61, 0x6c, 0x69, 0x61, 0x73, + 0x88, 0x01, 0x01, 0x12, 0x1b, 0x0a, 0x06, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x18, 0x03, 0x20, + 0x01, 0x28, 0x09, 0x48, 0x02, 0x52, 0x06, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x88, 0x01, 0x01, + 0x42, 0x05, 0x0a, 0x03, 0x5f, 0x69, 0x64, 0x42, 0x08, 0x0a, 0x06, 0x5f, 0x61, 0x6c, 0x69, 0x61, + 0x73, 0x42, 0x09, 0x0a, 0x07, 0x5f, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x22, 0x44, 0x0a, 0x16, + 0x47, 0x65, 0x74, 0x54, 0x68, 0x69, 0x72, 0x64, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x2a, 0x0a, 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x54, 0x68, 0x69, + 0x72, 0x64, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x6e, 0x66, 0x6f, 0x52, 0x04, 0x69, 0x6e, + 0x66, 0x6f, 0x32, 0x65, 0x0a, 0x12, 0x54, 0x68, 0x69, 0x72, 0x64, 0x43, 0x6c, 0x69, 0x65, 0x6e, + 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x4f, 0x0a, 0x0e, 0x47, 0x65, 0x74, 0x54, + 0x68, 0x69, 0x72, 0x64, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x12, 0x1c, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x2e, 0x47, 0x65, 0x74, 0x54, 0x68, 0x69, 0x72, 0x64, 0x43, 0x6c, 0x69, 0x65, 0x6e, + 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1d, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x2e, 0x47, 0x65, 0x74, 0x54, 0x68, 0x69, 0x72, 0x64, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x52, + 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x09, 0x5a, 0x07, 0x2e, 0x3b, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/pkg/proto/third_client.proto b/pkg/proto/third_client.proto index 1b33e73..5b7336a 100644 --- a/pkg/proto/third_client.proto +++ b/pkg/proto/third_client.proto @@ -18,8 +18,9 @@ message ThirdClientInfo { } message GetThirdClientRequest { - uint64 client_id = 1; - optional string secret = 2; + optional uint64 id = 1; + optional string alias = 2; + optional string secret = 3; } message GetThirdClientResponse {