Skip to content

Commit

Permalink
fix: add principal format
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie committed Jun 27, 2024
1 parent 9faac6c commit 4682d1f
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions goth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,30 @@ package authz

import (
"errors"
"fmt"

"github.com/gofiber/fiber/v2"
goth "github.com/zeiss/fiber-goth"
)

var _ AuthzPrincipalResolver = (*gothAuthzPrincipalResolver)(nil)
const authzPrincipalFormat = "user:%s"

type gothAuthzPrincipalResolver struct{}
var _ AuthzPrincipalResolver = (*GothAuthzPrincipalResolver)(nil)

// Resolve ...
func (g *gothAuthzPrincipalResolver) Resolve(c *fiber.Ctx) (AuthzPrincipal, error) {
// GothAuthzPrincipalResolver is the resolver that resolves the principal from the goth session.
type GothAuthzPrincipalResolver struct{}

// Resolve returns the principal from the goth session.
func (g *GothAuthzPrincipalResolver) Resolve(c *fiber.Ctx) (AuthzPrincipal, error) {
session, err := goth.SessionFromContext(c)
if err != nil && !errors.Is(err, goth.ErrMissingSession) {
return AuthzNoPrincipial, err
}

return AuthzPrincipal(session.UserID.String()), nil
return AuthzPrincipal(fmt.Sprintf(authzPrincipalFormat, session.UserID.String())), nil
}

// NewGothAuthzPrincipalResolver ...
// NewGothAuthzPrincipalResolver returns a new GothAuthzPrincipalResolver.
func NewGothAuthzPrincipalResolver() AuthzPrincipalResolver {
return &gothAuthzPrincipalResolver{}
return &GothAuthzPrincipalResolver{}
}

0 comments on commit 4682d1f

Please sign in to comment.