Skip to content
Snippets Groups Projects
Verified Commit 93ce9c76 authored by Gabriel Zachmann's avatar Gabriel Zachmann
Browse files

[web] return html instead of json on the action endpoint

parent c8e68e41
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,7 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/jmoiron/sqlx"
"github.com/oidc-mytoken/api/v0"
log "github.com/sirupsen/logrus"
"github.com/oidc-mytoken/server/internal/db/dbrepo/actionrepo"
......@@ -12,52 +13,51 @@ import (
"github.com/oidc-mytoken/server/internal/model"
"github.com/oidc-mytoken/server/internal/mytoken/pkg/mtid"
"github.com/oidc-mytoken/server/internal/server/routes"
"github.com/oidc-mytoken/server/internal/utils/ctxutils"
"github.com/oidc-mytoken/server/internal/utils/logger"
)
// HandleActions is the main entry function to handle the different actions of the action endpoint
func HandleActions(ctx *fiber.Ctx) error {
actionInfo := pkg.CtxGetActionInfo(ctx)
rlog := logger.GetRequestLogger(ctx)
switch actionInfo.Action {
case pkg.ActionRecreate:
return handleRecreate(rlog, actionInfo.Code).Send(ctx)
return handleRecreate(ctx, actionInfo.Code)
case pkg.ActionUnsubscribe:
return handleUnsubscribe(rlog, actionInfo.Code).Send(ctx)
return handleUnsubscribe(ctx, actionInfo.Code)
case pkg.ActionVerifyEmail:
return handleVerifyEmail(rlog, actionInfo.Code).Send(ctx)
return handleVerifyEmail(ctx, actionInfo.Code)
case pkg.ActionRemoveFromCalendar:
return handleRemoveFromCalendar(rlog, actionInfo.Code).Send(ctx)
return handleRemoveFromCalendar(ctx, actionInfo.Code)
}
return model.Response{
Status: http.StatusBadRequest,
Response: model.BadRequestError("unknown action"),
}.Send(ctx)
return ctxutils.RenderErrorPage(
ctx, fiber.StatusBadRequest, model.BadRequestError("unknown action").
CombinedMessage(),
)
}
func handleRecreate(rlog log.Ext1FieldLogger, code string) model.Response {
return model.ResponseNYI
func handleRecreate(ctx *fiber.Ctx, code string) error {
return ctxutils.RenderErrorPage(ctx, fiber.StatusNotImplemented, api.ErrorNYI.CombinedMessage())
}
func handleVerifyEmail(rlog log.Ext1FieldLogger, code string) *model.Response {
func handleVerifyEmail(ctx *fiber.Ctx, code string) error {
rlog := logger.GetRequestLogger(ctx)
verified, err := actionrepo.VerifyMail(rlog, nil, code)
if err != nil {
return model.ErrorToInternalServerErrorResponse(err)
return ctxutils.RenderInternalServerErrorPage(ctx, err)
}
if !verified {
return &model.Response{
Status: http.StatusBadRequest,
Response: model.BadRequestError("code not valid or expired"),
}
}
return &model.Response{
Status: http.StatusOK,
return ctxutils.RenderErrorPage(ctx, http.StatusBadRequest, "code not valid or expired")
}
return ctxutils.RenderErrorPage(
ctx, http.StatusOK, "The email address was successfully verified.", "Email Verified",
)
}
func handleUnsubscribe(rlog log.Ext1FieldLogger, code string) model.Response {
return model.ResponseNYI
func handleUnsubscribe(ctx *fiber.Ctx, code string) error {
return ctxutils.RenderErrorPage(ctx, fiber.StatusNotImplemented, api.ErrorNYI.CombinedMessage())
}
func handleRemoveFromCalendar(rlog log.Ext1FieldLogger, code string) model.Response {
return model.ResponseNYI
func handleRemoveFromCalendar(ctx *fiber.Ctx, code string) error {
return ctxutils.RenderErrorPage(ctx, fiber.StatusNotImplemented, api.ErrorNYI.CombinedMessage())
}
// CreateVerifyEmail creates an action url for verifying a mail address
......
package redirect
import (
"net/http"
"github.com/gofiber/fiber/v2"
"github.com/jmoiron/sqlx"
"github.com/oidc-mytoken/api/v0"
......@@ -40,12 +38,11 @@ func HandleOIDCRedirect(ctx *fiber.Ctx) error {
}
}
oidcErrorDescription := ctx.Query("error_description")
return ctx.Status(httpstatus.StatusOIDPError).Render(
"sites/error", map[string]interface{}{
"empty-navbar": true,
"error-heading": "OIDC error",
"msg": pkgModel.OIDCError(oidcError, oidcErrorDescription).CombinedMessage(),
}, "layouts/main",
return ctxutils.RenderErrorPage(
ctx, httpstatus.StatusOIDPError, pkgModel.OIDCError(
oidcError,
oidcErrorDescription,
).CombinedMessage(), "OIDC error",
)
}
code := ctx.Query("code")
......@@ -54,11 +51,5 @@ func HandleOIDCRedirect(ctx *fiber.Ctx) error {
if fasthttp.StatusCodeIsRedirect(res.Status) {
return res.Send(ctx)
}
return ctx.Status(res.Status).Render(
"sites/error", map[string]interface{}{
"empty-navbar": true,
"error-heading": http.StatusText(res.Status),
"msg": res.Response.(api.Error).CombinedMessage(),
}, "layouts/main",
)
return ctxutils.RenderErrorPage(ctx, res.Status, res.Response.(api.Error).CombinedMessage())
}
package ctxutils
import (
"net/http"
"github.com/gofiber/fiber/v2"
"github.com/oidc-mytoken/server/internal/model"
"github.com/oidc-mytoken/server/internal/utils/errorfmt"
)
func RenderErrorPage(ctx *fiber.Ctx, status int, errorMsg string, optionalErrorHeading ...string) error {
errorHeading := http.StatusText(status)
if len(optionalErrorHeading) > 0 {
errorHeading = optionalErrorHeading[0]
}
return ctx.Status(status).Render(
"sites/error", map[string]interface{}{
"empty-navbar": true,
"error-heading": errorHeading,
"msg": errorMsg,
}, "layouts/main",
)
}
func RenderInternalServerErrorPage(ctx *fiber.Ctx, err error) error {
return RenderErrorPage(
ctx, fiber.StatusInternalServerError, model.InternalServerError(errorfmt.Error(err)).CombinedMessage(),
)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment