authenticator

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2024 License: MIT Imports: 13 Imported by: 0

README

Go TOTP Example

A simple and clean implementation of Time-based One-Time Password (TOTP) using Go and HTML. This package provides both a library for TOTP operations and a working example server.

Features

  • TOTP secret generation
  • QR code generation for easy setup with authenticator apps
  • TOTP code validation
  • Clean, responsive web interface
  • Easy to use as a library or standalone example

Installation

go get github.com/ivikasavnish/totp

Usage

As a Library
import "github.com/ivikasavnish/totp/pkg/totp"

// Generate a new TOTP secret
secret, err := totp.GenerateSecret("Example App", "[email protected]")
if err != nil {
    log.Fatal(err)
}

// Validate a TOTP code
isValid := totp.ValidateCode(code, secret)
Running the Example Server
cd example
go run main.go

Then open http://localhost:8080 in your browser.

License

MIT License - see LICENSE file

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Documentation

Overview

Package authenticator provides TOTP (Time-based One-Time Password) authentication implementation compatible with Google Authenticator, Microsoft Authenticator, and other standard TOTP applications.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateSecret

func GenerateSecret(length int) (string, error)

GenerateSecret generates a new random secret key

Types

type Algorithm

type Algorithm string

Algorithm represents the supported hash algorithms

const (
	SHA1   Algorithm = "SHA1"
	SHA256 Algorithm = "SHA256"
	SHA512 Algorithm = "SHA512"
)

type Authenticator

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

Authenticator handles TOTP generation and validation

func NewAuthenticator

func NewAuthenticator(config Config) (*Authenticator, error)

NewAuthenticator creates a new Authenticator instance

func (*Authenticator) FormatSecretKey

func (a *Authenticator) FormatSecretKey() string

FormatSecretKey formats the secret key for manual entry

func (*Authenticator) GenerateQRCode

func (a *Authenticator) GenerateQRCode(accountName, issuer string) string

GenerateQRCode generates a QR code URI for the authenticator app

func (*Authenticator) GenerateTOTP

func (a *Authenticator) GenerateTOTP() (string, error)

GenerateTOTP generates a TOTP token

func (*Authenticator) ValidateTOTP

func (a *Authenticator) ValidateTOTP(token string, userId string) (bool, error)

ValidateTOTP validates a TOTP token

type Config

type Config struct {
	// Algorithm to use for TOTP generation
	Algorithm Algorithm
	// Digits is the length of the generated TOTP
	Digits int
	// Period is the time step in seconds
	Period int
	// Secret is the base32 encoded secret key
	Secret string
}

Config holds the configuration for TOTP generation

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with standard settings

type RateLimiter

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

RateLimiter handles rate limiting for validation attempts

func (*RateLimiter) CheckLimit

func (r *RateLimiter) CheckLimit(userId string) (bool, error)

CheckLimit checks if the rate limit has been exceeded

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

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