|
|
@ -2,6 +2,7 @@ package service |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"crypto/hmac" |
|
|
|
"crypto/hmac" |
|
|
|
|
|
|
|
"encoding/base64" |
|
|
|
"strconv" |
|
|
|
"strconv" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto" |
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto" |
|
|
@ -19,12 +20,13 @@ import ( |
|
|
|
type AuthService struct{} |
|
|
|
type AuthService struct{} |
|
|
|
|
|
|
|
|
|
|
|
type IAuthService interface { |
|
|
|
type IAuthService interface { |
|
|
|
CheckIsSafety(code string) (string, error) |
|
|
|
|
|
|
|
GetResponsePage() (string, error) |
|
|
|
GetResponsePage() (string, error) |
|
|
|
VerifyCode(code string) (bool, error) |
|
|
|
VerifyCode(code string) (bool, error) |
|
|
|
Login(c *gin.Context, info dto.Login, entrance string) (*dto.UserLoginInfo, error) |
|
|
|
Login(c *gin.Context, info dto.Login, entrance string) (*dto.UserLoginInfo, error) |
|
|
|
LogOut(c *gin.Context) error |
|
|
|
LogOut(c *gin.Context) error |
|
|
|
MFALogin(c *gin.Context, info dto.MFALogin, entrance string) (*dto.UserLoginInfo, error) |
|
|
|
MFALogin(c *gin.Context, info dto.MFALogin, entrance string) (*dto.UserLoginInfo, error) |
|
|
|
|
|
|
|
GetSecurityEntrance() string |
|
|
|
|
|
|
|
IsLogin(c *gin.Context) bool |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func NewIAuthService() IAuthService { |
|
|
|
func NewIAuthService() IAuthService { |
|
|
@ -64,7 +66,16 @@ func (u *AuthService) Login(c *gin.Context, info dto.Login, entrance string) (*d |
|
|
|
if mfa.Value == "enable" { |
|
|
|
if mfa.Value == "enable" { |
|
|
|
return &dto.UserLoginInfo{Name: nameSetting.Value, MfaStatus: mfa.Value}, nil |
|
|
|
return &dto.UserLoginInfo{Name: nameSetting.Value, MfaStatus: mfa.Value}, nil |
|
|
|
} |
|
|
|
} |
|
|
|
return u.generateSession(c, info.Name, info.AuthMethod) |
|
|
|
|
|
|
|
|
|
|
|
loginUser, err := u.generateSession(c, info.Name, info.AuthMethod) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if entrance != "" { |
|
|
|
|
|
|
|
entranceValue := base64.StdEncoding.EncodeToString([]byte(entrance)) |
|
|
|
|
|
|
|
c.SetCookie("SecurityEntrance", entranceValue, 0, "", "", false, true) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return loginUser, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (u *AuthService) MFALogin(c *gin.Context, info dto.MFALogin, entrance string) (*dto.UserLoginInfo, error) { |
|
|
|
func (u *AuthService) MFALogin(c *gin.Context, info dto.MFALogin, entrance string) (*dto.UserLoginInfo, error) { |
|
|
@ -103,7 +114,15 @@ func (u *AuthService) MFALogin(c *gin.Context, info dto.MFALogin, entrance strin |
|
|
|
return nil, constant.ErrAuth |
|
|
|
return nil, constant.ErrAuth |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return u.generateSession(c, info.Name, info.AuthMethod) |
|
|
|
loginUser, err := u.generateSession(c, info.Name, info.AuthMethod) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if entrance != "" { |
|
|
|
|
|
|
|
entranceValue := base64.StdEncoding.EncodeToString([]byte(entrance)) |
|
|
|
|
|
|
|
c.SetCookie("SecurityEntrance", entranceValue, 0, "", "", false, true) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return loginUser, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (u *AuthService) generateSession(c *gin.Context, name, authMethod string) (*dto.UserLoginInfo, error) { |
|
|
|
func (u *AuthService) generateSession(c *gin.Context, name, authMethod string) (*dto.UserLoginInfo, error) { |
|
|
@ -173,24 +192,30 @@ func (u *AuthService) VerifyCode(code string) (bool, error) { |
|
|
|
return hmac.Equal([]byte(setting.Value), []byte(code)), nil |
|
|
|
return hmac.Equal([]byte(setting.Value), []byte(code)), nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (u *AuthService) CheckIsSafety(code string) (string, error) { |
|
|
|
func (u *AuthService) GetResponsePage() (string, error) { |
|
|
|
status, err := settingRepo.Get(settingRepo.WithByKey("SecurityEntrance")) |
|
|
|
pageCode, err := settingRepo.Get(settingRepo.WithByKey("NoAuthSetting")) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return "", err |
|
|
|
return "", err |
|
|
|
} |
|
|
|
} |
|
|
|
if len(status.Value) == 0 { |
|
|
|
return pageCode.Value, nil |
|
|
|
return "disable", nil |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (u *AuthService) GetSecurityEntrance() string { |
|
|
|
|
|
|
|
status, err := settingRepo.Get(settingRepo.WithByKey("SecurityEntrance")) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return "" |
|
|
|
} |
|
|
|
} |
|
|
|
if status.Value == code { |
|
|
|
if len(status.Value) == 0 { |
|
|
|
return "pass", nil |
|
|
|
return "" |
|
|
|
} |
|
|
|
} |
|
|
|
return "unpass", nil |
|
|
|
return status.Value |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (u *AuthService) GetResponsePage() (string, error) { |
|
|
|
func (u *AuthService) IsLogin(c *gin.Context) bool { |
|
|
|
pageCode, err := settingRepo.Get(settingRepo.WithByKey("NoAuthSetting")) |
|
|
|
sID, _ := c.Cookie(constant.SessionName) |
|
|
|
|
|
|
|
_, err := global.SESSION.Get(sID) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return "", err |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
return pageCode.Value, nil |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|