You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.4 KiB
Go

package auth
import (
"cls-server/internal/application/crypto"
"cls-server/internal/domain/auth"
middleware "cls-server/internal/infrastructure/middleware/auth"
"cls-server/pkg/logger"
"errors"
)
type AuthService struct {
auth auth.AuthRepository
phoneEncryption *crypto.PhoneEncryptionService
passwordHash *crypto.PasswordHashService
log logger.Logger
authMiddleware *middleware.AuthMiddleware
jwtSecretKey string
}
func NewAuthService(auth auth.AuthRepository,
phoneEncryption *crypto.PhoneEncryptionService,
passwordHash *crypto.PasswordHashService,
log logger.New,
authMiddleware *middleware.AuthMiddleware) *AuthService {
return &AuthService{auth, phoneEncryption, passwordHash, log("cls:service:auth"), authMiddleware, ""}
}
func (a *AuthService) SetJWTSecretKey(secretKey string) {
a.jwtSecretKey = secretKey
}
func (a *AuthService) GetJWTSecretKey() string {
return a.jwtSecretKey
}
func (a *AuthService) LoginByPassword(username, password string) (string, error) {
ad, err := a.auth.GetAdminByUsername(username)
if err != nil {
return "", err
}
if !a.passwordHash.Verify(password, ad.Password) {
return "", errors.New("密码错误")
}
ad.Phone, _ = a.phoneEncryption.StringPhone(ad.Phone)
token, err := a.authMiddleware.GenerateUserToken(ad)
if err != nil {
a.log.Error(err.Error())
return "", err
}
return token, nil
}