passkey

package
v0.9.13 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 11 Imported by: 0 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

Overview

Package passkey is the WebAuthn side of passkeys (design/passkeys.md §14): the ceremonies of registering a credential and signing in with one, and the credential model with its store. It is a leaf: it knows nothing about sessions, strategies, throttling, email or organizations. It returns what a ceremony proved (the user, the credential, the flags, whether the counter went backwards) and the caller decides what that means.

It imports the standard library and go-webauthn only; a test fails if that changes.

Index

Constants

View Source
const (
	KindRegister = "passkey-register"
	KindLogin    = "passkey-login"
)

The kinds of ceremony state kept between the two calls of a ceremony.

View Source
const CeremonyTTL = 5 * time.Minute

CeremonyTTL is how long the state of a ceremony waits for its second call.

Variables

View Source
var (
	ErrInvalid           = errors.New("passkey: the ceremony did not verify")
	ErrUnknownCredential = errors.New("passkey: unknown credential")
	ErrNoCredentials     = errors.New("passkey: the user has no passkeys")
	ErrCeremony          = errors.New("passkey: the ceremony is unknown, used or expired")
)

The errors of the package. A verification failure is an *Error whose Reason says why, for the audit log, and which is ErrInvalid: callers answer all of them alike.

Functions

This section is empty.

Types

type Ceremonies

type Ceremonies interface {
	Put(ctx context.Context, kind string, data []byte, userID string, ttl time.Duration) (id string, err error)
	// Claim returns the data once: ErrCeremony for an unknown, used, expired or other-kind id.
	Claim(ctx context.Context, id, kind string) (data []byte, userID string, err error)
}

Ceremonies keeps the library's session data between the two calls of a ceremony.

type Config

type Config struct {
	RPID   string
	RPName string
	// Origins are web origins (https://app.example.com) and native app origins
	// (android:apk-key-hash:<hash>). A web origin's host is the RPID or a subdomain of it.
	Origins []string
	// AllowLoopbackHTTP lets http://localhost and other loopback origins stand (a development server).
	AllowLoopbackHTTP bool
}

Config is the relying party: the domain passkeys are bound to, its display name and the origins a ceremony may run from. The id is permanent in practice: a credential belongs to the id it was registered under.

func (Config) Validate

func (c Config) Validate() error

Validate says what is wrong with the configuration, or nil.

type Credential

type Credential struct {
	ID           string // backd's id
	UserID       string
	CredentialID []byte // the authenticator's id
	PublicKey    []byte // COSE key
	// RPID is the relying party id the credential was registered under.
	RPID       string
	SignCount  uint32
	Transports []string
	AAGUID     []byte
	// BackupEligible says the passkey syncs between devices; BackupState whether it has been synced.
	BackupEligible bool
	BackupState    bool
	// CloneSuspected is set when a counter went backwards.
	CloneSuspected bool
	Name           string
	CreatedAt      time.Time
	LastUsedAt     time.Time
}

Credential is a registered passkey. Everything in it is public.

type Error

type Error struct{ Reason string }

Error is a ceremony that did not verify.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Is

func (e *Error) Is(target error) bool

type LoginResult

type LoginResult struct {
	UserID     string
	Credential Credential
	// UserVerified, BackupEligible and BackupState are the flags of this assertion.
	UserVerified   bool
	BackupEligible bool
	BackupState    bool
	// CounterRegressed says the authenticator's counter did not go forward though it had counted
	// before: the credential may be cloned. The sign-in still verified; the stored counter is left
	// and the credential is flagged (CloneSuspected), so the caller can decide. A counter that
	// stays 0 (authenticators that do not count) is never a regression.
	CounterRegressed bool
}

LoginResult is what a sign-in proved.

type Service

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

Service runs the ceremonies.

func New

func New(cfg Config, store Store, ceremonies Ceremonies, now func() time.Time) (*Service, error)

New builds the service from a valid configuration.

func (*Service) BeginLogin

func (s *Service) BeginLogin(ctx context.Context, userID string) (options []byte, ceremony string, err error)

BeginLogin starts a sign-in. With userID empty it is discoverable: the browser offers the passkeys it holds for the relying party and the answer says who the user is. With a userID only that user's credentials are allowed (ErrNoCredentials when they have none).

func (*Service) BeginRegistration

func (s *Service) BeginRegistration(ctx context.Context, info UserInfo) (options []byte, ceremony string, err error)

BeginRegistration starts adding a passkey for a user: options are the JSON the browser's navigator.credentials.create takes (the "publicKey" member included), and ceremony the id to send back with FinishRegistration. Credentials the user already has are excluded; the credential must be discoverable (a resident key) and the user verified.

func (*Service) FinishLogin

func (s *Service) FinishLogin(ctx context.Context, ceremony string, body []byte) (LoginResult, error)

FinishLogin checks the browser's answer (the JSON of the assertion). Origin, relying party id, challenge, user verification and the signature are checked; anything that does not verify is an *Error, and a credential nobody registered is ErrUnknownCredential.

func (*Service) FinishRegistration

func (s *Service) FinishRegistration(ctx context.Context, ceremony, userID string, body []byte, name string) (Credential, error)

FinishRegistration checks the browser's answer (the JSON of the PublicKeyCredential) against the ceremony, which must have been begun for userID, and stores the credential under name. Origin, relying party id, challenge and user verification are checked by the library; a mismatch is an *Error.

type Store

type Store interface {
	Add(ctx context.Context, c Credential) error
	// ByCredentialID returns ErrUnknownCredential when there is none.
	ByCredentialID(ctx context.Context, id []byte) (Credential, error)
	ListByUser(ctx context.Context, userID string) ([]Credential, error)
	Update(ctx context.Context, credentialID []byte, u Update) error
	Delete(ctx context.Context, userID, id string) error
	CountByUser(ctx context.Context, userID string) (int, error)
	// UserHandle returns the user's handle (32 random bytes), making it the first time.
	UserHandle(ctx context.Context, userID string) ([]byte, error)
	// UserByHandle returns ErrUnknownCredential when no user has that handle.
	UserByHandle(ctx context.Context, handle []byte) (string, error)
}

Store keeps credentials and the opaque handle each user has. Eight methods, implemented by the MongoDB store and an in-memory one.

type Update

type Update struct {
	SignCount      *uint32
	LastUsedAt     *time.Time
	BackupState    *bool
	CloneSuspected *bool
	Name           *string
}

Update is a change to a stored credential; nil fields stay as they are.

type UserInfo

type UserInfo struct {
	ID          string
	Name        string // shown by the authenticator, such as the email
	DisplayName string
}

UserInfo is who a credential is registered for.

Directories

Path Synopsis
Package passkeytest has what tests of passkeys need: an in-memory store and ceremony keeper, and a software authenticator that plays the browser's and the authenticator's side of the ceremonies.
Package passkeytest has what tests of passkeys need: an in-memory store and ceremony keeper, and a software authenticator that plays the browser's and the authenticator's side of the ceremonies.

Jump to

Keyboard shortcuts

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