pwreset

package
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DatabaseService

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

DatabaseService manages password reset tokens using database instead of Redis

func NewDatabaseService

func NewDatabaseService(db *gorm.DB, ttl time.Duration) *DatabaseService

NewDatabaseService creates a new database-based password reset service

func (*DatabaseService) ClearExpiredTokens

func (s *DatabaseService) ClearExpiredTokens() error

ClearExpiredTokens removes expired tokens from the database

func (*DatabaseService) ConsumeToken

func (s *DatabaseService) ConsumeToken(ctx context.Context, token string) error

ConsumeToken deletes the token to enforce single-use semantics

func (*DatabaseService) GenerateToken

func (s *DatabaseService) GenerateToken(ctx context.Context, userID string) (string, error)

GenerateToken creates a new secure token for the given user ID and stores it in database Returns the token string which should be sent to the user via email link

func (*DatabaseService) GetExpiredTokenCount

func (s *DatabaseService) GetExpiredTokenCount() (int64, error)

GetExpiredTokenCount returns the number of expired tokens that can be cleaned up

func (*DatabaseService) GetTokenCount

func (s *DatabaseService) GetTokenCount() (int64, error)

GetTokenCount returns the number of active (unused and not expired) tokens

func (*DatabaseService) GetTotalTokenCount

func (s *DatabaseService) GetTotalTokenCount() (int64, error)

GetTotalTokenCount returns the total number of tokens in the database

func (*DatabaseService) GetUsedTokenCount

func (s *DatabaseService) GetUsedTokenCount() (int64, error)

GetUsedTokenCount returns the number of used tokens

func (*DatabaseService) IsTokenUsed

func (s *DatabaseService) IsTokenUsed(token string) (bool, error)

IsTokenUsed checks if a token has been used

func (*DatabaseService) ValidateToken

func (s *DatabaseService) ValidateToken(ctx context.Context, token string) (string, error)

ValidateToken returns the associated userID if the token exists and is not expired

type PasswordResetServiceInterface

type PasswordResetServiceInterface interface {
	GenerateToken(ctx context.Context, userID string) (string, error)
	ValidateToken(ctx context.Context, token string) (string, error)
	ConsumeToken(ctx context.Context, token string) error
}

PasswordResetServiceInterface defines the interface for password reset operations

func NewPasswordResetServiceFactory

func NewPasswordResetServiceFactory(redisClient *redis.Client, db *gorm.DB, ttl time.Duration) PasswordResetServiceInterface

NewPasswordResetServiceFactory creates password reset service based on environment configuration

type PasswordResetToken

type PasswordResetToken struct {
	model.Base
	Token     string    `gorm:"uniqueIndex;not null" json:"token"`
	UserID    string    `gorm:"not null;index" json:"user_id"`
	ExpiresAt time.Time `gorm:"not null;index" json:"expires_at"`
	IsUsed    bool      `gorm:"default:false;index" json:"is_used"`
}

PasswordResetToken represents a password reset token in the database

type Service

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

Service manages password reset tokens using Redis with expiry and single-use semantics.

func NewService

func NewService(rdb *redis.Client, ttl time.Duration) *Service

func (*Service) ConsumeToken

func (s *Service) ConsumeToken(ctx context.Context, token string) error

ConsumeToken deletes the token to enforce single-use semantics.

func (*Service) GenerateToken

func (s *Service) GenerateToken(ctx context.Context, userID string) (string, error)

GenerateToken creates a new secure token for the given user ID and stores it in Redis. Returns the token string which should be sent to the user via email link.

func (*Service) ValidateToken

func (s *Service) ValidateToken(ctx context.Context, token string) (string, error)

ValidateToken returns the associated userID if the token exists and is not expired.

Jump to

Keyboard shortcuts

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