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:
parent
a7ecfee795
commit
a4744ab7f4
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue