otp

package module
v0.0.0-...-e313bc2 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2014 License: MIT Imports: 12 Imported by: 0

README

##Introduction## This is a package for GO for timebased (TOTP) or counterbased (HOTP) One-Time Passwords as defined in RFC 6238 and RFC 4226.

##License## MIT License

##Documentation## See GoDoc

Documentation

Overview

One Time Password generator compatible with RFC 6238 and RFC 4226

Index

Examples

Constants

View Source
const DefaultCounterTolerance = 5

Default tolerance value for HOTP generators

View Source
const DefaultTimePeriod time.Duration = 30 * time.Second

the default time period an otp is valid

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

type Algorithm byte

constants defining which hash algorithm should be used.

const (
	// (default)
	SHA1 Algorithm = iota
	SHA256
	SHA512
	MD5
)

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 DigitMode

type DigitMode byte

indicating the number of digits used for OTP generation

const (
	// Decimal 6 digits
	Dec6 DigitMode = 6
	// Decimal 8 digits
	Dec8 DigitMode = 8
)

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

func NewDefaultTOTP(issuer string) *TimeBased

returns a new timebased otp generator for the given issuer (6 digits, SHA1, based on current system time, every 30 seconds)

func (*TimeBased) CurrentOTP

func (tbi *TimeBased) CurrentOTP(account Account) string

calculates the current otp for the given account

func (*TimeBased) IsValid

func (tbi *TimeBased) IsValid(account Account, otp string, tolerance time.Duration) bool

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

func (tbi *TimeBased) 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

Jump to

Keyboard shortcuts

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