|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/NdoleStudio/httpsms/pkg/repositories" |
| 7 | + "github.com/NdoleStudio/httpsms/pkg/requests" |
| 8 | + "github.com/NdoleStudio/httpsms/pkg/services" |
| 9 | + "github.com/NdoleStudio/httpsms/pkg/telemetry" |
| 10 | + "github.com/NdoleStudio/httpsms/pkg/validators" |
| 11 | + "github.com/davecgh/go-spew/spew" |
| 12 | + "github.com/gofiber/fiber/v2" |
| 13 | + "github.com/google/uuid" |
| 14 | + "github.com/palantir/stacktrace" |
| 15 | +) |
| 16 | + |
| 17 | +// PhoneAPIKeyHandler handles phone API key http requests |
| 18 | +type PhoneAPIKeyHandler struct { |
| 19 | + handler |
| 20 | + logger telemetry.Logger |
| 21 | + tracer telemetry.Tracer |
| 22 | + validator *validators.PhoneAPIKeyHandlerValidator |
| 23 | + service *services.PhoneAPIKeyService |
| 24 | + phoneService *services.PhoneService |
| 25 | +} |
| 26 | + |
| 27 | +// NewPhoneAPIKeyHandler creates a new PhoneAPIKeyHandler |
| 28 | +func NewPhoneAPIKeyHandler( |
| 29 | + logger telemetry.Logger, |
| 30 | + tracer telemetry.Tracer, |
| 31 | + validator *validators.PhoneAPIKeyHandlerValidator, |
| 32 | + service *services.PhoneAPIKeyService, |
| 33 | + phoneService *services.PhoneService, |
| 34 | +) *PhoneAPIKeyHandler { |
| 35 | + return &PhoneAPIKeyHandler{ |
| 36 | + logger: logger.WithService(fmt.Sprintf("%T", &PhoneAPIKeyHandler{})), |
| 37 | + tracer: tracer, |
| 38 | + validator: validator, |
| 39 | + service: service, |
| 40 | + phoneService: phoneService, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// RegisterRoutes registers the routes for the PhoneAPIKeyHandler |
| 45 | +func (h *PhoneAPIKeyHandler) RegisterRoutes(app *fiber.App, middlewares ...fiber.Handler) { |
| 46 | + router := app.Group("/v1/api-keys/") |
| 47 | + router.Post("/", h.computeRoute(middlewares, h.Store)...) |
| 48 | + router.Delete("/:phoneAPIKeyID", h.computeRoute(middlewares, h.Delete)...) |
| 49 | + router.Delete("/:phoneAPIKeyID/phones/:phoneID", h.computeRoute(middlewares, h.DeletePhone)...) |
| 50 | +} |
| 51 | + |
| 52 | +// Store a new Phone API key |
| 53 | +// @Summary Store phone API key |
| 54 | +// @Description Creates a new phone API key which can be used to log in to the httpSMS app on your Android phone |
| 55 | +// @Security ApiKeyAuth |
| 56 | +// @Tags PhoneAPIKeys |
| 57 | +// @Accept json |
| 58 | +// @Produce json |
| 59 | +// @Param payload body requests.PhoneAPIKeyStoreRequest true "Payload of new phone API key." |
| 60 | +// @Success 200 {object} responses.Ok[*entities.PhoneAPIKey] |
| 61 | +// @Failure 400 {object} responses.BadRequest |
| 62 | +// @Failure 401 {object} responses.Unauthorized |
| 63 | +// @Failure 422 {object} responses.UnprocessableEntity |
| 64 | +// @Failure 500 {object} responses.InternalServerError |
| 65 | +// @Router /api-keys [post] |
| 66 | +func (h *PhoneAPIKeyHandler) Store(c *fiber.Ctx) error { |
| 67 | + ctx, span := h.tracer.StartFromFiberCtx(c) |
| 68 | + defer span.End() |
| 69 | + |
| 70 | + ctxLogger := h.tracer.CtxLogger(h.logger, span) |
| 71 | + |
| 72 | + var request requests.PhoneAPIKeyStoreRequest |
| 73 | + if err := c.BodyParser(&request); err != nil { |
| 74 | + msg := fmt.Sprintf("cannot marshall params [%s] into %T", c.OriginalURL(), request) |
| 75 | + ctxLogger.Warn(stacktrace.Propagate(err, msg)) |
| 76 | + return h.responseBadRequest(c, err) |
| 77 | + } |
| 78 | + |
| 79 | + if errors := h.validator.ValidateStore(ctx, request.Sanitize()); len(errors) != 0 { |
| 80 | + msg := fmt.Sprintf("validation errors [%s], while updating phones [%+#v]", spew.Sdump(errors), request) |
| 81 | + ctxLogger.Warn(stacktrace.NewError(msg)) |
| 82 | + return h.responseUnprocessableEntity(c, errors, "validation errors while updating phones") |
| 83 | + } |
| 84 | + |
| 85 | + phone, err := h.service.Create(ctx, h.userFromContext(c), request.Name) |
| 86 | + if err != nil { |
| 87 | + msg := fmt.Sprintf("cannot update phones with params [%+#v]", request) |
| 88 | + ctxLogger.Error(stacktrace.Propagate(err, msg)) |
| 89 | + return h.responseInternalServerError(c) |
| 90 | + } |
| 91 | + |
| 92 | + return h.responseOK(c, "phone updated successfully", phone) |
| 93 | +} |
| 94 | + |
| 95 | +// Delete a phone API Key |
| 96 | +// @Summary Delete a phone API key from the database. |
| 97 | +// @Description Delete a phone API Key from the database and cannot be used for authentication anymore. |
| 98 | +// @Security ApiKeyAuth |
| 99 | +// @Tags PhoneAPIKeys |
| 100 | +// @Accept json |
| 101 | +// @Produce json |
| 102 | +// @Param phoneAPIKeyID path string true "ID of the phone API key" default(32343a19-da5e-4b1b-a767-3298a73703ca) |
| 103 | +// @Success 204 {object} responses.NoContent |
| 104 | +// @Failure 400 {object} responses.BadRequest |
| 105 | +// @Failure 401 {object} responses.Unauthorized |
| 106 | +// @Failure 404 {object} responses.NotFound |
| 107 | +// @Failure 422 {object} responses.UnprocessableEntity |
| 108 | +// @Failure 500 {object} responses.InternalServerError |
| 109 | +// @Router /messages/{phoneAPIKeyID} [delete] |
| 110 | +func (h *PhoneAPIKeyHandler) Delete(c *fiber.Ctx) error { |
| 111 | + ctx, span, ctxLogger := h.tracer.StartFromFiberCtxWithLogger(c, h.logger) |
| 112 | + defer span.End() |
| 113 | + |
| 114 | + phoneAPIKeyID := c.Params("phoneAPIKeyID") |
| 115 | + if errors := h.validator.ValidateUUID(phoneAPIKeyID, "phoneAPIKeyID"); len(errors) != 0 { |
| 116 | + msg := fmt.Sprintf("validation errors [%s], while deleting a phone API key with ID [%s]", spew.Sdump(errors), phoneAPIKeyID) |
| 117 | + ctxLogger.Warn(stacktrace.NewError(msg)) |
| 118 | + return h.responseUnprocessableEntity(c, errors, "validation errors while storing event") |
| 119 | + } |
| 120 | + |
| 121 | + err := h.service.Delete(ctx, h.userIDFomContext(c), uuid.MustParse(phoneAPIKeyID)) |
| 122 | + if stacktrace.GetCode(err) == repositories.ErrCodeNotFound { |
| 123 | + return h.responseNotFound(c, fmt.Sprintf("cannot find phone API key with ID [%s]", phoneAPIKeyID)) |
| 124 | + } |
| 125 | + |
| 126 | + if err != nil { |
| 127 | + msg := fmt.Sprintf("cannot delete phone API key with ID [%s] for user with ID [%s]", phoneAPIKeyID, h.userIDFomContext(c)) |
| 128 | + ctxLogger.Error(h.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))) |
| 129 | + return h.responseInternalServerError(c) |
| 130 | + } |
| 131 | + |
| 132 | + return h.responseNoContent(c, "phone API key deleted successfully") |
| 133 | +} |
| 134 | + |
| 135 | +// DeletePhone removes a phone from a phone API key |
| 136 | +// @Summary Remove the association of a phone from the phone API key. |
| 137 | +// @Description You will need to login again to the httpSMS app on your Android phone with a new phone API key. |
| 138 | +// @Security ApiKeyAuth |
| 139 | +// @Tags PhoneAPIKeys |
| 140 | +// @Accept json |
| 141 | +// @Produce json |
| 142 | +// @Param phoneAPIKeyID path string true "ID of the phone API key" default(32343a19-da5e-4b1b-a767-3298a73703ca) |
| 143 | +// @Param phoneID path string true "ID of the phone" default(32343a19-da5e-4b1b-a767-3298a73703ca) |
| 144 | +// @Success 204 {object} responses.NoContent |
| 145 | +// @Failure 400 {object} responses.BadRequest |
| 146 | +// @Failure 401 {object} responses.Unauthorized |
| 147 | +// @Failure 404 {object} responses.NotFound |
| 148 | +// @Failure 422 {object} responses.UnprocessableEntity |
| 149 | +// @Failure 500 {object} responses.InternalServerError |
| 150 | +// @Router /messages/{phoneAPIKeyID}/phones/{phoneID} [delete] |
| 151 | +func (h *PhoneAPIKeyHandler) DeletePhone(c *fiber.Ctx) error { |
| 152 | + ctx, span, ctxLogger := h.tracer.StartFromFiberCtxWithLogger(c, h.logger) |
| 153 | + defer span.End() |
| 154 | + |
| 155 | + phoneAPIKeyID := c.Params("phoneAPIKeyID") |
| 156 | + phoneID := c.Params("phoneID") |
| 157 | + if errors := h.mergeErrors(h.validator.ValidateUUID(phoneAPIKeyID, "phoneAPIKeyID"), h.validator.ValidateUUID(phoneID, "phoneID")); len(errors) != 0 { |
| 158 | + msg := fmt.Sprintf("validation errors [%s], while deleting a phone API key with ID [%s]", spew.Sdump(errors), phoneAPIKeyID) |
| 159 | + ctxLogger.Warn(stacktrace.NewError(msg)) |
| 160 | + return h.responseUnprocessableEntity(c, errors, "validation errors while storing event") |
| 161 | + } |
| 162 | + |
| 163 | + err := h.service.RemovePhone(ctx, h.userIDFomContext(c), uuid.MustParse(phoneAPIKeyID), uuid.MustParse(phoneID)) |
| 164 | + if stacktrace.GetCode(err) == repositories.ErrCodeNotFound { |
| 165 | + return h.responseNotFound(c, fmt.Sprintf("cannot find phone with ID [%s] which is associated with phone API key with ID [%s]", phoneID, phoneAPIKeyID)) |
| 166 | + } |
| 167 | + |
| 168 | + if err != nil { |
| 169 | + msg := fmt.Sprintf("cannot remove phone with ID [%s] from phone API key with ID [%s] for user with ID [%s]", phoneID, phoneAPIKeyID, h.userIDFomContext(c)) |
| 170 | + ctxLogger.Error(h.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))) |
| 171 | + return h.responseInternalServerError(c) |
| 172 | + } |
| 173 | + |
| 174 | + return h.responseNoContent(c, "phone has been dissociated from phone API key successfully") |
| 175 | +} |
0 commit comments