gitea源码

authenticate.go 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package db
  4. import (
  5. "context"
  6. "fmt"
  7. user_model "code.gitea.io/gitea/models/user"
  8. "code.gitea.io/gitea/modules/setting"
  9. "code.gitea.io/gitea/modules/util"
  10. )
  11. // ErrUserPasswordNotSet represents a "ErrUserPasswordNotSet" kind of error.
  12. type ErrUserPasswordNotSet struct {
  13. UID int64
  14. Name string
  15. }
  16. func (err ErrUserPasswordNotSet) Error() string {
  17. return fmt.Sprintf("user's password isn't set [uid: %d, name: %s]", err.UID, err.Name)
  18. }
  19. // Unwrap unwraps this error as a ErrInvalidArgument error
  20. func (err ErrUserPasswordNotSet) Unwrap() error {
  21. return util.ErrInvalidArgument
  22. }
  23. // ErrUserPasswordInvalid represents a "ErrUserPasswordInvalid" kind of error.
  24. type ErrUserPasswordInvalid struct {
  25. UID int64
  26. Name string
  27. }
  28. func (err ErrUserPasswordInvalid) Error() string {
  29. return fmt.Sprintf("user's password is invalid [uid: %d, name: %s]", err.UID, err.Name)
  30. }
  31. // Unwrap unwraps this error as a ErrInvalidArgument error
  32. func (err ErrUserPasswordInvalid) Unwrap() error {
  33. return util.ErrInvalidArgument
  34. }
  35. // Authenticate authenticates the provided user against the DB
  36. func Authenticate(ctx context.Context, user *user_model.User, login, password string) (*user_model.User, error) {
  37. if user == nil {
  38. return nil, user_model.ErrUserNotExist{Name: login}
  39. }
  40. if !user.IsPasswordSet() {
  41. return nil, ErrUserPasswordNotSet{UID: user.ID, Name: user.Name}
  42. } else if !user.ValidatePassword(password) {
  43. return nil, ErrUserPasswordInvalid{UID: user.ID, Name: user.Name}
  44. }
  45. // Update password hash if server password hash algorithm have changed
  46. // Or update the password when the salt length doesn't match the current
  47. // recommended salt length, this in order to migrate user's salts to a more secure salt.
  48. if user.PasswdHashAlgo != setting.PasswordHashAlgo || len(user.Salt) != user_model.SaltByteLength*2 {
  49. if err := user.SetPassword(password); err != nil {
  50. return nil, err
  51. }
  52. if err := user_model.UpdateUserCols(ctx, user, "passwd", "passwd_hash_algo", "salt"); err != nil {
  53. return nil, err
  54. }
  55. }
  56. // WARN: DON'T check user.IsActive, that will be checked on reqSign so that
  57. // user could be hinted to resend confirm email.
  58. if user.ProhibitLogin {
  59. return nil, user_model.ErrUserProhibitLogin{
  60. UID: user.ID,
  61. Name: user.Name,
  62. }
  63. }
  64. // attempting to login as a non-user account
  65. if user.Type != user_model.UserTypeIndividual {
  66. return nil, user_model.ErrUserProhibitLogin{
  67. UID: user.ID,
  68. Name: user.Name,
  69. }
  70. }
  71. return user, nil
  72. }