totp

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: Apache-2.0 Imports: 12 Imported by: 1 module, 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 totp manages authenticator-app MFA and recovery codes.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAlreadyEnabled     = errors.New("TOTP is already enabled")
	ErrAttemptBlocked     = errors.New("TOTP attempt blocked")
	ErrConflict           = errors.New("TOTP record conflict")
	ErrInactiveChallenge  = errors.New("TOTP challenge is inactive")
	ErrInactiveEnrollment = errors.New("TOTP enrollment is inactive")
	ErrInvalidChallenge   = errors.New("invalid or expired TOTP challenge")
	ErrInvalidCode        = errors.New("invalid TOTP or recovery code")
	ErrInvalidConfig      = errors.New("invalid TOTP configuration")
	ErrInvalidInput       = errors.New("invalid TOTP input")
	ErrInvalidRecord      = errors.New("invalid TOTP record")
	ErrNotFound           = errors.New("TOTP record not found")
)

Functions

This section is empty.

Types

type Attempt

type Attempt struct {
	Operation Operation
	SubjectID string
	SourceKey string
}

type AttemptGuard

type AttemptGuard interface {
	Check(ctx context.Context, attempt Attempt) error
}

type Authentication

type Authentication struct {
	SubjectID        string
	UsedRecoveryCode bool
}

type Challenge

type Challenge struct {
	SubjectID string
	TokenHash ChallengeHash
	CreatedAt time.Time
	ExpiresAt time.Time
}

type ChallengeHash

type ChallengeHash token.Hash

type ChallengeStarted

type ChallengeStarted struct {
	Token     string
	ExpiresAt time.Time
}

type Config

type Config struct {
	Issuer             string
	EnrollmentLifetime time.Duration
	ChallengeLifetime  time.Duration
	RecoveryCodeCount  int
	AttemptGuard       AttemptGuard
	SecurityEvents     SecurityEventSink
	Now                func() time.Time
}

type Credential

type Credential struct {
	SubjectID          string
	Secret             string
	EnabledAt          time.Time
	LastUsedCounter    uint64
	HasLastUsedCounter bool
}

type Enrollment

type Enrollment struct {
	SubjectID string
	Secret    string
	CreatedAt time.Time
	ExpiresAt time.Time
}

type EnrollmentConfirmed

type EnrollmentConfirmed struct {
	RecoveryCodes []string
}

type EnrollmentStarted

type EnrollmentStarted struct {
	Secret    string
	URI       string
	ExpiresAt time.Time
}

type EventType

type EventType string
const (
	EventEnabled      EventType = "totp_enabled"
	EventDisabled     EventType = "totp_disabled"
	EventFailed       EventType = "totp_failed"
	EventVerified     EventType = "totp_verified"
	EventRecoveryUsed EventType = "totp_recovery_used"
)

type Manager

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

func NewManager

func NewManager(store Store, config Config) (*Manager, error)

func (*Manager) BeginChallenge

func (manager *Manager) BeginChallenge(ctx context.Context, subjectID string) (ChallengeStarted, error)

func (*Manager) BeginEnrollment

func (manager *Manager) BeginEnrollment(
	ctx context.Context,
	subjectID string,
	accountName string,
) (EnrollmentStarted, error)

func (*Manager) ConfirmEnrollment

func (manager *Manager) ConfirmEnrollment(
	ctx context.Context,
	subjectID string,
	code string,
	sourceKey string,
) (EnrollmentConfirmed, error)

func (*Manager) Disable

func (manager *Manager) Disable(ctx context.Context, subjectID, sourceKey string) error

func (*Manager) IsEnabled

func (manager *Manager) IsEnabled(ctx context.Context, subjectID string) (bool, error)

func (*Manager) Recover

func (manager *Manager) Recover(ctx context.Context, input VerifyInput) (Authentication, error)

func (*Manager) Verify

func (manager *Manager) Verify(ctx context.Context, input VerifyInput) (Authentication, error)

type Operation

type Operation string
const (
	OperationConfirmEnrollment Operation = "confirm_enrollment"
	OperationVerifyCode        Operation = "verify_code"
	OperationUseRecovery       Operation = "use_recovery"
)

type RecoveryCodeHash

type RecoveryCodeHash [32]byte

func HashRecoveryCode

func HashRecoveryCode(rawCode string) (RecoveryCodeHash, error)

type SecurityEvent

type SecurityEvent struct {
	Type       EventType
	SubjectID  string
	SourceKey  string
	OccurredAt time.Time
}

type SecurityEventSink

type SecurityEventSink interface {
	Record(ctx context.Context, event SecurityEvent)
}

type Store

type Store interface {
	IsEnabled(ctx context.Context, subjectID string) (bool, error)
	BeginEnrollment(ctx context.Context, enrollment Enrollment) error
	FindEnrollment(ctx context.Context, subjectID string) (Enrollment, error)
	Enable(ctx context.Context, subjectID string, confirmedCounter uint64, recoveryCodes []RecoveryCodeHash, enabledAt time.Time) error
	Disable(ctx context.Context, subjectID string, disabledAt time.Time) error
	CreateChallenge(ctx context.Context, challenge Challenge) error
	FindChallenge(ctx context.Context, challengeHash ChallengeHash) (Challenge, Credential, error)
	CompleteCode(ctx context.Context, challengeHash ChallengeHash, counter uint64, completedAt time.Time) (string, error)
	CompleteRecovery(ctx context.Context, challengeHash ChallengeHash, recoveryCode RecoveryCodeHash, completedAt time.Time) (string, error)
}

Store encrypts TOTP secrets at rest. Enable consumes the enrollment; challenge completion and replay protection are atomic.

type VerifyInput

type VerifyInput struct {
	ChallengeToken string
	Code           string
	SourceKey      string
}

Jump to

Keyboard shortcuts

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