totp

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: AGPL-3.0 Imports: 21 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.

Documentation

Overview

Package totp is the second factor of the users (C-03.FR-10, FR-11, FR-27): TOTP of RFC 6238 — six digits, 30-second steps, HMAC-SHA-1 for authenticator compatibility and one step of tolerance either way — with the seed stored as a Secret through the Keyring, a pending seed between the start of an enrolment and its confirmation, a replay guard that accepts each time step once, and single-use recovery codes hashed with argon2id. Time steps follow the real clock, so that the development clock never moves them away from the authenticator app's; timestamps follow the business clock.

Index

Constants

View Source
const (
	Digits = 6
	Period = 30 * time.Second
	// Skew is how many steps before and after the current one a code may come from.
	Skew = 1
	// Issuer names Muster in the authenticator app.
	Issuer = "Muster"
)

The parameters of the codes (RFC 6238).

View Source
const (
	ActionEnrolled                 = "totp.enrolled"
	ActionRemoved                  = "totp.removed"
	ActionRecoveryCodesRegenerated = "totp.recovery_codes_regenerated"
	ActionReset                    = "totp.reset"
)

The Audit log actions of TOTP (C-03.FR-14).

View Source
const RecoveryCodes = 10

RecoveryCodes is auth.totp_recovery_codes (P-08): how many single-use recovery codes an enrolment issues.

Variables

View Source
var (
	// ErrAlreadyEnrolled is an enrolment of a user who has TOTP.
	ErrAlreadyEnrolled = errors.New("TOTP is already enrolled")
	// ErrNotStarted is a confirmation without a begun enrolment.
	ErrNotStarted = errors.New("no TOTP enrolment was begun")
	// ErrNotEnrolled is a change of TOTP that the user does not have.
	ErrNotEnrolled = errors.New("the user has no TOTP")
	// ErrUserNotFound is a user that does not exist in the Organization or was deleted.
	ErrUserNotFound = errors.New("no such user")
	// ErrOwnTOTP is an Admin's reset of their own TOTP, which needs the proof of a removal (C-03.FR-27).
	ErrOwnTOTP = errors.New("remove your own TOTP from your profile, with a proof")
)

Functions

func Code

func Code(seed []byte, step int64) string

Code is the code of seed at the time step step (RFC 4226 with the step as the counter, RFC 6238).

Types

type Attempts

type Attempts interface {
	Attempt(ctx context.Context, sess auth.Session, addr netip.Addr, method string,
		check func(context.Context) (bool, error)) error
	CheckPassword(ctx context.Context, sess auth.Session, password string) (bool, error)
}

Attempts is what the changes of a user's own TOTP need of internal/auth: proofs checked under the sign-in throttle.

type Enrolment

type Enrolment struct {
	Secret string
	URI    string
}

Enrolment is a begun enrolment: the seed in base32 for manual entry and the otpauth URI of the QR code.

type Queries

type Queries interface {
	GetTOTPUser(ctx context.Context, arg dbgen.GetTOTPUserParams) (dbgen.GetTOTPUserRow, error)
	GetTOTPUserByPublicID(ctx context.Context, arg dbgen.GetTOTPUserByPublicIDParams) (dbgen.GetTOTPUserByPublicIDRow,
		error)
	GetTOTPUserByLogin(ctx context.Context, arg dbgen.GetTOTPUserByLoginParams) (dbgen.GetTOTPUserByLoginRow, error)
	GetTOTP(ctx context.Context, arg dbgen.GetTOTPParams) (dbgen.GetTOTPRow, error)
	SetPendingSeed(ctx context.Context, arg dbgen.SetPendingSeedParams) (int64, error)
	ActivateSeed(ctx context.Context, arg dbgen.ActivateSeedParams) (int64, error)
	UseStep(ctx context.Context, arg dbgen.UseStepParams) (int64, error)
	DeleteTOTP(ctx context.Context, arg dbgen.DeleteTOTPParams) (int64, error)
	ListUnusedRecoveryCodes(ctx context.Context, arg dbgen.ListUnusedRecoveryCodesParams) (
		[]dbgen.ListUnusedRecoveryCodesRow, error)
	CountUnusedRecoveryCodes(ctx context.Context, arg dbgen.CountUnusedRecoveryCodesParams) (int64, error)
	UseRecoveryCode(ctx context.Context, arg dbgen.UseRecoveryCodeParams) (int64, error)
	InsertRecoveryCode(ctx context.Context, arg dbgen.InsertRecoveryCodeParams) error
	DeleteUnusedRecoveryCodes(ctx context.Context, arg dbgen.DeleteUnusedRecoveryCodesParams) (int64, error)
	DeleteRecoveryCodes(ctx context.Context, arg dbgen.DeleteRecoveryCodesParams) (int64, error)
	ActivateSession(ctx context.Context, arg dbgen.ActivateSessionParams) (int64, error)
	EndSessionsOfUser(ctx context.Context, arg dbgen.EndSessionsOfUserParams) (int64, error)
	audit.Store
}

