Documentation
¶
Overview ¶
The package twofactor implements the RFC 6238 TOTP: Time-Based One-Time Password Algorithm
The library provides a simple and secure way to generate and verify the OTP tokens and provides the possibility to display QR codes out of the box
The library supports HMAC-SHA1, HMAC-SHA256, HMAC-SHA512
Index ¶
- Variables
- func BCryptRecoveryCodes(codes []string) ([]string, error)
- func DecodeRecoveryCodes(codes string) []string
- func EncodeRecoveryCodes(codes []string) string
- func GenerateRecoveryCodes() ([]string, error)
- func UseRecoveryCode(codes []string, inputCode string) ([]string, bool)
- func ValidRecoveryCode(code string) bool
- type Totp
Constants ¶
This section is empty.
Variables ¶
var ( // ErrorLockDown means verification is locked for a period of time due to too many trials ErrorLockDown = errors.New("The verification is locked down, because of too many trials") // ErrorTokenMismatch tokens don't match ErrorTokenMismatch = errors.New("tokens mismatch") )
Functions ¶
func BCryptRecoveryCodes ¶ added in v1.0.7
BCryptRecoveryCodes hashes each recovery code given and return them in a new slice.
func DecodeRecoveryCodes ¶ added in v1.0.7
DecodeRecoveryCodes is an alias for strings.Split(",")
func EncodeRecoveryCodes ¶ added in v1.0.7
EncodeRecoveryCodes is an alias for strings.Join(",")
func GenerateRecoveryCodes ¶ added in v1.0.7
GenerateRecoveryCodes creates 10 recovery codes of the form: abd34-1b24do (using alphabet, of length recoveryCodeLength).
func UseRecoveryCode ¶ added in v1.0.7
UseRecoveryCode deletes the code that was used from the string slice and returns it, the bool is true if a code was used
func ValidRecoveryCode ¶ added in v1.0.7
ValidRecoveryCode returns true if the code matches recovery code format
Types ¶
type Totp ¶
type Totp struct {
// contains filtered or unexported fields
}
Totp struct should never be instantiated manually! Use the `NewTOTP` function
func NewTOTP ¶
NewTOTP creates a new TOTP object This is the function which is needed to start the whole process account: usually the user email issuer: the name of the company/service hash: is the crypto function used: crypto.SHA1, crypto.SHA256, crypto.SHA512 digits: is the token amount of digits (6 or 7 or 8) steps: the amount of second the token is valid
func TOTPFromBytes ¶
TOTPFromBytes converts a byte array to a totp object it stores the state of the TOTP object, like the key, the current counter, the client offset, the total amount of verification failures and the last time a verification happened
func (*Totp) HashFunction ¶ added in v1.0.8
HashFunction returns the hash function used
func (*Totp) NumDigits ¶ added in v1.0.8
NumDigits returns total amount of digits of the code displayed on the device
func (*Totp) QR ¶
QR generates a byte array containing QR code encoded PNG image, with level Q error correction, needed for the client apps to generate tokens The QR code should be displayed only the first time the user enabled the Two-Factor authentication. The QR code contains the shared KEY between the server application and the client application, therefore the QR code should be delivered via secure connection.
func (*Totp) Secret ¶ added in v1.0.7
Secret returns the underlying base32 encoded secret. This should only be displayed the first time a user enables 2FA, and should be transmitted over a secure connection. Useful for supporting TOTP clients that don't support QR scanning.
func (*Totp) ToBytes ¶
ToBytes serialises a TOTP object in a byte array Sizes: 4 4 N 8 4 4 N 4 N 4 4 8 4 Format: |total_bytes|key_size|key|counter|digits|issuer_size|issuer|account_size|account|offset|total_failures|verification_time|hashFunction_type| hashFunction_type: 0 = SHA1; 1 = SHA256; 2 = SHA512 TODO: 1- improve sizes. For instance the hashFunction_type could be a short.
func (*Totp) Validate ¶
Validate validates the user provided token It calculates 3 different tokens. The current one, one before now and one after now. The difference is driven by the TOTP step size Based on which of the 3 steps it succeeds to validates, the client offset is updated. It also updates the total amount of verification failures and the last time a verification happened in UTC time Returns an error in case of verification failure, with the reason There is a very basic method which protects from timing attacks, although if the step time used is low it should not be necessary An attacker can still learn the synchronization offset. This is however irrelevant because the attacker has then 30 seconds to guess the code and after 3 failures the function returns an error for the following 5 minutes