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.
This commit is contained in:
Simone Gotti 2019-05-13 14:23:41 +02:00
parent a7ecfee795
commit a4744ab7f4
2 changed files with 8 additions and 0 deletions

View File

@ -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)

View File

@ -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)