auth

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthService

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

AuthService handles authentication operations

func NewAuthService

func NewAuthService(db *gorm.DB, jwtManager *JWTManager) *AuthService

NewAuthService creates a new authentication service

func (*AuthService) ForgotPassword

func (s *AuthService) ForgotPassword(email string) error

ForgotPassword initiates password reset

func (*AuthService) Login

func (s *AuthService) Login(req *LoginRequest) (*LoginResponse, error)

Login authenticates a user

func (*AuthService) Logout

func (s *AuthService) Logout(token string) error

Logout logs out a user

func (*AuthService) RefreshToken

func (s *AuthService) RefreshToken(refreshToken string) (*TokenPair, error)

RefreshToken refreshes an access token

func (*AuthService) Register

func (s *AuthService) Register(req *RegisterRequest) error

Register registers a new user

func (*AuthService) ResetPassword

func (s *AuthService) ResetPassword(token, newPassword string) error

ResetPassword resets a user's password

type Claims

type Claims struct {
	UserID    string   `json:"user_id"`
	Email     string   `json:"email"`
	Roles     []string `json:"roles"`
	SessionID string   `json:"session_id"`
	jwt.RegisteredClaims
}

Claims represents JWT claims

type JWTManager

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

JWTManager handles JWT token operations

func NewJWTManager

func NewJWTManager(secretKey string, tokenDuration time.Duration, issuer string) *JWTManager

NewJWTManager creates a new JWT manager

func (*JWTManager) BlacklistToken

func (j *JWTManager) BlacklistToken(tokenString string, expiresAt time.Time) error

