Documentation
¶
Overview ¶
Package magiclink issues and consumes single-use, short-lived login links.
A link carries a 256-bit random token (base64url, 43 characters). Only its SHA-256 digest is stored, together with the principal it authenticates, the exact registered redirect URI, the issue and expiry times and an optional purpose. Consume removes the record atomically before any other check, so a token is accepted at most once even when it is presented concurrently, and an expired or otherwise invalid token is burned rather than left for retry. Link lifetimes are bounded to 15 minutes.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Link ¶
Link is an issued magic link. Token is the raw secret; deliver URL to the user and do not log or persist either.
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore is a process-local Store for tests and single-process callers. Distributed deployments should provide a durable atomic implementation.
func NewMemoryStore ¶
func NewMemoryStore() *MemoryStore
NewMemoryStore returns an empty memory-backed store.
type Options ¶
type Options struct {
// Store persists link records. Required.
Store Store
// BaseURL is the https URL the token is appended to as the "token"
// query parameter. Required.
BaseURL string
// RedirectURIs lists the exact https redirect URIs a link may carry.
// Required, without duplicates.
RedirectURIs []string
// TTL is the link lifetime: positive and at most 15 minutes.
TTL time.Duration
// Random supplies token entropy. Defaults to crypto/rand.Reader; any
// replacement must be cryptographically secure.
Random io.Reader
// Clock returns the current time used for issue and expiry checks; its
// result is converted to UTC. Defaults to time.Now.
Clock func() time.Time
// Purpose is an optional label (at most 256 bytes) stored with records.
Purpose string
}
Options configures a Service.
type Record ¶
type Record struct {
Digest [32]byte
Principal authentication.Principal
RedirectURI string
IssuedAt time.Time
ExpiresAt time.Time
Purpose string
}
Record is the durable state for one magic link. Digest is SHA-256 over the returned base64url token; the raw token is never stored.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service issues and consumes magic links. It is safe for concurrent use when its Store is.
func NewService ¶
NewService validates options and returns a Service. It returns authentication.ErrInvalidCredential or authentication.ErrInvalidCallback for invalid options.
func (*Service) Consume ¶
Consume atomically consumes token and returns its principal. It returns authentication.ErrInvalidCredential for a malformed token, authentication.ErrReplay for an unknown or already consumed token, authentication.ErrExpired once the link's TTL has elapsed and authentication.ErrUnauthenticated for any other failure. Store errors are never returned verbatim.
func (*Service) Issue ¶
func (s *Service) Issue(ctx context.Context, principal authentication.Principal, redirectURI string) (Link, error)
Issue creates a link for a currently valid principal and one of the registered redirect URIs. It returns authentication.ErrInvalidCallback for an unregistered redirect URI and authentication.ErrInvalidPrincipal for an invalid or expired principal.
type Store ¶
type Store interface {
Create(context.Context, Record) error
Consume(context.Context, [32]byte) (Record, error)
}
Store persists magic-link state.
Create must fail with authentication.ErrReplay when the digest exists. Consume must atomically remove and return the record for a digest, and return authentication.ErrReplay when none exists, so that concurrent callers cannot both consume it.