auth

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2026 License: AGPL-3.0 Imports: 15 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 (
	ErrUserNotFound        = errors.New("user not found")
	ErrUserAlreadyExists   = errors.New("user already exists")
	ErrInvalidToken        = errors.New("invalid token")
	ErrExpiredToken        = errors.New("token has expired")
	ErrMissingToken        = errors.New("token is required")
	ErrTokenAlreadyUsed    = errors.New("token already used")
	ErrInvalidRefreshToken = errors.New("invalid refresh token")
	ErrExpiredRefreshToken = errors.New("refresh token has expired")
	ErrRevokedRefreshToken = errors.New("refresh token has been revoked")
)

Functions

This section is empty.

Types

type AuthHandler

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

func NewAuthHandler

func NewAuthHandler(service service, tracker analytics.Tracker) *AuthHandler

func (*AuthHandler) Logout

func (h *AuthHandler) Logout(c *echo.Context) error

func (*AuthHandler) Refresh

func (h *AuthHandler) Refresh(c *echo.Context) error

func (*AuthHandler) Register

func (h *AuthHandler) Register(c *echo.Context) error
func (h *AuthHandler) RequestMagicLink(c *echo.Context) error

func (*AuthHandler) Verify

func (h *AuthHandler) Verify(c *echo.Context) error

func (*AuthHandler) VerifyRedirect

func (h *AuthHandler) VerifyRedirect(c *echo.Context) error

type AuthRepository

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

AuthRepository provides database access for authentication-related entities.

func NewAuthRepository

func NewAuthRepository(db *gorm.DB) *AuthRepository

NewAuthRepository constructs an AuthRepository backed by the given GORM database.

func (*AuthRepository) CreateMagicLinkToken

func (r *AuthRepository) CreateMagicLinkToken(ctx context.Context, token MagicLinkToken) error

CreateMagicLinkToken persists a new magic-link token record.

func (*AuthRepository) CreateRefreshToken

func (r *AuthRepository) CreateRefreshToken(ctx context.Context, token RefreshToken) error

func (*AuthRepository) GetRefreshTokenByHash

func (r *AuthRepository) GetRefreshTokenByHash(ctx context.Context, hash string) (*RefreshToken, error)

func (*AuthRepository) RevokeAllUserRefreshTokens

func (r *AuthRepository) RevokeAllUserRefreshTokens(ctx context.Context, userID uuid.UUID) error

func (*AuthRepository) RevokeRefreshToken

func (r *AuthRepository) RevokeRefreshToken(ctx context.Context, tokenID uuid.UUID) error

func (*AuthRepository) VerifyAndConsumeMagicLinkToken added in v0.2.0

func (r *AuthRepository) VerifyAndConsumeMagicLinkToken(ctx context.Context, tokenHash string) (uuid.UUID, error)

VerifyAndConsumeMagicLinkToken atomically validates and marks a magic-link token as used in a single UPDATE ... RETURNING statement, eliminating the race window that existed when verification and consumption were separate round-trips.

It returns the associated user_id on success. If no row is updated — because the token does not exist, has already been used, or has expired — ErrInvalidToken is returned. Callers must not perform additional expiry or used_at checks; the database enforces those invariants atomically.

type AuthService

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

func NewAuthService

func NewAuthService(repository repository, users userRepository, emailSender emailSender,
	jwtManager *utils.JWTManager, magicLinkTokenDuration time.Duration, refreshTokenDuration time.Duration) *AuthService
func (s *AuthService) CreateMagicLink(ctx context.Context, email string) error
func (s *AuthService) LoginWithMagicLink(ctx context.Context, token string) (*LoginResponse, error)

LoginWithMagicLink verifies a raw magic-link token and, on success, issues a new access token and refresh token pair. The verification is performed atomically in the database to prevent replay attacks from concurrent requests.

func (*AuthService) Logout

func (s *AuthService) Logout(ctx context.Context, rawRefreshToken string) error

func (*AuthService) RefreshAccessToken

func (s *AuthService) RefreshAccessToken(ctx context.Context, rawRefreshToken string) (*LoginResponse, error)

func (*AuthService) Register

func (s *AuthService) Register(ctx context.Context, email string, name string) error

type LoginResponse

type LoginResponse struct {
	// UserID is the authenticated user's UUID. Clients can use this to
	// initialise their local state without needing to decode the JWT.
	UserID       string `json:"user_id"`
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
}

type LogoutRequest

type LogoutRequest struct {
	RefreshToken string `json:"refresh_token" validate:"required"`
}

type MagicLinkRequest

type MagicLinkRequest struct {
	Email string `json:"email" validate:"required,email"`
}

type MagicLinkToken

type MagicLinkToken struct {
	TokenID   uuid.UUID  `gorm:"primaryKey;type:uuid;default:gen_random_uuid()"`
	UserID    uuid.UUID  `gorm:"not null;index"`
	User      users.User `gorm:"constraint:OnDelete:CASCADE;"`
	Email     string     `gorm:"not null"`
	Token     string     `gorm:"not null;uniqueIndex"`
	ExpiresAt time.Time  `gorm:"not null"`
	UsedAt    *time.Time `gorm:"index"`
}

type RefreshRequest

type RefreshRequest struct {
	RefreshToken string `json:"refresh_token" validate:"required"`
}

type RefreshToken

type RefreshToken struct {
	TokenID   uuid.UUID  `gorm:"primaryKey;type:uuid;default:gen_random_uuid()"`
	UserID    uuid.UUID  `gorm:"not null;index"`
	User      users.User `gorm:"constraint:OnDelete:CASCADE;"`
	TokenHash string     `gorm:"column:token_hash;not null;uniqueIndex"`
	ExpiresAt time.Time  `gorm:"not null"`
	CreatedAt time.Time  `gorm:"autoCreateTime"`
	RevokedAt *time.Time
}

type RegisterRequest

type RegisterRequest struct {
	Email string `json:"email" validate:"required,email"`
	Name  string `json:"name" validate:"required"`
}

type VerifyRequest

type VerifyRequest struct {
	Token string `json:"token" validate:"required"`
}

Jump to

Keyboard shortcuts

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