Public subject

This commit is contained in:
Daniel Ponte 2025-02-13 08:13:07 -05:00
parent b6ad5e5f8c
commit 61d1875b63
2 changed files with 18 additions and 4 deletions

View file

@ -94,8 +94,16 @@ func (a *Auth) AuthMiddleware() func(http.Handler) http.Handler {
return
}
if token != nil && jwt.Validate(token, a.jwt.ValidateOptions()...) == nil {
ctx := r.Context()
ctx := r.Context()
if token != nil {
err := jwt.Validate(token, a.jwt.ValidateOptions()...)
if err != nil {
err = jwtauth.ErrorReason(err)
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
username := token.Subject()
sub, err := users.FromCtx(ctx).GetUser(ctx, username)
@ -111,8 +119,9 @@ func (a *Auth) AuthMiddleware() func(http.Handler) http.Handler {
return
}
// Token is authenticated, pass it through
next.ServeHTTP(w, r)
// Public subject
ctx = entities.CtxWithSubject(ctx, entities.NewPublicSubject(r))
next.ServeHTTP(w, r.WithContext(ctx))
}
return http.HandlerFunc(hfn)
}

View file

@ -2,6 +2,7 @@ package entities
import (
"context"
"net/http"
"github.com/el-mike/restrict/v2"
)
@ -66,6 +67,10 @@ func (s *PublicSubject) GetRoles() []string {
return []string{RolePublic}
}
func NewPublicSubject(r *http.Request) *PublicSubject {
return &PublicSubject{RemoteAddr: r.RemoteAddr}
}
type SystemServiceSubject struct {
Name string
}