Documentation
¶
Overview ¶
Package otp implements both HOTP and TOTP based one time passcodes in a Google Authenticator compatible manner.
When adding a TOTP for a user, you must store the "secret" value persistently. It is recommended to store the secret in an encrypted field in your datastore. Due to how TOTP works, it is not possible to store a hash for the secret value like you would a password.
To enroll a user, you must first generate an OTP for them. Google Authenticator supports using a QR code as an enrollment method:
import (
"github.com/unitsvc/otp/totp"
"bytes"
"image/png"
)
key, err := totp.Generate(totp.GenerateOpts{
Issuer: "Example.com",
AccountName: "alice@example.com",
})
// Convert TOTP key into a QR code encoded as a PNG image.
var buf bytes.Buffer
img, err := key.Image(200, 200)
png.Encode(&buf, img)
// display the QR code to the user.
display(buf.Bytes())
// Now Validate that the user's successfully added the passcode.
passcode := promptForPasscode()
valid := totp.Validate(passcode, key.Secret())
if valid {
// User successfully used their TOTP, save it to your backend!
storeSecret("alice@example.com", key.Secret())
}
Validating a TOTP passcode is very easy, just prompt the user for a passcode and retrieve the associated user's previously stored secret.
import "github.com/unitsvc/otp/totp"
passcode := promptForPasscode()
secret := getSecret("alice@example.com")
valid := totp.Validate(passcode, secret)
if valid {
// Success! continue login process.
}
Index ¶
- Constants
- Variables
- type Algorithm
- type Digits
- type Encoder
- type Key
- func (k *Key) AccountName() string
- func (k *Key) Algorithm() Algorithm
- func (k *Key) Counter() uint64
- func (k *Key) Digits() Digits
- func (k *Key) Encoder() Encoder
- func (k *Key) GetExtraParam(key string) string
- func (k *Key) Image(width int, height int) (image.Image, error)
- func (k *Key) ImageURL() string
- 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 ValidationResult
Constants ¶
const AlgorithmCompat = AlgorithmSHA1
AlgorithmCompat is the compatibility default (SHA1). Use for maximum compatibility with all authenticator apps including Google Authenticator, Microsoft Authenticator, Authy, and others.
This is the zero-value default for ValidateOpts.Algorithm.
const AlgorithmSecure = AlgorithmSHA256
AlgorithmSecure is the security-recommended algorithm (SHA256). Use for new deployments where all clients support SHA256: - Google Authenticator 2.0+ - Microsoft Authenticator - Authy - KeePassXC - WinAuth - Bitwarden
Note: Some older authenticator apps may not support SHA256.
Variables ¶
var ErrAlgorithmDigestInsufficient = errors.New("algorithm produces insufficient digest size for OTP generation")
var ErrColonInAccountName = errors.New("invalid colon in account name")
var ErrColonInIssuer = errors.New("invalid colon in issuer name")
var ErrDigestTooSmall = errors.New("HMAC digest size too small, must be at least 19 bytes for dynamic truncation safety")
Security validation errors.
var ErrDigitsOutOfRange = errors.New("digits out of range: default encoder requires 6-10, Steam encoder allows 5-10")
var ErrGenerateMissingAccountName = errors.New("accountName must be set")
When generating a Key, the Account Name must be set.
var ErrGenerateMissingIssuer = errors.New("issuer must be set")
When generating a Key, the Issuer must be set.
var ErrInvalidAlgorithm = errors.New("invalid 'algorithm' parameter")
var ErrInvalidCounter = errors.New("invalid 'counter' parameter")
var ErrInvalidDigits = errors.New("invalid 'digits' parameter")
var ErrInvalidEncoder = errors.New("invalid encoder: must be EncoderDefault or EncoderSteam")
var ErrInvalidImageURL = errors.New("image URL must be a valid HTTPS URL")
var ErrInvalidPeriod = errors.New("invalid 'period' parameter")
var ErrInvalidSecretFormat = errors.New("invalid 'secret' format, must be valid Base32")
var ErrInvalidURIChars = errors.New("issuer or account name contains invalid characters (control chars)")
var ErrInvalidURIFormat = errors.New("invalid URI format")
URI format validation errors.
var ErrInvalidURIScheme = errors.New("invalid URI scheme, must be otpauth://")
var ErrInvalidURIType = errors.New("invalid OTP type, must be 'hotp' or 'totp'")
var ErrMissingSecret = errors.New("missing required 'secret' parameter")
var ErrPeriodOutOfRange = errors.New("period out of range, must be between 1 and 300 seconds")
var ErrReplayAttack = errors.New("replay attack detected: time step already used")
var ErrSecretTooLong = errors.New("secret too long, must be at most 64 bytes")
var ErrSecretTooShort = errors.New("secret too short, must be at least 16 bytes")
var ErrURITooLong = errors.New("URI exceeds maximum allowed length")
var ErrValidateInputInvalidChars = errors.New("passcode contains invalid characters")
var ErrValidateInputInvalidLength = errors.New("input length unexpected")
The user provided passcode length was not expected.
var ErrValidateSecretInvalidBase32 = errors.New("decoding of secret as base32 failed")
Error when attempting to convert the secret from base32 to raw bytes.
var ErrWindowTooLarge = errors.New("window exceeds maximum allowed value")
Functions ¶
This section is empty.
Types ¶
type Algorithm ¶
type Algorithm int
Algorithm represents the hashing function to use in the HMAC operation needed for OTPs.
const ( // AlgorithmSHA1 should be used for compatibility with Google Authenticator. // // HMAC-SHA1 remains cryptographically secure for OTP despite SHA1 collision attacks. // See RFC 4226 Section 9: https://tools.ietf.org/html/rfc4226#section-9 // // For new deployments where all clients support SHA256, consider AlgorithmSHA256. AlgorithmSHA1 Algorithm = iota AlgorithmSHA256 AlgorithmSHA512 AlgorithmMD5 // Extended algorithms (appended to preserve numeric values of original constants) AlgorithmSHA224 AlgorithmSHA384 // SHA3 variants (Keccak) - extended algorithm support AlgorithmSHA3_224 AlgorithmSHA3_256 AlgorithmSHA3_384 AlgorithmSHA3_512 )
func ParseAlgorithm ¶
ParseAlgorithm parses an algorithm name string (case-insensitive) into an Algorithm. Accepts common aliases: "SHA-256", "SHA256", "SHA2-256", "SHA-1", "SHA1", "SSL3-SHA1". Returns an error if the name is not recognized.
func (Algorithm) HashChecked ¶
HashChecked returns the hash.Hash for the algorithm, returning an error if the algorithm is unknown or invalid. For safe usage, prefer this over Hash() which silently falls back to SHA1 for unknown algorithms.
type Digits ¶
type Digits int
Digits represents the number of digits present in the user's OTP passcode. Six and Eight are the most common values.
type Encoder ¶
type Encoder string
Encoder represents the output encoding format for OTP codes.
const ( EncoderDefault Encoder = "" // EncoderSteam produces 5-character alphanumeric codes using the Steam Guard // alphabet (23456789BCDFGHJKMNPQRTVWXY). Only compatible with KeePassXC, WinAuth, // Bitwarden, and the Steam Mobile App. Google Authenticator and Microsoft // Authenticator do not support this encoder. EncoderSteam Encoder = "steam" )
type Key ¶
type Key struct {
// contains filtered or unexported fields
}
Key represents a TOTP or HOTP key.
func NewKeyFromURL ¶
NewKeyFromURL creates a new Key from an TOTP or HOTP url.
The URL format is documented here:
https://github.com/google/google-authenticator/wiki/Key-Uri-Format
This function performs strict validation per IETF draft-andesco-otpauth-uri: - Validates URI scheme is "otpauth://" - Validates OTP type is "hotp" or "totp" - Validates required "secret" parameter exists and is valid Base32 - Validates optional parameters have correct formats - Rejects URIs with colons in parsed issuer or account name
func (*Key) AccountName ¶
AccountName returns the name of the user's account.
func (*Key) Counter ¶
Counter returns the initial HOTP counter value, or 0 if not set or not an HOTP key.
func (*Key) GetExtraParam ¶
GetExtraParam returns the value of a custom query parameter from the OTP URI. Returns empty string if the parameter does not exist. This is useful for reading custom parameters that were set via ExtraParams during key generation (e.g., "lock", "source", etc.).
func (*Key) Image ¶
Image returns a QR-Code image of the specified width and height, suitable for use by many clients like Google-Authenticator to enroll a user's TOTP/HOTP key.
func (*Key) ImageURL ¶
ImageURL returns the image parameter from the OTP URL, if set. Only FreeOTP and FreeOTP+ support this parameter; other authenticator apps ignore it.
type ValidationResult ¶
type ValidationResult struct {
// Valid indicates whether the passcode was accepted.
Valid bool
// Delta is the offset from the expected counter/step (0 = exact match,
// negative = past, positive = future). Useful for clock drift detection.
Delta int
// Step is the matched time step (TOTP) or counter value (HOTP) for replay prevention tracking.
Step uint64
}
ValidationResult provides a structured result for OTP validation operations. It includes details about whether validation succeeded and metadata about the match for replay protection and clock drift detection.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package main demonstrates a basic TOTP setup flow.
|
Package main demonstrates a basic TOTP setup flow. |
|
hotp
command
Package main demonstrates HOTP (HMAC-based One-Time Password) functionality.
|
Package main demonstrates HOTP (HMAC-based One-Time Password) functionality. |
|
otp
command
Package main demonstrates the core otp package functionality.
|
Package main demonstrates the core otp package functionality. |
|
secret
command
Package main demonstrates the secret package functionality.
|
Package main demonstrates the secret package functionality. |
|
totp
command
Package main demonstrates TOTP (Time-based One-Time Password) functionality.
|
Package main demonstrates TOTP (Time-based One-Time Password) functionality. |