now setting user id to custom header after proxy oauthkeeper

This commit is contained in:
pasha1coil 2025-03-10 12:49:16 +03:00
parent 35c5839575
commit eb772811e1
4 changed files with 13 additions and 9 deletions

@ -35,7 +35,7 @@ func main() {
if !ok {
return c.SendStatus(fiber.StatusUnauthorized)
}
return c.SendString("private content, token: " + token)
return c.SendString("private content, account id: " + token)
})
go func() {

@ -36,5 +36,10 @@ mutators:
{
"session": {{ .Extra | toJson }}
}
header:
enabled: true
config:
headers:
Pena-Account-ID: "{{ .Subject }}"
noop:
enabled: true

@ -18,7 +18,7 @@
},
"mutators": [
{
"handler": "id_token"
"handler": "header"
}
]
},
@ -41,7 +41,7 @@
},
"mutators": [
{
"handler": "id_token"
"handler": "header"
}
]
}

@ -4,10 +4,9 @@ import (
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/ory/client-go"
"strings"
)
const CtxTokenKey = "token-key"
const CtxTokenKey = "Pena-Account-Id"
func KratosMiddleware(ory *client.APIClient) fiber.Handler {
return func(ctx *fiber.Ctx) error {
@ -45,12 +44,12 @@ func getSessionFromToken(ctx *fiber.Ctx, ory *client.APIClient, token string) (*
func OauthKeeperMiddleware() fiber.Handler {
return func(ctx *fiber.Ctx) error {
token := strings.ReplaceAll(ctx.Get("Authorization"), "Bearer ", "")
if token == "" {
return fiber.NewError(fiber.StatusUnauthorized, "empty token")
accountID := ctx.Get(CtxTokenKey)
if accountID == "" {
return fiber.NewError(fiber.StatusUnauthorized, "empty account id")
}
ctx.Locals(CtxTokenKey, token)
ctx.Locals(CtxTokenKey, accountID)
return ctx.Next()
}