Skip to content

nhatthm/go-otp

Repository files navigation

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