satellite/console: auth API error handling refactored (#3540)

This commit is contained in:
Nikolai Siedov 2019-11-12 13:53:00 +02:00 committed by GitHub
parent 7355065dc9
commit c1ae8c332f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -58,18 +58,13 @@ func (a *Auth) Token(w http.ResponseWriter, r *http.Request) {
err = json.NewDecoder(r.Body).Decode(&tokenRequest)
if err != nil {
a.serveJSONError(w, http.StatusBadRequest, err)
a.serveJSONError(w, err)
return
}
token, err := a.service.Token(ctx, tokenRequest.Email, tokenRequest.Password)
if err != nil {
if console.ErrConsoleInternal.Has(err) {
a.serveJSONError(w, http.StatusInternalServerError, err)
return
}
a.serveJSONError(w, http.StatusUnauthorized, err)
a.serveJSONError(w, err)
return
}
@ -98,13 +93,13 @@ func (a *Auth) Register(w http.ResponseWriter, r *http.Request) {
err = json.NewDecoder(r.Body).Decode(&registerData)
if err != nil {
a.serveJSONError(w, http.StatusBadRequest, err)
a.serveJSONError(w, err)
return
}
secret, err := console.RegistrationSecretFromBase64(registerData.SecretInput)
if err != nil {
a.serveJSONError(w, http.StatusBadRequest, err)
a.serveJSONError(w, err)
return
}
@ -120,18 +115,12 @@ func (a *Auth) Register(w http.ResponseWriter, r *http.Request) {
registerData.ReferrerUserID,
)
if err != nil {
if console.ErrConsoleInternal.Has(err) {
a.serveJSONError(w, http.StatusInternalServerError, err)
return
}
a.serveJSONError(w, http.StatusBadRequest, err)
return
a.serveJSONError(w, err)
}
token, err := a.service.GenerateActivationToken(ctx, user.ID, user.Email)
if err != nil {
a.serveJSONError(w, http.StatusInternalServerError, err)
a.serveJSONError(w, err)
return
}
@ -170,13 +159,12 @@ func (a *Auth) UpdateAccount(w http.ResponseWriter, r *http.Request) {
err = json.NewDecoder(r.Body).Decode(&updatedInfo)
if err != nil {
a.serveJSONError(w, http.StatusBadRequest, err)
a.serveJSONError(w, err)
return
}
if err = a.service.UpdateAccount(ctx, updatedInfo.FullName, updatedInfo.ShortName); err != nil {
a.log.Error("failed to write json error response", zap.Error(ErrAuthAPI.Wrap(err)))
return
a.serveJSONError(w, err)
}
}
@ -196,7 +184,7 @@ func (a *Auth) GetAccount(w http.ResponseWriter, r *http.Request) {
auth, err := console.GetAuth(ctx)
if err != nil {
a.serveJSONError(w, http.StatusUnauthorized, err)
a.serveJSONError(w, err)
return
}
@ -225,19 +213,13 @@ func (a *Auth) DeleteAccount(w http.ResponseWriter, r *http.Request) {
err = json.NewDecoder(r.Body).Decode(&deleteRequest)
if err != nil {
a.serveJSONError(w, http.StatusBadRequest, err)
a.serveJSONError(w, err)
return
}
err = a.service.DeleteAccount(ctx, deleteRequest.Password)
if err != nil {
if console.ErrUnauthorized.Has(err) {
a.serveJSONError(w, http.StatusUnauthorized, err)
return
}
a.serveJSONError(w, http.StatusInternalServerError, err)
return
a.serveJSONError(w, err)
}
}
@ -254,23 +236,13 @@ func (a *Auth) ChangePassword(w http.ResponseWriter, r *http.Request) {
err = json.NewDecoder(r.Body).Decode(&passwordChange)
if err != nil {
a.serveJSONError(w, http.StatusBadRequest, err)
a.serveJSONError(w, err)
return
}
err = a.service.ChangePassword(ctx, passwordChange.CurrentPassword, passwordChange.NewPassword)
if err != nil {
if console.ErrConsoleInternal.Has(err) {
a.serveJSONError(w, http.StatusInternalServerError, err)
return
}
if console.ErrUnauthorized.Has(err) {
a.serveJSONError(w, http.StatusUnauthorized, err)
return
}
a.serveJSONError(w, http.StatusBadRequest, err)
a.serveJSONError(w, err)
return
}
}
@ -285,19 +257,19 @@ func (a *Auth) ForgotPassword(w http.ResponseWriter, r *http.Request) {
email, ok := params["email"]
if !ok {
err = errs.New("email expected")
a.serveJSONError(w, http.StatusBadRequest, err)
a.serveJSONError(w, err)
return
}
user, err := a.service.GetUserByEmail(ctx, email)
if err != nil {
a.serveJSONError(w, http.StatusInternalServerError, err)
a.serveJSONError(w, err)
return
}
recoveryToken, err := a.service.GeneratePasswordRecoveryToken(ctx, user.ID)
if err != nil {
a.serveJSONError(w, http.StatusInternalServerError, err)
a.serveJSONError(w, err)
return
}
@ -336,25 +308,25 @@ func (a *Auth) ResendEmail(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
id, ok := params["id"]
if !ok {
a.serveJSONError(w, http.StatusBadRequest, errs.New("id expected"))
a.serveJSONError(w, err)
return
}
userID, err := uuid.Parse(id)
if err != nil {
a.serveJSONError(w, http.StatusBadRequest, err)
a.serveJSONError(w, err)
return
}
user, err := a.service.GetUser(ctx, *userID)
if err != nil {
a.serveJSONError(w, http.StatusNotFound, err)
a.serveJSONError(w, err)
return
}
token, err := a.service.GenerateActivationToken(ctx, user.ID, user.Email)
if err != nil {
a.serveJSONError(w, http.StatusInternalServerError, err)
a.serveJSONError(w, err)
return
}
@ -380,8 +352,8 @@ func (a *Auth) ResendEmail(w http.ResponseWriter, r *http.Request) {
}
// serveJSONError writes JSON error to response output stream.
func (a *Auth) serveJSONError(w http.ResponseWriter, status int, err error) {
w.WriteHeader(status)
func (a *Auth) serveJSONError(w http.ResponseWriter, err error) {
w.WriteHeader(a.getStatusCode(err))
var response struct {
Error string `json:"error"`
@ -394,3 +366,15 @@ func (a *Auth) serveJSONError(w http.ResponseWriter, status int, err error) {
a.log.Error("failed to write json error response", zap.Error(ErrAuthAPI.Wrap(err)))
}
}
// getStatusCode returns http.StatusCode depends on console error class.
func (a *Auth) getStatusCode(err error) int {
switch {
case console.ErrValidation.Has(err):
return http.StatusBadRequest
case console.ErrUnauthorized.Has(err):
return http.StatusUnauthorized
default:
return http.StatusInternalServerError
}
}