Documentation
¶
Overview ¶
Package user provides the user domain model.
Index ¶
- Variables
- func AlreadyExistsError(email string) error
- func NotFoundByEmailError(email string) error
- func NotFoundByKeycloakIDError(keycloakID string) error
- func NotFoundError(userID shared.ID) error
- type AuthProvider
- type Filter
- type Preferences
- type Repository
- type Status
- type User
- func New(email, name string) (*User, error)
- func NewFromKeycloak(keycloakID, email, name string) (*User, error)
- func NewLocalUser(email, name, passwordHash string) (*User, error)
- func NewLocalUserWithID(id shared.ID, email, name string) (*User, error)
- func NewOAuthUser(email, name, avatarURL string, provider AuthProvider) (*User, error)
- func Reconstitute(id shared.ID, keycloakID *string, email, name, avatarURL, phone string, ...) *User
- func (u *User) Activate() error
- func (u *User) AuthProvider() AuthProvider
- func (u *User) AvatarURL() string
- func (u *User) CanLogin() bool
- func (u *User) ClearPasswordResetToken()
- func (u *User) CreatedAt() time.Time
- func (u *User) Deactivate() error
- func (u *User) Email() string
- func (u *User) EmailVerificationExpiresAt() *time.Time
- func (u *User) EmailVerificationToken() *string
- func (u *User) EmailVerified() bool
- func (u *User) FailedLoginAttempts() int
- func (u *User) ID() shared.ID
- func (u *User) IsActive() bool
- func (u *User) IsEmailVerificationTokenValid(token string) bool
- func (u *User) IsLocalUser() bool
- func (u *User) IsLocked() bool
- func (u *User) IsOIDCUser() bool
- func (u *User) IsPasswordResetTokenValid(token string) bool
- func (u *User) IsSuspended() bool
- func (u *User) KeycloakID() *string
- func (u *User) LastLoginAt() *time.Time
- func (u *User) LockedUntil() *time.Time
- func (u *User) Name() string
- func (u *User) PasswordHash() *string
- func (u *User) PasswordResetExpiresAt() *time.Time
- func (u *User) PasswordResetToken() *string
- func (u *User) Phone() string
- func (u *User) Preferences() Preferences
- func (u *User) RecordFailedLogin(maxAttempts int, lockoutDuration time.Duration)
- func (u *User) RecordSuccessfulLogin()
- func (u *User) SetEmailVerificationToken(token string, expiresAt time.Time)
- func (u *User) SetPasswordHash(hash string) error
- func (u *User) SetPasswordResetToken(token string, expiresAt time.Time)
- func (u *User) Status() Status
- func (u *User) Suspend() error
- func (u *User) SyncFromKeycloak(email, name string)
- func (u *User) Unlock()
- func (u *User) UpdateEmail(email string) error
- func (u *User) UpdateLastLogin()
- func (u *User) UpdatePreferences(prefs Preferences)
- func (u *User) UpdateProfile(name, phone, avatarURL string)
- func (u *User) UpdatedAt() time.Time
- func (u *User) VerifyEmail()
Constants ¶
This section is empty.
Variables ¶
var ( ErrUserNotFound = fmt.Errorf("user %w", shared.ErrNotFound) ErrUserAlreadyExists = fmt.Errorf("user %w", shared.ErrAlreadyExists) ErrUserSuspended = errors.New("user is suspended") ErrUserInactive = errors.New("user is inactive") ErrInvalidEmail = fmt.Errorf("%w: invalid email", shared.ErrValidation) // Authentication errors ErrInvalidCredentials = errors.New("invalid email or password") ErrAccountLocked = errors.New("account is locked due to too many failed attempts") ErrEmailNotVerified = errors.New("email address not verified") ErrPasswordTooWeak = errors.New("password does not meet requirements") ErrInvalidVerificationToken = errors.New("invalid or expired verification token") ErrInvalidPasswordResetToken = errors.New("invalid or expired password reset token") ErrCannotChangeOIDCPassword = errors.New("cannot change password for OIDC users") )
Domain errors for user operations.
Functions ¶
func AlreadyExistsError ¶
AlreadyExistsError creates an already exists error for a specific email.
func NotFoundByEmailError ¶
NotFoundByEmailError creates a not found error for a specific email.
func NotFoundByKeycloakIDError ¶
NotFoundByKeycloakIDError creates a not found error for a specific Keycloak ID.
func NotFoundError ¶
NotFoundError creates a not found error for a specific user.
Types ¶
type AuthProvider ¶
type AuthProvider string
AuthProvider represents the authentication provider for a user.
const ( // AuthProviderLocal indicates local email/password authentication. AuthProviderLocal AuthProvider = "local" // AuthProviderOIDC indicates external OIDC authentication (Keycloak). AuthProviderOIDC AuthProvider = "oidc" // AuthProviderGoogle indicates Google OAuth authentication. AuthProviderGoogle AuthProvider = "google" // AuthProviderGitHub indicates GitHub OAuth authentication. AuthProviderGitHub AuthProvider = "github" // AuthProviderMicrosoft indicates Microsoft/EntraID OAuth authentication. AuthProviderMicrosoft AuthProvider = "microsoft" )
func (AuthProvider) IsOAuth ¶
func (p AuthProvider) IsOAuth() bool
IsOAuth returns true if the auth provider is an OAuth provider.
func (AuthProvider) IsValid ¶
func (p AuthProvider) IsValid() bool
IsValid checks if the auth provider is valid.
func (AuthProvider) String ¶
func (p AuthProvider) String() string
String returns the string representation of the auth provider.
type Filter ¶
Filter represents criteria for filtering users.
func (Filter) WithStatus ¶
WithStatus sets a single status filter.
func (Filter) WithStatuses ¶
WithStatuses sets multiple status filters.
type Preferences ¶
type Preferences struct {
Theme string `json:"theme,omitempty"` // "light", "dark", "system"
Language string `json:"language,omitempty"` // "en", "vi"
Notifications bool `json:"notifications,omitempty"` // Enable notifications
}
Preferences represents user preferences stored as JSONB.
type Repository ¶
type Repository interface {
// CRUD operations
Create(ctx context.Context, user *User) error
GetByID(ctx context.Context, id shared.ID) (*User, error)
GetByKeycloakID(ctx context.Context, keycloakID string) (*User, error)
GetByEmail(ctx context.Context, email string) (*User, error)
Update(ctx context.Context, user *User) error
Delete(ctx context.Context, id shared.ID) error
// Existence checks
ExistsByEmail(ctx context.Context, email string) (bool, error)
ExistsByKeycloakID(ctx context.Context, keycloakID string) (bool, error)
// Upsert for Keycloak sync - creates or updates user, returns the user
UpsertFromKeycloak(ctx context.Context, keycloakID, email, name string) (*User, error)
// Batch operations
GetByIDs(ctx context.Context, ids []shared.ID) ([]*User, error)
// Count
Count(ctx context.Context, filter Filter) (int64, error)
// Local auth operations
// GetByEmailForAuth retrieves a local user by email for authentication.
GetByEmailForAuth(ctx context.Context, email string) (*User, error)
// GetByEmailVerificationToken retrieves a user by email verification token.
GetByEmailVerificationToken(ctx context.Context, token string) (*User, error)
// GetByPasswordResetToken retrieves a user by password reset token.
GetByPasswordResetToken(ctx context.Context, token string) (*User, error)
}
Repository defines the interface for user persistence.
type User ¶
type User struct {
// contains filtered or unexported fields
}
User represents a user entity in the domain.
func New ¶
New creates a new User without Keycloak (for future standalone auth). Deprecated: Use NewLocalUser for local authentication.
func NewFromKeycloak ¶
NewFromKeycloak creates a new User from Keycloak claims.
func NewLocalUser ¶
NewLocalUser creates a new local user with email/password authentication.
func NewLocalUserWithID ¶
NewLocalUserWithID creates a new local user with a specific ID (for syncing from JWT tokens). This is used when the user ID already exists in the JWT but not in the database.
func NewOAuthUser ¶
func NewOAuthUser(email, name, avatarURL string, provider AuthProvider) (*User, error)
NewOAuthUser creates a new user from OAuth provider (Google, GitHub, Microsoft).
func Reconstitute ¶
func Reconstitute( id shared.ID, keycloakID *string, email, name, avatarURL, phone string, status Status, preferences Preferences, lastLoginAt *time.Time, createdAt, updatedAt time.Time, authProvider AuthProvider, passwordHash *string, emailVerified bool, emailVerificationToken *string, emailVerificationExpiresAt *time.Time, passwordResetToken *string, passwordResetExpiresAt *time.Time, failedLoginAttempts int, lockedUntil *time.Time, ) *User
Reconstitute recreates a User from persistence.
func (*User) AuthProvider ¶
func (u *User) AuthProvider() AuthProvider
AuthProvider returns the authentication provider.
func (*User) ClearPasswordResetToken ¶
func (u *User) ClearPasswordResetToken()
ClearPasswordResetToken clears the password reset token.
func (*User) Deactivate ¶
Deactivate deactivates the user account.
func (*User) EmailVerificationExpiresAt ¶
EmailVerificationExpiresAt returns when the verification token expires.
func (*User) EmailVerificationToken ¶
EmailVerificationToken returns the email verification token.
func (*User) EmailVerified ¶
EmailVerified returns whether the email is verified.
func (*User) FailedLoginAttempts ¶
FailedLoginAttempts returns the number of failed login attempts.
func (*User) IsEmailVerificationTokenValid ¶
IsEmailVerificationTokenValid returns true if the verification token is valid.
func (*User) IsLocalUser ¶
IsLocalUser returns true if this is a local auth user.
func (*User) IsOIDCUser ¶
IsOIDCUser returns true if this is an OIDC auth user.
func (*User) IsPasswordResetTokenValid ¶
IsPasswordResetTokenValid returns true if the reset token is valid.
func (*User) IsSuspended ¶
IsSuspended returns true if the user is suspended.
func (*User) KeycloakID ¶
KeycloakID returns the Keycloak user ID (may be nil).
func (*User) LastLoginAt ¶
LastLoginAt returns the last login timestamp.
func (*User) LockedUntil ¶
LockedUntil returns when the account lockout expires.
func (*User) PasswordHash ¶
PasswordHash returns the password hash (nil for OIDC users).
func (*User) PasswordResetExpiresAt ¶
PasswordResetExpiresAt returns when the password reset token expires.
func (*User) PasswordResetToken ¶
PasswordResetToken returns the password reset token.
func (*User) Preferences ¶
func (u *User) Preferences() Preferences
Preferences returns the user preferences.
func (*User) RecordFailedLogin ¶
RecordFailedLogin increments the failed login counter.
func (*User) RecordSuccessfulLogin ¶
func (u *User) RecordSuccessfulLogin()
RecordSuccessfulLogin clears failed login attempts and updates last login.
func (*User) SetEmailVerificationToken ¶
SetEmailVerificationToken sets a new email verification token.
func (*User) SetPasswordHash ¶
SetPasswordHash sets the password hash for local auth users.
func (*User) SetPasswordResetToken ¶
SetPasswordResetToken sets a new password reset token.
func (*User) SyncFromKeycloak ¶
SyncFromKeycloak updates user info from Keycloak claims.
func (*User) UpdateEmail ¶
UpdateEmail updates the user email.
func (*User) UpdateLastLogin ¶
func (u *User) UpdateLastLogin()
UpdateLastLogin updates the last login timestamp to now.
func (*User) UpdatePreferences ¶
func (u *User) UpdatePreferences(prefs Preferences)
UpdatePreferences updates the user preferences.
func (*User) UpdateProfile ¶
UpdateProfile updates the user profile.