Documentation
¶
Overview ¶
The otp package provides support for TOTP and HOTP authentication
Example ¶
package main
import (
"fmt"
"net/url"
"github.com/heliorosa/otp"
)
func main() {
// create a new key
k, err := otp.NewKeyWithDefaults(otp.TypeHotp, "mydomain.com", "", url.Values{"counter": []string{"1"}})
if err != nil {
fmt.Println(err)
return
}
// set key from a base32 string
if err = k.SetKey32("UYMIODYLDUSYMBVV"); err != nil {
fmt.Println(err)
return
}
fmt.Println(k.Code(), k)
// import key from url
k, err = otp.ImportKey("otpauth://totp/mydomain.com?secret=UYMIODYLDUSYMBVV")
if err != nil {
fmt.Println(err)
return
}
kt := k.(*otp.Totp)
fmt.Println(kt.CodePeriod(0), kt)
}
Output: 511108 otpauth://hotp/mydomain.com?counter=1&secret=UYMIODYLDUSYMBVV 453613 otpauth://totp/mydomain.com?secret=UYMIODYLDUSYMBVV
Index ¶
Examples ¶
Constants ¶
const ( TypeTotp = "totp" //TOTP TypeHotp = "hotp" // HOTP )
Types of OTP auth supported.
const ( DefaultDigits = 6 // 6 digit code. DefaultKeyLength = 10 // 10 bytes (16 base32 characters). DefaultAlgorithm = "sha1" // SHA1 is the only supported. )
Common defaults for TOTP and HOTP
const ( // Common errors for TOTP and HOTP. ECMissingLabel = iota // Missing (or empty) label. ECInvalidAlgorithm // Invalid algorithm. ECCantReadRandom // Something went wrong while reading random bytes. ECNotEnoughRandom // Didn't read enough random bytes. ECUrlParseError // Error parsing the url. ECWrongScheme // Url scheme != "otpauth". ECInvalidOtpType // Host in the url must be either "totp" or "hotp". ECBase32Decoding // Base32 decoding error. ECInvalidDigits // Invalid number of digits. ECMissingSecret // Secret parameter is missing. // HOTP specific errors. ECNotHotp // Url is not HOTP. ECMissingCounter // Counter parameter is missing. ECInvalidCounter // Can't parse counter. // TOTP specific errors. ECNotTotp // Url is not TOTP. ECInvalidPeriod // Can't parse period parameter. )
Error codes.
const (
// Default period is 30 seconds.
DefaultPeriod = 30
)
TOTP specific defaults.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Error ¶
type Error struct {
// The field Code can hold any of the EC* error codes.
Code int
// The field Desc is a description of the error.
Desc string
// The field Err holds the original error if any.
Err error
}
Error is a common error struct returned by new/import functions.
type Hotp ¶
type Hotp struct {
// Counter
Counter int
// contains filtered or unexported fields
}
Hotp key.
Example ¶
package main
import (
"fmt"
"github.com/heliorosa/otp"
)
func main() {
k, err := otp.ImportHotp("otpauth://hotp/mydomain.com?secret=UYMIODYLDUSYMBVV&counter=0")
if err != nil {
fmt.Println(err)
return
}
if err = k.SetKey32("UYMIODYLDUSYMBVV"); err != nil {
fmt.Println(err)
return
}
fmt.Println(k.Counter, k.Code())
fmt.Println(k.CodeCounter(0))
fmt.Println(k.CodeCounter(1))
fmt.Println(k.CodeCounter(2))
}
Output: 0 453613 453613 511108 686989
func ImportHotp ¶
ImportHotp imports an url in the otpauth format.
func NewHotp ¶
NewHotp creates a new HOTP key. keyLen <= 0, defaults to 10. digits <= 0, defaults to 6. algorithm == "", defaults to "sha1".
func NewHotpWithDefaults ¶
NewHotpWithDefaults calls NewHotp with the default values.
func (*Hotp) CodeCounter ¶
CodeCounter returns the code for the counter c.
type Key ¶
type Key interface {
Code() int
CodeN(n int) int
Key32() string
SetKey32(string) error
Url() string
Type() string
fmt.Stringer
}
Key represents an OTP key.
func NewKey ¶
func NewKey(keyType string, keyLen int, label, issuer, algorithm string, digits int, extraParams url.Values) (Key, error)
NewKey creates a new OTP key. keyType must be either TypeTotp or TypeHotp. label is required. keyLen <= 0, defaults to 10. algorithm == "", defaults to "sha1". digits <= 0, defaults to 6
type Totp ¶
type Totp struct {
// Period in seconds
Period int
// contains filtered or unexported fields
}
Totp key.
Example ¶
package main
import (
"fmt"
"github.com/heliorosa/otp"
)
func main() {
k, err := otp.ImportTotp("otpauth://totp/mydomain.com?secret=UYMIODYLDUSYMBVV")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(k.CodePeriod(0))
fmt.Println(k.CodePeriod(1))
fmt.Println(k.CodePeriod(2))
}
Output: 453613 511108 686989
func ImportTotp ¶
ImportTotp imports an url in the otpauth format.
func NewTotp ¶
NewTotp creates a new TOTP key. keyLen <= 0, defaults to 10. digits <= 0, defaults to 6. period <= 0, defaults to 30. algorithm == "", defaults to "sha1".
func NewTotpWithDefaults ¶
NewTotpWithDefaults calls NewTotp() with the default values.
func (*Totp) CodePeriod ¶
CodePeriod returns the code for the period p.