Documentation
¶
Overview ¶
Package otp provides functionalities to manage and generate one-time passwords.
Index ¶
- Constants
- Variables
- type EnvTOTPSecret
- func (e EnvTOTPSecret) DeleteTOTPSecret(_ context.Context) error
- func (e EnvTOTPSecret) SetTOTPSecret(_ context.Context, secret TOTPSecret, _ string) error
- func (e EnvTOTPSecret) TOTPSecret(_ context.Context) TOTPSecret
- func (e EnvTOTPSecret) TOTPSecretDeleter() TOTPSecretDeleter
- func (e EnvTOTPSecret) TOTPSecretGetter() TOTPSecretGetter
- func (e EnvTOTPSecret) TOTPSecretSetter() TOTPSecretSetter
- type Generator
- type OTP
- type Option
- type TOTPGenerator
- type TOTPGeneratorOption
- type TOTPSecret
- func (s TOTPSecret) DeleteTOTPSecret(context.Context) error
- func (s TOTPSecret) MarshalText() ([]byte, error)
- func (s TOTPSecret) SetTOTPSecret(context.Context, TOTPSecret, string) error
- func (s TOTPSecret) String() string
- func (s TOTPSecret) TOTPSecret(context.Context) TOTPSecret
- func (s TOTPSecret) TOTPSecretGetter() TOTPSecretGetter
- func (s *TOTPSecret) UnmarshalText(text []byte) error
- type TOTPSecretDeleter
- type TOTPSecretGetter
- type TOTPSecretGetters
- type TOTPSecretProvider
- type TOTPSecretProviders
- func (ps TOTPSecretProviders) DeleteTOTPSecret(ctx context.Context) error
- func (ps TOTPSecretProviders) SetTOTPSecret(ctx context.Context, secret TOTPSecret, issuer string) error
- func (ps TOTPSecretProviders) TOTPSecret(ctx context.Context) TOTPSecret
- func (ps TOTPSecretProviders) TOTPSecretDeleter() TOTPSecretDeleter
- func (ps TOTPSecretProviders) TOTPSecretGetter() TOTPSecretGetter
- func (ps TOTPSecretProviders) TOTPSecretSetter() TOTPSecretSetter
- type TOTPSecretSetter
Examples ¶
Constants ¶
const NoTOTPSecret = TOTPSecret("")
NoTOTPSecret is a TOTP secret that is empty.
Variables ¶
var ErrNoTOTPSecret = errors.New("no totp secret")
ErrNoTOTPSecret indicates that the user has not configured the TOTP secret.
var ErrTOTPSecretReadOnly = errors.New("totp secret is read-only")
ErrTOTPSecretReadOnly indicates that the TOTP secret is read-only.
Functions ¶
This section is empty.
Types ¶
type EnvTOTPSecret ¶ added in v0.9.0
type EnvTOTPSecret struct {
// contains filtered or unexported fields
}
EnvTOTPSecret is a TOTP secret provider that gets the TOTP secret from the environment.
func TOTPSecretFromEnv ¶
func TOTPSecretFromEnv(env string) EnvTOTPSecret
TOTPSecretFromEnv returns a TOTP secret getter that gets the TOTP secret from the environment.
func (EnvTOTPSecret) DeleteTOTPSecret ¶ added in v0.9.0
func (e EnvTOTPSecret) DeleteTOTPSecret(_ context.Context) error
DeleteTOTPSecret deletes the TOTP secret from the environment.
func (EnvTOTPSecret) SetTOTPSecret ¶ added in v0.9.0
func (e EnvTOTPSecret) SetTOTPSecret(_ context.Context, secret TOTPSecret, _ string) error
SetTOTPSecret sets the TOTP secret to the environment.
func (EnvTOTPSecret) TOTPSecret ¶ added in v0.9.0
func (e EnvTOTPSecret) TOTPSecret(_ context.Context) TOTPSecret
TOTPSecret returns the TOTP secret from the environment.
func (EnvTOTPSecret) TOTPSecretDeleter ¶ added in v0.9.0
func (e EnvTOTPSecret) TOTPSecretDeleter() TOTPSecretDeleter
TOTPSecretDeleter returns TOTPSecretDeleter.
func (EnvTOTPSecret) TOTPSecretGetter ¶ added in v0.9.0
func (e EnvTOTPSecret) TOTPSecretGetter() TOTPSecretGetter
TOTPSecretGetter returns TOTPSecretGetter.
func (EnvTOTPSecret) TOTPSecretSetter ¶ added in v0.9.0
func (e EnvTOTPSecret) TOTPSecretSetter() TOTPSecretSetter
TOTPSecretSetter returns TOTPSecretSetter.
type OTP ¶
type OTP string
OTP is a one-time password.
func GenerateTOTP ¶
func GenerateTOTP(ctx context.Context, secret TOTPSecretGetter, opts ...TOTPGeneratorOption) (OTP, error)
GenerateTOTP generates a TOTP.
Example ¶
package main import ( "context" "fmt" "time" "go.nhat.io/clock" "go.nhat.io/otp" ) func main() { c := clock.Fix(time.Date(2024, time.January, 1, 0, 0, 0, 0, time.UTC)) result, err := otp.GenerateTOTP(context.Background(), otp.TOTPSecret("NBSWY3DP"), otp.WithClock(c)) if err != nil { panic(err) } fmt.Println(result) }
Output: 191882
type Option ¶
type Option interface { TOTPGeneratorOption }
Option configures the apis of the authenticator package.
type TOTPGenerator ¶
type TOTPGenerator struct {
// contains filtered or unexported fields
}
TOTPGenerator is a .TOTPGenerator.
func NewTOTPGenerator ¶
func NewTOTPGenerator(secretGetter TOTPSecretGetter, opts ...TOTPGeneratorOption) *TOTPGenerator
NewTOTPGenerator initiates a new .TOTPGenerator.
func (*TOTPGenerator) GenerateOTP ¶
func (g *TOTPGenerator) GenerateOTP(ctx context.Context) (OTP, error)
GenerateOTP generates a TOTP.
type TOTPGeneratorOption ¶
type TOTPGeneratorOption interface {
// contains filtered or unexported methods
}
TOTPGeneratorOption is an option to configure TOTPGenerator.
type TOTPSecret ¶
type TOTPSecret string
TOTPSecret is a TOTP secret.
func (TOTPSecret) DeleteTOTPSecret ¶ added in v0.7.0
func (s TOTPSecret) DeleteTOTPSecret(context.Context) error
DeleteTOTPSecret deletes the TOTP secret.
func (TOTPSecret) MarshalText ¶ added in v0.3.0
func (s TOTPSecret) MarshalText() ([]byte, error)
MarshalText returns the TOTP secret as text.
func (TOTPSecret) SetTOTPSecret ¶ added in v0.7.0
func (s TOTPSecret) SetTOTPSecret(context.Context, TOTPSecret, string) error
SetTOTPSecret sets the TOTP secret.
func (TOTPSecret) String ¶ added in v0.2.0
func (s TOTPSecret) String() string
String returns the TOTP secret as a string.
func (TOTPSecret) TOTPSecret ¶
func (s TOTPSecret) TOTPSecret(context.Context) TOTPSecret
TOTPSecret returns the TOTP secret.
func (TOTPSecret) TOTPSecretGetter ¶ added in v0.9.0
func (s TOTPSecret) TOTPSecretGetter() TOTPSecretGetter
TOTPSecretGetter returns TOTPSecretGetter.
func (*TOTPSecret) UnmarshalText ¶ added in v0.3.0
func (s *TOTPSecret) UnmarshalText(text []byte) error
UnmarshalText unmarshals the TOTP secret from text.
type TOTPSecretDeleter ¶ added in v0.5.0
TOTPSecretDeleter is an interface that deletes a TOTP secret.
type TOTPSecretGetter ¶
type TOTPSecretGetter interface {
TOTPSecret(ctx context.Context) TOTPSecret
}
TOTPSecretGetter is an interface that provides a TOTP secret.
type TOTPSecretGetters ¶
type TOTPSecretGetters []TOTPSecretGetter
TOTPSecretGetters is a list of TOTP secret getters.
func ChainTOTPSecretGetters ¶
func ChainTOTPSecretGetters(getters ...TOTPSecretGetter) TOTPSecretGetters
ChainTOTPSecretGetters chains the TOTP secret getters.
func (TOTPSecretGetters) TOTPSecret ¶
func (p TOTPSecretGetters) TOTPSecret(ctx context.Context) TOTPSecret
TOTPSecret returns the first non-empty TOTP secret that it finds from the list of TOTP secret getters.
func (TOTPSecretGetters) TOTPSecretGetter ¶ added in v0.9.0
func (p TOTPSecretGetters) TOTPSecretGetter() TOTPSecretGetter
TOTPSecretGetter returns TOTPSecretGetter.
type TOTPSecretProvider ¶ added in v0.5.0
type TOTPSecretProvider interface { TOTPSecretGetter TOTPSecretSetter TOTPSecretDeleter }
TOTPSecretProvider is an interface that manages a TOTP secret.
type TOTPSecretProviders ¶ added in v0.7.0
type TOTPSecretProviders []TOTPSecretProvider
TOTPSecretProviders is a list of TOTP secret getters.
func ChainTOTPSecretProviders ¶ added in v0.7.0
func ChainTOTPSecretProviders(providers ...TOTPSecretProvider) TOTPSecretProviders
ChainTOTPSecretProviders chains the TOTP secret providers.
func (TOTPSecretProviders) DeleteTOTPSecret ¶ added in v0.7.0
func (ps TOTPSecretProviders) DeleteTOTPSecret(ctx context.Context) error
DeleteTOTPSecret deletes the TOTP secret.
func (TOTPSecretProviders) SetTOTPSecret ¶ added in v0.7.0
func (ps TOTPSecretProviders) SetTOTPSecret(ctx context.Context, secret TOTPSecret, issuer string) error
SetTOTPSecret sets the TOTP secret.
func (TOTPSecretProviders) TOTPSecret ¶ added in v0.7.0
func (ps TOTPSecretProviders) TOTPSecret(ctx context.Context) TOTPSecret
TOTPSecret returns the first non-empty TOTP secret that it finds from the list of TOTP secret getters.
func (TOTPSecretProviders) TOTPSecretDeleter ¶ added in v0.9.0
func (ps TOTPSecretProviders) TOTPSecretDeleter() TOTPSecretDeleter
TOTPSecretDeleter returns TOTPSecretDeleter.
func (TOTPSecretProviders) TOTPSecretGetter ¶ added in v0.9.0
func (ps TOTPSecretProviders) TOTPSecretGetter() TOTPSecretGetter
TOTPSecretGetter returns TOTPSecretGetter.
func (TOTPSecretProviders) TOTPSecretSetter ¶ added in v0.9.0
func (ps TOTPSecretProviders) TOTPSecretSetter() TOTPSecretSetter
TOTPSecretSetter returns TOTPSecretSetter.
type TOTPSecretSetter ¶
type TOTPSecretSetter interface {
SetTOTPSecret(ctx context.Context, secret TOTPSecret, issuer string) error
}
TOTPSecretSetter is an interface that sets a TOTP secret.