otp

package module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2024 License: MIT Imports: 6 Imported by: 1

README

OTP

GitHub Releases Build Status codecov Go Report Card GoDevDoc Donate

Manage and generate one-time passwords.

Prerequisites

  • Go >= 1.22
Keyring (Optional)

Support OS X, Linux/BSD (dbus) and Windows.

OS X

The OS X implementation depends on the /usr/bin/security binary for interfacing with the OS X keychain. It should be available by default.

Linux and *BSD

The Linux and *BSD implementation depends on the Secret Service dbus interface, which is provided by GNOME Keyring.

It's expected that the default collection login exists in the keyring, because it's the default in most distros. If it doesn't exist, you can create it through the keyring frontend program Seahorse:

  • Open seahorse
  • Go to File > New > Password Keyring
  • Click Continue
  • When asked for a name, use: login

Install

go get go.nhat.io/otp

Usage

Example 1: Generate a new TOTP using a provided secret.

package main

import (
	"context"

	"go.nhat.io/otp"
)

func do(ctx context.Context) {
	result, err := otp.GenerateTOTP(ctx, otp.TOTPSecret("NBSWY3DP"))

	if err != nil {
		// Handle error.
	}

	// Use the result.
}

Example 2: Generate a new TOTP using a secret that persisted in keychain.

package main

import (
    "context"

    "go.nhat.io/otp"
    "go.nhat.io/otp/keyring"
)

func do(ctx context.Context) {
    result, err := otp.GenerateTOTP(ctx, keyring.TOTPSecretFromKeyring("john.doe@example.com"))

    if err != nil {
        // Handle error.
    }

    // Use the result.
}

Donation

If this project help you reduce time to develop, you can give me a cup of coffee :)

Paypal donation

paypal

       or scan this

Documentation

Overview

Package otp provides functionalities to manage and generate one-time passwords.

Index

Examples

Constants

View Source
const NoTOTPSecret = TOTPSecret("")

NoTOTPSecret is a TOTP secret that is empty.

Variables

View Source
var ErrNoTOTPSecret = errors.New("no totp secret")

ErrNoTOTPSecret indicates that the user has not configured the TOTP secret.

View Source
var ErrTOTPSecretReadOnly = errors.New("totp secret is read-only")

ErrTOTPSecretReadOnly indicates that the TOTP secret is read-only.

Functions

This section is empty.

Types

type EnvTOTPSecret added in v0.9.0

type EnvTOTPSecret struct {
	// contains filtered or unexported fields
}

EnvTOTPSecret is a TOTP secret provider that gets the TOTP secret from the environment.

func TOTPSecretFromEnv

func TOTPSecretFromEnv(env string) EnvTOTPSecret

TOTPSecretFromEnv returns a TOTP secret getter that gets the TOTP secret from the environment.

func (EnvTOTPSecret) DeleteTOTPSecret added in v0.9.0

func (e EnvTOTPSecret) DeleteTOTPSecret(_ context.Context) error

DeleteTOTPSecret deletes the TOTP secret from the environment.

func (EnvTOTPSecret) SetTOTPSecret added in v0.9.0

func (e EnvTOTPSecret) SetTOTPSecret(_ context.Context, secret TOTPSecret, _ string) error

SetTOTPSecret sets the TOTP secret to the environment.

func (EnvTOTPSecret) TOTPSecret added in v0.9.0

func (e EnvTOTPSecret) TOTPSecret(_ context.Context) TOTPSecret

TOTPSecret returns the TOTP secret from the environment.

func (EnvTOTPSecret) TOTPSecretDeleter added in v0.9.0

func (e EnvTOTPSecret) TOTPSecretDeleter() TOTPSecretDeleter

TOTPSecretDeleter returns TOTPSecretDeleter.

func (EnvTOTPSecret) TOTPSecretGetter added in v0.9.0

func (e EnvTOTPSecret) TOTPSecretGetter() TOTPSecretGetter

TOTPSecretGetter returns TOTPSecretGetter.

func (EnvTOTPSecret) TOTPSecretSetter added in v0.9.0

func (e EnvTOTPSecret) TOTPSecretSetter() TOTPSecretSetter

TOTPSecretSetter returns TOTPSecretSetter.

type Generator

type Generator interface {
	GenerateOTP(ctx context.Context) (OTP, error)
}

Generator is a one-time password generator.

type OTP

type OTP string

OTP is a one-time password.

func GenerateTOTP

func GenerateTOTP(ctx context.Context, secret TOTPSecretGetter, opts ...TOTPGeneratorOption) (OTP, error)

GenerateTOTP generates a TOTP.

Example
package main

import (
	"context"
	"fmt"
	"time"

	"go.nhat.io/clock"

	"go.nhat.io/otp"
)

func main() {
	c := clock.Fix(time.Date(2024, time.January, 1, 0, 0, 0, 0, time.UTC))

	result, err := otp.GenerateTOTP(context.Background(), otp.TOTPSecret("NBSWY3DP"), otp.WithClock(c))
	if err != nil {
		panic(err)
	}

	fmt.Println(result)

}
Output:

191882

func (OTP) String added in v0.2.0

func (o OTP) String() string

String returns the string representation of the one-time password.

type Option

type Option interface {
	TOTPGeneratorOption
}

Option configures the apis of the authenticator package.

func WithClock

func WithClock(c clock.Clock) Option

WithClock sets the clock of the TOTPGenerator.

type TOTPGenerator

type TOTPGenerator struct {
	// contains filtered or unexported fields
}

TOTPGenerator is a .TOTPGenerator.

func NewTOTPGenerator

func NewTOTPGenerator(secretGetter TOTPSecretGetter, opts ...TOTPGeneratorOption) *TOTPGenerator

