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 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() username := token.Subject()
sub, err := users.FromCtx(ctx).GetUser(ctx, username) sub, err := users.FromCtx(ctx).GetUser(ctx, username)
@ -111,8 +119,9 @@ func (a *Auth) AuthMiddleware() func(http.Handler) http.Handler {
return return
} }
// Token is authenticated, pass it through // Public subject
next.ServeHTTP(w, r) ctx = entities.CtxWithSubject(ctx, entities.NewPublicSubject(r))
next.ServeHTTP(w, r.WithContext(ctx))
} }
return http.HandlerFunc(hfn) return http.HandlerFunc(hfn)
} }

View file

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