authz

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package authz 提供授权(authorization)机制:在认证(pkg/middleware/auth、pkg/token 确认 "你是谁 + 有哪些角色")之上,判定"这个身份能否对某资源做某动作"。

分层:

  • Subject:主体(id + 角色 + 属性),由认证层放进 context;
  • Enforcer:决策接口,Authorize(sub, action, resource) → nil 放行 / ErrDenied 拒绝;
  • 内置 RBAC(NewRBAC):零依赖、支持通配的角色→权限模型,覆盖多数场景;
  • 中间件:HTTP / gRPC 拦截器,从 context 取 Subject、按 mapper 推出 (action, resource)、 调 Authorize,拒绝即 403 / PermissionDenied。

更复杂的策略(ABAC、动态/DB 策略、关系授权 ReBAC)由实现同一 Enforcer 接口的 contrib 模块提供(contrib/casbin、contrib/openfga),调用点不变。

边界(机制而非策略):策略内容、角色分配(谁是 admin)、资源命名、租户模型都由使用方定。

Index

Constants

This section is empty.

Variables

View Source
var ErrDenied = errors.New("authz: permission denied")

ErrDenied 表示拒绝授权。Enforcer 拒绝时返回它(或包装它)。

Functions

func ContextWithSubject

func ContextWithSubject(ctx context.Context, s Subject) context.Context

ContextWithSubject 把主体放入 context(通常在认证中间件里,验完 token 后调用)。

func HTTP

func HTTP(e Enforcer, mapper RequestMapper) func(http.Handler) http.Handler

HTTP 返回一个授权中间件:从 context 取 Subject(认证层填),按 mapper 推 (action, resource), 调 e.Authorize。无主体→401;拒绝→403;放行→next。mapper 为 nil 时用 MethodResourceMapper。

func MethodAction

func MethodAction(method string) string

MethodAction 把 HTTP 方法映射成动作:GET/HEAD→read,POST→create,PUT/PATCH→update, DELETE→delete,其它→方法名小写。

func MethodResourceMapper

func MethodResourceMapper(r *http.Request) (string, string)

MethodResourceMapper 是默认映射:action 由 HTTP 方法推(见 MethodAction),resource 取请求路径。

func UnaryServerInterceptor

func UnaryServerInterceptor(e Enforcer, mapper MethodMapper) grpc.UnaryServerInterceptor

UnaryServerInterceptor 返回一个 gRPC 一元拦截器:从 context 取 Subject,按 mapper 推 (action, resource),调 e.Authorize。无主体→Unauthenticated;拒绝→PermissionDenied。

Types

type Enforcer

type Enforcer interface {
	Authorize(ctx context.Context, sub Subject, action, resource string) error
}

Enforcer 是授权决策接口。Authorize 返回 nil 表示放行,返回(包装了)ErrDenied 表示拒绝。 各引擎(内置 RBAC、casbin、openfga)实现它,应用面向接口编程、可无缝替换。

type MethodMapper

type MethodMapper func(fullMethod string) (action, resource string)

MethodMapper 从 gRPC 全方法名(/pkg.Svc/Method)推出 (action, resource)。policy。

type RBAC

type RBAC struct {
	// contains filtered or unexported fields
}

RBAC 是内置的基于角色的授权器(零依赖,内存策略,并发安全)。为角色授予 (action, resource)权限;主体只要任一角色命中即放行。action/resource 支持通配:

  • action:"*" 匹配任意,否则精确;
  • resource:"*" 匹配任意;以 "/*" 结尾按前缀匹配(如 "article/*" 命中 "article/123");否则精确。

零值不可用,用 NewRBAC 构造。适合静态角色→权限;要 ABAC/动态策略用 contrib/casbin。

func NewRBAC

func NewRBAC() *RBAC

NewRBAC 创建一个空的 RBAC 授权器。

func (*RBAC) Authorize

func (r *RBAC) Authorize(_ context.Context, sub Subject, action, resource string) error

Authorize 实现 Enforcer:主体任一角色拥有匹配 (action, resource) 的授权即放行,否则 ErrDenied。

func (*RBAC) Grant

func (r *RBAC) Grant(role, action, resource string) *RBAC

Grant 给 role 授予对 resource 执行 action 的权限(可链式)。

type RequestMapper

type RequestMapper func(*http.Request) (action, resource string)

RequestMapper 从 HTTP 请求推出被授权的 (action, resource)。这是 policy——按你的路由/资源规范定。

type Subject

type Subject struct {
	ID    string
	Roles []string
	Attrs map[string]string
}

Subject 是被授权的主体。ID 是身份;Roles 供 RBAC;Attrs 供属性判定(tenant/dept 等)。

func SubjectFromContext

func SubjectFromContext(ctx context.Context) (Subject, bool)

SubjectFromContext 取出主体;未认证(无主体)时 ok=false。

func (Subject) HasRole

func (s Subject) HasRole(role string) bool

HasRole 报告主体是否具备某角色。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL