Documentation
¶
Overview ¶
Package passkey provides WebAuthn registration and authentication. Package passkey 提供 WebAuthn 注册与身份验证。
Index ¶
- Variables
- type Credential
- type Flow
- type FlowStore
- type Info
- type Login
- type LoginResult
- type MemoryFlowStore
- type Options
- type Registration
- type Service
- func (s *Service) BeginLogin(ctx context.Context, sessionOnly bool) (Login, error)
- func (s *Service) BeginRegistration(ctx context.Context, userID string) (Registration, error)
- func (s *Service) Delete(ctx context.Context, userID, id string) error
- func (s *Service) FinishLogin(ctx context.Context, flowID string, response []byte) (LoginResult, error)
- func (s *Service) FinishRegistration(ctx context.Context, userID, flowID, name string, response []byte) (Info, error)
- func (s *Service) List(ctx context.Context, userID string) ([]Info, error)
- func (s *Service) Rename(ctx context.Context, userID, id, name string) error
- type Store
- type User
Constants ¶
This section is empty.
Variables ¶
var ( // ErrRejected reports an invalid, expired, or already consumed ceremony. // ErrRejected 表示流程无效、过期或已消费。 ErrRejected = errors.New("passkey authentication rejected") // ErrNotFound reports a credential absent from the requested user's account. // ErrNotFound 表示指定用户账户中不存在该凭据。 ErrNotFound = errors.New("passkey not found") // ErrConflict reports an existing credential ID or flow ID. // ErrConflict 表示凭据 ID 或流程 ID 已存在。 ErrConflict = errors.New("passkey already exists") // ErrInvalidName reports an empty or oversized credential name. // ErrInvalidName 表示凭据名称为空或过长。 ErrInvalidName = errors.New("passkey name must contain 1 to 100 characters") // ErrCapacity reports a full flow store. // ErrCapacity 表示流程存储已满。 ErrCapacity = errors.New("passkey flow capacity reached") )
Functions ¶
This section is empty.
Types ¶
type Credential ¶
type Credential struct {
UserID string
Name string
CreatedAt time.Time
LastUsedAt time.Time
Data webauthn.Credential
}
Credential is the persisted record. Data must be stored in full. Credential 是持久化记录。Data 必须完整保存。
type Flow ¶
type Flow struct {
RPID string
Operation string
UserID string
SessionOnly bool
Session webauthn.SessionData
}
Flow is server-owned ceremony state; never accept it from clients. Flow 是服务端管理的流程状态,不能从客户端接收。
type FlowStore ¶
type FlowStore interface {
Create(ctx context.Context, id string, flow Flow) error
Consume(ctx context.Context, id string) (Flow, bool, error)
}
FlowStore stores ceremonies until Flow.Session.Expires. Create must not overwrite; Consume must atomically remove and return a flow, returning false for absent/expired IDs. FlowStore 存储流程至 Flow.Session.Expires。Create 不能覆盖;Consume 必须原子移除并返回流程, 对不存在或过期的 ID 返回 false。
type Info ¶
type Info struct {
ID string `json:"id"`
Name string `json:"name"`
CreatedAt time.Time `json:"created_at"`
LastUsedAt time.Time `json:"last_used_at,omitzero"`
}
Info is user-visible credential metadata; it excludes the public key. Info 是用户可见的凭据元数据,不含公钥。
type Login ¶
type Login struct {
FlowID string `json:"flow_id"`
Options *protocol.CredentialAssertion `json:"options"`
}
Login contains browser assertion options and an opaque flow ID. Login 包含浏览器断言选项与不透明流程 ID。
type LoginResult ¶
LoginResult contains the verified account and server-bound session preference. LoginResult 包含已验证账户与绑定在服务端的会话偏好。
type MemoryFlowStore ¶
type MemoryFlowStore struct {
// contains filtered or unexported fields
}
MemoryFlowStore stores single-use flows in one process without background goroutines. MemoryFlowStore 在单进程内存储一次性流程,不启动后台 goroutine。
func NewMemoryFlowStore ¶
func NewMemoryFlowStore(capacity int) *MemoryFlowStore
NewMemoryFlowStore constructs a bounded store. Non-positive capacity panics. NewMemoryFlowStore 构造有容量上限的存储。capacity 非正数时 panic。
type Options ¶
type Options struct {
RPID string
RPDisplayName string
Origins []string
Store Store
Flows FlowStore
LookupUser func(context.Context, string) (User, bool, error)
// Timeout defaults to five minutes.
// Timeout 默认五分钟。
Timeout time.Duration
}
Options configures one relying party. LookupUser must return ok=false for missing or disabled accounts. Stores must be scoped to this relying party. Options 配置单个依赖方。LookupUser 对不存在或禁用账户必须返回 ok=false。 存储必须限定在此依赖方范围内。
type Registration ¶
type Registration struct {
FlowID string `json:"flow_id"`
Options *protocol.CredentialCreation `json:"options"`
}
Registration contains browser creation options and an opaque flow ID. Registration 包含浏览器创建选项与不透明流程 ID。
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service verifies passkeys without issuing sessions. Construct with New. Service 验证通行密钥,不签发会话。使用 New 构造。
func New ¶
New constructs a service requiring discoverable credentials and user verification. New 构造要求可发现凭据及用户验证的服务。
func (*Service) BeginLogin ¶
BeginLogin starts a username-less login and binds its session preference. BeginLogin 开始无用户名登录并绑定会话偏好。
func (*Service) BeginRegistration ¶
BeginRegistration starts binding a credential. The caller must authorize userID. BeginRegistration 开始绑定凭据。调用方必须已授权 userID。
func (*Service) Delete ¶
Delete removes an authorized user's credential without revoking sessions. Delete 删除已授权用户的凭据,不撤销现有会话。
func (*Service) FinishLogin ¶
func (s *Service) FinishLogin(ctx context.Context, flowID string, response []byte) (LoginResult, error)
FinishLogin consumes a flow, verifies the assertion, checks the current account, and persists usage before returning an identity. It never issues tokens. FinishLogin 消费流程、验证断言、检查当前账户并保存使用状态后返回身份,不签发 token。
func (*Service) FinishRegistration ¶
func (s *Service) FinishRegistration(ctx context.Context, userID, flowID, name string, response []byte) (Info, error)
FinishRegistration consumes a flow and saves the verified credential. The caller must authorize the same userID as BeginRegistration. FinishRegistration 消费流程并保存验证后的凭据。 调用方必须授权与 BeginRegistration 相同的 userID。
type Store ¶
type Store interface {
Insert(context.Context, Credential) error
Get(ctx context.Context, id string) (Credential, bool, error)
List(ctx context.Context, userID string) ([]Credential, error)
UpdateUsage(ctx context.Context, id, userID string, authenticator webauthn.Authenticator, flags webauthn.CredentialFlags, at time.Time) (bool, error)
Rename(ctx context.Context, id, userID, name string) (bool, error)
Delete(ctx context.Context, id, userID string) (bool, error)
}
Store persists credentials. All methods must be concurrency-safe and return owned data. String credential IDs are unpadded base64url encodings of Data.ID. Insert must return ErrConflict for duplicate Data.ID and never overwrite a record. UpdateUsage must only update usage fields on an existing (id,userID) record; it must not insert, replace the key/owner/name, or decrease stored counters. Rename and Delete must also match both id and userID. False means not found. Store 持久化凭据。所有方法必须并发安全,返回独立数据。 字符串凭据 ID 是 Data.ID 的无填充 base64url 编码。 Insert 遇到重复 Data.ID 必须返回 ErrConflict,不能覆盖记录。 UpdateUsage 只能更新现有 (id,userID) 记录的使用状态,不能插入、替换密钥、所有者、名称或降低计数器。 Rename 和 Delete 也必须匹配 id 和 userID;false 表示不存在。