Documentation
¶
Index ¶
- Variables
- func GenerateSecureToken() (string, string, error)
- func HashToken(rawToken string) (string, error)
- type Account
- type InMemoryAccountRepository
- func (r *InMemoryAccountRepository) Create(a *Account) error
- func (r *InMemoryAccountRepository) Delete(id string) error
- func (r *InMemoryAccountRepository) Exists(id string) bool
- func (r *InMemoryAccountRepository) Get(id string) (*Account, error)
- func (r *InMemoryAccountRepository) GetByEmail(email string) (*Account, error)
- func (r *InMemoryAccountRepository) GetByUsername(email string) (*Account, error)
- func (r *InMemoryAccountRepository) Update(a *Account) error
- type PasswordResetToken
- type PasswordResetTokenRepository
- type RefreshToken
- type RefreshTokenRepository
- type TokenRepositories
Constants ¶
This section is empty.
Variables ¶
var ( ErrPasswordTooShort = fmt.Errorf("password too short, minimum %d characters", minPasswordLength) ErrPasswordTooLong = fmt.Errorf("password too long, maximum %d characters", maxPasswordLength) )
Functions ¶
func GenerateSecureToken ¶
GenerateSecureToken generates a cryptographically secure random token Returns the raw token (to send to user) and the hashed token (to store in DB)
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 (*Account) ValidatePassword ¶
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 (r *InMemoryAccountRepository) Get(id string) (*Account, error)
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