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.

68 lines
1.5 KiB
Go

package auth
import (
"cls/internal/application/crypto"
"cls/internal/domain/auth"
"errors"
"cls/pkg/logger"
)
type AuthService struct {
auth auth.AuthRepository
phoneEncryption *crypto.PhoneEncryptionService
passwordHash *crypto.PasswordHashService
log logger.Logger
jwtSecretKey string
}
func NewAuthService(auth auth.AuthRepository,
phoneEncryption *crypto.PhoneEncryptionService,
passwordHash *crypto.PasswordHashService,
log logger.New) *AuthService {
return &AuthService{auth, phoneEncryption, passwordHash, log("cls:service:auth"), ""}
}
func (a *AuthService) SetJWTSecretKey(secretKey string) {
a.jwtSecretKey = secretKey
}
func (a *AuthService) GetJWTSecretKey() string {
return a.jwtSecretKey
}
func (a *AuthService) LoginByCaptcha(phone, openid string) (string, error) {
if openid == "" {
return "", errors.New("openid为空")
}
ePhone, err := a.phoneEncryption.Encrypt(phone)
if err != nil {
a.log.Error(err.Error())
return "", err
}
token, err := a.auth.LoginByCaptcha(ePhone, openid)
if err != nil {
a.log.Error(err)
return "", err
}
return token, nil
}
func (a *AuthService) LoginByPassword(phone, password string) (string, error) {
ePhone, err := a.phoneEncryption.Encrypt(phone)
if err != nil {
a.log.Error(err.Error())
return "", err
}
ePassword, err := a.passwordHash.Hash(password)
if err != nil {
a.log.Error(err.Error())
return "", err
}
token, err := a.auth.LoginByPassword(ePhone, ePassword)
if err != nil {
return "", err
}
return token, nil
}