Documentation
¶
Overview ¶
Package authn 是用户与服务访问令牌的验签机制件:
- 用户令牌经 OIDCVerifier(多 Realm JWKS / 静态公钥,支持 RS256/ES256/EdDSA、 org_id 租户映射、Realm Partition 绑定与 MFA step-up)解析并注入 appkit.Actor;
- 服务间调用经 ServiceVerifier 解析短期服务 JWT 并注入 appkit.ServicePrincipal。
权限判定(Require/Check)在框架根包,本包只负责「凭证 → Principal/Actor」。
Index ¶
- Constants
- type OIDCRealm
- type OIDCVerifier
- func (v *OIDCVerifier) Middleware(next http.Handler) http.Handler
- func (v *OIDCVerifier) PartitionForIssuer(issuer string) (string, bool)
- func (v *OIDCVerifier) Realms() int
- func (v *OIDCVerifier) Validate() error
- func (v *OIDCVerifier) Verify(ctx context.Context, raw string) (appkit.Actor, string, error)
- type OIDCVerifierOptions
- type ServiceDelegation
- type ServiceDelegationPolicy
- type ServiceIssuer
- type ServiceSigner
- type ServiceSignerOptions
- type ServiceVerifier
- type ServiceVerifierOptions
Constants ¶
const HeaderServiceAuthorization = "X-Service-Authorization"
HeaderServiceAuthorization carries a Bearer service JWT, independently of the user's Authorization header. It must never contain a forwarded user token.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type OIDCRealm ¶ added in v0.11.0
type OIDCRealm struct {
// Issuer 是该 Realm 签发令牌的精确 iss(例如 "https://demo.sso.example.com/")。
Issuer string
// Audience 是目标 Resource Server(API)的精确 aud 标识。
Audience string
// Partition 是该 Realm 绑定的可信数据分区键;非空时验签后焊进 callctx.Meta.Partition。
Partition string
// JWKSURL 是该 Realm 的 JWKS 公钥集合地址。StaticKeys 为空且未指定时默认取
// strings.TrimRight(Issuer, "/") + "/.well-known/jwks.json"。
JWKSURL string
// StaticKeys 允许不经网络直接按 kid 查公钥(单测、离线验收或钉死公钥场景)。
// 支持 *rsa.PublicKey(>=2048 位)、*ecdsa.PublicKey(P-256)与 ed25519.PublicKey。
StaticKeys map[string]crypto.PublicKey
// RequireOrg 为 true 时要求令牌携带非空 org_id(映射为 Actor.TenantID 与 callctx.TenantID)。
RequireOrg bool
}
OIDCRealm 描述一个受信任的 SSO Realm(如 forgeplex/sso 的单个身份域)。 每个 Realm 拥有独立的 Issuer、Audience 与 JWKS(或静态测试公钥),并可映射 到组合根内的一个数据分区(Partition)。
type OIDCVerifier ¶ added in v0.11.0
type OIDCVerifier struct {
// contains filtered or unexported fields
}
OIDCVerifier 是不可变的多 Realm 用户访问令牌验签器;零值无效。
func NewOIDCVerifier ¶ added in v0.11.0
func NewOIDCVerifier(o OIDCVerifierOptions) (*OIDCVerifier, error)
NewOIDCVerifier 校验并构造多 Realm 用户令牌验签器。所有配置切片与公钥映射 在构造期快照,避免运行期外部修改。
func (*OIDCVerifier) Middleware ¶ added in v0.11.0
func (v *OIDCVerifier) Middleware(next http.Handler) http.Handler
Middleware 验签 Bearer 访问令牌并注入 appkit.Actor 与 callctx.Meta:
- /healthz、/readyz 探针直接放行;
- 无 Authorization 头(或非 Bearer)匿名放行,由路由守卫决定是否 401;
- 有 Bearer 头但验签失败(非法签名、未知 iss/kid、错 aud、过期、机器令牌、 缺 sub、缺必需 org_id、与服务委托冲突)一律返回 401,不降级匿名。
func (*OIDCVerifier) PartitionForIssuer ¶ added in v0.11.0
func (v *OIDCVerifier) PartitionForIssuer(issuer string) (string, bool)
PartitionForIssuer 返回已配置 issuer 绑定的可信分区键。
func (*OIDCVerifier) Realms ¶ added in v0.11.0
func (v *OIDCVerifier) Realms() int
Realms 返回已配置 Realm 的数量(供组合根启动测试检查)。
func (*OIDCVerifier) Validate ¶ added in v0.11.0
func (v *OIDCVerifier) Validate() error
Validate 供组合根与 bootstrap 在打开基础设施前校验验签器有效性。
type OIDCVerifierOptions ¶ added in v0.11.0
type OIDCVerifierOptions struct {
Realms []OIDCRealm
HTTPClient *http.Client
CacheTTL time.Duration
MinRefresh time.Duration
ClockSkew time.Duration
// StepUpACRs 列举可视为已通过 MFA step-up 的 acr 取值(除 amr 含 "mfa" 外)。
StepUpACRs []string
// contains filtered or unexported fields
}
OIDCVerifierOptions 配置多 Realm 用户令牌验签器。
type ServiceDelegation ¶ added in v0.9.1
ServiceDelegation is the explicitly authorized data scope of one service call. Empty fields do not mean all tenants/merchants/partitions. MerchantID stays in ServicePrincipal; it is deliberately not added to callctx's propagation list.
type ServiceDelegationPolicy ¶ added in v0.9.1
type ServiceDelegationPolicy func(context.Context, appkit.ServicePrincipal, ServiceDelegation) error
ServiceDelegationPolicy authorizes the complete requested scope after signature verification (or before signing). A nil policy denies any nonempty delegation. The callback must be concurrency-safe and honor ctx cancellation. It receives a detached principal snapshot; changing it cannot change the verified identity.
type ServiceIssuer ¶ added in v0.9.1
ServiceIssuer is a static trust entry. Keys are selected by the exact iss+kid pair; Subjects is a nonempty allowlist. Neither token-provided URLs nor dynamic key lookup are supported. Keep old and new kids during a deliberate rotation.
type ServiceSigner ¶ added in v0.9.1
type ServiceSigner struct {
// contains filtered or unexported fields
}
ServiceSigner signs only explicitly supplied audience and delegation. It does not infer authorization from an Actor, incoming credential, header or callctx.
func NewServiceSigner ¶ added in v0.9.1
func NewServiceSigner(o ServiceSignerOptions) (*ServiceSigner, error)
NewServiceSigner validates the fixed identity, lifetime and key without panicking. Callers retain ownership of their private-key buffer.
func (*ServiceSigner) Sign ¶ added in v0.9.1
func (s *ServiceSigner) Sign(ctx context.Context, audience string, scope ServiceDelegation) (string, error)
Sign returns a compact service JWT. Use SignWithExpiry when a transport must check freshness without interpreting the token or caching past its expiry.
func (*ServiceSigner) SignWithExpiry ¶ added in v0.9.1
func (s *ServiceSigner) SignWithExpiry(ctx context.Context, audience string, scope ServiceDelegation) (string, time.Time, error)
SignWithExpiry returns the exact signed expiry, not a separately sampled clock.
type ServiceSignerOptions ¶ added in v0.9.1
type ServiceSignerOptions struct {
Issuer string
Subject string
KeyID string
Key ed25519.PrivateKey
TTL time.Duration
AuthorizeDelegation ServiceDelegationPolicy
}
ServiceSignerOptions describes a fixed service identity. The constructor copies Key. TTL defaults to one minute and must be an integral second in 1s..5m.
type ServiceVerifier ¶ added in v0.9.1
type ServiceVerifier struct {
// contains filtered or unexported fields
}
ServiceVerifier is immutable after construction; its zero value is invalid.
func NewServiceVerifier ¶ added in v0.9.1
func NewServiceVerifier(o ServiceVerifierOptions) (*ServiceVerifier, error)
NewServiceVerifier validates configuration without panicking and snapshots all maps, slices, and key bytes. Subsequent caller changes do not rotate live keys.
func (*ServiceVerifier) Middleware ¶ added in v0.9.1
func (v *ServiceVerifier) Middleware(next http.Handler) http.Handler
Middleware verifies a present service credential, then injects ServicePrincipal and callctx.Caller from signed sub. Missing credentials remain anonymous; route guards decide whether authentication is required. Invalid credentials fail even on public application routes, except the built-in health/readiness probes. In mixed mode install user authentication first. Both credentials must agree on the exact tenant and partition, including empty scope. No user Actor is created.
func (*ServiceVerifier) Validate ¶ added in v0.9.1
func (v *ServiceVerifier) Validate() error
Validate permits composition roots to fail before migrations, Setup or listen.
type ServiceVerifierOptions ¶ added in v0.9.1
type ServiceVerifierOptions struct {
Audience string
Issuers map[string]ServiceIssuer
MaxTTL time.Duration
ClockSkew time.Duration
AuthorizeDelegation ServiceDelegationPolicy
}
ServiceVerifierOptions configures one recipient. Audience must be explicit. MaxTTL defaults to five minutes and may only reduce that limit. ClockSkew is optional, capped at 30 seconds, and never increases the allowed exp-iat span.