Documentation
¶
Index ¶
- Variables
- func LoginRateLimit(opts *RateLimitOptions) func(stdhttp.Handler) stdhttp.Handler
- func OptionalBearer(auth AccessTokenValidator) (func(stdhttp.Handler) stdhttp.Handler, error)
- func RequireAPIKey(auth APIKeyValidator, header string) (func(stdhttp.Handler) stdhttp.Handler, error)
- func RequireBearer(auth AccessTokenValidator) (func(stdhttp.Handler) stdhttp.Handler, error)
- func ValidateStaticPassword(password string) error
- func VerifyCredential(expected, got string) bool
- type APIKeyValidator
- type APIKeyValidatorFunc
- type AccessTokenValidator
- type Handler
- type LoginAuthenticator
- type LoginAuthenticatorFunc
- type Options
- type RateLimitOptions
- type TokenManager
Constants ¶
This section is empty.
Variables ¶
var ErrAPIKeyValidatorMissing = errors.New("api key validator is required")
ErrAPIKeyValidatorMissing means an API-key middleware was built without a validator. ErrAPIKeyValidatorMissing 表示 API-key 中间件缺少 validator。
var ErrAccessTokenValidatorMissing = errors.New("access token validator is required")
ErrAccessTokenValidatorMissing means a Bearer middleware was built without a validator. ErrAccessTokenValidatorMissing 表示 Bearer 中间件缺少 token validator。
Functions ¶
func LoginRateLimit ¶
func LoginRateLimit(opts *RateLimitOptions) func(stdhttp.Handler) stdhttp.Handler
LoginRateLimit returns a login rate-limit middleware. Disabled or nil options return nil. LoginRateLimit 返回登录限流中间件。 传入 nil 或 Disabled 时返回 nil。
func OptionalBearer ¶ added in v0.1.7
OptionalBearer returns middleware that attaches claims when a Bearer token is present. Missing Authorization is accepted; malformed or invalid tokens are rejected. OptionalBearer 返回 Bearer token 可选中间件。 未提供 Authorization 会继续执行;格式错误或 token 无效会被拒绝。
func RequireAPIKey ¶ added in v0.1.7
func RequireAPIKey(auth APIKeyValidator, header string) (func(stdhttp.Handler) stdhttp.Handler, error)
RequireAPIKey returns middleware that validates an API key from header. Empty header uses X-API-Key. RequireAPIKey 返回从 header 校验 API key 的中间件。 header 为空时使用 X-API-Key。
func RequireBearer ¶
RequireBearer returns a Bearer-token middleware. Call bearer, err := RequireBearer(auth) and then r.Use(bearer). RequireBearer 返回 Bearer token 中间件。 调用 bearer, err := RequireBearer(auth),再执行 r.Use(bearer)。 Example / 示例:
bearer, err := authhttp.RequireBearer(jwtManager)
if err != nil { ... }
r.Use(bearer)
func ValidateStaticPassword ¶ added in v0.1.8
ValidateStaticPassword validates a static password. NewHandler does not call it automatically. ValidateStaticPassword 校验静态密码。 NewHandler 不会自动调用它。
func VerifyCredential ¶
VerifyCredential compares expected and got with an exact byte match. VerifyCredential 使用精确字节匹配比较 expected 和 got。
Types ¶
type APIKeyValidator ¶ added in v0.1.7
APIKeyValidator validates API keys for RequireAPIKey. APIKeyValidator 为 RequireAPIKey 校验 API key。
type APIKeyValidatorFunc ¶ added in v0.1.7
APIKeyValidatorFunc adapts a function to APIKeyValidator. APIKeyValidatorFunc 将函数适配为 APIKeyValidator。
func (APIKeyValidatorFunc) ValidateAPIKey ¶ added in v0.1.7
ValidateAPIKey calls f(ctx, key). ValidateAPIKey 调用 f(ctx, key)。
type AccessTokenValidator ¶ added in v0.1.5
type AccessTokenValidator = auth.AccessTokenValidator
AccessTokenValidator validates access tokens for RequireBearer. AccessTokenValidator 为 RequireBearer 校验 access token。
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler serves auth endpoints. Handler 提供认证端点。
func NewHandler ¶
func NewHandler(manager TokenManager, opts Options) (*Handler, error)
NewHandler returns a Handler. Call NewHandler(manager, opts). Zero-valued options fall back to DefaultOptions. NewHandler 返回 Handler。 调用 NewHandler(manager, opts)。 零值选项会回退到 DefaultOptions。
type LoginAuthenticator ¶
type LoginAuthenticator = auth.LoginAuthenticator
LoginAuthenticator validates login credentials. Implementations may ignore username. LoginAuthenticator 校验登录凭据。 实现可以忽略 username。
func NewStaticPassword ¶ added in v0.1.8
func NewStaticPassword(userID, expectedPassword string) (LoginAuthenticator, error)
NewStaticPassword returns a LoginAuthenticator for one fixed password. NewStaticPassword 返回基于固定密码的 LoginAuthenticator。
type LoginAuthenticatorFunc ¶
type LoginAuthenticatorFunc = auth.LoginAuthenticatorFunc
LoginAuthenticatorFunc adapts a function to LoginAuthenticator. LoginAuthenticatorFunc 将函数适配为 LoginAuthenticator。
type Options ¶
type Options struct {
// LoginAuthenticator validates login credentials.
// LoginAuthenticator 校验登录凭据。
LoginAuthenticator LoginAuthenticator
// BasePath is the auth route prefix.
// BasePath 是认证路由前缀。
BasePath string
// RefreshCookiePath defaults to BasePath when empty.
// RefreshCookiePath 为空时默认等于 BasePath。
RefreshCookiePath string
// CSRFCookiePath defaults to "/".
// CSRFCookiePath 默认为 "/"。
CSRFCookiePath string
// RefreshCookieName is the refresh cookie name.
// RefreshCookieName 是 refresh cookie 名。
RefreshCookieName string
// CSRFCookieName is the CSRF cookie name.
// CSRFCookieName 是 CSRF cookie 名。
CSRFCookieName string
// CSRFHeaderName is the CSRF request header name.
// CSRFHeaderName 是 CSRF 请求头名。
CSRFHeaderName string
// CookieSameSite overrides the derived auth cookie SameSite mode when non-zero.
// CookieSameSite 非零时覆盖认证 cookie 自动推导的 SameSite 模式。
CookieSameSite http.SameSite
// TrustedProxies enables forwarded-header trust.
// TrustedProxies 启用转发头信任。
TrustedProxies []netip.Prefix
// MaxBodyBytes limits the login body size.
// MaxBodyBytes 限制登录请求体大小。
MaxBodyBytes int64
// RateLimit configures login rate limiting.
// RateLimit 配置登录限流。
RateLimit *RateLimitOptions
}
Options configures NewHandler. Options 配置 NewHandler。
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions returns the default handler options. DefaultOptions 返回默认 handler 选项。
type RateLimitOptions ¶
type RateLimitOptions struct {
Disabled bool
Requests int
Window time.Duration
IPv4PrefixBits int
IPv6PrefixBits int
TrustedProxies []netip.Prefix
KeyFunc httprate.KeyFunc
}
RateLimitOptions configures LoginRateLimit. RateLimitOptions 配置 LoginRateLimit。
func DefaultRateLimitOptions ¶
func DefaultRateLimitOptions() RateLimitOptions
DefaultRateLimitOptions returns the default login rate limit options. DefaultRateLimitOptions 返回默认登录限流选项。
type TokenManager ¶
type TokenManager = auth.TokenManager
TokenManager provides the auth operations used by Handler. TokenManager 为 Handler 提供认证操作。