forwardauth.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package auth
  2. /*
  3. Forward authentication hook and account primitives
  4. A ForwardAuthHandler lets another subsystem (the cluster identity
  5. service) decide a login before the local password table is consulted.
  6. It receives the SHA-512 password hash, never the clear text, so it can be
  7. compared against the hashes stored on a remote node without shipping the
  8. password around.
  9. The account primitives below expose the auth table in terms of hashes and
  10. group lists so accounts can be replicated between nodes as-is.
  11. */
  12. import (
  13. "crypto/subtle"
  14. "errors"
  15. )
  16. // ForwardAuthResult is the verdict of a ForwardAuthHandler.
  17. type ForwardAuthResult struct {
  18. Decided bool //false means "no opinion", fall back to the local password table
  19. Accepted bool //valid only when Decided
  20. Reason string //shown to the user when Decided and not Accepted
  21. }
  22. // ForwardAuthHandler decides a login attempt from the username and the
  23. // SHA-512 hash of the supplied password.
  24. type ForwardAuthHandler func(username string, passwordHash string) ForwardAuthResult
  25. // ValidateUsernameAndPasswordHash checks an already hashed password.
  26. func (a *AuthAgent) ValidateUsernameAndPasswordHash(username string, passwordHash string) bool {
  27. stored, err := a.GetPasswordHash(username)
  28. if err != nil || stored == "" {
  29. return false
  30. }
  31. return subtle.ConstantTimeCompare([]byte(stored), []byte(passwordHash)) == 1
  32. }
  33. // GetPasswordHash returns the stored password hash of a user.
  34. func (a *AuthAgent) GetPasswordHash(username string) (string, error) {
  35. hash := ""
  36. if err := a.Database.Read("auth", "passhash/"+username, &hash); err != nil {
  37. return "", err
  38. }
  39. if hash == "" {
  40. return "", errors.New("user not found")
  41. }
  42. return hash, nil
  43. }
  44. // SetPasswordHash stores an already hashed password for a user, creating the
  45. // password entry when it does not exist yet.
  46. func (a *AuthAgent) SetPasswordHash(username string, passwordHash string) error {
  47. if username == "" || passwordHash == "" {
  48. return errors.New("username and password hash are required")
  49. }
  50. return a.Database.Write("auth", "passhash/"+username, passwordHash)
  51. }
  52. // GetUserGroups returns the permission group names of a user.
  53. func (a *AuthAgent) GetUserGroups(username string) ([]string, error) {
  54. if !a.Database.KeyExists("auth", "group/"+username) {
  55. return nil, errors.New("user not found")
  56. }
  57. groups := []string{}
  58. if err := a.Database.Read("auth", "group/"+username, &groups); err != nil {
  59. return nil, err
  60. }
  61. if groups == nil {
  62. groups = []string{}
  63. }
  64. return groups, nil
  65. }
  66. // SetUserGroups replaces the permission group names of a user.
  67. func (a *AuthAgent) SetUserGroups(username string, groups []string) error {
  68. if username == "" {
  69. return errors.New("username is required")
  70. }
  71. if groups == nil {
  72. groups = []string{}
  73. }
  74. return a.Database.Write("auth", "group/"+username, groups)
  75. }
  76. // validateLogin decides a login, consulting the forward auth hook first.
  77. func (a *AuthAgent) validateLogin(username string, password string) (bool, string) {
  78. if a.ForwardAuth != nil {
  79. if res := a.ForwardAuth(username, Hash(password)); res.Decided {
  80. return res.Accepted, res.Reason
  81. }
  82. }
  83. return a.ValidateUsernameAndPasswordWithReason(username, password)
  84. }