emailauth

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

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

Go to latest
Published: Sep 1, 2024 License: MIT Imports: 10 Imported by: 1

README

Email Authentication Package

This package provides a secure and flexible email authentication system using one-time codes. It's designed to be easily integrated into Go applications, offering both in-memory and Redis-based storage options for authentication codes.

Features

  • Secure code generation and verification
  • Flexible storage options (in-memory and Redis)
  • SMTP email sending capability
  • Rate limiting to prevent abuse

Installation

go get github.com/yourusername/emailauth

Usage

Here's a basic example of how to use the package:

import (
    "github.com/yourusername/emailauth"
)

// Initialize the email sender
emailSender := emailauth.NewSMTPEmailSender(
    "smtp.example.com",
    "587",
    "username",
    "password",
    "noreply@example.com",
    false,
)

// Initialize the code store (using in-memory store for this example)
codeStore := emailauth.NewInMemoryCodeStore()

// Create the auth service
authService := emailauth.NewAuthService(emailSender, codeStore)

// Send an authentication code
err := authService.SendAuthCode(ctx, "user@example.com")
if err != nil {
    // Handle error
}

// Verify the code
isValid, err := authService.VerifyCode(ctx, "user@example.com", "123456")
if err != nil {
    // Handle error
}
if isValid {
    // Code is valid, proceed with authentication
}

Configuration

The package can be configured using environment variables. See the example/main.go file for a complete example of how to set up and run a server using this package.

Testing

To run the tests:

go test ./...

Contributing

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

License

This package is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthCode

type AuthCode struct {
	Code      string    `json:"code"`
	ExpiresAt time.Time `json:"expires_at"`
}

type AuthService

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

func NewAuthService

func NewAuthService(emailSender EmailSender, store CodeStore) *AuthService

func (*AuthService) SendAuthCode

func (as *AuthService) SendAuthCode(ctx context.Context, email string) error

func (*AuthService) VerifyCode

func (as *AuthService) VerifyCode(ctx context.Context, email, code string) (bool, error)

type CodeStore

type CodeStore interface {
	Set(ctx context.Context, email string, code *AuthCode) error
	Get(ctx context.Context, email string) (*AuthCode, error)
	Delete(ctx context.Context, email string) error
}

type EmailSender

type EmailSender interface {
	SendEmail(to, subject, body string) error
}

type InMemoryCodeStore

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

func NewInMemoryCodeStore

func NewInMemoryCodeStore() *InMemoryCodeStore

NewInMemoryCodeStore initializes and returns a new InMemoryCodeStore.

func (*InMemoryCodeStore) Delete

func (s *InMemoryCodeStore) Delete(ctx context.Context, email string) error

Delete removes the authentication code for a given email from memory.

func (*InMemoryCodeStore) Get

func (s *InMemoryCodeStore) Get(ctx context.Context, email string) (*AuthCode, error)

Get retrieves the authentication code for a given email from memory.

func (*InMemoryCodeStore) Set

func (s *InMemoryCodeStore) Set(ctx context.Context, email string, code *AuthCode) error

Set stores the authentication code for a given email in memory.

type RedisClient

type RedisClient interface {
	Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
	Get(ctx context.Context, key string) (string, error)
	Del(ctx context.Context, keys ...string) error
}

type RedisCodeStore

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

func NewRedisCodeStore

func NewRedisCodeStore(client RedisClient) *RedisCodeStore

func (*RedisCodeStore) Delete

func (r *RedisCodeStore) Delete(ctx context.Context, email string) error

func (*RedisCodeStore) Get

func (r *RedisCodeStore) Get(ctx context.Context, email string) (*AuthCode, error)

func (*RedisCodeStore) Set

func (r *RedisCodeStore) Set(ctx context.Context, email string, code *AuthCode) error

type SMTPEmailSender

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

func NewSMTPEmailSender

func NewSMTPEmailSender(smtpHost, smtpPort, smtpUsername, smtpPassword, senderEmail string, allowUnencrypted bool) *SMTPEmailSender

func (*SMTPEmailSender) SendEmail

func (s *SMTPEmailSender) SendEmail(to, subject, body string) error

Directories

Path Synopsis
example module

Jump to

Keyboard shortcuts

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