Documentation
¶
Overview ¶
Package otp implements one-time passwords following the TOTP (RFC 6238) and HOTP (RFC 4226) standards, together with helpers for generating shared secrets, provisioning URIs, and QR codes for authenticator apps.
The default configuration matches what authenticator apps expect: 6-digit codes, a 30-second TOTP period, and HMAC-SHA1. These can be overridden per OTP value via the Digits, Period, and Algorithm fields.
Original implementation adapted from: http://www.inanzzz.com/index.php/post/y5nu/creating-a-one-time-password-otp-library-for-two-factor-authentication-2fa-with-golang
Index ¶
Constants ¶
const ( // OTPLength is the default number of digits in a generated code. OTPLength = 6 // OTPPeriod is the default TTL of a TOTP code in seconds. OTPPeriod = 30 )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Algorithm ¶ added in v1.0.0
type Algorithm string
Algorithm identifies the HMAC hash function used to derive codes. The zero value is treated as AlgorithmSHA1, which is the default mandated by RFC 4226 and understood by virtually all authenticator apps.
type OTP ¶
type OTP struct {
// Issuer represents the service provider. It is you! e.g. your service,
// your application, your organisation so on.
Issuer string
// Account represents the service user. It is the user! e.g. username, email
// address so on.
Account string
// Secret is an arbitrary key value encoded in Base32 and belongs to the
// service user.
Secret string
// Window is used for time (TOTP) and counter (HOTP) synchronization. Given
// that the possible time and counter drifts between client and server, this
// parameter helps overcome such issue. TOTP uses backward and forward time
// window whereas HOTP uses look-ahead counter window that depends on the
// Counter parameter.
// Resynchronisation is an official recommended practise, however the
// lower the better.
// 0 = not recommended as synchronization is disabled
// TOTP: current time
// HOTP: current counter
// 1 = recommended option
// TOTP: previous - current - next
// HOTP: current counter - next counter
// 2 = being overcautious
// TOTP: previous,previous - current - next,next
// HOTP: current counter - next counter - next counter
// * = Higher numbers may cause denial-of-service attacks.
// https://datatracker.ietf.org/doc/html/rfc6238#page-7
// https://datatracker.ietf.org/doc/html/rfc4226#page-11
Window int
// Counter is required for HOTP only and used for provisioning the code. Set
// it to 0 if you with to use TOTP. Start from 1 for HOTP then fetch and use
// the one in the persistent storage. The server counter is incremented only
// after a successful code verification, however the counter on the code is
// incremented every time a new code is requested by the user which causes
// counters being out of sync. For that reason, time-synchronization should
// be enabled.
// https://datatracker.ietf.org/doc/html/rfc4226#page-11
Counter int
// Digits is the number of digits in a generated code. It defaults to
// OTPLength (6) when zero. Authenticator apps typically support 6 or 8.
Digits int
// Period is the TOTP time step in seconds. It defaults to OTPPeriod (30)
// when zero and is ignored for HOTP.
Period int
// Algorithm selects the HMAC hash function. The zero value defaults to
// AlgorithmSHA1.
Algorithm Algorithm
}
func (*OTP) CreateHOTPCode ¶
CreateHOTPCode creates a new HOTP with a specific counter. This method is ideal if you are planning to send manually created code via email, SMS etc. The user should not be present a QR code for this option otherwise there is a high posibility that the client and server counters will be out of sync, unless the user will be forced to rescan a newly generaed QR with up to date counter value.
func (*OTP) CreateURI ¶
CreateURI builds the authentication URI which is used to create a QR code. If the counter is set to 0, the algorithm is assumed to be TOTP, otherwise HOTP. https://github.com/google/google-authenticator/wiki/Key-Uri-Format