otp

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 14, 2016 License: BSD-2-Clause Imports: 14 Imported by: 0

README

Build Status Coverage Status godoc reference Go Report Card

otp

go package with support for TOTP and HOTP

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

View Source
const (
	TypeTotp = "totp" //TOTP
	TypeHotp = "hotp" // HOTP
)

Types of OTP auth supported.

View Source
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

View Source
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.

View Source
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.

func (*Error) Error

func (g *Error) Error() string

Implement error.

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

func ImportHotp(u string) (*Hotp, error)

ImportHotp imports an url in the otpauth format.

func NewHotp

func NewHotp(keyLen int, label, issuer, algorithm string, digits, counter int) (*Hotp, error)

NewHotp creates a new HOTP key. keyLen <= 0, defaults to 10. digits <= 0, defaults to 6. algorithm == "", defaults to "sha1".

func NewHotpWithDefaults

func NewHotpWithDefaults(label, issuer string) (*Hotp, error)

NewHotpWithDefaults calls NewHotp with the default values.

func (*Hotp) Code

func (h *Hotp) Code() int

Code returns the current code.

func (*Hotp) CodeCounter

func (h *Hotp) CodeCounter(c int) int

CodeCounter returns the code for the counter c.

func (*Hotp) CodeN added in v0.0.2

func (h *Hotp) CodeN(n int) int

CodeN returns the code for counter+n.

func (Hotp) Key32

func (k Hotp) Key32() string

Key32 returns the Key field encoded in base32.

func (Hotp) SetKey32

func (k Hotp) SetKey32(key string) error

SetKey32 sets the Key field from a base32 string.

func (*Hotp) String

func (h *Hotp) String() string

String returns the same as Url().

func (*Hotp) Type

func (h *Hotp) Type() string

Type returns TypeHotp

func (*Hotp) Url

func (h *Hotp) Url() string

Url returns the key in the otpauth format.

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 ImportKey

func ImportKey(u string) (Key, error)

Import an OTP key from an otpauth url.

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

func NewKeyWithDefaults

func NewKeyWithDefaults(keyType, label, issuer string, extraParams url.Values) (Key, error)

NewKeyWithDefaults calls NewKey with the default values.

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

func ImportTotp(u string) (*Totp, error)

ImportTotp imports an url in the otpauth format.

func NewTotp

func NewTotp(keyLen int, label, issuer, algorithm string, digits, period int) (*Totp, error)

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

func NewTotpWithDefaults(label, issuer string) (*Totp, error)

NewTotpWithDefaults calls NewTotp() with the default values.

func (*Totp) Code

func (t *Totp) Code() int

Code returns the current code.

func (*Totp) CodeN added in v0.0.2

func (t *Totp) CodeN(n int) int

CodeN returns the code for the current period+n.

func (*Totp) CodePeriod

func (t *Totp) CodePeriod(p int) int

CodePeriod returns the code for the period p.

func (*Totp) CodeTime

func (t *Totp) CodeTime(tm time.Time) int

CodeTime returns the code for the time tm.

func (Totp) Key32

func (k Totp) Key32() string

Key32 returns the Key field encoded in base32.

func (Totp) SetKey32

func (k Totp) SetKey32(key string) error

SetKey32 sets the Key field from a base32 string.

func (*Totp) String

func (t *Totp) String() string

String returns the same as Url().

func (*Totp) Type

func (t *Totp) Type() string

Type returns TypeTotp.

func (*Totp) Url

func (t *Totp) Url() string

Url returns the key in otpauth format.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL