-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
204 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2024 ROC. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package conf | ||
|
||
import ( | ||
"github.com/alimy/tryst/cfg" | ||
"github.com/rocboss/paopao-ce/pkg/auth" | ||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
func NewPasswordProvider() (provider auth.PasswordProvider) { | ||
cfg.On(cfg.Actions{ | ||
"Md5AuthMethod": func() { | ||
provider = auth.NewMd5PasswordProvider() | ||
}, | ||
"BcryptAuthMethod": func() { | ||
provider = auth.NewBcryptPasswordProvider(bcrypt.DefaultCost) | ||
}, | ||
}, | ||
func() { | ||
provider = auth.NewMd5PasswordProvider() | ||
}, | ||
) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2024 ROC. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package auth | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"github.com/gofrs/uuid/v5" | ||
"github.com/rocboss/paopao-ce/pkg/utils" | ||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
type PasswordProvider interface { | ||
Generate(password string) (string, error) | ||
Compare(hashedPassword, password string) error | ||
} | ||
|
||
type bcryptPasswordProvider struct { | ||
cost int | ||
} | ||
|
||
type md5PasswordProvider struct{} | ||
|
||
func (p *bcryptPasswordProvider) Generate(password string) (string, error) { | ||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), p.cost) | ||
return utils.String(hashedPassword), err | ||
} | ||
|
||
func (p *bcryptPasswordProvider) Compare(hashedPassword, password string) error { | ||
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password)) | ||
} | ||
|
||
func (p md5PasswordProvider) Generate(password string) (string, error) { | ||
salt := uuid.Must(uuid.NewV4()).String()[:8] | ||
return utils.EncodeMD5(utils.EncodeMD5(password)+salt) + ":" + salt, nil | ||
} | ||
|
||
func (p md5PasswordProvider) Compare(hashedPassword, password string) error { | ||
passwordSalt := strings.Split(string(hashedPassword), ":") | ||
if len(passwordSalt) != 2 { | ||
return errors.New("invalid hashed password") | ||
} | ||
if strings.Compare(passwordSalt[0], utils.EncodeMD5(utils.EncodeMD5(password)+passwordSalt[1])) != 0 { | ||
return errors.New("invalid password") | ||
} | ||
return nil | ||
} | ||
|
||
func NewBcryptPasswordProvider(cost int) PasswordProvider { | ||
return &bcryptPasswordProvider{ | ||
cost: cost, | ||
} | ||
} | ||
|
||
func NewMd5PasswordProvider() PasswordProvider { | ||
return md5PasswordProvider{} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
ALTER TABLE `p_user` ADD COLUMN `salt` VARCHAR(32) NOT NULL DEFAULT '' COMMENT '盐值'; | ||
|
||
UPDATE | ||
`p_user` | ||
SET | ||
`salt` = SUBSTRING_INDEX(`password`, ':', -1), | ||
`password` = SUBSTRING_INDEX(`password`, ':', 1); | ||
|
||
ALTER TABLE `p_user` MODIFY COLUMN `password` VARCHAR(64) NOT NULL DEFAULT '' COMMENT '密码'; |
Oops, something went wrong.