otp

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRedisConnection  = &OTPError{Code: "ERR_REDIS_CONNECTION", Message: "Failed to connect to Redis"}
	ErrGenerateOTP      = &OTPError{Code: "ERR_GENERATE_OTP", Message: "Failed to generate OTP"}
	ErrGenerateToken    = &OTPError{Code: "ERR_GENERATE_TOKEN", Message: "Failed to generate token"}
	ErrInvalidOTP       = &OTPError{Code: "ERR_INVALID_OTP", Message: "Invalid OTP"}
	ErrExpiredOTP       = &OTPError{Code: "ERR_EXPIRED_OTP", Message: "OTP expired"}
	ErrTokenUsed        = &OTPError{Code: "ERR_TOKEN_USED", Message: "Token already used"}
	ErrMaxAttempts      = &OTPError{Code: "ERR_MAX_ATTEMPTS", Message: "Maximum attempts exceeded"}
	ErrWaitBeforeResend = &OTPError{Code: "ERR_WAIT_BEFORE_RESEND", Message: "Please wait before requesting a new OTP"}
)

Functions

This section is empty.

Types

type EmailRequest

type EmailRequest struct {
	Email string `json:"email"`
}

type EmailResponse

type EmailResponse struct {
	Token     string    `json:"token"`
	Status    bool      `json:"status"`
	IssuedAt  time.Time `json:"issued_at"`
	ExpiresAt time.Time `json:"expires_at"`
}

type ErrorHandler

type ErrorHandler interface {
	Error(err *OTPError) string
}

type NewOTP

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

NewOTP struct to hold Redis client and context

func Initialize

func Initialize(redisAddr, redisPassword string, redisDB int, maxAttempts int, expirationTime time.Duration) (*NewOTP, error)

Initialize initializes the Redis client and returns a NewOTP struct

func (*NewOTP) GenerateOTP

func (n *NewOTP) GenerateOTP(email string) (*EmailResponse, string, error)

GenerateOTP generates an OTP and stores it along with a session token in Redis

func (*NewOTP) ResendOTP

func (n *NewOTP) ResendOTP(email string) (*EmailResponse, string, error)

ResendOTP resends a new OTP and token to the user

func (*NewOTP) VerifyOTP

func (n *NewOTP) VerifyOTP(req VerifyRequest) (*VerifyResponse, error)

VerifyOTP verifies the provided OTP and session token

type OTP

type OTP struct {
	CodeHash    string
	MaxAttempts int
	IssuedAt    time.Time
	ExpiresAt   time.Time
}

OTP and SessionToken structures

type OTPError

type OTPError struct {
	Code    string
	Message string
}

func (*OTPError) Error

func (e *OTPError) Error() string

type OTPService

type OTPService interface {
	GenerateOTP(email string) (*EmailResponse, string, error)
	VerifyOTP(req VerifyRequest) (*VerifyResponse, error)
	ResendOTP(email string) (*EmailResponse, string, error)
}

OTPService defines the methods for generating, sending, verifying, and resending OTPs

type SessionToken

type SessionToken struct {
	Token     string
	IssuedAt  time.Time
	ExpiresAt time.Time
	Used      bool
	Attempts  int
}

type VerifyRequest

type VerifyRequest struct {
	Email string `json:"email"`
	OTP   string `json:"otp"`
	Token string `json:"token"`
}

type VerifyResponse

type VerifyResponse struct {
	Message string `json:"message,omitempty"`
	Status  bool   `json:"status"`
	Token   string `json:"token,omitempty"`
}

Jump to

Keyboard shortcuts

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