Documentation
¶
Overview ¶
The otp package provides an opinionated way to generate and verify TOTP tokens as well as providing an easy way to encrypt the secrets for persistent storage (e.g., in the database alongside user records). It also provides a facility to dump QR codes to the terminal so that they can be scanned by an authenticator application.
Index ¶
- func BuildTotpUri(username, issuer, secret string) string
- func DecryptOtpSecret(b64Secret string, key *[32]byte) (string, error)
- func DefaultInterval() int
- func EncryptOtpSecret(secret string, key *[32]byte) (string, error)
- func GenerateNewSecret() (string, error)
- func GenerateToken(secret string, interval int) (string, error)
- func QrCodeToTerminal(username, issuer, secret string) error
- func RawKeyToBytes(rawKey string) (*[32]byte, error)
- func VerifyToken(providedToken, secret string) (bool, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildTotpUri ¶
BuildTotpUri returns a otpauth:// scheme URI suitable for input into a QR code generator. It requires the username of the user, an issuer and the user's OTP secret (generated with GenerateNewSecret) and reurns the URI:
otpauth://totp/[issuer]:[username]?issuer=[issuer]&secret=[secret]
func DecryptOtpSecret ¶
DecryptOtpSecret will return the original OTP secret after decryption.
func DefaultInterval ¶ added in v1.1.0
func DefaultInterval() int
The DefaultInterval calculates the default 30 second interval.
func EncryptOtpSecret ¶
EncryptOtpSecret encrypts the OTP secret using secretbox: https://pkg.go.dev/golang.org/x/crypto/nacl/secretbox
func GenerateNewSecret ¶
GenerateNewSecret does what it says on the tin: it generates a new secret that is suitable for use as a TOTP secret.
func GenerateToken ¶
GenerateToken returns the current time-based token for the given secret and interval.
func QrCodeToTerminal ¶
QrCodeToTerminal constructs a TOTP URI and then outputs a terminal-based (using pterm) QR code that can be scanned by an authentication app.
func RawKeyToBytes ¶
Given an encryption key as a tring RawKeyToBytes returns it as bytes that can be used by the encryption functions.
func VerifyToken ¶
VerifyToken returns true if the provided token matches the current token (+/- one interval) and false otherwise.
Types ¶
This section is empty.