emailcode

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2026 License: Apache-2.0 Imports: 14 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.

README

emailcode

Package emailcode issues and redeems single-use codes that an application sends by email, for example to confirm an address or reset a password. password uses it.

The package documentation is in emailcode.go. To read it with the API, run go doc ./internal/emailcode.

Contents

Name What it holds
emailcode.go The package and its documentation

The tests are in the *_test.go files next to the code.

See also

Documentation

Overview

Package emailcode issues and redeems single-use codes that an application sends by email, for example to confirm an address or reset a password. It knows nothing about passwords.

A code is a token of kind token.KindEmailCode in the library's token format: the brand, "e_", 43 base62 characters from crypto/rand (about 256 bits), and a 6-character checksum, so a mistyped code is refused before any store read and secret scanners recognize a leaked one. It is stored only as the SHA-256 of the token, as the ID of an auth.Ceremony: with 256 bits, a plain hash meets OWASP ASVS 5.0 requirement 6.5.2. A code works once, only for its kind, and for at most MaxTTL, or MaxProofTTL for a code that proves possession, such as a password reset.

When the flow started on a device that can show it was the starter, for example with a cookie or a session, the same email also carries a typed code: 8 characters from Crockford base32 (40 bits), shown as ABCD-EFGH. A typed code works only on that device, only once, and dies after MaxTries wrong guesses. With fewer than 112 bits, it is stored as an Argon2id hash with a 16-byte random salt (ASVS 6.5.2). Redeeming it spends the link too, so the email works once either way.

A code is never a factor: nothing here returns an auth.Proof.

Index

Constants

View Source
const MaxProofTTL = 10 * time.Minute

MaxProofTTL is the longest a code lives when it proves possession, such as a password reset code: ASVS 6.5.5 caps out-of-band codes at 10 minutes.

View Source
const MaxTTL = 72 * time.Hour

MaxTTL is the longest a code lives: 72 hours, for a code that confirms an address and proves nothing else.

View Source
const MaxTries = 5

MaxTries is how many wrong guesses a typed code survives: the fifth wrong guess deletes it (ASVS 6.6.3). The link in the same email keeps working.

View Source
const TypedLen = 8

TypedLen is the length of a typed code without its dash.

Variables

View Source
var DefaultParams = Params{Memory: 7168, Time: 5}

DefaultParams are the lightest row of the OWASP Password Storage Cheat Sheet's table of equal Argon2id settings: 7,168 KiB and 5 iterations, with 1 thread. A typed code lives at most a day or two and dies after MaxTries guesses, so the hash only has to outlast an offline search of 2^40 codes for the code's lifetime, and the low memory keeps many concurrent sign-ups cheap.

View Source
var ErrInvalid = errors.New("emailcode: invalid code")

ErrInvalid means the code is malformed, unknown, used, expired, or of another kind. Every cause returns this error.

Functions

func FormatTyped

func FormatTyped(plain string) string

FormatTyped shows a canonical typed code as ABCD-EFGH.

func IsTyped

func IsTyped(raw string) bool

IsTyped reports whether raw has the form of a typed code rather than a link code.

func ParseTyped

func ParseTyped(raw string) (string, bool)

ParseTyped returns the canonical form of a typed code: 8 characters of Crockford base32 in upper case. It ignores case, spaces, and dashes, and reads O as 0 and I and L as 1. It returns false for anything else.

Types

type Code

type Code struct {
	Kind      string
	User      auth.UserID
	Binding   string
	Data      []byte
	ExpiresAt time.Time
	// contains filtered or unexported fields
}

Code is a code that Peek or Redeem found.

func Peek

func Peek(ctx context.Context, rt auth.Runtime, fs auth.FactorStore, raw string, kinds ...string) (Code, error)

Peek returns the code raw names, without spending it, when it is one of kinds and hasn't expired. It returns ErrInvalid otherwise, and a store error as it is.

func PeekTyped

func PeekTyped(ctx context.Context, rt auth.Runtime, fs auth.FactorStore, typed, device string, kinds ...string) (Code, error)

PeekTyped returns the code that device's typed code came with, without spending it, when typed matches, the code is one of kinds, and it hasn't expired. Pass the result to Spend. A wrong guess counts toward MaxTries, and the last one deletes the typed code. A typed code of another kind counts nothing. Every failure is ErrInvalid; a store error returns as it is. It costs at most one Argon2id computation.

Each guess takes the typed code out of the store and puts it back, so concurrent guesses can't exceed MaxTries: a guess that finds nothing fails.

func Redeem

func Redeem(ctx context.Context, rt auth.Runtime, fs auth.FactorStore, raw string, kinds ...string) (Code, error)

Redeem spends the code raw names and returns it, in one store operation, when it is one of kinds and hasn't expired, so a code works at most once, even under concurrency. It returns ErrInvalid otherwise, and a store error as it is.

func RedeemTyped

func RedeemTyped(ctx context.Context, rt auth.Runtime, fs auth.FactorStore, typed, device string, kinds ...string) (Code, error)

RedeemTyped is PeekTyped followed by Spend.

func Spend

func Spend(ctx context.Context, rt auth.Runtime, fs auth.FactorStore, c Code, kinds ...string) (Code, error)

Spend spends a code that Peek or PeekTyped returned, in one store operation, so it works at most once, even under concurrency. It returns the code as stored, ErrInvalid when it was spent or expired in the meantime or isn't one of kinds, and a store error as it is.

type Issued

type Issued struct {
	// Token is the link code: put it in the URL fragment of a link.
	Token string
	// Code is the typed code, formatted as ABCD-EFGH, or empty when
	// Spec.Device was empty.
	Code string
	// ExpiresAt is when both stop working.
	ExpiresAt time.Time
}

Issued is a code that Issue stored.

func Issue

func Issue(ctx context.Context, rt auth.Runtime, fs auth.FactorStore, s Spec) (Issued, error)

Issue stores a new code for s and returns it and when it expires. Send the code to the person; it is never stored. With s.Device set, Issue also stores a typed code, which costs one Argon2id computation.

type Params

type Params struct {
	Memory uint32 `json:"m"` // KiB
	Time   uint32 `json:"t"`
}

Params are the Argon2id parameters of a typed code's hash.

type Spec

type Spec struct {
	// Kind binds the code to its purpose, for example "password.reset".
	Kind string
	// User binds the code to its user, or is empty.
	User auth.UserID
	// Binding ties the code to the browser that asked for it, or is empty.
	Binding string
	// Data binds the code to the rest of what it confirms.
	Data []byte
	// TTL is how long the code lives: more than 0 and at most MaxTTL, or
	// MaxProofTTL when Proves is set.
	TTL time.Duration
	// Proves is true for a code that proves possession of the mailbox to
	// change a credential, such as a password reset code.
	Proves bool
	// Device, when not empty, adds a typed code that works only when the
	// same Device is presented: a secret that only the device that started
	// the flow holds, such as a cookie value or a session ID. A later
	// typed code for the same Device replaces an earlier one; the earlier
	// link keeps working.
	Device string
	// Params hash the typed code; zero means DefaultParams.
	Params Params
}

Spec describes a code to issue.

Jump to

Keyboard shortcuts

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