NewTOTPGenerator initiates a new .TOTPGenerator.

func (*TOTPGenerator) GenerateOTP

func (g *TOTPGenerator) GenerateOTP(ctx context.Context) (OTP, error)

GenerateOTP generates a TOTP.

type TOTPGeneratorOption

type TOTPGeneratorOption interface {
	// contains filtered or unexported methods
}

TOTPGeneratorOption is an option to configure TOTPGenerator.

type TOTPSecret

type TOTPSecret string

TOTPSecret is a TOTP secret.

func (TOTPSecret) DeleteTOTPSecret added in v0.7.0

func (s TOTPSecret) DeleteTOTPSecret(context.Context) error

DeleteTOTPSecret deletes the TOTP secret.

func (TOTPSecret) MarshalText added in v0.3.0

func (s TOTPSecret) MarshalText() ([]byte, error)

MarshalText returns the TOTP secret as text.

func (TOTPSecret) SetTOTPSecret added in v0.7.0

func (s TOTPSecret) SetTOTPSecret(context.Context, TOTPSecret, string) error

SetTOTPSecret sets the TOTP secret.

func (TOTPSecret) String added in v0.2.0

func (s TOTPSecret) String() string

String returns the TOTP secret as a string.

func (TOTPSecret) TOTPSecret

func (s TOTPSecret) TOTPSecret(context.Context) TOTPSecret

TOTPSecret returns the TOTP secret.

func (TOTPSecret) TOTPSecretGetter added in v0.9.0

func (s TOTPSecret) TOTPSecretGetter() TOTPSecretGetter

TOTPSecretGetter returns TOTPSecretGetter.

func (*TOTPSecret) UnmarshalText added in v0.3.0

func (s *TOTPSecret) UnmarshalText(text []byte) error

UnmarshalText unmarshals the TOTP secret from text.

type TOTPSecretDeleter added in v0.5.0

type TOTPSecretDeleter interface {
	DeleteTOTPSecret(ctx context.Context) error
}

TOTPSecretDeleter is an interface that deletes a TOTP secret.

type TOTPSecretGetter

type TOTPSecretGetter interface {
	TOTPSecret(ctx context.Context) TOTPSecret
}

TOTPSecretGetter is an interface that provides a TOTP secret.

type TOTPSecretGetters

type TOTPSecretGetters []TOTPSecretGetter

TOTPSecretGetters is a list of TOTP secret getters.

func ChainTOTPSecretGetters

func ChainTOTPSecretGetters(getters ...TOTPSecretGetter) TOTPSecretGetters

ChainTOTPSecretGetters chains the TOTP secret getters.

func (TOTPSecretGetters) TOTPSecret

func (p TOTPSecretGetters) TOTPSecret(ctx context.Context) TOTPSecret

TOTPSecret returns the first non-empty TOTP secret that it finds from the list of TOTP secret getters.

func (TOTPSecretGetters) TOTPSecretGetter added in v0.9.0

func (p TOTPSecretGetters) TOTPSecretGetter() TOTPSecretGetter

TOTPSecretGetter returns TOTPSecretGetter.

type TOTPSecretProvider added in v0.5.0

type TOTPSecretProvider interface {
	TOTPSecretGetter
	TOTPSecretSetter
	TOTPSecretDeleter
}

TOTPSecretProvider is an interface that manages a TOTP secret.

type TOTPSecretProviders added in v0.7.0

type TOTPSecretProviders []TOTPSecretProvider

TOTPSecretProviders is a list of TOTP secret getters.

func ChainTOTPSecretProviders added in v0.7.0

func ChainTOTPSecretProviders(providers ...TOTPSecretProvider) TOTPSecretProviders

ChainTOTPSecretProviders chains the TOTP secret providers.

func (TOTPSecretProviders) DeleteTOTPSecret added in v0.7.0

func (ps TOTPSecretProviders) DeleteTOTPSecret(ctx context.Context) error

DeleteTOTPSecret deletes the TOTP secret.

func (TOTPSecretProviders) SetTOTPSecret added in v0.7.0

func (ps TOTPSecretProviders) SetTOTPSecret(ctx context.Context, secret TOTPSecret, issuer string) error

SetTOTPSecret sets the TOTP secret.

func (TOTPSecretProviders) TOTPSecret added in v0.7.0

func (ps TOTPSecretProviders) TOTPSecret(ctx context.Context) TOTPSecret

TOTPSecret returns the first non-empty TOTP secret that it finds from the list of TOTP secret getters.

func (TOTPSecretProviders) TOTPSecretDeleter added in v0.9.0

func (ps TOTPSecretProviders) TOTPSecretDeleter() TOTPSecretDeleter

TOTPSecretDeleter returns TOTPSecretDeleter.

func (TOTPSecretProviders) TOTPSecretGetter added in v0.9.0

func (ps TOTPSecretProviders) TOTPSecretGetter() TOTPSecretGetter

TOTPSecretGetter returns TOTPSecretGetter.

func (TOTPSecretProviders) TOTPSecretSetter added in v0.9.0

func (ps TOTPSecretProviders) TOTPSecretSetter() TOTPSecretSetter

TOTPSecretSetter returns TOTPSecretSetter.

type TOTPSecretSetter

type TOTPSecretSetter interface {
	SetTOTPSecret(ctx context.Context, secret TOTPSecret, issuer string) error
}

TOTPSecretSetter is an interface that sets a TOTP secret.

Directories

Path Synopsis
Package keyring provides totp secret storage using keyring.
Package keyring provides totp secret storage using keyring.
Package mock provides mocks for totp package.
Package mock provides mocks for totp package.

Jump to

Keyboard shortcuts

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