From 38de345144adb0e8234b58f79eb4aba9a714eb57 Mon Sep 17 00:00:00 2001
From: zachmann <gabriel.zachmann@kit.edu>
Date: Mon, 6 Dec 2021 09:36:28 +0100
Subject: [PATCH] fix 404 on api paths returning html instead of json;
 resulting in wrong handling of client

---
 internal/server/apiPath/apiPath.go | 5 ++++-
 internal/server/server.go          | 9 +++++++--
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/internal/server/apiPath/apiPath.go b/internal/server/apiPath/apiPath.go
index c2efc1e9..abd6ad8a 100644
--- a/internal/server/apiPath/apiPath.go
+++ b/internal/server/apiPath/apiPath.go
@@ -1,4 +1,7 @@
 package apiPath
 
+// Prefix is the api prefix path component
+const Prefix = "/api"
+
 // V0 gives the api path for v0
-const V0 = "/api/v0"
+const V0 = Prefix + "/v0"
diff --git a/internal/server/server.go b/internal/server/server.go
index fddb392b..92d97da9 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -20,6 +20,7 @@ import (
 	"github.com/oidc-mytoken/server/internal/endpoints/consent"
 	"github.com/oidc-mytoken/server/internal/endpoints/redirect"
 	"github.com/oidc-mytoken/server/internal/model"
+	"github.com/oidc-mytoken/server/internal/server/apiPath"
 	"github.com/oidc-mytoken/server/internal/server/routes"
 	"github.com/oidc-mytoken/server/internal/server/ssh"
 )
@@ -70,7 +71,10 @@ func Init() {
 	addRoutes(server)
 	server.Use(
 		func(ctx *fiber.Ctx) error {
-			if ctx.Accepts(fiber.MIMETextHTML, fiber.MIMETextHTMLCharsetUTF8) != "" {
+			path := ctx.Path()
+			if !strings.HasPrefix(path, apiPath.Prefix) && ctx.Accepts(
+				fiber.MIMETextHTML, fiber.MIMETextHTMLCharsetUTF8,
+			) != "" {
 				ctx.Status(fiber.StatusNotFound)
 				return ctx.Render(
 					"sites/404", map[string]interface{}{
@@ -81,7 +85,8 @@ func Init() {
 			return model.Response{
 				Status: fiber.StatusNotFound,
 				Response: api.Error{
-					Error: "not_found",
+					Error:            "not_found",
+					ErrorDescription: path,
 				},
 			}.Send(ctx)
 		},
-- 
GitLab