You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

v166.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package migrations
  5. import (
  6. "crypto/sha256"
  7. "fmt"
  8. "golang.org/x/crypto/argon2"
  9. "golang.org/x/crypto/bcrypt"
  10. "golang.org/x/crypto/pbkdf2"
  11. "golang.org/x/crypto/scrypt"
  12. "xorm.io/builder"
  13. "xorm.io/xorm"
  14. )
  15. func recalculateUserEmptyPWD(x *xorm.Engine) (err error) {
  16. const (
  17. algoBcrypt = "bcrypt"
  18. algoScrypt = "scrypt"
  19. algoArgon2 = "argon2"
  20. algoPbkdf2 = "pbkdf2"
  21. )
  22. type User struct {
  23. ID int64 `xorm:"pk autoincr"`
  24. Passwd string `xorm:"NOT NULL"`
  25. PasswdHashAlgo string `xorm:"NOT NULL DEFAULT 'argon2'"`
  26. MustChangePassword bool `xorm:"NOT NULL DEFAULT false"`
  27. LoginType int
  28. LoginName string
  29. Type int
  30. Salt string `xorm:"VARCHAR(10)"`
  31. }
  32. // hashPassword hash password based on algo and salt
  33. // state 461406070c
  34. hashPassword := func(passwd, salt, algo string) string {
  35. var tempPasswd []byte
  36. switch algo {
  37. case algoBcrypt:
  38. tempPasswd, _ = bcrypt.GenerateFromPassword([]byte(passwd), bcrypt.DefaultCost)
  39. return string(tempPasswd)
  40. case algoScrypt:
  41. tempPasswd, _ = scrypt.Key([]byte(passwd), []byte(salt), 65536, 16, 2, 50)
  42. case algoArgon2:
  43. tempPasswd = argon2.IDKey([]byte(passwd), []byte(salt), 2, 65536, 8, 50)
  44. case algoPbkdf2:
  45. fallthrough
  46. default:
  47. tempPasswd = pbkdf2.Key([]byte(passwd), []byte(salt), 10000, 50, sha256.New)
  48. }
  49. return fmt.Sprintf("%x", tempPasswd)
  50. }
  51. // ValidatePassword checks if given password matches the one belongs to the user.
  52. // state 461406070c, changed since it's not necessary to be time constant
  53. ValidatePassword := func(u *User, passwd string) bool {
  54. tempHash := hashPassword(passwd, u.Salt, u.PasswdHashAlgo)
  55. if u.PasswdHashAlgo != algoBcrypt && u.Passwd == tempHash {
  56. return true
  57. }
  58. if u.PasswdHashAlgo == algoBcrypt && bcrypt.CompareHashAndPassword([]byte(u.Passwd), []byte(passwd)) == nil {
  59. return true
  60. }
  61. return false
  62. }
  63. sess := x.NewSession()
  64. defer sess.Close()
  65. const batchSize = 100
  66. for start := 0; ; start += batchSize {
  67. users := make([]*User, 0, batchSize)
  68. if err = sess.Limit(batchSize, start).Where(builder.Neq{"passwd": ""}, 0).Find(&users); err != nil {
  69. return
  70. }
  71. if len(users) == 0 {
  72. break
  73. }
  74. if err = sess.Begin(); err != nil {
  75. return
  76. }
  77. for _, user := range users {
  78. if ValidatePassword(user, "") {
  79. user.Passwd = ""
  80. user.Salt = ""
  81. user.PasswdHashAlgo = ""
  82. if _, err = sess.ID(user.ID).Cols("passwd", "salt", "passwd_hash_algo").Update(user); err != nil {
  83. return err
  84. }
  85. }
  86. }
  87. if err = sess.Commit(); err != nil {
  88. return
  89. }
  90. }
  91. // delete salt and algo where password is empty
  92. _, err = sess.Where(builder.Eq{"passwd": ""}.And(builder.Neq{"salt": ""}.Or(builder.Neq{"passwd_hash_algo": ""}))).
  93. Cols("salt", "passwd_hash_algo").Update(&User{})
  94. return err
  95. }