twofactor

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 22 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 twofactor adds TOTP two-factor authentication to Canopy.

The module implements canopy.Module, canopy.RouteModule, and canopy.SignInInterceptor. Add it through canopy.Config.Modules.

The module stores an encrypted TOTP secret and hashed one-time backup codes. The default codec derives an AES-256-GCM key from the Canopy secret. The store must implement canopy.TwoFactorStore.

When a user enables two-factor, sign-in returns a step-up challenge instead of a session. The client completes the challenge with a TOTP code or a backup code to receive the session.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Authenticator

type Authenticator interface {
	GenerateSecret() (string, error)
	URI(secret, account, issuer string) string
	Validate(secret, code string, at time.Time) bool
	ValidateCounter(secret, code string, at time.Time) (counter int64, ok bool)
}

Authenticator abstracts the one-time password algorithm. The default is RFC 6238 TOTP. An application can supply its own.

type Codec

type Codec interface {
	Encrypt(plaintext []byte) (string, error)
	Decrypt(ciphertext string) ([]byte, error)
}

Codec encrypts and decrypts the TOTP secret at rest. The default derives an AES-256-GCM key from the Canopy secret.

type Module

type Module struct {
	// contains filtered or unexported fields
}

Module adds TOTP two-factor authentication with backup codes. It implements canopy.Module, canopy.RouteModule, and canopy.SignInInterceptor.

func New

func New(o Options) *Module

New returns a two-factor module.

func (*Module) AfterPrimaryAuth

func (m *Module) AfterPrimaryAuth(ctx context.Context, user canopy.User) (*canopy.StepUpChallenge, error)

AfterPrimaryAuth returns a challenge when the user has two-factor enabled. It returns nil to continue a normal sign-in.

func (*Module) ID

func (m *Module) ID() string

func (*Module) Init

func (m *Module) Init(core canopy.Core) error

func (*Module) Routes

func (m *Module) Routes() []canopy.Route

type Options

type Options struct {
	Issuer           string        // label shown in the authenticator app
	Authenticator    Authenticator // default: RFC 6238 TOTP
	Codec            Codec         // default: AES-GCM key derived from the Canopy secret
	BackupCodeCount  int           // default: 10
	ChallengeTTL     time.Duration // default: 10 minutes
	RecentAuthMaxAge time.Duration // default: 10 minutes
}

Options configures the two-factor module.

type SecretCodec

type SecretCodec struct {
	// contains filtered or unexported fields
}

SecretCodec is an AES-256-GCM codec. It derives its key from a secret with HKDF and a domain label. This keeps the key separate from other uses of the same secret.

func NewRotatingSecretCodec

func NewRotatingSecretCodec(secret string, previousSecrets ...string) (*SecretCodec, error)

NewRotatingSecretCodec derives its encryption key from secret and accepts ciphertext created with previousSecrets during a controlled key rotation. New ciphertext is always encrypted with secret.

func NewSecretCodec

func NewSecretCodec(secret string) (*SecretCodec, error)

NewSecretCodec derives an AES-256-GCM codec from secret.

func NewSecretCodecFromKeys

func NewSecretCodecFromKeys(current []byte, previous ...[]byte) (*SecretCodec, error)

NewSecretCodecFromKeys constructs a codec from AES-256 keys obtained from Core.ModuleKeys.

func (*SecretCodec) Decrypt

func (c *SecretCodec) Decrypt(ciphertext string) ([]byte, error)

func (*SecretCodec) Encrypt

func (c *SecretCodec) Encrypt(plaintext []byte) (string, error)

type TOTP

type TOTP struct {
	Digits int
	Period time.Duration
	Skew   int
	Hash   func() hash.Hash
}

TOTP is an RFC 6238 authenticator. The zero value uses safe defaults.

func NewTOTP

func NewTOTP() *TOTP

NewTOTP returns a TOTP authenticator with standard defaults: six digits, a thirty second period, one step of skew, and SHA-1.

func (*TOTP) GenerateCode

func (t *TOTP) GenerateCode(secret string, at time.Time) (string, error)

GenerateCode returns the TOTP code for secret at time at.

func (*TOTP) GenerateSecret

func (t *TOTP) GenerateSecret() (string, error)

func (*TOTP) URI

func (t *TOTP) URI(secret, account, issuer string) string

func (*TOTP) Validate

func (t *TOTP) Validate(secret, code string, at time.Time) bool

func (*TOTP) ValidateCounter

func (t *TOTP) ValidateCounter(secret, code string, at time.Time) (int64, bool)

ValidateCounter validates code and returns the matching RFC 6238 counter. Callers persist the counter to reject reuse of the same one-time code.

Jump to

Keyboard shortcuts

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