Documentation
¶
Overview ¶
Package selfservice implements self-service authentication: password recovery, customer registration, email verification, and CAPTCHA.
Index ¶
- Constants
- func GenerateToken() (string, error)
- func HandleAdminApproveRegistration(c *gin.Context)
- func HandleAdminListPendingRegistrations(c *gin.Context)
- func HandleAdminRejectRegistration(c *gin.Context)
- func HandleCustomerRegister(captchaCfg *CAPTCHAConfig) gin.HandlerFunc
- func HandleForgotPassword(c *gin.Context)
- func HandleForgotPasswordSubmit(captchaCfg *CAPTCHAConfig) gin.HandlerFunc
- func HandleResetPassword(c *gin.Context)
- func HandleVerifyEmail(c *gin.Context)
- func VerifyCAPTCHA(cfg *CAPTCHAConfig, responseToken string) error
- type AuthToken
- type CAPTCHAConfig
- type RegistrationRequest
- type Repository
- func (r *Repository) ApproveRegistration(id int64, approvedBy int) error
- func (r *Repository) CleanupExpired() (int64, error)
- func (r *Repository) ConsumeToken(token string) error
- func (r *Repository) CreateRegistration(req *RegistrationRequest) (int64, error)
- func (r *Repository) CreateToken(t *AuthToken) error
- func (r *Repository) GetRegistration(id int64) (*RegistrationRequest, error)
- func (r *Repository) GetToken(token string) (*AuthToken, error)
- func (r *Repository) ListPendingRegistrations() ([]RegistrationRequest, error)
- func (r *Repository) RejectRegistration(id int64, reason string, rejectedBy int) error
Constants ¶
const ( TokenPasswordReset = "password_reset" TokenEmailVerify = "email_verify" TokenRegistrationApprove = "registration_approve" )
Token types.
const ( UserAgent = "agent" UserCustomer = "customer" )
User types.
const ( StatusPending = "pending" StatusApproved = "approved" StatusRejected = "rejected" )
Registration statuses.
const ( CAPTCHANone = "" CAPTCHARecaptcha = "recaptcha_v3" CAPTCHAHCaptcha = "hcaptcha" )
CAPTCHA providers.
const DefaultTokenExpiry = 1 * time.Hour
DefaultTokenExpiry is the expiry duration for password reset tokens.
const DefaultVerifyExpiry = 24 * time.Hour
DefaultVerifyExpiry is the expiry duration for email verification tokens.
Variables ¶
This section is empty.
Functions ¶
func GenerateToken ¶
GenerateToken creates a cryptographically random 32-byte hex token.
func HandleAdminApproveRegistration ¶
HandleAdminApproveRegistration approves a pending registration.
func HandleAdminListPendingRegistrations ¶
HandleAdminListPendingRegistrations lists pending registrations.
func HandleAdminRejectRegistration ¶
HandleAdminRejectRegistration rejects a pending registration.
func HandleCustomerRegister ¶
func HandleCustomerRegister(captchaCfg *CAPTCHAConfig) gin.HandlerFunc
HandleCustomerRegister processes customer self-registration.
func HandleForgotPassword ¶
HandleForgotPassword renders the forgot password form. Title is set via i18n in the template using t("self_service.forgot_password.title").
func HandleForgotPasswordSubmit ¶
func HandleForgotPasswordSubmit(captchaCfg *CAPTCHAConfig) gin.HandlerFunc
HandleForgotPasswordSubmit processes the forgot password form. Generates a reset token and sends an email with the reset link.
func HandleResetPassword ¶
HandleResetPassword processes the password reset form.
func HandleVerifyEmail ¶
HandleVerifyEmail processes the email verification link.
func VerifyCAPTCHA ¶
func VerifyCAPTCHA(cfg *CAPTCHAConfig, responseToken string) error
VerifyCAPTCHA validates a CAPTCHA response token against the provider. Returns nil if CAPTCHA is disabled or verification passes.
Types ¶
type AuthToken ¶
type AuthToken struct {
ID int64 `json:"id" db:"id"`
Token string `json:"token" db:"token"`
TokenType string `json:"token_type" db:"token_type"`
UserType string `json:"user_type" db:"user_type"`
UserID *int `json:"user_id,omitempty" db:"user_id"`
CustomerLogin *string `json:"customer_login,omitempty" db:"customer_login"`
Email string `json:"email" db:"email"`
ExpiresAt time.Time `json:"expires_at" db:"expires_at"`
UsedAt *time.Time `json:"used_at,omitempty" db:"used_at"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
}
AuthToken represents a row in gk_auth_token.
type CAPTCHAConfig ¶
type CAPTCHAConfig struct {
Provider string `json:"provider"` // recaptcha_v3, hcaptcha, or empty (disabled)
SiteKey string `json:"site_key"` // public key for frontend
SecretKey string `json:"secret_key"` // server-side verification key
Threshold float64 `json:"threshold"` // minimum score for reCAPTCHA v3 (default: 0.5)
}
CAPTCHAConfig holds CAPTCHA provider configuration.
type RegistrationRequest ¶
type RegistrationRequest struct {
ID int64 `json:"id" db:"id"`
Email string `json:"email" db:"email"`
FirstName string `json:"first_name" db:"first_name"`
LastName string `json:"last_name" db:"last_name"`
CustomerID *string `json:"customer_id,omitempty" db:"customer_id"`
Status string `json:"status" db:"status"`
ApprovalToken *string `json:"approval_token,omitempty" db:"approval_token"`
ApprovedBy *int `json:"approved_by,omitempty" db:"approved_by"`
ApprovedAt *time.Time `json:"approved_at,omitempty" db:"approved_at"`
RejectedReason *string `json:"rejected_reason,omitempty" db:"rejected_reason"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
}
RegistrationRequest represents a row in gk_registration_request.
type Repository ¶
type Repository struct {
// contains filtered or unexported fields
}
Repository provides CRUD for auth tokens and registration requests.
func NewRepository ¶
func NewRepository() (*Repository, error)
NewRepository creates a repository using the global DB.
func NewRepositoryWithDB ¶
func NewRepositoryWithDB(db *sql.DB) *Repository
NewRepositoryWithDB creates a repository with an explicit DB.
func (*Repository) ApproveRegistration ¶
func (r *Repository) ApproveRegistration(id int64, approvedBy int) error
ApproveRegistration approves a registration request.
func (*Repository) CleanupExpired ¶
func (r *Repository) CleanupExpired() (int64, error)
CleanupExpired removes expired and used tokens older than 24 hours.
func (*Repository) ConsumeToken ¶
func (r *Repository) ConsumeToken(token string) error
ConsumeToken marks a token as used.
func (*Repository) CreateRegistration ¶
func (r *Repository) CreateRegistration(req *RegistrationRequest) (int64, error)
CreateRegistration creates a new registration request.
func (*Repository) CreateToken ¶
func (r *Repository) CreateToken(t *AuthToken) error
CreateToken creates a new auth token.
func (*Repository) GetRegistration ¶
func (r *Repository) GetRegistration(id int64) (*RegistrationRequest, error)
GetRegistration retrieves a registration request by ID.
func (*Repository) GetToken ¶
func (r *Repository) GetToken(token string) (*AuthToken, error)
GetToken retrieves and validates a token. Returns nil if not found.
func (*Repository) ListPendingRegistrations ¶
func (r *Repository) ListPendingRegistrations() ([]RegistrationRequest, error)
ListPendingRegistrations lists registration requests with pending status.
func (*Repository) RejectRegistration ¶
func (r *Repository) RejectRegistration(id int64, reason string, rejectedBy int) error
RejectRegistration rejects a registration request.