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
- Variables
- func Code(seed []byte, step int64) string
- type Attempts
- type Enrolment
- type Queries
- type Removal
- type Service
- func (s *Service) Begin(ctx context.Context, sess auth.Session) (Enrolment, error)
- func (s *Service) Confirm(ctx context.Context, sess auth.Session, code string, addr netip.Addr) ([]string, error)
- func (s *Service) RegenerateRecoveryCodes(ctx context.Context, sess auth.Session, code string, addr netip.Addr) ([]string, error)
- func (s *Service) Remove(ctx context.Context, sess auth.Session, p Removal, addr netip.Addr) error
- func (s *Service) Reset(ctx context.Context, actor audit.Actor, t audit.Transport, addr netip.Addr, ...) error
- func (s *Service) ResetByLogin(ctx context.Context, actorName, login string) (string, bool, error)
- func (s *Service) Status(ctx context.Context, userID int64) (Status, error)
- func (s *Service) Verify(ctx context.Context, userID int64, p auth.Proof) (bool, error)
- type Status
- type Store
Constants ¶
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).
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).
const RecoveryCodes = 10
RecoveryCodes is auth.totp_recovery_codes (P-08): how many single-use recovery codes an enrolment issues.
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.