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
- Variables
- type Ceremonies
- type Config
- type Credential
- type Error
- type LoginResult
- type Service
- func (s *Service) BeginLogin(ctx context.Context, userID string) (options []byte, ceremony string, err error)
- func (s *Service) BeginRegistration(ctx context.Context, info UserInfo) (options []byte, ceremony string, err error)
- func (s *Service) FinishLogin(ctx context.Context, ceremony string, body []byte) (LoginResult, error)
- func (s *Service) FinishRegistration(ctx context.Context, ceremony, userID string, body []byte, name string) (Credential, error)
- type Store
- type Update
- type UserInfo
Constants ¶
const ( KindRegister = "passkey-register" KindLogin = "passkey-login" )
The kinds of ceremony state kept between the two calls of a ceremony.
const CeremonyTTL = 5 * time.Minute
CeremonyTTL is how long the state of a ceremony waits for its second call.
Variables ¶
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.
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 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 (*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.
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. |