This commit is contained in:
Sakurasan
2025-04-17 02:17:19 +08:00
parent d4cbc27a77
commit b0d68ba338
7 changed files with 55 additions and 35 deletions

View File

@@ -2,11 +2,13 @@ package controller
import (
"opencatd-open/internal/service"
"opencatd-open/pkg/config"
"gorm.io/gorm"
)
type Api struct {
cfg *config.Config
db *gorm.DB
userService *service.UserServiceImpl
tokenService *service.TokenServiceImpl
@@ -15,8 +17,9 @@ type Api struct {
usageService *service.UsageService
}
func NewApi(db *gorm.DB, userService *service.UserServiceImpl, tokenService *service.TokenServiceImpl, keyService *service.ApiKeyServiceImpl, webAuthService *service.WebAuthnService, usageService *service.UsageService) *Api {
func NewApi(cfg *config.Config, db *gorm.DB, userService *service.UserServiceImpl, tokenService *service.TokenServiceImpl, keyService *service.ApiKeyServiceImpl, webAuthService *service.WebAuthnService, usageService *service.UsageService) *Api {
return &Api{
cfg: cfg,
db: db,
userService: userService,
tokenService: tokenService,

View File

@@ -9,6 +9,7 @@ import (
"opencatd-open/internal/dto"
"opencatd-open/internal/model"
"opencatd-open/internal/utils"
"opencatd-open/pkg/config"
"strings"
"time"
@@ -17,12 +18,14 @@ import (
)
type UserServiceImpl struct {
cfg *config.Config
db *gorm.DB
userRepo dao.UserRepository
}
func NewUserService(db *gorm.DB, userRepo dao.UserRepository) *UserServiceImpl {
func NewUserService(cfg *config.Config, db *gorm.DB, userRepo dao.UserRepository) *UserServiceImpl {
return &UserServiceImpl{
cfg: cfg,
db: db,
userRepo: userRepo,
}
@@ -40,11 +43,19 @@ func (s *UserServiceImpl) Register(ctx context.Context, req *model.User) error {
_user.Role = utils.ToPtr(consts.RoleRoot)
_user.Active = utils.ToPtr(true)
_user.UnlimitedQuota = utils.ToPtr(true)
} else {
if !s.cfg.AllowRegister {
return fmt.Errorf("register is not allowed")
}
}
_user.Password, err = utils.HashPassword(req.Password)
if err != nil {
return err
}
_user.Active = &s.cfg.DefaultActive
_user.UnlimitedQuota = &s.cfg.UnlimitedQuota
_user.Username = req.Username
_user.Email = req.Email
_user.Tokens = []model.Token{

View File

@@ -62,6 +62,7 @@ func (u *WebAuthnUser) WebAuthnCredentialDescriptors() (descriptors []protocol.C
// WebAuthnService 提供WebAuthn相关功能
type WebAuthnService struct {
cfg *config.Config
DB *gorm.DB
WebAuthn *webauthn.WebAuthn
// Sessions map[string]webauthn.SessionData // 用于存储注册和认证过程中的会话数据
@@ -69,12 +70,12 @@ type WebAuthnService struct {
}
// NewWebAuthnService 创建新的WebAuthn服务
func NewWebAuthnService(db *gorm.DB, cfg *config.Config) (*WebAuthnService, error) {
func NewWebAuthnService(cfg *config.Config, db *gorm.DB) (*WebAuthnService, error) {
// 创建WebAuthn配置
wconfig := &webauthn.Config{
RPDisplayName: config.Cfg.AppName, // 依赖方(Relying Party)显示名称
RPID: config.Cfg.Domain, // 依赖方ID(通常为域名)
RPOrigins: []string{config.Cfg.AppURL}, // 依赖方源(URL)
RPDisplayName: cfg.AppName, // 依赖方(Relying Party)显示名称
RPID: cfg.RPID, // 依赖方ID(通常为域名)
RPOrigins: cfg.RPOrigins, // 依赖方源(URL)
AuthenticatorSelection: protocol.AuthenticatorSelection{
RequireResidentKey: protocol.ResidentKeyRequired(), // 要求认证器存储用户 ID (resident key)
ResidentKey: protocol.ResidentKeyRequirementRequired, // 使用 Discoverable 模式
@@ -90,6 +91,7 @@ func NewWebAuthnService(db *gorm.DB, cfg *config.Config) (*WebAuthnService, erro
}
return &WebAuthnService{
cfg: cfg,
DB: db,
WebAuthn: wa,
// Sessions: make(map[string]webauthn.SessionData),