auth

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

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

Go to latest
Published: May 15, 2020 License: BSD-3-Clause Imports: 8 Imported by: 0

README

2fa-auth

2fa-auth is a one-time passcode (time-based) generator, ideal for use in two-factor authentication, that supports Google Authenticator and other two-factor devices.

Install

go get github.com/tsirysndr/2fa-auth

Usage

Let's say you have a user that wants to enable two-factor authentication, and you intend to do two-factor authentication using an app like Google Authenticator, Duo Security, Authy, etc. This is a three-step process:

  1. Generate a secret
  2. Show a QR code for the user to scan in
  3. Authenticate the token for the first time

Generating a key

This will generate a secret key of length 16, which will be the secret key for the user.

import (
  fmt
  auth "github.com/tsirysndr/2fa-auth"
)

...

g, _ := auth.GenerateSecret(&auth.Options{
  Length:     16,
  Symbols:    true,
  OtpauthURL: true,
  Name:       name,
  Issuer:     issuer,
})
secret, _ := json.Marshal(g)
fmt.Println(string(secret))

Verifying the token

After the user scans the QR code, ask the user to enter in the token that they see in their app. Then, verify it against the secret.

import (
  fmt
  auth "github.com/tsirysndr/2fa-auth"
)

...

fmt.Println(auth.VerifyOTP(secret, code))

Author

👤 Tsiry Sandratraina

Show your support

Give a ⭐️ if this project helped you!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateOtpauthURL

func GenerateOtpauthURL(opt *OtpauthOptions) (string, error)

func GenerateSecretASCII

func GenerateSecretASCII(length int, symbols bool) string

func VerifyOTP

func VerifyOTP(secret, code string) (bool, error)

Types

type GeneratedSecret

type GeneratedSecret struct {
	Ascii        string `json:"ascii,omitempty"`
	Hex          string `json:"hex,omitempty"`
	Base32       string `json:"base32,omitempty"`
	QrCodeAscii  string `json:"qr_code_ascii,omitempty"`
	QrCodeHex    string `json:"qr_code_hex,omitempty"`
	QrCodeBase32 string `json:"qr_code_base32,omitempty"`
	GoogleAuthQR string `json:"google_auth_qr,omitempty"`
	OtpAuthQR    string `json:"otp_auth_qr,omitempty"`
	OtpType      string `json:"otp_type,omitempty"`
}

func GenerateSecret

func GenerateSecret(opt *Options) (*GeneratedSecret, error)

type Options

type Options struct {
	Length       int
	Name         string
	QrCodes      bool
	GoogleAuthQr bool
	OtpauthURL   bool
	Symbols      bool
	Issuer       string
}

type OtpauthOptions

type OtpauthOptions struct {
	Secret    string
	Label     string
	Type      string
	Counter   int
	Issuer    string
	Algorithm string
	Digits    int
	Period    int
	Encoding  string
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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