From a4744ab7f409a5a68dada25e6b6df6a2cec90ad8 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Mon, 13 May 2019 14:23:41 +0200 Subject: [PATCH] gateway: return right error when remotesource login fails If the remote source username/password based login fails return the right error code: 401 (unauthorized) on wrong username/password or a 500 on other errors. --- internal/services/gateway/action/user.go | 3 +++ internal/services/gateway/api/api.go | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/internal/services/gateway/action/user.go b/internal/services/gateway/action/user.go index a585169..658ea86 100644 --- a/internal/services/gateway/action/user.go +++ b/internal/services/gateway/action/user.go @@ -566,6 +566,9 @@ func (h *ActionHandler) HandleRemoteSourceAuth(ctx context.Context, remoteSource tokenName := "agola-" + h.agolaID accessToken, err := passwordSource.LoginPassword(loginName, loginPassword, tokenName) if err != nil { + if err == gitsource.ErrUnauthorized { + return nil, util.NewErrUnauthorized(errors.Wrapf(err, "failed to login to remotesource %q", remoteSourceName)) + } return nil, errors.Wrapf(err, "failed to login to remote source %q with login name %q", rs.Name, loginName) } h.log.Infof("access token: %s", accessToken) diff --git a/internal/services/gateway/api/api.go b/internal/services/gateway/api/api.go index 41e1018..0722944 100644 --- a/internal/services/gateway/api/api.go +++ b/internal/services/gateway/api/api.go @@ -36,6 +36,8 @@ func ErrorResponseFromError(err error) *ErrorResponse { case util.IsErrNotFound(err): fallthrough case util.IsErrForbidden(err): + fallthrough + case util.IsErrUnauthorized(err): return &ErrorResponse{Message: err.Error()} } @@ -64,6 +66,9 @@ func httpError(w http.ResponseWriter, err error) bool { case util.IsErrForbidden(err): w.WriteHeader(http.StatusForbidden) w.Write(resj) + case util.IsErrUnauthorized(err): + w.WriteHeader(http.StatusUnauthorized) + w.Write(resj) default: w.WriteHeader(http.StatusInternalServerError) w.Write(resj)