local_user_login.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package api
  2. import (
  3. "crypto/rand"
  4. "errors"
  5. "runtime"
  6. "sync"
  7. config "github.com/OliveTin/OliveTin/internal/config"
  8. "github.com/alexedwards/argon2id"
  9. log "github.com/sirupsen/logrus"
  10. )
  11. var ErrArgon2Busy = errors.New("too many concurrent password operations")
  12. const argon2MaxConcurrent = 10
  13. var argon2Sem = make(chan struct{}, argon2MaxConcurrent)
  14. var defaultParams = argon2id.Params{
  15. Memory: 64 * 1024,
  16. Iterations: 4,
  17. Parallelism: uint8(runtime.NumCPU()),
  18. SaltLength: 16,
  19. KeyLength: 32,
  20. }
  21. func CreateHash(password string) (string, error) {
  22. select {
  23. case argon2Sem <- struct{}{}:
  24. defer func() { <-argon2Sem }()
  25. default:
  26. return "", ErrArgon2Busy
  27. }
  28. hash, err := argon2id.CreateHash(password, &defaultParams)
  29. if err != nil {
  30. log.Warnf("Error creating hash: %v", err)
  31. return "", err
  32. }
  33. return hash, nil
  34. }
  35. func createHash(password string) (string, error) {
  36. return CreateHash(password)
  37. }
  38. func comparePasswordAndHash(password, hash string) (bool, error) {
  39. select {
  40. case argon2Sem <- struct{}{}:
  41. defer func() { <-argon2Sem }()
  42. default:
  43. return false, ErrArgon2Busy
  44. }
  45. match, err := argon2id.ComparePasswordAndHash(password, hash)
  46. if err != nil {
  47. log.Errorf("Error comparing password and hash: %v", err)
  48. return false, nil
  49. }
  50. return match, nil
  51. }
  52. // A login for an unknown username, or for a user with no password, still runs
  53. // one Argon2id comparison against a dummy hash, through the same concurrency
  54. // limit. Otherwise the response time, and whether the limit is reported as
  55. // busy, would show which usernames exist.
  56. // The dummy password is random, so it is not known to anyone, and the result of
  57. // the comparison is never used.
  58. var (
  59. dummyPassword string
  60. dummyPasswordHash string
  61. dummyPasswordHashErr error
  62. dummyPasswordHashOnce sync.Once
  63. )
  64. func getDummyPasswordHash() (string, error) {
  65. dummyPasswordHashOnce.Do(func() {
  66. dummyPassword = rand.Text()
  67. dummyPasswordHash, dummyPasswordHashErr = argon2id.CreateHash(dummyPassword, &defaultParams)
  68. })
  69. return dummyPasswordHash, dummyPasswordHashErr
  70. }
  71. func compareAgainstDummyHash(password string) error {
  72. hash, err := getDummyPasswordHash()
  73. if err != nil {
  74. return err
  75. }
  76. _, err = comparePasswordAndHash(password, hash)
  77. return err
  78. }
  79. func checkUserPassword(cfg *config.Config, username, password string) (bool, error) {
  80. user := cfg.FindUserByUsername(username)
  81. if user == nil || user.Password == "" {
  82. log.WithFields(log.Fields{"username": username}).Warn("Failed to check password for user, as the username was not found or has no password")
  83. return false, compareAgainstDummyHash(password)
  84. }
  85. return comparePasswordAndLogResult(password, user.Password, username)
  86. }
  87. func comparePasswordAndLogResult(password, hash, username string) (bool, error) {
  88. match, err := comparePasswordAndHash(password, hash)
  89. if err != nil {
  90. return false, err
  91. }
  92. if !match {
  93. log.WithFields(log.Fields{"username": username}).Warn("Password does not match for user")
  94. return false, nil
  95. }
  96. return true, nil
  97. }