Documentation
¶
Overview ¶
One Time Password generator compatible with RFC 6238 and RFC 4226
Index ¶
Examples ¶
Constants ¶
const DefaultCounterTolerance = 5
Default tolerance value for HOTP generators
const DefaultTimePeriod time.Duration = 30 * time.Second
the default time period an otp is valid
const DefaultTimeTolerance time.Duration = 3 * time.Second
the default tolerance when checking a otp
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Account ¶
type Account interface { // returns the shared secret. The secret must be at least 10 bytes long Secret() []byte // returns a label for this account. Label() string }
An user account which can be used to calculate an OTP.
type AccountStore ¶
type AccountStore interface { // returns the current counter value for the given account CounterValue(account Account) uint64 // stores a new counter value for the given account SetCounterValue(account Account, counter uint64) }
Interface to store current counter values for HOTP generators.
type CounterBased ¶
type CounterBased struct { // General calculation information Info // Keeps track of the current counter Values for the Accounts AccountStore }
Counter base one time password generator
func NewDefaultHOTP ¶
func NewDefaultHOTP(issuer string, accountStore AccountStore) *CounterBased
returns a new counterbased otp generator for the given issuer (6 digits, SHA1)
func (*CounterBased) IsValid ¶
func (cbi *CounterBased) IsValid(account Account, otp string, tolerance uint64) bool
checks if the given otp is valid. if the otp is wrong it also checks the next few otps the tolerance argument controls how many additional otps are checked.
func (*CounterBased) OTPAuthUri ¶
func (cbi *CounterBased) OTPAuthUri(account Account) string
returns an uri which could be provided to the user via qr code. it contains all necessary information to setup the otp generator
type Info ¶
type Info struct { // an Issuer (site name etc.) Issuer string // Number of digits. Can be Dec6 or Dec8 Digits DigitMode // The hashing algorithm used to calculate the otp. default is SHA1 Algorithm Algorithm }
General information about the otp calculation
type TimeBased ¶
type TimeBased struct { // General calculation information Info // a function which retrieves the time which is used for calculation GetTimeFn func() time.Time // indicates how long a single otp is valid. Period time.Duration }
Time base one time password generator
Example ¶
package main import ( "fmt" "github.com/boombuler/barcode/qr" "github.com/boombuler/otp" ) type User string func (u User) Secret() []byte { // Return a user specific secret which must be at least 10 bytes long // and should be random and persistent for each account. return []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} } func (u User) Label() string { return string(u) // Return a display label for the user account } // Initialize TOTP var totp = otp.NewDefaultTOTP("Test Service") func main() { usr := User("Test Account") // on "first contact" with the user show a barcode with all needed information to the user: uri := totp.OTPAuthUri(usr) img, err := qr.Encode(uri, qr.H, qr.Unicode) if err != nil { // handle error... } else { _ = img // send img to the user... } // After that we can check the OTPs from the user: userProvidedOTP := "123456" if totp.IsValid(account, userProvidedOTP, otp.DefaultTimeTolerance) { fmt.Println("Login OK!") } else { fmt.Println("Failed!") } }
Output:
func NewDefaultTOTP ¶
returns a new timebased otp generator for the given issuer (6 digits, SHA1, based on current system time, every 30 seconds)
func (*TimeBased) CurrentOTP ¶
calculates the current otp for the given account
func (*TimeBased) IsValid ¶
checks if the given otp is valid. also takes some tolerance into account. a tolerance might be usefull for network latency etc.
func (*TimeBased) OTPAuthUri ¶
returns an uri which could be provided to the user via qr code. it contains all necessary information to setup the otp generator