feat(user): add encryption algorithm and salt fields to User model for migrate
This commit is contained in:
parent
de4386ff68
commit
8562734fde
3
initialize/migrate/database/02115_user_algo.down.sql
Normal file
3
initialize/migrate/database/02115_user_algo.down.sql
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
ALTER TABLE `User`
|
||||||
|
DROP COLUMN `algo`,
|
||||||
|
DROP COLUMN `salt`;
|
||||||
3
initialize/migrate/database/02115_user_algo.up.sql
Normal file
3
initialize/migrate/database/02115_user_algo.up.sql
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
ALTER TABLE `User`
|
||||||
|
ADD COLUMN `algo` VARCHAR(20) NOT NULL DEFAULT 'default' COMMENT 'Encryption Algorithm' AFTER `password`,
|
||||||
|
ADD COLUMN `salt` VARCHAR(20) NOT NULL DEFAULT 'default' COMMENT 'Password Salt' AFTER `algo`;
|
||||||
@ -40,6 +40,7 @@ func (l *CreateUserLogic) CreateUser(req *types.CreateUserRequest) error {
|
|||||||
pwd := tool.EncodePassWord(req.Password)
|
pwd := tool.EncodePassWord(req.Password)
|
||||||
newUser := &user.User{
|
newUser := &user.User{
|
||||||
Password: pwd,
|
Password: pwd,
|
||||||
|
Algo: "default",
|
||||||
ReferralPercentage: req.ReferralPercentage,
|
ReferralPercentage: req.ReferralPercentage,
|
||||||
OnlyFirstPurchase: &req.OnlyFirstPurchase,
|
OnlyFirstPurchase: &req.OnlyFirstPurchase,
|
||||||
ReferCode: req.ReferCode,
|
ReferCode: req.ReferCode,
|
||||||
|
|||||||
@ -129,6 +129,7 @@ func (l *UpdateUserBasicInfoLogic) UpdateUserBasicInfo(req *types.UpdateUserBasi
|
|||||||
return errors.Wrapf(xerr.NewErrCodeMsg(503, "Demo mode does not allow modification of the admin user password"), "UpdateUserBasicInfo failed: cannot update admin user password in demo mode")
|
return errors.Wrapf(xerr.NewErrCodeMsg(503, "Demo mode does not allow modification of the admin user password"), "UpdateUserBasicInfo failed: cannot update admin user password in demo mode")
|
||||||
}
|
}
|
||||||
userInfo.Password = tool.EncodePassWord(req.Password)
|
userInfo.Password = tool.EncodePassWord(req.Password)
|
||||||
|
userInfo.Algo = "default"
|
||||||
}
|
}
|
||||||
|
|
||||||
err = l.svcCtx.UserModel.Update(l.ctx, userInfo)
|
err = l.svcCtx.UserModel.Update(l.ctx, userInfo)
|
||||||
|
|||||||
@ -104,7 +104,8 @@ func (l *ResetPasswordLogic) ResetPassword(req *types.ResetPasswordRequest) (res
|
|||||||
|
|
||||||
// Update password
|
// Update password
|
||||||
userInfo.Password = tool.EncodePassWord(req.Password)
|
userInfo.Password = tool.EncodePassWord(req.Password)
|
||||||
if err := l.svcCtx.UserModel.Update(l.ctx, userInfo); err != nil {
|
userInfo.Algo = "default"
|
||||||
|
if err = l.svcCtx.UserModel.Update(l.ctx, userInfo); err != nil {
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "update user info failed: %v", err.Error())
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "update user info failed: %v", err.Error())
|
||||||
}
|
}
|
||||||
// Generate session id
|
// Generate session id
|
||||||
|
|||||||
@ -98,7 +98,7 @@ func (l *TelephoneLoginLogic) TelephoneLogin(req *types.TelephoneLoginRequest, r
|
|||||||
|
|
||||||
if req.TelephoneCode == "" {
|
if req.TelephoneCode == "" {
|
||||||
// Verify password
|
// Verify password
|
||||||
if !tool.VerifyPassWord(req.Password, userInfo.Password) {
|
if !tool.MultiPasswordVerify(userInfo.Algo, userInfo.Salt, req.Password, userInfo.Password) {
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.UserPasswordError), "user password")
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.UserPasswordError), "user password")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -78,6 +78,7 @@ func (l *TelephoneResetPasswordLogic) TelephoneResetPassword(req *types.Telephon
|
|||||||
// Generate password
|
// Generate password
|
||||||
pwd := tool.EncodePassWord(req.Password)
|
pwd := tool.EncodePassWord(req.Password)
|
||||||
userInfo.Password = pwd
|
userInfo.Password = pwd
|
||||||
|
userInfo.Algo = "default"
|
||||||
err = l.svcCtx.UserModel.Update(l.ctx, userInfo)
|
err = l.svcCtx.UserModel.Update(l.ctx, userInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "update user password failed: %v", err.Error())
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "update user password failed: %v", err.Error())
|
||||||
|
|||||||
@ -107,6 +107,7 @@ func (l *TelephoneUserRegisterLogic) TelephoneUserRegister(req *types.TelephoneR
|
|||||||
pwd := tool.EncodePassWord(req.Password)
|
pwd := tool.EncodePassWord(req.Password)
|
||||||
userInfo := &user.User{
|
userInfo := &user.User{
|
||||||
Password: pwd,
|
Password: pwd,
|
||||||
|
Algo: "default",
|
||||||
OnlyFirstPurchase: &l.svcCtx.Config.Invite.OnlyFirstPurchase,
|
OnlyFirstPurchase: &l.svcCtx.Config.Invite.OnlyFirstPurchase,
|
||||||
AuthMethods: []user.AuthMethods{
|
AuthMethods: []user.AuthMethods{
|
||||||
{
|
{
|
||||||
|
|||||||
@ -76,7 +76,7 @@ func (l *UserLoginLogic) UserLogin(req *types.UserLoginRequest) (resp *types.Log
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Verify password
|
// Verify password
|
||||||
if !tool.VerifyPassWord(req.Password, userInfo.Password) {
|
if !tool.MultiPasswordVerify(userInfo.Algo, userInfo.Salt, req.Password, userInfo.Password) {
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.UserPasswordError), "user password")
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.UserPasswordError), "user password")
|
||||||
}
|
}
|
||||||
// Generate session id
|
// Generate session id
|
||||||
|
|||||||
@ -90,6 +90,7 @@ func (l *UserRegisterLogic) UserRegister(req *types.UserRegisterRequest) (resp *
|
|||||||
pwd := tool.EncodePassWord(req.Password)
|
pwd := tool.EncodePassWord(req.Password)
|
||||||
userInfo := &user.User{
|
userInfo := &user.User{
|
||||||
Password: pwd,
|
Password: pwd,
|
||||||
|
Algo: "default",
|
||||||
OnlyFirstPurchase: &l.svcCtx.Config.Invite.OnlyFirstPurchase,
|
OnlyFirstPurchase: &l.svcCtx.Config.Invite.OnlyFirstPurchase,
|
||||||
}
|
}
|
||||||
if referer != nil {
|
if referer != nil {
|
||||||
|
|||||||
@ -7,6 +7,8 @@ import (
|
|||||||
type User struct {
|
type User struct {
|
||||||
Id int64 `gorm:"primaryKey"`
|
Id int64 `gorm:"primaryKey"`
|
||||||
Password string `gorm:"type:varchar(100);not null;comment:User Password"`
|
Password string `gorm:"type:varchar(100);not null;comment:User Password"`
|
||||||
|
Algo string `gorm:"type:varchar(20);default:'default';comment:Encryption Algorithm"`
|
||||||
|
Salt string `gorm:"type:varchar(20);default:null;comment:Password Salt"`
|
||||||
Avatar string `gorm:"type:MEDIUMTEXT;comment:User Avatar"`
|
Avatar string `gorm:"type:MEDIUMTEXT;comment:User Avatar"`
|
||||||
Balance int64 `gorm:"default:0;comment:User Balance"` // User Balance Amount
|
Balance int64 `gorm:"default:0;comment:User Balance"` // User Balance Amount
|
||||||
ReferCode string `gorm:"type:varchar(20);default:'';comment:Referral Code"`
|
ReferCode string `gorm:"type:varchar(20);default:'';comment:Referral Code"`
|
||||||
|
|||||||
@ -40,15 +40,13 @@ func MultiPasswordVerify(algo, salt, password, hash string) bool {
|
|||||||
case "md5":
|
case "md5":
|
||||||
sum := md5.Sum([]byte(password))
|
sum := md5.Sum([]byte(password))
|
||||||
return hex.EncodeToString(sum[:]) == hash
|
return hex.EncodeToString(sum[:]) == hash
|
||||||
|
|
||||||
case "sha256":
|
case "sha256":
|
||||||
sum := sha256.Sum256([]byte(password))
|
sum := sha256.Sum256([]byte(password))
|
||||||
return hex.EncodeToString(sum[:]) == hash
|
return hex.EncodeToString(sum[:]) == hash
|
||||||
|
|
||||||
case "md5salt":
|
case "md5salt":
|
||||||
sum := md5.Sum([]byte(password + salt))
|
sum := md5.Sum([]byte(password + salt))
|
||||||
return hex.EncodeToString(sum[:]) == hash
|
return hex.EncodeToString(sum[:]) == hash
|
||||||
case "default":
|
case "default": // PPanel's default algorithm
|
||||||
return VerifyPassWord(password, hash)
|
return VerifyPassWord(password, hash)
|
||||||
case "bcrypt":
|
case "bcrypt":
|
||||||
// Bcrypt (corresponding to PHP's password_hash/password_verify)
|
// Bcrypt (corresponding to PHP's password_hash/password_verify)
|
||||||
|
|||||||
@ -10,6 +10,6 @@ func TestEncodePassWord(t *testing.T) {
|
|||||||
|
|
||||||
func TestMultiPasswordVerify(t *testing.T) {
|
func TestMultiPasswordVerify(t *testing.T) {
|
||||||
pwd := "$2y$10$WFO17pdtohfeBILjEChoGeVxpDG.u9kVCKhjDAeEeNmCjIlj3tDRy"
|
pwd := "$2y$10$WFO17pdtohfeBILjEChoGeVxpDG.u9kVCKhjDAeEeNmCjIlj3tDRy"
|
||||||
status := MultiPasswordVerify("", "", "admin", pwd)
|
status := MultiPasswordVerify("bcrypt", "", "admin1", pwd)
|
||||||
t.Logf("MultiPasswordVerify: %v", status)
|
t.Logf("MultiPasswordVerify: %v", status)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -223,6 +223,7 @@ func (l *ActivateOrderLogic) createGuestUser(ctx context.Context, orderInfo *ord
|
|||||||
|
|
||||||
userInfo := &user.User{
|
userInfo := &user.User{
|
||||||
Password: tool.EncodePassWord(tempOrder.Password),
|
Password: tool.EncodePassWord(tempOrder.Password),
|
||||||
|
Algo: "default",
|
||||||
AuthMethods: []user.AuthMethods{
|
AuthMethods: []user.AuthMethods{
|
||||||
{
|
{
|
||||||
AuthType: tempOrder.AuthType,
|
AuthType: tempOrder.AuthType,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user