Documentation
¶
Overview ¶
Package otp provides implementations for generating and validating One-Time Passwords.
Index ¶
- Variables
- type Algorithm
- type Digits
- type Encoder
- type Key
- func (k *Key) AccountName() string
- func (k *Key) Algorithm() Algorithm
- func (k *Key) Digits() Digits
- func (k *Key) Encoder() Encoder
- func (k *Key) Image(width, height int, color ...color.Color) (image.Image, error)
- func (k *Key) Issuer() string
- func (k *Key) Period() uint64
- func (k *Key) Secret() string
- func (k *Key) String() string
- func (k *Key) Type() string
- func (k *Key) URL() string
- type Option
- func WithAccountName(name string) Option
- func WithAlgorithm(alg Algorithm) Option
- func WithDigits(digits Digits) Option
- func WithIssuer(issuer string) Option
- func WithPeriod(period uint) Option
- func WithSecretSize(secretSize uint) Option
- func WithSkew(skew uint) Option
- func WithTime(t time.Time) Option
- type Options
Constants ¶
This section is empty.
Variables ¶
var ( // ErrValidateSecretInvalidBase32 is returned when the secret cannot be decoded from base32. ErrValidateSecretInvalidBase32 = errors.New("Decoding of secret as base32 failed.") // ErrValidateInputInvalidLength is returned when the provided OTP input has an unexpected length. ErrValidateInputInvalidLength = errors.New("Input length unexpected") // ErrGenerateMissingIssuer is returned when attempting to generate a key without an issuer. ErrGenerateMissingIssuer = errors.New("Issuer must be set") // ErrGenerateMissingAccountName is returned when attempting to generate a key without an account name. ErrGenerateMissingAccountName = errors.New("AccountName must be set") )
Standard errors for OTP validation and generation.
Functions ¶
This section is empty.
Types ¶
type Algorithm ¶
type Algorithm int
Algorithm represents the hashing algorithm used for the OTP.
type Digits ¶
type Digits int
Digits represents the number of digits in a generated OTP.
type Key ¶
type Key struct {
// contains filtered or unexported fields
}
Key represents an OTP key derived from an otpauth:// URI.
func NewKeyFromURL ¶
NewKeyFromURL parses an otpauth:// URI string and returns a new Key instance. It returns an error if the URI string cannot be parsed.
func (*Key) AccountName ¶
AccountName returns the account name associated with the OTP, extracting it from the path.
func (*Key) Algorithm ¶
Algorithm returns the hashing algorithm for the OTP. It defaults to AlgorithmSHA1 if the algorithm parameter is unrecognized or not set.
func (*Key) Digits ¶
Digits returns the number of digits for the OTP. It defaults to Six (6) if the digits parameter is not specified or cannot be parsed.
func (*Key) Encoder ¶
Encoder returns the encoder type for the OTP. It defaults to EncoderDefault if the encoder parameter is unrecognized or not set.
func (*Key) Image ¶
Image generates a QR code image for the Key with the specified width and height. It returns an error if the QR code encoding fails.
func (*Key) Issuer ¶
Issuer returns the issuer of the OTP. It first attempts to extract it from the query parameters, falling back to the path prefix if not found.
func (*Key) Period ¶
Period returns the validity period of the OTP in seconds. It defaults to 30 if the period is not specified or cannot be parsed.
type Option ¶
type Option func(*Options)
Option is a function that applies a configuration value to Options.
func WithAccountName ¶
WithAccountName sets the account name for the OTP.
func WithAlgorithm ¶
WithAlgorithm sets the hashing algorithm used for the OTP.
func WithDigits ¶
WithDigits sets the number of digits for the generated OTP.
func WithPeriod ¶
WithPeriod sets the validity period of the OTP in seconds.
func WithSecretSize ¶
WithSecretSize sets the byte size of the generated secret.
type Options ¶
type Options struct {
// Issuer is the name of the organization or service issuing the OTP (e.g., "Google", "GitHub").
// This is typically displayed in the authenticator app to identify the source of the code.
Issuer string
// AccountName is the name used to identify the specific account (e.g., "user@example.com").
// In most apps, this is displayed alongside the Issuer to distinguish between multiple accounts.
AccountName string
// Period is the amount of time in seconds that a TOTP (Time-based One-Time Password)
// is valid for. The default is typically 30 seconds.
Period uint
// Skew is the number of time steps (periods) allowed before and after the current time
// during validation. A skew of 1 allows the previous, current, and next OTP to be valid,
// accounting for clock drift between the server and the client's device.
Skew uint
// SecretSize is the length in bytes of the secret key to be generated if a
// custom Secret is not provided. The default is usually 20 bytes.
SecretSize uint
// Secret is the raw byte array of the shared secret key. If this is provided,
// SecretSize is ignored. This key must be kept secure.
Secret []byte
// Digits represents the length of the generated OTP code, usually Six (6) or Eight (8).
Digits Digits
// Algorithm is the hashing function used to generate the HMAC (e.g., SHA1, SHA256).
// SHA1 is the most common default for compatibility with most authenticator apps.
Algorithm Algorithm
// Encoder specifies the formatting logic for the resulting code.
// Use EncoderDefault for standard numeric codes or EncoderSteam for Steam-specific formats.
Encoder Encoder
// Time is the reference point used for TOTP calculation.
// In production, this should always be set to the current UTC time.
Time time.Time
// Rand is the source of entropy used to generate a new Secret.
// It defaults to crypto/rand.Reader for secure, cryptographically strong randomness.
Rand io.Reader
}
Options holds configuration settings for generating and validating OTPs.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package encoder provides interfaces and implementations for encoding various data structures into string representations.
|
Package encoder provides interfaces and implementations for encoding various data structures into string representations. |
|
Package hotp implements the HMAC-based One-Time Password (HOTP) algorithm.
|
Package hotp implements the HMAC-based One-Time Password (HOTP) algorithm. |
|
Package totp implements the Time-Based One-Time Password algorithm as specified in RFC 6238.
|
Package totp implements the Time-Based One-Time Password algorithm as specified in RFC 6238. |