onetime

package
v2.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 3 Imported by: 3 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 onetime defines single-use, expiring tokens for account actions: password reset and email verification.

A token is a 256-bit random secret handed to the application once, to deliver (IAM sends no email). Stores keep only its SHA-256 hash, exactly like session secrets and invites.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalid is returned for unknown, used or expired tokens, and for a
	// token presented for another purpose.
	ErrInvalid = errors.New("onetime: invalid, used or expired token")

	// ErrNotFound is returned by stores for missing tokens.
	ErrNotFound = errors.New("onetime: not found")
)

Functions

This section is empty.

Types

type Purpose

type Purpose string

Purpose says what a token may be used for. A token never works for another purpose.

const (
	// PasswordReset tokens let the holder set a new password for Login.
	PasswordReset Purpose = "password_reset"

	// EmailVerification tokens prove that the holder receives mail at Email.
	EmailVerification Purpose = "email_verification"
)

func (Purpose) Valid

func (p Purpose) Valid() bool

Valid reports whether p is a known purpose.

type Store

type Store interface {
	// Create persists a new token. The ID and TokenHash are unique.
	Create(ctx context.Context, t *Token) error

	// GetByTokenHash returns a token (used or not), or ErrNotFound.
	GetByTokenHash(ctx context.Context, hash []byte) (*Token, error)

	// Consume marks the token as used at time at, but only if it has this
	// purpose and is still usable at that time; otherwise ErrInvalid.
	Consume(ctx context.Context, hash []byte, purpose Purpose, at time.Time) (*Token, error)

	// DeleteBySubject removes a subject's tokens of one purpose, used or
	// not. A new request supersedes older ones this way.
	DeleteBySubject(ctx context.Context, subjectID string, purpose Purpose) error
}

Store persists tokens. Implementations must be safe for concurrent use, and Consume must be atomic: of two concurrent calls for the same hash, at most one succeeds.

type Token

type Token struct {
	ID        string // public identifier, for audit
	TokenHash []byte // SHA-256 of the secret
	Purpose   Purpose
	SubjectID string // the subject the action applies to

	Login string // PasswordReset: the password login (provider ID)
	Email string // EmailVerification: the address being verified

	CreatedAt time.Time
	ExpiresAt time.Time
	UsedAt    time.Time // zero until consumed
}

Token is the stored record. It never contains the secret.

func (*Token) Usable

func (t *Token) Usable(now time.Time) bool

Usable reports whether the token can still be consumed at now.

Jump to

Keyboard shortcuts

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