BlacklistToken adds a token to the blacklist (in a real implementation, you'd use Redis)

func (*JWTManager) ExtractTokenFromHeader

func (j *JWTManager) ExtractTokenFromHeader(authHeader string) (string, error)

ExtractTokenFromHeader extracts token from Authorization header

func (*JWTManager) GenerateTokenPair

func (j *JWTManager) GenerateTokenPair(userID, email string, roles []string, sessionID string) (*TokenPair, error)

GenerateTokenPair generates both access and refresh tokens

func (*JWTManager) GetTokenExpiration

func (j *JWTManager) GetTokenExpiration() time.Duration

GetTokenExpiration returns the token expiration time

func (*JWTManager) IsTokenBlacklisted

func (j *JWTManager) IsTokenBlacklisted(tokenString string) bool

IsTokenBlacklisted checks if a token is blacklisted

func (*JWTManager) RefreshToken

func (j *JWTManager) RefreshToken(refreshTokenString string) (*TokenPair, error)

RefreshToken generates a new access token from a refresh token

func (*JWTManager) ValidateToken

func (j *JWTManager) ValidateToken(tokenString string) (*Claims, error)

ValidateToken validates and parses a JWT token

type LoginRequest

type LoginRequest struct {
	Email    string `json:"email" validate:"required,email"`
	Password string `json:"password" validate:"required"`
	OTPCode  string `json:"otp_code,omitempty"`
	Remember bool   `json:"remember,omitempty"`
}

LoginRequest represents a user login request

type LoginResponse

type LoginResponse struct {
	User        interface{} `json:"user"`
	Tokens      *TokenPair  `json:"tokens"`
	Requires2FA bool        `json:"requires_2fa"`
	RequiresOTP bool        `json:"requires_otp"`
}

LoginResponse represents a login response

type OTPCode

type OTPCode struct {
	Code      string    `json:"code"`
	Phone     string    `json:"phone"`
	ExpiresAt time.Time `json:"expires_at"`
	CreatedAt time.Time `json:"created_at"`
}

OTPCode represents an OTP code

type OTPManager

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

OTPManager handles OTP generation and validation

func DefaultOTPManager

func DefaultOTPManager() *OTPManager

DefaultOTPManager returns a default OTP manager

func NewOTPManager

func NewOTPManager(codeLength, expiryMinutes int) *OTPManager

NewOTPManager creates a new OTP manager

func (*OTPManager) FormatPhoneNumber

func (o *OTPManager) FormatPhoneNumber(phone string) string

FormatPhoneNumber formats a phone number for OTP

func (*OTPManager) GenerateOTP

func (o *OTPManager) GenerateOTP(phone string) (*OTPCode, error)

GenerateOTP generates a new OTP code

func (*OTPManager) GetExpiryDuration

func (o *OTPManager) GetExpiryDuration() time.Duration

GetExpiryDuration returns the OTP expiry duration

func (*OTPManager) IsExpired

func (o *OTPManager) IsExpired(otp *OTPCode) bool

IsExpired checks if an OTP code is expired

func (*OTPManager) SendOTP

func (o *OTPManager) SendOTP(phone, code string) error

SendOTP sends an OTP code (placeholder - implement with SMS provider)

func (*OTPManager) ValidateOTP

func (o *OTPManager) ValidateOTP(code, phone string, storedCode *OTPCode) error

ValidateOTP validates an OTP code

type PasswordHash

type PasswordHash struct {
	Algorithm   string `json:"algorithm"`
	Memory      uint32 `json:"memory"`
	Iterations  uint32 `json:"iterations"`
	Parallelism uint8  `json:"parallelism"`
	Salt        string `json:"salt"`
	Hash        string `json:"hash"`
}

PasswordHash represents a hashed password

type PasswordManager

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

PasswordManager handles password hashing and verification

func DefaultPasswordManager

func DefaultPasswordManager() *PasswordManager

DefaultPasswordManager returns a default password manager

func NewPasswordManager

func NewPasswordManager() *PasswordManager

NewPasswordManager creates a new password manager

func (*PasswordManager) GenerateRandomPassword

func (p *PasswordManager) GenerateRandomPassword(length int) (string, error)

GenerateRandomPassword generates a random password

func (*PasswordManager) HashPassword

func (p *PasswordManager) HashPassword(password string) (string, error)

HashPassword hashes a password using Argon2id

func (*PasswordManager) ValidatePasswordStrength

func (p *PasswordManager) ValidatePasswordStrength(password string) error

ValidatePasswordStrength validates password strength

func (*PasswordManager) VerifyPassword

func (p *PasswordManager) VerifyPassword(password, hashedPassword string) (bool, error)

VerifyPassword verifies a password against a hash

type RegisterRequest

type RegisterRequest struct {
	Email     string `json:"email" validate:"required,email"`
	Password  string `json:"password" validate:"required,password"`
	FirstName string `json:"first_name" validate:"required,min=2,max=50"`
	LastName  string `json:"last_name" validate:"required,min=2,max=50"`
	Phone     string `json:"phone" validate:"omitempty,phone"`
}

RegisterRequest represents a user registration request

type TokenPair

type TokenPair struct {
	AccessToken  string    `json:"access_token"`
	RefreshToken string    `json:"refresh_token"`
	ExpiresAt    time.Time `json:"expires_at"`
	TokenType    string    `json:"token_type"`
}

TokenPair represents access and refresh tokens

type TwoFactorManager

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

TwoFactorManager handles 2FA operations

func DefaultTwoFactorManager

func DefaultTwoFactorManager(issuer string) *TwoFactorManager

DefaultTwoFactorManager returns a default 2FA manager

func NewTwoFactorManager

func NewTwoFactorManager(issuer string) *TwoFactorManager

NewTwoFactorManager creates a new 2FA manager

func (*TwoFactorManager) GenerateSecret

func (t *TwoFactorManager) GenerateSecret(userEmail string) (*TwoFactorSecret, error)

GenerateSecret generates a new 2FA secret for a user

func (*TwoFactorManager) GenerateTOTP

func (t *TwoFactorManager) GenerateTOTP(secret string) (string, error)

GenerateTOTP generates a TOTP code for testing

func (*TwoFactorManager) GetQRCodeData

func (t *TwoFactorManager) GetQRCodeData(secret, userEmail string) (string, error)

GetQRCodeData returns the QR code data for display

func (*TwoFactorManager) IsValidSecret

func (t *TwoFactorManager) IsValidSecret(secret string) bool

IsValidSecret checks if a secret is valid

func (*TwoFactorManager) ValidateBackupCode

func (t *TwoFactorManager) ValidateBackupCode(code string, usedCodes []string) bool

ValidateBackupCode validates a backup code

func (*TwoFactorManager) ValidateTOTP

func (t *TwoFactorManager) ValidateTOTP(secret, code string) bool

ValidateTOTP validates a TOTP code

type TwoFactorSecret

type TwoFactorSecret struct {
	Secret      string   `json:"secret"`
	QRCodeURL   string   `json:"qr_code_url"`
	BackupCodes []string `json:"backup_codes,omitempty"`
}

TwoFactorSecret represents a 2FA secret

Jump to

Keyboard shortcuts

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