2022-09-08 18:47:15 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2022-11-21 15:20:04 +08:00
|
|
|
"fmt"
|
2022-09-08 18:47:15 +08:00
|
|
|
"strconv"
|
2022-11-16 18:27:22 +08:00
|
|
|
"time"
|
2022-09-08 18:47:15 +08:00
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/encrypt"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/jwt"
|
2022-09-08 18:47:15 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
uuid "github.com/satori/go.uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AuthService struct{}
|
|
|
|
|
|
|
|
type IAuthService interface {
|
2022-09-15 17:15:03 +08:00
|
|
|
SafetyStatus(c *gin.Context) error
|
2022-11-21 15:20:04 +08:00
|
|
|
CheckIsFirst() bool
|
|
|
|
InitUser(c *gin.Context, req dto.InitUser) error
|
2022-09-15 17:15:03 +08:00
|
|
|
VerifyCode(code string) (bool, error)
|
2022-11-16 18:27:22 +08:00
|
|
|
SafeEntrance(c *gin.Context, code string) error
|
2022-09-08 18:47:15 +08:00
|
|
|
Login(c *gin.Context, info dto.Login) (*dto.UserLoginInfo, error)
|
|
|
|
LogOut(c *gin.Context) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIAuthService() IAuthService {
|
|
|
|
return &AuthService{}
|
|
|
|
}
|
|
|
|
|
2022-11-16 18:27:22 +08:00
|
|
|
func (u *AuthService) SafeEntrance(c *gin.Context, code string) error {
|
|
|
|
codeWithMD5 := encrypt.Md5(code)
|
|
|
|
cookieValue, _ := encrypt.StringEncrypt(codeWithMD5)
|
|
|
|
c.SetCookie(codeWithMD5, cookieValue, 604800, "", "", false, false)
|
|
|
|
|
|
|
|
expiredSetting, err := settingRepo.Get(settingRepo.WithByKey("ExpirationDays"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
timeout, _ := strconv.Atoi(expiredSetting.Value)
|
2022-12-02 10:23:35 +08:00
|
|
|
if err := settingRepo.Update("ExpirationTime", time.Now().AddDate(0, 0, timeout).Format("2006-01-02 15:04:05")); err != nil {
|
2022-11-16 18:27:22 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-08 18:47:15 +08:00
|
|
|
func (u *AuthService) Login(c *gin.Context, info dto.Login) (*dto.UserLoginInfo, error) {
|
|
|
|
nameSetting, err := settingRepo.Get(settingRepo.WithByKey("UserName"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessage(constant.ErrRecordNotFound, err.Error())
|
|
|
|
}
|
|
|
|
passwrodSetting, err := settingRepo.Get(settingRepo.WithByKey("Password"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessage(constant.ErrRecordNotFound, err.Error())
|
|
|
|
}
|
|
|
|
pass, err := encrypt.StringDecrypt(passwrodSetting.Value)
|
|
|
|
if err != nil {
|
2022-12-02 18:52:43 +08:00
|
|
|
return nil, constant.ErrAuth
|
2022-09-08 18:47:15 +08:00
|
|
|
}
|
2023-03-01 14:05:55 +08:00
|
|
|
if info.Password != pass || nameSetting.Value != info.Name {
|
2022-12-02 18:52:43 +08:00
|
|
|
return nil, constant.ErrAuth
|
2022-09-08 18:47:15 +08:00
|
|
|
}
|
2022-09-15 18:43:41 +08:00
|
|
|
mfa, err := settingRepo.Get(settingRepo.WithByKey("MFAStatus"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if mfa.Value == "enable" {
|
|
|
|
mfaSecret, err := settingRepo.Get(settingRepo.WithByKey("MFASecret"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &dto.UserLoginInfo{Name: nameSetting.Value, MfaStatus: mfa.Value, MfaSecret: mfaSecret.Value}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.generateSession(c, info.Name, info.AuthMethod)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *AuthService) MFALogin(c *gin.Context, info dto.MFALogin) (*dto.UserLoginInfo, error) {
|
|
|
|
nameSetting, err := settingRepo.Get(settingRepo.WithByKey("UserName"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessage(constant.ErrRecordNotFound, err.Error())
|
|
|
|
}
|
|
|
|
passwrodSetting, err := settingRepo.Get(settingRepo.WithByKey("Password"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessage(constant.ErrRecordNotFound, err.Error())
|
|
|
|
}
|
|
|
|
pass, err := encrypt.StringDecrypt(passwrodSetting.Value)
|
|
|
|
if err != nil {
|
2022-12-02 18:52:43 +08:00
|
|
|
return nil, constant.ErrAuth
|
2022-09-15 18:43:41 +08:00
|
|
|
}
|
|
|
|
if info.Password != pass && nameSetting.Value == info.Name {
|
2022-12-02 18:52:43 +08:00
|
|
|
return nil, constant.ErrAuth
|
2022-09-15 18:43:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return u.generateSession(c, info.Name, info.AuthMethod)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *AuthService) generateSession(c *gin.Context, name, authMethod string) (*dto.UserLoginInfo, error) {
|
2022-09-08 18:47:15 +08:00
|
|
|
setting, err := settingRepo.Get(settingRepo.WithByKey("SessionTimeout"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lifeTime, err := strconv.Atoi(setting.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-09-15 18:43:41 +08:00
|
|
|
if authMethod == constant.AuthMethodJWT {
|
2022-09-08 18:47:15 +08:00
|
|
|
j := jwt.NewJWT()
|
|
|
|
claims := j.CreateClaims(jwt.BaseClaims{
|
2022-09-15 18:43:41 +08:00
|
|
|
Name: name,
|
2022-09-08 18:47:15 +08:00
|
|
|
}, lifeTime)
|
|
|
|
token, err := j.CreateToken(claims)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-15 18:43:41 +08:00
|
|
|
return &dto.UserLoginInfo{Name: name, Token: token}, nil
|
2022-09-08 18:47:15 +08:00
|
|
|
}
|
|
|
|
sID, _ := c.Cookie(constant.SessionName)
|
|
|
|
sessionUser, err := global.SESSION.Get(sID)
|
|
|
|
if err != nil {
|
|
|
|
sID = uuid.NewV4().String()
|
2022-09-19 19:42:06 +08:00
|
|
|
c.SetCookie(constant.SessionName, sID, 604800, "", "", false, false)
|
2022-09-08 18:47:15 +08:00
|
|
|
err := global.SESSION.Set(sID, sessionUser, lifeTime)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-15 18:43:41 +08:00
|
|
|
return &dto.UserLoginInfo{Name: name}, nil
|
2022-09-08 18:47:15 +08:00
|
|
|
}
|
|
|
|
if err := global.SESSION.Set(sID, sessionUser, lifeTime); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-09-15 18:43:41 +08:00
|
|
|
return &dto.UserLoginInfo{Name: name}, nil
|
2022-09-08 18:47:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *AuthService) LogOut(c *gin.Context) error {
|
|
|
|
sID, _ := c.Cookie(constant.SessionName)
|
|
|
|
if sID != "" {
|
|
|
|
c.SetCookie(constant.SessionName, sID, -1, "", "", false, false)
|
|
|
|
err := global.SESSION.Delete(sID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-15 17:15:03 +08:00
|
|
|
|
|
|
|
func (u *AuthService) VerifyCode(code string) (bool, error) {
|
|
|
|
setting, err := settingRepo.Get(settingRepo.WithByKey("SecurityEntrance"))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return setting.Value == code, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *AuthService) SafetyStatus(c *gin.Context) error {
|
|
|
|
setting, err := settingRepo.Get(settingRepo.WithByKey("SecurityEntrance"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
codeWithEcrypt, err := c.Cookie(encrypt.Md5(setting.Value))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
code, err := encrypt.StringDecrypt(codeWithEcrypt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if code != encrypt.Md5(setting.Value) {
|
|
|
|
return errors.New("code not match")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-21 15:20:04 +08:00
|
|
|
|
|
|
|
func (u *AuthService) CheckIsFirst() bool {
|
|
|
|
user, _ := settingRepo.Get(settingRepo.WithByKey("UserName"))
|
|
|
|
pass, _ := settingRepo.Get(settingRepo.WithByKey("Password"))
|
|
|
|
return len(user.Value) == 0 || len(pass.Value) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *AuthService) InitUser(c *gin.Context, req dto.InitUser) error {
|
|
|
|
user, _ := settingRepo.Get(settingRepo.WithByKey("UserName"))
|
|
|
|
pass, _ := settingRepo.Get(settingRepo.WithByKey("Password"))
|
|
|
|
if len(user.Value) == 0 || len(pass.Value) == 0 {
|
|
|
|
newPass, err := encrypt.StringEncrypt(req.Password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := settingRepo.Update("UserName", req.Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := settingRepo.Update("Password", newPass); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
expiredSetting, err := settingRepo.Get(settingRepo.WithByKey("ExpirationDays"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
timeout, _ := strconv.Atoi(expiredSetting.Value)
|
|
|
|
if timeout != 0 {
|
2022-12-02 10:23:35 +08:00
|
|
|
if err := settingRepo.Update("ExpirationTime", time.Now().AddDate(0, 0, timeout).Format("2006-01-02 15:04:05")); err != nil {
|
2022-11-21 15:20:04 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("can't init user because user %s is in system", user.Value)
|
|
|
|
}
|