auth

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2025 License: GPL-2.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidExpireTime = errors.New("expire time must be in the future")

ErrInvalidExpireTime is returned when attempting to create a token with an expiration time in the past.

View Source
var ErrTokenExpired = errors.New("token expired")

ErrTokenExpired is returned when a token's expiration time has passed.

View Source
var ErrTokenNotFound = errors.New("token not found")

ErrTokenNotFound is returned when a token cannot be found in the database.

Functions

This section is empty.

Types

type Auth

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

Auth is the service that handles token generation and validation.

func NewAuth

func NewAuth(db *sql.DB, repo Repository, logger *slog.Logger) *Auth

NewAuth creates a new Auth service.

func (*Auth) CreateToken

func (a *Auth) CreateToken(ctx context.Context, expireTime time.Time) (*Token, string, error)

CreateToken creates a new token and stores it in the database.

func (*Auth) ValidateToken

func (a *Auth) ValidateToken(ctx context.Context, token string) (bool, error)

ValidateToken validates a token and returns true if it is valid, false otherwise.

type Repository

type Repository interface {
	// CreateToken stores a new token in the database.
	CreateToken(ctx context.Context, tx *sql.Tx, token Token) error

	// GetToken retrieves a token from the database by its hash.
	GetToken(ctx context.Context, tx *sql.Tx, hash string) (*Token, error)

	// DeleteToken removes a token from the database by its ID.
	DeleteToken(ctx context.Context, tx *sql.Tx, id string) error
}

Repository provides an interface for token persistence operations.

type Token

type Token struct {
	ID         string
	Suffix     string
	Hash       string
	CreateTime time.Time
	ExpireTime time.Time
}

func NewToken

func NewToken(expireTime time.Time) (*Token, string)

NewToken creates a new token with the given expiration time. It returns the token and a pretty value that can be used to identify the token.

func (*Token) IsExpired

func (t *Token) IsExpired() bool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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