Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update error message on auth failure #60

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions aggregator/auth.go
Original file line number Diff line number Diff line change
@@ -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
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
@@ -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",
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"
)
Loading