Queries are the queries of the package, with the insert of the Audit log.

type Removal

type Removal struct {
	Password     string
	TOTPCode     string
	RecoveryCode string
}

Removal is the proof a user gives to remove their TOTP: the current password, a current TOTP code or a recovery code (C-03.FR-27). The password is checked when it is given.

type Service

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

Service keeps the TOTP of the users of the Organization.

func New

func New(orgID int64, s Store, k *keyring.Keyring, w *audit.Writer, clocks clock.Clocks, a Attempts) *Service

New returns the Service of the Organization orgID. The Keyring encrypts and decrypts the seeds; attempts throttles the proofs a user gives on their own TOTP. A Service that only resets TOTP, as the CLI does, needs neither.

func (*Service) Begin

func (s *Service) Begin(ctx context.Context, sess auth.Session) (Enrolment, error)

Begin starts the enrolment of sess's user with a new pending seed, which replaces a pending one; it is ErrAlreadyEnrolled when the user has TOTP. The seed is returned once and stored only as a Secret.

func (*Service) Confirm

func (s *Service) Confirm(ctx context.Context, sess auth.Session, code string, addr netip.Addr) ([]string, error)

Confirm completes the enrolment of sess's user with a current code of the pending seed and returns new recovery codes, shown once. Without a begun enrolment it is ErrNotStarted, with TOTP already on ErrAlreadyEnrolled. The code is checked under the sign-in throttle; a wrong one is auth.ErrInvalidCredentials. The time step of the code is used, and a session in the state totp_enrolment_required becomes active.

func (*Service) RegenerateRecoveryCodes

func (s *Service) RegenerateRecoveryCodes(ctx context.Context, sess auth.Session, code string,
	addr netip.Addr) ([]string, error)

RegenerateRecoveryCodes replaces the unused recovery codes of sess's user with new ones, shown once, after a current TOTP code that was not used before; without TOTP it is ErrNotEnrolled, with a wrong code auth.ErrInvalidCredentials.

func (*Service) Remove

func (s *Service) Remove(ctx context.Context, sess auth.Session, p Removal, addr netip.Addr) error

Remove removes the TOTP of sess's user, an enrolment that is only begun included, with its recovery codes, after a right proof; a wrong proof is auth.ErrInvalidCredentials and changes nothing, and a user without TOTP is ErrNotEnrolled. The proof is checked under the sign-in throttle. Under a TOTP policy that covers the user, the next sign-in demands enrolment again.

func (*Service) Reset

func (s *Service) Reset(ctx context.Context, actor audit.Actor, t audit.Transport, addr netip.Addr,
	publicID string) error

Reset removes the TOTP of the user with publicID and its recovery codes for an Admin (C-03.FR-11), and ends the user's sessions, so that the user signs in again and, under a TOTP policy that covers them, enrols again. A user that does not exist or was deleted is ErrUserNotFound; a user without TOTP is left as it is and nothing is recorded. An Admin's own TOTP is ErrOwnTOTP: removing it needs the proof of Remove, so that a stolen session cannot drop it.

func (*Service) ResetByLogin

func (s *Service) ResetByLogin(ctx context.Context, actorName, login string) (string, bool, error)

ResetByLogin is `muster admin reset-totp --actor <name> <login>`: Reset of the account with the login, compared lowercased, recorded with the --actor name and the Transport cli. It returns the public_id of the account and whether it had TOTP.

func (*Service) Status

func (s *Service) Status(ctx context.Context, userID int64) (Status, error)

Status reads the TOTP of the user userID.

func (*Service) Verify

func (s *Service) Verify(ctx context.Context, userID int64, p auth.Proof) (bool, error)

Verify is the second factor of sign-in (auth.SecondFactor): a current TOTP code of the user's seed whose time step was not used before, or else an unused recovery code; a right one is used up. A user without TOTP has no right code.

type Status

type Status struct {
	Enrolled               bool
	EnrolledAt             *time.Time
	Pending                bool
	RecoveryCodesRemaining int64
}

Status is the TOTP of a user as the profile shows it.

type Store

type Store interface {
	Queries
	InTx(ctx context.Context, f func(Queries) error) error
}

Store runs the queries alone or in one transaction.

func NewStore

func NewStore(pool *pgxpool.Pool) Store

NewStore is the Store over the main pool.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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