127 lines
3.3 KiB
Go
127 lines
3.3 KiB
Go
package rbac
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"path"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
|
|
"online-back/internal/appresult"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type ruleKind int
|
|
|
|
const (
|
|
rulePublic ruleKind = iota // no token needed
|
|
ruleAuthenticated // any logged-in user
|
|
ruleStaff // any logged-in user whose scope is not own
|
|
rulePermission // perm required (+ scope enforcement)
|
|
)
|
|
|
|
type rule struct {
|
|
kind ruleKind
|
|
perm string
|
|
staffOnly bool
|
|
}
|
|
|
|
// Policy is the access rule of a route group: one rule for reads (GET/HEAD)
|
|
// and one for writes. Build it with Public, Authenticated, Staff, Require,
|
|
// RequireStaff, Catalog or StaffRead.
|
|
type Policy struct {
|
|
name string
|
|
read rule
|
|
write rule
|
|
}
|
|
|
|
func (p Policy) String() string { return p.name }
|
|
|
|
func (p Policy) public() bool { return p.read.kind == rulePublic && p.write.kind == rulePublic }
|
|
|
|
func (p Policy) ruleFor(method string) rule {
|
|
if method == http.MethodGet || method == http.MethodHead {
|
|
return p.read
|
|
}
|
|
return p.write
|
|
}
|
|
|
|
// registry maps full route prefixes (e.g. "/api/admin/card-orders") to the
|
|
// policy declared for them. Guard resolves a request's policy from it by
|
|
// longest prefix, and VerifyRoutes fails startup for any route with none.
|
|
type registry struct {
|
|
mu sync.RWMutex
|
|
policies map[string]Policy
|
|
}
|
|
|
|
var routes = ®istry{policies: make(map[string]Policy)}
|
|
|
|
func (r *registry) add(prefix string, p Policy) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
if existing, ok := r.policies[prefix]; ok && existing.name != p.name {
|
|
panic(fmt.Sprintf("rbac: %s registered with policy %s and %s", prefix, existing.name, p.name))
|
|
}
|
|
r.policies[prefix] = p
|
|
}
|
|
|
|
func (r *registry) lookup(fullPath string) (Policy, bool) {
|
|
r.mu.RLock()
|
|
defer r.mu.RUnlock()
|
|
best := ""
|
|
var found Policy
|
|
ok := false
|
|
for prefix, p := range r.policies {
|
|
if (fullPath == prefix || strings.HasPrefix(fullPath, prefix+"/")) && len(prefix) >= len(best) {
|
|
best, found, ok = prefix, p, true
|
|
}
|
|
}
|
|
return found, ok
|
|
}
|
|
|
|
func (r *registry) reset() {
|
|
r.mu.Lock()
|
|
r.policies = make(map[string]Policy)
|
|
r.mu.Unlock()
|
|
}
|
|
|
|
// Group creates a route group under parent at relativePath and declares its
|
|
// policy. Every /api/admin route must be registered through Group - see
|
|
// VerifyRoutes.
|
|
func Group(parent *gin.RouterGroup, relativePath string, p Policy) *gin.RouterGroup {
|
|
full := joinPath(parent.BasePath(), relativePath)
|
|
routes.add(full, p)
|
|
if p.public() {
|
|
return parent.Group(relativePath)
|
|
}
|
|
return parent.Group(relativePath, appresult.JwtTokenCheck(), p.handler())
|
|
}
|
|
|
|
// VerifyRoutes returns an error naming every route under prefix that has no
|
|
// policy. Call it after all handlers are registered.
|
|
func VerifyRoutes(all gin.RoutesInfo, prefix string) error {
|
|
var missing []string
|
|
for _, route := range all {
|
|
if route.Path != prefix && !strings.HasPrefix(route.Path, prefix+"/") {
|
|
continue
|
|
}
|
|
if _, ok := routes.lookup(route.Path); !ok {
|
|
missing = append(missing, route.Method+" "+route.Path)
|
|
}
|
|
}
|
|
if len(missing) == 0 {
|
|
return nil
|
|
}
|
|
sort.Strings(missing)
|
|
return fmt.Errorf("rbac: routes without a policy (use rbac.Group): %s", strings.Join(missing, ", "))
|
|
}
|
|
|
|
func joinPath(base, rel string) string {
|
|
if rel == "" {
|
|
return strings.TrimSuffix(base, "/")
|
|
}
|
|
return strings.TrimSuffix(path.Join(base, rel), "/")
|
|
}
|