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 ¶
- type Authenticator
- type Codec
- type Module
- type Options
- type SecretCodec
- type TOTP
- func (t *TOTP) GenerateCode(secret string, at time.Time) (string, error)
- func (t *TOTP) GenerateSecret() (string, error)
- func (t *TOTP) URI(secret, account, issuer string) string
- func (t *TOTP) Validate(secret, code string, at time.Time) bool
- func (t *TOTP) ValidateCounter(secret, code string, at time.Time) (int64, bool)
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 (*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.
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.
type TOTP ¶
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 ¶
GenerateCode returns the TOTP code for secret at time at.