adapter

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPasswordTooShort = fmt.Errorf("password too short, minimum %d characters", minPasswordLength)
	ErrPasswordTooLong  = fmt.Errorf("password too long, maximum %d characters", maxPasswordLength)
)
View Source
var (
	ErrAccountNotFound    = errors.New("account not found")
	ErrCannotCreateWithID = errors.New("cannot create account with ID")
	ErrPasswordNotSet     = errors.New("password not set")
)
View Source
var (
	ErrTokenNotFound = errors.New("token not found")
	ErrTokenExpired  = errors.New("token expired")
	ErrTokenRevoked  = errors.New("token revoked")
	ErrTokenUsed     = errors.New("token already used")
)

Functions

func GenerateSecureToken

func GenerateSecureToken() (string, string, error)

GenerateSecureToken generates a cryptographically secure random token Returns the raw token (to send to user) and the hashed token (to store in DB)

func HashToken

func HashToken(rawToken string) (string, error)

HashToken hashes a raw token for database storage

Types

type Account

type Account struct {
	ID              string    `json:"id,omitempty"`
	CreatedAt       time.Time `json:"created_at"`
	UpdatedAt       time.Time `json:"updated_at"`
	LastLogin       time.Time `json:"last"`
	Username        string    `json:"username"     validate:"required,min=4,max=50"`
	Name            string    `json:"name"         validate:"required,min=3,max=120"`
	Email           string    `json:"email"        validate:"required,email"`
	Enabled         bool      `json:"enabled"`
	Role            string    `json:"role"         validate:"required,min=3,max=10"  default:"user"`
	CryptedPassword []byte    `json:"_"`
}

func (*Account) SetPassword

func (a *Account) SetPassword(password string) error

func (Account) Validate

func (a Account) Validate() error

func (*Account) ValidatePassword

func (a *Account) ValidatePassword(password string) error

type InMemoryAccountRepository

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

func NewInMemoryAccountRepository

func NewInMemoryAccountRepository() *InMemoryAccountRepository

func (*InMemoryAccountRepository) Create

func (r *InMemoryAccountRepository) Create(a *Account) error

func (*InMemoryAccountRepository) Delete

func (r *InMemoryAccountRepository) Delete(id string) error

func (*InMemoryAccountRepository) Exists

func (r *InMemoryAccountRepository) Exists(id string) bool

func (*InMemoryAccountRepository) Get

func (*InMemoryAccountRepository) GetByEmail

func (r *InMemoryAccountRepository) GetByEmail(email string) (*Account, error)

func (*InMemoryAccountRepository) GetByUsername

func (r *InMemoryAccountRepository) GetByUsername(email string) (*Account, error)

func (*InMemoryAccountRepository) Update

func (r *InMemoryAccountRepository) Update(a *Account) error

type PasswordResetToken

type PasswordResetToken struct {
	ID        string
	UserID    string
	TokenHash string
	ExpiresAt time.Time
	CreatedAt time.Time
	UsedAt    *time.Time
}

PasswordResetToken represents a password reset token in the database

func NewPasswordResetToken

func NewPasswordResetToken(userID, tokenHash string, expiresIn time.Duration) *PasswordResetToken

NewPasswordResetToken creates a new PasswordResetToken with the given parameters

func (*PasswordResetToken) IsExpired

func (t *PasswordResetToken) IsExpired() bool

IsExpired checks if the password reset token has expired

func (*PasswordResetToken) IsUsed

func (t *PasswordResetToken) IsUsed() bool

IsUsed checks if the password reset token has been used

func (*PasswordResetToken) IsValid

func (t *PasswordResetToken) IsValid() bool

IsValid checks if the token is valid (not expired, not used)

type PasswordResetTokenRepository

type PasswordResetTokenRepository interface {
	// Create stores a new password reset token
	Create(token *PasswordResetToken) error

	// GetByTokenHash retrieves a password reset token by its hash
	GetByTokenHash(tokenHash string) (*PasswordResetToken, error)

	// MarkAsUsed marks a password reset token as used
	MarkAsUsed(tokenHash string) error

	// DeleteExpired removes all expired tokens
	DeleteExpired() error

	// DeleteByUserID removes all password reset tokens for a user
	DeleteByUserID(userID string) error
}

PasswordResetTokenRepository defines the interface for password reset token operations

type RefreshToken

type RefreshToken struct {
	ID        string
	UserID    string
	TokenHash string
	ExpiresAt time.Time
	CreatedAt time.Time
	RevokedAt *time.Time
}

RefreshToken represents a refresh token in the database

func NewRefreshToken

func NewRefreshToken(userID, tokenHash string, expiresIn time.Duration) *RefreshToken

NewRefreshToken creates a new RefreshToken with the given parameters

func (*RefreshToken) IsExpired

func (t *RefreshToken) IsExpired() bool

IsExpired checks if the refresh token has expired

func (*RefreshToken) IsRevoked

func (t *RefreshToken) IsRevoked() bool

IsRevoked checks if the refresh token has been revoked

func (*RefreshToken) IsValid

func (t *RefreshToken) IsValid() bool

IsValid checks if the token is valid (not expired, not revoked)

type RefreshTokenRepository

type RefreshTokenRepository interface {
	// Create stores a new refresh token
	Create(token *RefreshToken) error

	// GetByTokenHash retrieves a refresh token by its hash
	GetByTokenHash(tokenHash string) (*RefreshToken, error)

	// GetByUserID retrieves all refresh tokens for a user
	GetByUserID(userID string) ([]*RefreshToken, error)

	// Revoke marks a refresh token as revoked
	Revoke(tokenHash string) error

	// RevokeAll revokes all refresh tokens for a user
	RevokeAll(userID string) error

	// DeleteExpired removes all expired tokens
	DeleteExpired() error

	// Delete removes a specific token
	Delete(id string) error
}

RefreshTokenRepository defines the interface for refresh token operations

type TokenRepositories

type TokenRepositories struct {
	RefreshToken       RefreshTokenRepository
	PasswordResetToken PasswordResetTokenRepository
}

TokenRepositories combines both token repositories

Jump to

Keyboard shortcuts

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