magiclink

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: Apache-2.0 Imports: 12 Imported by: ? 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 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 struct {
	URL       *url.URL
	Token     string
	ExpiresAt time.Time
}

Link is an issued magic link. Token is the raw secret; deliver URL to the user and do not log or persist either.

func (Link) Clone

func (l Link) Clone() Link

Clone returns an independently owned link.

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.

func (*MemoryStore) Consume

func (s *MemoryStore) Consume(ctx context.Context, digest [32]byte) (Record, error)

Consume atomically removes and returns the record for digest.

func (*MemoryStore) Create

func (s *MemoryStore) Create(ctx context.Context, record Record) error

Create stores a valid record unless its digest is already present.

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

func NewService(options Options) (*Service, error)

NewService validates options and returns a Service. It returns authentication.ErrInvalidCredential or authentication.ErrInvalidCallback for invalid options.

func (*Service) Consume

func (s *Service) Consume(ctx context.Context, token string) (authentication.Principal, error)

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.

Jump to

Keyboard shortcuts

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