From 32f6c20034677e51a591034ee18e65b7b371da4e Mon Sep 17 00:00:00 2001 From: SpeedReach <37238439+SpeedReach@users.noreply.github.com> Date: Sat, 1 Jun 2024 19:16:33 +0800 Subject: [PATCH] fix: return perm denied instead of internal error when login fail --- internal/services/auth/login.go | 9 +++--- protobuf/gen/go/groups_bill.pb.go | 16 +++++----- protobuf/gen/go/groups_bill.pb.gw.go | 44 +++++++++++++++------------- protobuf/gen/monify.swagger.json | 14 ++++----- protobuf/groups_bill.proto | 2 +- 5 files changed, 44 insertions(+), 41 deletions(-) diff --git a/internal/services/auth/login.go b/internal/services/auth/login.go index 9ab77a9..a8829e7 100644 --- a/internal/services/auth/login.go +++ b/internal/services/auth/login.go @@ -3,7 +3,6 @@ package auth import ( "context" "database/sql" - "errors" "github.com/golang-jwt/jwt/v5" "github.com/google/uuid" "go.uber.org/zap" @@ -16,6 +15,7 @@ import ( ) func matchEmailUser(ctx context.Context, email string, password string, db *sql.DB) (uuid.UUID, error) { + logger := ctx.Value(middlewares.LoggerContextKey{}).(*zap.Logger) query, err := db.QueryContext(ctx, ` SELECT user_id, password FROM email_login @@ -33,12 +33,13 @@ func matchEmailUser(ctx context.Context, email string, password string, db *sql. var hashedPassword string err = query.Scan(&userId, &hashedPassword) if err != nil { - return uuid.Nil, err + logger.Error("", zap.Error(err)) + return uuid.Nil, status.Error(codes.Internal, "internal err.") } err = bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password)) if err != nil { - return uuid.Nil, errors.New("incorrect password") + return uuid.Nil, status.Error(codes.PermissionDenied, "Password incorrect.") } return userId, nil @@ -66,7 +67,7 @@ func (s Service) EmailLogin(ctx context.Context, req *monify.EmailLoginRequest) userId, err := matchEmailUser(ctx, req.Email, req.Password, db) if err != nil { - return nil, status.Errorf(codes.Internal, "internal err.") + return nil, err } if userId == uuid.Nil { diff --git a/protobuf/gen/go/groups_bill.pb.go b/protobuf/gen/go/groups_bill.pb.go index 4d139dd..daba3d1 100644 --- a/protobuf/gen/go/groups_bill.pb.go +++ b/protobuf/gen/go/groups_bill.pb.go @@ -1173,7 +1173,7 @@ var file_groups_bill_proto_rawDesc = []byte{ 0x6d, 0x70, 0x2a, 0x3a, 0x0a, 0x14, 0x47, 0x72, 0x6f, 0x75, 0x70, 0x42, 0x69, 0x6c, 0x6c, 0x48, 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x54, 0x79, 0x70, 0x65, 0x12, 0x0a, 0x0a, 0x06, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x10, 0x00, 0x12, 0x0a, 0x0a, 0x06, 0x64, 0x65, 0x6c, 0x65, 0x74, 0x65, - 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x6d, 0x6f, 0x64, 0x69, 0x66, 0x79, 0x10, 0x02, 0x32, 0x8c, + 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x6d, 0x6f, 0x64, 0x69, 0x66, 0x79, 0x10, 0x02, 0x32, 0x97, 0x04, 0x0a, 0x11, 0x47, 0x72, 0x6f, 0x75, 0x70, 0x73, 0x42, 0x69, 0x6c, 0x6c, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x60, 0x0a, 0x0f, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x47, 0x72, 0x6f, 0x75, 0x70, 0x42, 0x69, 0x6c, 0x6c, 0x12, 0x17, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, @@ -1200,16 +1200,16 @@ var file_groups_bill_proto_rawDesc = []byte{ 0x6f, 0x75, 0x70, 0x47, 0x72, 0x6f, 0x75, 0x70, 0x42, 0x69, 0x6c, 0x6c, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x24, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x1e, 0x3a, 0x01, 0x2a, 0x1a, 0x19, 0x2f, 0x76, 0x31, 0x2f, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x73, 0x5f, 0x62, 0x69, 0x6c, 0x6c, 0x2f, 0x7b, 0x62, - 0x69, 0x6c, 0x6c, 0x5f, 0x69, 0x64, 0x7d, 0x12, 0x64, 0x0a, 0x0a, 0x47, 0x65, 0x74, 0x48, 0x69, + 0x69, 0x6c, 0x6c, 0x5f, 0x69, 0x64, 0x7d, 0x12, 0x6f, 0x0a, 0x0a, 0x47, 0x65, 0x74, 0x48, 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x12, 0x12, 0x2e, 0x47, 0x65, 0x74, 0x48, 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x47, 0x65, 0x74, 0x48, - 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x2d, - 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x27, 0x12, 0x25, 0x2f, 0x76, 0x31, 0x2f, 0x67, 0x72, 0x6f, 0x75, + 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x38, + 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x32, 0x12, 0x30, 0x2f, 0x76, 0x31, 0x2f, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x5f, 0x62, 0x69, 0x6c, 0x6c, 0x2f, 0x68, 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x2f, 0x7b, - 0x73, 0x6b, 0x69, 0x70, 0x7d, 0x2f, 0x7b, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x7d, 0x42, 0x1e, 0x5a, - 0x1c, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x53, 0x70, 0x65, 0x65, - 0x64, 0x52, 0x65, 0x61, 0x63, 0x68, 0x2f, 0x6d, 0x6f, 0x6e, 0x69, 0x66, 0x79, 0x62, 0x06, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x67, 0x72, 0x6f, 0x75, 0x70, 0x5f, 0x69, 0x64, 0x7d, 0x2f, 0x7b, 0x73, 0x6b, 0x69, 0x70, 0x7d, + 0x2f, 0x7b, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x7d, 0x42, 0x1e, 0x5a, 0x1c, 0x67, 0x69, 0x74, 0x68, + 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x53, 0x70, 0x65, 0x65, 0x64, 0x52, 0x65, 0x61, 0x63, + 0x68, 0x2f, 0x6d, 0x6f, 0x6e, 0x69, 0x66, 0x79, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/protobuf/gen/go/groups_bill.pb.gw.go b/protobuf/gen/go/groups_bill.pb.gw.go index bc71e05..c788243 100644 --- a/protobuf/gen/go/groups_bill.pb.gw.go +++ b/protobuf/gen/go/groups_bill.pb.gw.go @@ -221,10 +221,6 @@ func local_request_GroupsBillService_ModifyGroupBill_0(ctx context.Context, mars } -var ( - filter_GroupsBillService_GetHistory_0 = &utilities.DoubleArray{Encoding: map[string]int{"skip": 0, "limit": 1}, Base: []int{1, 1, 2, 0, 0}, Check: []int{0, 1, 1, 2, 3}} -) - func request_GroupsBillService_GetHistory_0(ctx context.Context, marshaler runtime.Marshaler, client GroupsBillServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq GetHistoryRequest var metadata runtime.ServerMetadata @@ -236,6 +232,16 @@ func request_GroupsBillService_GetHistory_0(ctx context.Context, marshaler runti _ = err ) + val, ok = pathParams["group_id"] + if !ok { + return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "group_id") + } + + protoReq.GroupId, err = runtime.String(val) + if err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "group_id", err) + } + val, ok = pathParams["skip"] if !ok { return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "skip") @@ -256,13 +262,6 @@ func request_GroupsBillService_GetHistory_0(ctx context.Context, marshaler runti return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "limit", err) } - if err := req.ParseForm(); err != nil { - return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) - } - if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_GroupsBillService_GetHistory_0); err != nil { - return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) - } - msg, err := client.GetHistory(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) return msg, metadata, err @@ -279,6 +278,16 @@ func local_request_GroupsBillService_GetHistory_0(ctx context.Context, marshaler _ = err ) + val, ok = pathParams["group_id"] + if !ok { + return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "group_id") + } + + protoReq.GroupId, err = runtime.String(val) + if err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "group_id", err) + } + val, ok = pathParams["skip"] if !ok { return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "skip") @@ -299,13 +308,6 @@ func local_request_GroupsBillService_GetHistory_0(ctx context.Context, marshaler return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "limit", err) } - if err := req.ParseForm(); err != nil { - return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) - } - if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_GroupsBillService_GetHistory_0); err != nil { - return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) - } - msg, err := server.GetHistory(ctx, &protoReq) return msg, metadata, err @@ -425,7 +427,7 @@ func RegisterGroupsBillServiceHandlerServer(ctx context.Context, mux *runtime.Se inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) var err error var annotatedContext context.Context - annotatedContext, err = runtime.AnnotateIncomingContext(ctx, mux, req, "/.GroupsBillService/GetHistory", runtime.WithHTTPPathPattern("/v1/group_bill/history/{skip}/{limit}")) + annotatedContext, err = runtime.AnnotateIncomingContext(ctx, mux, req, "/.GroupsBillService/GetHistory", runtime.WithHTTPPathPattern("/v1/group_bill/history/{group_id}/{skip}/{limit}")) if err != nil { runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) return @@ -577,7 +579,7 @@ func RegisterGroupsBillServiceHandlerClient(ctx context.Context, mux *runtime.Se inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) var err error var annotatedContext context.Context - annotatedContext, err = runtime.AnnotateContext(ctx, mux, req, "/.GroupsBillService/GetHistory", runtime.WithHTTPPathPattern("/v1/group_bill/history/{skip}/{limit}")) + annotatedContext, err = runtime.AnnotateContext(ctx, mux, req, "/.GroupsBillService/GetHistory", runtime.WithHTTPPathPattern("/v1/group_bill/history/{group_id}/{skip}/{limit}")) if err != nil { runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) return @@ -605,7 +607,7 @@ var ( pattern_GroupsBillService_ModifyGroupBill_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 1, 0, 4, 1, 5, 2}, []string{"v1", "groups_bill", "bill_id"}, "")) - pattern_GroupsBillService_GetHistory_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 1, 0, 4, 1, 5, 3, 1, 0, 4, 1, 5, 4}, []string{"v1", "group_bill", "history", "skip", "limit"}, "")) + pattern_GroupsBillService_GetHistory_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 1, 0, 4, 1, 5, 3, 1, 0, 4, 1, 5, 4, 1, 0, 4, 1, 5, 5}, []string{"v1", "group_bill", "history", "group_id", "skip", "limit"}, "")) ) var ( diff --git a/protobuf/gen/monify.swagger.json b/protobuf/gen/monify.swagger.json index bac6f0d..b725290 100644 --- a/protobuf/gen/monify.swagger.json +++ b/protobuf/gen/monify.swagger.json @@ -302,7 +302,7 @@ ] } }, - "/v1/group_bill/history/{skip}/{limit}": { + "/v1/group_bill/history/{groupId}/{skip}/{limit}": { "get": { "operationId": "GroupsBillService_GetHistory", "responses": { @@ -314,6 +314,12 @@ } }, "parameters": [ + { + "name": "groupId", + "in": "path", + "required": true, + "type": "string" + }, { "name": "skip", "in": "path", @@ -327,12 +333,6 @@ "required": true, "type": "integer", "format": "int32" - }, - { - "name": "groupId", - "in": "query", - "required": false, - "type": "string" } ], "tags": [ diff --git a/protobuf/groups_bill.proto b/protobuf/groups_bill.proto index ac37181..6b2b6ac 100644 --- a/protobuf/groups_bill.proto +++ b/protobuf/groups_bill.proto @@ -35,7 +35,7 @@ service GroupsBillService{ rpc GetHistory(GetHistoryRequest) returns (GetHistoryResponse) { option (google.api.http) = { - get: "/v1/group_bill/history/{skip}/{limit}" + get: "/v1/group_bill/history/{group_id}/{skip}/{limit}" }; } }