auth

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUserNotFound      = errors.New("user not found")
	ErrInvalidPassword   = errors.New("invalid password")
	ErrUserAlreadyExists = errors.New("user already exists")
	ErrUserInactive      = errors.New("user is inactive")
	ErrTenantRequired    = errors.New("tenant selection required")
	ErrTenantNotMember   = errors.New("user is not a member of tenant")
)

Functions

func JWTMiddleware

func JWTMiddleware(jwtService *jwt.JWTService) func(http.Handler) http.Handler

JWTMiddleware verifies the JWT token and injects Identity into context

func SetupAuthRoutes

func SetupAuthRoutes(r chi.Router, authHandler *AuthHandler, jwtMiddleware func(http.Handler) http.Handler)

SetupAuthRoutes registers auth routes

Types

type AuthHandler

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

func NewAuthHandler

func NewAuthHandler(
	authService authService,
	logger logging.Logger,
	captchaService frameCaptcha.Service,
	captchaEnabled bool,
) *AuthHandler

func (*AuthHandler) ChangePassword

func (h *AuthHandler) ChangePassword(w http.ResponseWriter, r *http.Request)

ChangePassword handles password change @Summary Change Password @Tags Auth @Accept json @Produce json @Param request body ChangePasswordRequest true "Password change info" @Success 200 {object} map[string]string @Router /auth/change-password [post]

func (*AuthHandler) Login

func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request)

Login handles user login @Summary Login @Tags Auth @Accept json @Produce json @Param request body LoginRequest true "Login credentials" @Success 200 {object} LoginResponse @Router /auth/login [post]

func (*AuthHandler) Logout

func (h *AuthHandler) Logout(w http.ResponseWriter, r *http.Request)

Logout handles user logout @Summary Logout @Tags Auth @Accept json @Produce json @Success 200 {object} map[string]string @Router /auth/logout [post]

func (*AuthHandler) Refresh

func (h *AuthHandler) Refresh(w http.ResponseWriter, r *http.Request)

Refresh handles token refresh @Summary Refresh Access Token @Tags Auth @Accept json @Produce json @Param request body RefreshRequest true "Refresh Token" @Success 200 {object} map[string]string @Router /auth/refresh [post]

func (*AuthHandler) Register

func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request)

Register handles user registration @Summary Register a new user @Tags Auth @Accept json @Produce json @Param request body RegisterRequest true "Registration info" @Success 200 {object} UserDTO @Router /auth/register [post]

type AuthModule

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

func NewAuthModule

func NewAuthModule(logger logging.Logger, deps *core.Dependencies) *AuthModule

func (*AuthModule) GetJWTService

func (m *AuthModule) GetJWTService() *jwt.JWTService

GetJWTService exposes the JWT service for middleware creation in main app

func (*AuthModule) Name

func (m *AuthModule) Name() string

func (*AuthModule) RegisterPrivateRoutes

func (m *AuthModule) RegisterPrivateRoutes(r chi.Router)

RegisterPrivateRoutes registers protected auth endpoints (logout, change password)

func (*AuthModule) RegisterPublicRoutes

func (m *AuthModule) RegisterPublicRoutes(r chi.Router)

RegisterPublicRoutes registers public auth endpoints (login, register)

type AuthService

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

func NewAuthService

func NewAuthService(client *ent.Client, logger logging.Logger, jwtService *jwt.JWTService, cfg *config.Config) *AuthService

func (*AuthService) ChangePassword

func (s *AuthService) ChangePassword(ctx context.Context, userID uuid.UUID, oldPassword, newPassword string) error

ChangePassword changes user password

func (*AuthService) GetConfig

func (s *AuthService) GetConfig() *config.SecurityConfig

GetConfig 获取配置

func (*AuthService) Login

func (s *AuthService) Login(ctx context.Context, username, password, tenantID string) (*LoginResponse, error)

Login authenticates a user and returns tokens

func (*AuthService) Logout

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

Logout invalidates the access token

func (*AuthService) RefreshToken

func (s *AuthService) RefreshToken(ctx context.Context, refreshToken string) (string, error)

RefreshToken refreshes the access token

func (*AuthService) Register

func (s *AuthService) Register(ctx context.Context, username, email, password, nickname string) (*UserDTO, error)

Register creates a new user

type ChangePasswordRequest

type ChangePasswordRequest struct {
	OldPassword string `json:"oldPassword"`
	NewPassword string `json:"newPassword"`
}

ChangePasswordRequest represents password change request

type ClientType

type ClientType string

ClientType 客户端类型

const (
	ClientTypeWeb     ClientType = "web"     // Web浏览器
	ClientTypeMobile  ClientType = "mobile"  // 移动端应用
	ClientTypeUnknown ClientType = "unknown" // 未知类型
)

func DetectClientType

func DetectClientType(r *http.Request) ClientType

DetectClientType 检测客户端类型 优先级:自定义请求头 > User-Agent检测 > 默认策略

type LoginRequest

type LoginRequest struct {
	Username      string `json:"username"`
	Password      string `json:"password"`
	RememberMe    bool   `json:"rememberMe"`    // 是否长期记住(控制Cookie持久化)
	CaptchaID     string `json:"captchaId"`     // 验证码 ID
	CaptchaAnswer string `json:"captchaAnswer"` // 验证码答案
}

LoginRequest represents login data

type LoginResponse

type LoginResponse struct {
	AccessToken  string   `json:"accessToken"`
	RefreshToken string   `json:"refreshToken,omitempty"` // Web端不返回(使用Cookie)
	ExpiresIn    int      `json:"expiresIn"`              // seconds
	User         *UserDTO `json:"user"`
}

type RefreshRequest

type RefreshRequest struct {
	RefreshToken string `json:"refreshToken"`
}

RefreshRequest represents token refresh request

type RegisterRequest

type RegisterRequest struct {
	Username string `json:"username"`
	Password string `json:"password"`
	Email    string `json:"email"`
	Nickname string `json:"nickname"`
}

RegisterRequest represents registration data

type UserDTO

type UserDTO struct {
	ID       uuid.UUID `json:"id"`
	Username string    `json:"username"`
	Nickname string    `json:"nickname"`
	Email    string    `json:"email"`
	Avatar   string    `json:"avatar,omitempty"`
	Roles    []string  `json:"roles"`
	TenantID string    `json:"tenantId,omitempty"`
}

Jump to

Keyboard shortcuts

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