2fas-server/internal/api/browser_extension/app/command/close_2fa_request.go

52 lines
1.2 KiB
Go
Raw Normal View History

2022-12-31 10:22:38 +01:00
package command
import (
"github.com/google/uuid"
2023-01-30 19:59:42 +01:00
"github.com/twofas/2fas-server/internal/api/browser_extension/domain"
2022-12-31 10:22:38 +01:00
)
type Close2FaRequest struct {
ExtensionId string `uri:"extension_id" validate:"required,uuid4"`
TokenRequestId string `uri:"token_request_id" validate:"required,uuid4"`
Status string `json:"status" validate:"required,oneof=completed terminated"`
}
type Close2FaRequestHandler struct {
BrowserExtensionsRepository domain.BrowserExtensionRepository
BrowserExtension2FaRequestRepository domain.BrowserExtension2FaRequestRepository
}
func (h *Close2FaRequestHandler) Handle(cmd *Close2FaRequest) error {
extId, _ := uuid.Parse(cmd.ExtensionId)
browserExtension, err := h.BrowserExtensionsRepository.FindById(extId)
if err != nil {
return err
}
tokenRequestId, _ := uuid.Parse(cmd.TokenRequestId)
tokenRequest, err := h.BrowserExtension2FaRequestRepository.FindById(tokenRequestId, browserExtension.Id)
if err != nil {
return err
}
tokenRequest.Close(domain.Status(cmd.Status))
err = h.BrowserExtension2FaRequestRepository.Update(tokenRequest)
if err != nil {
return err
}
err = h.BrowserExtension2FaRequestRepository.Delete(tokenRequest)
if err != nil {
return err
}
return nil
}