Skip to content

Cleanup models.User.HashPassword #3334

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 3 commits into from
Jan 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,10 @@ func runChangePassword(c *cli.Context) error {
if err != nil {
return fmt.Errorf("%v", err)
}
user.Passwd = c.String("password")
if user.Salt, err = models.GetUserSalt(); err != nil {
return fmt.Errorf("%v", err)
}
user.HashPassword()
user.HashPassword(c.String("password"))
if err := models.UpdateUserCols(user, "passwd", "salt"); err != nil {
return fmt.Errorf("%v", err)
}
Expand Down
17 changes: 10 additions & 7 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,17 +388,20 @@ func (u *User) NewGitSig() *git.Signature {
}
}

func hashPassword(passwd, salt string) string {
tempPasswd := pbkdf2.Key([]byte(passwd), []byte(salt), 10000, 50, sha256.New)
return fmt.Sprintf("%x", tempPasswd)
}

// HashPassword hashes a password using PBKDF.
func (u *User) HashPassword() {
newPasswd := pbkdf2.Key([]byte(u.Passwd), []byte(u.Salt), 10000, 50, sha256.New)
u.Passwd = fmt.Sprintf("%x", newPasswd)
func (u *User) HashPassword(passwd string) {
u.Passwd = hashPassword(passwd, u.Salt)
}

// ValidatePassword checks if given password matches the one belongs to the user.
func (u *User) ValidatePassword(passwd string) bool {
newUser := &User{Passwd: passwd, Salt: u.Salt}
newUser.HashPassword()
return subtle.ConstantTimeCompare([]byte(u.Passwd), []byte(newUser.Passwd)) == 1
tempHash := hashPassword(passwd, u.Salt)
return subtle.ConstantTimeCompare([]byte(u.Passwd), []byte(tempHash)) == 1
}

// IsPasswordSet checks if the password is set or left empty
Expand Down Expand Up @@ -711,7 +714,7 @@ func CreateUser(u *User) (err error) {
if u.Salt, err = GetUserSalt(); err != nil {
return err
}
u.HashPassword()
u.HashPassword(u.Passwd)
u.AllowCreateOrganization = setting.Service.DefaultAllowCreateOrganization
u.MaxRepoCreation = -1

Expand Down
9 changes: 3 additions & 6 deletions models/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,11 @@ func TestHashPasswordDeterministic(t *testing.T) {
pass := string(b)

// save the current password in the user - hash it and store the result
u.Passwd = pass
u.HashPassword()
u.HashPassword(pass)
r1 := u.Passwd

// run again
u.Passwd = pass
u.HashPassword()
u.HashPassword(pass)
r2 := u.Passwd

// assert equal (given the same salt+pass, the same result is produced)
Expand All @@ -158,7 +156,6 @@ func BenchmarkHashPassword(b *testing.B) {
u := &User{Salt: string(bs), Passwd: pass}
b.ResetTimer()
for i := 0; i < b.N; i++ {
u.HashPassword()
u.Passwd = pass
u.HashPassword(pass)
}
}
3 changes: 1 addition & 2 deletions routers/admin/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,12 @@ func EditUserPost(ctx *context.Context, form auth.AdminEditUserForm) {
}

if len(form.Password) > 0 {
u.Passwd = form.Password
var err error
if u.Salt, err = models.GetUserSalt(); err != nil {
ctx.ServerError("UpdateUser", err)
return
}
u.HashPassword()
u.HashPassword(form.Password)
}

u.LoginName = form.LoginName
Expand Down
3 changes: 1 addition & 2 deletions routers/api/v1/admin/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,12 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) {
}

if len(form.Password) > 0 {
u.Passwd = form.Password
var err error
if u.Salt, err = models.GetUserSalt(); err != nil {
ctx.Error(500, "UpdateUser", err)
return
}
u.HashPassword()
u.HashPassword(form.Password)
}

u.LoginName = form.LoginName
Expand Down
3 changes: 1 addition & 2 deletions routers/user/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,6 @@ func ResetPasswdPost(ctx *context.Context) {
return
}

u.Passwd = passwd
var err error
if u.Rands, err = models.GetUserSalt(); err != nil {
ctx.ServerError("UpdateUser", err)
Expand All @@ -994,7 +993,7 @@ func ResetPasswdPost(ctx *context.Context) {
ctx.ServerError("UpdateUser", err)
return
}
u.HashPassword()
u.HashPassword(passwd)
if err := models.UpdateUserCols(u, "passwd", "rands", "salt"); err != nil {
ctx.ServerError("UpdateUser", err)
return
Expand Down
3 changes: 1 addition & 2 deletions routers/user/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,12 @@ func SettingsSecurityPost(ctx *context.Context, form auth.ChangePasswordForm) {
} else if form.Password != form.Retype {
ctx.Flash.Error(ctx.Tr("form.password_not_match"))
} else {
ctx.User.Passwd = form.Password
var err error
if ctx.User.Salt, err = models.GetUserSalt(); err != nil {
ctx.ServerError("UpdateUser", err)
return
}
ctx.User.HashPassword()
ctx.User.HashPassword(form.Password)
if err := models.UpdateUserCols(ctx.User, "salt", "passwd"); err != nil {
ctx.ServerError("UpdateUser", err)
return
Expand Down