-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
[Refactor] Passwort Hash/Set #14282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[Refactor] Passwort Hash/Set #14282
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f8f8109
move SaltGeneration into HashPasswort and rename it to what it does
6543 867ee87
Migration: Where Password is Valid with Empty String delete it
6543 e14f791
Merge branch 'master' into passwd-things
6543 85cbd3d
Merge branch 'master' into passwd-things
6543 7d4f7fb
prohibit empty password hash
6543 e0bbf54
Merge branch 'master' into passwd-things
6543 3003bee
let SetPassword("") unset pwd stuff
6543 6295eea
Merge branch 'master' into passwd-things
6543 1b611ab
Merge branch 'master' into passwd-things
6543 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,115 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package migrations | ||
|
||
import ( | ||
"crypto/sha256" | ||
"fmt" | ||
|
||
"golang.org/x/crypto/argon2" | ||
"golang.org/x/crypto/bcrypt" | ||
"golang.org/x/crypto/pbkdf2" | ||
"golang.org/x/crypto/scrypt" | ||
"xorm.io/builder" | ||
"xorm.io/xorm" | ||
) | ||
|
||
func recalculateUserEmptyPWD(x *xorm.Engine) (err error) { | ||
const ( | ||
algoBcrypt = "bcrypt" | ||
algoScrypt = "scrypt" | ||
algoArgon2 = "argon2" | ||
algoPbkdf2 = "pbkdf2" | ||
) | ||
|
||
type User struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
Passwd string `xorm:"NOT NULL"` | ||
PasswdHashAlgo string `xorm:"NOT NULL DEFAULT 'argon2'"` | ||
MustChangePassword bool `xorm:"NOT NULL DEFAULT false"` | ||
LoginType int | ||
LoginName string | ||
Type int | ||
Salt string `xorm:"VARCHAR(10)"` | ||
} | ||
|
||
// hashPassword hash password based on algo and salt | ||
// state 461406070c | ||
hashPassword := func(passwd, salt, algo string) string { | ||
var tempPasswd []byte | ||
|
||
switch algo { | ||
case algoBcrypt: | ||
tempPasswd, _ = bcrypt.GenerateFromPassword([]byte(passwd), bcrypt.DefaultCost) | ||
return string(tempPasswd) | ||
case algoScrypt: | ||
tempPasswd, _ = scrypt.Key([]byte(passwd), []byte(salt), 65536, 16, 2, 50) | ||
case algoArgon2: | ||
tempPasswd = argon2.IDKey([]byte(passwd), []byte(salt), 2, 65536, 8, 50) | ||
case algoPbkdf2: | ||
fallthrough | ||
default: | ||
tempPasswd = pbkdf2.Key([]byte(passwd), []byte(salt), 10000, 50, sha256.New) | ||
} | ||
|
||
return fmt.Sprintf("%x", tempPasswd) | ||
} | ||
|
||
// ValidatePassword checks if given password matches the one belongs to the user. | ||
// state 461406070c, changed since it's not necessary to be time constant | ||
ValidatePassword := func(u *User, passwd string) bool { | ||
tempHash := hashPassword(passwd, u.Salt, u.PasswdHashAlgo) | ||
|
||
if u.PasswdHashAlgo != algoBcrypt && u.Passwd == tempHash { | ||
return true | ||
} | ||
if u.PasswdHashAlgo == algoBcrypt && bcrypt.CompareHashAndPassword([]byte(u.Passwd), []byte(passwd)) == nil { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
sess := x.NewSession() | ||
defer sess.Close() | ||
|
||
const batchSize = 100 | ||
|
||
for start := 0; ; start += batchSize { | ||
users := make([]*User, 0, batchSize) | ||
if err = sess.Limit(batchSize, start).Where(builder.Neq{"passwd": ""}, 0).Find(&users); err != nil { | ||
return | ||
} | ||
if len(users) == 0 { | ||
break | ||
} | ||
|
||
if err = sess.Begin(); err != nil { | ||
return | ||
} | ||
|
||
for _, user := range users { | ||
if ValidatePassword(user, "") { | ||
user.Passwd = "" | ||
user.Salt = "" | ||
user.PasswdHashAlgo = "" | ||
if _, err = sess.ID(user.ID).Cols("passwd", "salt", "passwd_hash_algo").Update(user); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
if err = sess.Commit(); err != nil { | ||
return | ||
} | ||
} | ||
|
||
// delete salt and algo where password is empty | ||
if _, err = sess.Where(builder.Eq{"passwd": ""}.And(builder.Neq{"salt": ""}.Or(builder.Neq{"passwd_hash_algo": ""}))). | ||
Cols("salt", "passwd_hash_algo").Update(&User{}); err != nil { | ||
return err | ||
} | ||
|
||
return sess.Commit() | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.