totp

package module
v1.0.1 Latest Latest
Warning

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

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

README

totp-go

Go port of my rust library https://github.com/constantoine/totp-rs

Documentation

Overview

package totp describes and provides needed types and interfaces to generate and validate 2fa codes easily

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSecretTooShort   = Error{"secret is too short", 1, nil}
	ErrInvalidAlgorithm = Error{"secret is too short", 2, nil}
)

Functions

This section is empty.

Types

type Algorithm

type Algorithm uint

Algorithm enum holds the three standards algorithms for TOTP as per the [reference implementation](https://tools.ietf.org/html/rfc6238#appendix-A)

const (
	SHA1 Algorithm = iota
	SHA256
	SHA512
)

The different values of the Algorith enum

type Error

type Error struct {
	ID    uint64
	Cause error
	// contains filtered or unexported fields
}

Error is a custom error type

func (Error) Error

func (e Error) Error() string

Error prints error message

func (Error) Is

func (e Error) Is(target error) bool

Is makes errors.Is work by comparing internal ID

func (Error) Unwrap

func (e Error) Unwrap() error

Unwrap returns wrapped error if any, or nil Makes errors.Unwrap(err) work

func (Error) Wrap

func (e Error) Wrap(err error) Error

Wrap is made to work in pair with Unwrap.

type TOTP

type TOTP interface {
	// Generate a token for the provided timestamp
	Generate(time time.Time) (token string, err error)
	// Check if a token is valid, accounting for the skew field
	Check(token string, time time.Time) (valid bool, err error)
	// Sign a timestamp with a secret
	Sign(time time.Time) ([]byte, error)
	// Returns the secret as unpadded base32
	Secret() string
	// Returns the standard URL for adding an account
	Url(label, issuer string) (url string, err error)
	// Generates a standard QrCode scannable by common apps, as its base64 representation
	Qr(label, issuer string) (base64 string, err error)
}

TOTP holds informations as to how to generate an auth code and validate it. Its secret field is sensitive data, treat it accordingly

func New

func New(algorithm Algorithm, digits, skew uint, step uint64, secret []byte) (TOTP, error)

New will return a new, initialized totp Will return an error if secret is smaller than 128 bits

Jump to

Keyboard shortcuts

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