auth

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: MIT Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnauthenticated   = errors.New("unauthenticated")
	ErrForbidden         = errors.New("forbidden")
	ErrAuthorizerMissing = errors.New("authorizer not configured for authorized route")
)

Functions

func WithClaims

func WithClaims(ctx context.Context, claims Claims) context.Context

WithClaims 将 Claims 写入 context。

Types

type Authorizer

type Authorizer interface {
	Authenticate(ctx context.Context, rawToken string) (Claims, error)
	Authorize(ctx context.Context, claims Claims, permission string) error
	IsSuperAdmin(claims Claims) bool
}

Authorizer 认证与授权契约,不依赖 Gin。

type Claims

type Claims struct {
	Subject    string
	Roles      []string
	SuperAdmin bool
	Extra      map[string]any
}

Claims 是框架级主体声明;业务登录负责填充 Subject/Roles/SuperAdmin。

func ClaimsFromContext

func ClaimsFromContext(ctx context.Context) (Claims, bool)

ClaimsFromContext 从 context 读取 Claims。

type GuardFactory

type GuardFactory interface {
	Guard(level string, permission string) any
}

GuardFactory 由 router.Build 调用,按访问级别生成守卫中间件。 返回 nil 表示不挂载(例如 Public 或未启用自动守卫)。

type TokenService

type TokenService interface {
	Issue(ctx context.Context, claims Claims, ttlSeconds int) (string, error)
	Parse(ctx context.Context, token string) (Claims, error)
}

TokenService 负责 access token 签发与解析(不含业务登录)。

Source Files

  • auth.go
  • context.go
  • errors.go

Jump to

Keyboard shortcuts

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