onasty/internal/transport/http/apiv1/response.go (view raw)
Smirnov Oleksandr
Smirnov Oleksandr
ss2316544@gmail.com feat(jwt): handle tokens with invalid signature (#133), 11 months ago
ss2316544@gmail.com feat(jwt): handle tokens with invalid signature (#133), 11 months ago
| 1 | package apiv1 |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "log/slog" |
| 6 | "net/http" |
| 7 | |
| 8 | "github.com/gin-gonic/gin" |
| 9 | "github.com/olexsmir/onasty/internal/jwtutil" |
| 10 | "github.com/olexsmir/onasty/internal/models" |
| 11 | "github.com/olexsmir/onasty/internal/service/notesrv" |
| 12 | "github.com/olexsmir/onasty/internal/service/usersrv" |
| 13 | ) |
| 14 | |
| 15 | var ErrUnauthorized = errors.New("unauthorized") |
| 16 | |
| 17 | type response struct { |
| 18 | Message string `json:"message"` |
| 19 | } |
| 20 | |
| 21 | func errorResponse(c *gin.Context, err error) { |
| 22 | if errors.Is(err, usersrv.ErrProviderNotSupported) || |
| 23 | errors.Is(err, models.ErrResetPasswordTokenAlreadyUsed) || |
| 24 | errors.Is(err, models.ErrResetPasswordTokenExpired) || |
| 25 | errors.Is(err, models.ErrUserEmailIsAlreadyInUse) || |
| 26 | errors.Is(err, models.ErrUserIsAlreadyVerified) || |
| 27 | errors.Is(err, models.ErrUserIsNotActivated) || |
| 28 | errors.Is(err, models.ErrUserInvalidEmail) || |
| 29 | errors.Is(err, models.ErrUserInvalidPassword) || |
| 30 | errors.Is(err, models.ErrUserNotFound) || |
| 31 | // notes |
| 32 | errors.Is(err, notesrv.ErrNotePasswordNotProvided) || |
| 33 | errors.Is(err, models.ErrNoteContentIsEmpty) || |
| 34 | errors.Is(err, models.ErrNoteSlugIsAlreadyInUse) { |
| 35 | newError(c, http.StatusBadRequest, err.Error()) |
| 36 | return |
| 37 | } |
| 38 | |
| 39 | if errors.Is(err, models.ErrNoteExpired) { |
| 40 | newError(c, http.StatusGone, err.Error()) |
| 41 | return |
| 42 | } |
| 43 | |
| 44 | if errors.Is(err, models.ErrNoteNotFound) || |
| 45 | errors.Is(err, models.ErrVerificationTokenNotFound) { |
| 46 | newErrorStatus(c, http.StatusNotFound, err.Error()) |
| 47 | return |
| 48 | } |
| 49 | |
| 50 | if errors.Is(err, ErrUnauthorized) || |
| 51 | errors.Is(err, jwtutil.ErrTokenExpired) || |
| 52 | errors.Is(err, jwtutil.ErrTokenSignatureInvalid) || |
| 53 | errors.Is(err, models.ErrUserWrongCredentials) { |
| 54 | newErrorStatus(c, http.StatusUnauthorized, err.Error()) |
| 55 | return |
| 56 | } |
| 57 | |
| 58 | newInternalError(c, err) |
| 59 | } |
| 60 | |
| 61 | func newError(c *gin.Context, status int, msg string) { |
| 62 | slog.ErrorContext(c.Request.Context(), msg, "status", status) |
| 63 | c.AbortWithStatusJSON(status, response{msg}) |
| 64 | } |
| 65 | |
| 66 | func newErrorStatus(c *gin.Context, status int, msg string) { |
| 67 | slog.ErrorContext(c.Request.Context(), msg, "status", status) |
| 68 | c.AbortWithStatus(status) |
| 69 | } |
| 70 | |
| 71 | func newInternalError(c *gin.Context, err error, msg ...string) { |
| 72 | slog.ErrorContext(c.Request.Context(), err.Error(), "status", "internal error") |
| 73 | |
| 74 | if len(msg) != 0 { |
| 75 | c.AbortWithStatusJSON(http.StatusInternalServerError, response{ |
| 76 | Message: msg[0], |
| 77 | }) |
| 78 | return |
| 79 | } |
| 80 | |
| 81 | c.AbortWithStatusJSON(http.StatusInternalServerError, response{ |
| 82 | Message: "internal error", |
| 83 | }) |
| 84 | } |