hydra-webauthn/internal/route/api/get_registration_link.go

46 lines
1.2 KiB
Go

package api
import (
"net/http"
"forge.cadoles.com/wpetit/hydra-webauthn/internal/storage"
"github.com/go-chi/chi"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/api"
"gitlab.com/wpetit/goweb/logger"
"gitlab.com/wpetit/goweb/middleware/container"
)
type GetRegistrationLinkResponse struct {
RegistrationLink *storage.RegistrationLink `json:"registrationLink"`
}
func GetRegistrationLink(w http.ResponseWriter, r *http.Request) {
userID := chi.URLParam(r, "userID")
ctx := r.Context()
ctn := container.Must(ctx)
storageSrv := storage.Must(ctn)
registrationLink, err := storageSrv.User().GetRegistrationLink(ctx, userID)
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
logger.Warn(ctx, "registration link or associated user not found", logger.E(errors.WithStack(err)), logger.F("userID", userID))
api.ErrorResponse(w, http.StatusNotFound, api.ErrCodeNotFound, nil)
return
}
logger.Error(ctx, "could not get registration link", logger.E(errors.WithStack(err)))
api.ErrorResponse(w, http.StatusInternalServerError, api.ErrCodeUnknownError, nil)
return
}
response := GetRegistrationLinkResponse{
RegistrationLink: registrationLink,
}
api.DataResponse(w, http.StatusOK, response)
}