diff --git a/aggregator/auth.go b/aggregator/auth.go index 931555d..fe11c7a 100644 --- a/aggregator/auth.go +++ b/aggregator/auth.go @@ -36,12 +36,8 @@ func (r *RpcServer) GetKey(ctx context.Context, payload *avsproto.GetKeyReq) (*a if strings.Contains(payload.Signature, ".") { authenticated, err := auth.VerifyJwtKeyForUser(r.config.JwtSecret, payload.Signature, submitAddress) - if err != nil { - return nil, err - } - - if !authenticated { - return nil, auth.ErrorUnAuthorized + if err != nil || !authenticated { + return nil, status.Errorf(codes.Unauthenticated, "%s: %s", auth.AuthenticationError, auth.InvalidAPIKey) } } else { // We need to have 3 things to verify the signature: the signature, the hash of the original data, and the public key of the signer. With this information we can determine if the private key holder of the public key pair did indeed sign the message @@ -66,10 +62,10 @@ func (r *RpcServer) GetKey(ctx context.Context, payload *avsproto.GetKeyReq) (*a sigPublicKey, err := crypto.SigToPub(hash, signature) recoveredAddr := crypto.PubkeyToAddress(*sigPublicKey) if err != nil { - return nil, err + return nil, status.Errorf(codes.Unauthenticated, auth.InvalidAuthenticationKey) } if submitAddress.String() != recoveredAddr.String() { - return nil, fmt.Errorf("Invalid signature") + return nil, status.Errorf(codes.Unauthenticated, auth.InvalidAuthenticationKey) } } @@ -83,7 +79,7 @@ func (r *RpcServer) GetKey(ctx context.Context, payload *avsproto.GetKeyReq) (*a ss, err := token.SignedString(r.config.JwtSecret) if err != nil { - return nil, err + return nil, status.Errorf(codes.Internal, InternalError) } return &avsproto.KeyResp{ @@ -114,7 +110,7 @@ func (r *RpcServer) verifyAuth(ctx context.Context) (*model.User, error) { token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { // Don't forget to validate the alg is what you expect: if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { - return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"]) + return nil, fmt.Errorf("%s", auth.InvalidAuthenticationKey) } // hmacSampleSecret is a []byte containing your @@ -123,16 +119,16 @@ func (r *RpcServer) verifyAuth(ctx context.Context) (*model.User, error) { }) if err != nil { - return nil, err + return nil, fmt.Errorf("%s", auth.InvalidAuthenticationKey) } if token.Header["alg"] != auth.JwtAlg { - return nil, fmt.Errorf("invalid signing algorithm") + return nil, fmt.Errorf("%s", auth.InvalidAuthenticationKey) } if claims, ok := token.Claims.(jwt.MapClaims); ok { if claims["sub"] == "" { - return nil, fmt.Errorf("Missing subject") + return nil, fmt.Errorf("%s", auth.InvalidAuthenticationKey) } user := model.User{ @@ -155,7 +151,7 @@ func (r *RpcServer) verifyAuth(ctx context.Context) (*model.User, error) { return &user, nil } - return nil, fmt.Errorf("Malform claims") + return nil, fmt.Errorf("%s", auth.InvalidAuthenticationKey) } // verifyOperator checks validity of the signature submit by operator related request diff --git a/aggregator/errors.go b/aggregator/errors.go new file mode 100644 index 0000000..1d58384 --- /dev/null +++ b/aggregator/errors.go @@ -0,0 +1,5 @@ +package aggregator + +const ( + InternalError = "Internal Error" +) diff --git a/aggregator/rpc_server.go b/aggregator/rpc_server.go index bd90e41..f728b0d 100644 --- a/aggregator/rpc_server.go +++ b/aggregator/rpc_server.go @@ -48,7 +48,7 @@ func (r *RpcServer) GetWallet(ctx context.Context, payload *avsproto.GetWalletRe user, err := r.verifyAuth(ctx) if err != nil { - return nil, status.Errorf(codes.Unauthenticated, "%s: %s", auth.InvalidAuthenticationKey, err.Error()) + return nil, status.Errorf(codes.Unauthenticated, "%s: %s", auth.AuthenticationError, err.Error()) } r.config.Logger.Info("process create wallet", "user", user.Address.String(), @@ -76,7 +76,7 @@ func (r *RpcServer) GetNonce(ctx context.Context, payload *avsproto.NonceRequest func (r *RpcServer) ListWallets(ctx context.Context, payload *avsproto.ListWalletReq) (*avsproto.ListWalletResp, error) { user, err := r.verifyAuth(ctx) if err != nil { - return nil, status.Errorf(codes.Unauthenticated, "%s: %s", auth.InvalidAuthenticationKey, err.Error()) + return nil, status.Errorf(codes.Unauthenticated, "%s: %s", auth.AuthenticationError, err.Error()) } r.config.Logger.Info("process list wallet", @@ -95,7 +95,7 @@ func (r *RpcServer) ListWallets(ctx context.Context, payload *avsproto.ListWalle func (r *RpcServer) CancelTask(ctx context.Context, taskID *avsproto.IdReq) (*wrapperspb.BoolValue, error) { user, err := r.verifyAuth(ctx) if err != nil { - return nil, status.Errorf(codes.Unauthenticated, "%s: %s", auth.InvalidAuthenticationKey, err.Error()) + return nil, status.Errorf(codes.Unauthenticated, "%s: %s", auth.AuthenticationError, err.Error()) } r.config.Logger.Info("process cancel task", @@ -115,7 +115,7 @@ func (r *RpcServer) CancelTask(ctx context.Context, taskID *avsproto.IdReq) (*wr func (r *RpcServer) DeleteTask(ctx context.Context, taskID *avsproto.IdReq) (*wrapperspb.BoolValue, error) { user, err := r.verifyAuth(ctx) if err != nil { - return nil, status.Errorf(codes.Unauthenticated, "%s: %s", auth.InvalidAuthenticationKey, err.Error()) + return nil, status.Errorf(codes.Unauthenticated, "%s: %s", auth.AuthenticationError, err.Error()) } r.config.Logger.Info("process delete task", @@ -151,7 +151,7 @@ func (r *RpcServer) CreateTask(ctx context.Context, taskPayload *avsproto.Create func (r *RpcServer) ListTasks(ctx context.Context, payload *avsproto.ListTasksReq) (*avsproto.ListTasksResp, error) { user, err := r.verifyAuth(ctx) if err != nil { - return nil, status.Errorf(codes.Unauthenticated, "%s: %s", auth.InvalidAuthenticationKey, err.Error()) + return nil, status.Errorf(codes.Unauthenticated, "%s: %s", auth.AuthenticationError, err.Error()) } r.config.Logger.Info("process list task", @@ -164,7 +164,7 @@ func (r *RpcServer) ListTasks(ctx context.Context, payload *avsproto.ListTasksRe func (r *RpcServer) ListExecutions(ctx context.Context, payload *avsproto.ListExecutionsReq) (*avsproto.ListExecutionsResp, error) { user, err := r.verifyAuth(ctx) if err != nil { - return nil, status.Errorf(codes.Unauthenticated, "%s: %s", auth.InvalidAuthenticationKey, err.Error()) + return nil, status.Errorf(codes.Unauthenticated, "%s: %s", auth.AuthenticationError, err.Error()) } r.config.Logger.Info("process list execution", @@ -177,7 +177,7 @@ func (r *RpcServer) ListExecutions(ctx context.Context, payload *avsproto.ListEx func (r *RpcServer) GetTask(ctx context.Context, payload *avsproto.IdReq) (*avsproto.Task, error) { user, err := r.verifyAuth(ctx) if err != nil { - return nil, status.Errorf(codes.Unauthenticated, "%s: %s", auth.InvalidAuthenticationKey, err.Error()) + return nil, status.Errorf(codes.Unauthenticated, "%s: %s", auth.AuthenticationError, err.Error()) } r.config.Logger.Info("process get task", @@ -202,7 +202,7 @@ func (r *RpcServer) GetTask(ctx context.Context, payload *avsproto.IdReq) (*avsp func (r *RpcServer) TriggerTask(ctx context.Context, payload *avsproto.UserTriggerTaskReq) (*avsproto.UserTriggerTaskResp, error) { user, err := r.verifyAuth(ctx) if err != nil { - return nil, status.Errorf(codes.Unauthenticated, "%s: %s", auth.InvalidAuthenticationKey, err.Error()) + return nil, status.Errorf(codes.Unauthenticated, "%s: %s", auth.AuthenticationError, err.Error()) } r.config.Logger.Info("process trigger task", diff --git a/core/auth/errors.go b/core/auth/errors.go index 6a2ac62..d6197aa 100644 --- a/core/auth/errors.go +++ b/core/auth/errors.go @@ -1,6 +1,8 @@ package auth const ( - InvalidAuthenticationKey = "Invalid authentication key" + AuthenticationError = "User authentication error" InvalidSignatureFormat = "Invalid Signature Format" + InvalidAuthenticationKey = "User Auth key is invalid" + InvalidAPIKey = "API key is invalid" )