Skip to content

Commit

Permalink
update error message on auth failure
Browse files Browse the repository at this point in the history
  • Loading branch information
v9n committed Dec 12, 2024
1 parent 3764c9f commit 89cca78
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
18 changes: 9 additions & 9 deletions aggregator/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (r *RpcServer) GetKey(ctx context.Context, payload *avsproto.GetKeyReq) (*a
}

if !authenticated {
return nil, auth.ErrorUnAuthorized
return nil, status.Errorf(codes.Unauthenticated, 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
Expand All @@ -66,10 +66,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)
}
}

Expand All @@ -83,7 +83,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{
Expand Down Expand Up @@ -114,7 +114,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
Expand All @@ -123,16 +123,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{
Expand All @@ -155,7 +155,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
Expand Down
5 changes: 5 additions & 0 deletions aggregator/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package aggregator

const (
InternalError = "Internal Error"
)
16 changes: 8 additions & 8 deletions aggregator/rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion core/auth/errors.go
Original file line number Diff line number Diff line change
@@ -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"
)

0 comments on commit 89cca78

Please sign in to comment.