auth.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Package auth deals with authentication and authorization against topics
  2. package auth
  3. import (
  4. "errors"
  5. "regexp"
  6. )
  7. // Auther is a generic interface to implement password and token based authentication and authorization
  8. type Auther interface {
  9. // Authenticate checks username and password and returns a user if correct. The method
  10. // returns in constant-ish time, regardless of whether the user exists or the password is
  11. // correct or incorrect.
  12. Authenticate(username, password string) (*User, error)
  13. AuthenticateToken(token string) (*User, error)
  14. GenerateToken(user *User) (string, error)
  15. // Authorize returns nil if the given user has access to the given topic using the desired
  16. // permission. The user param may be nil to signal an anonymous user.
  17. Authorize(user *User, topic string, perm Permission) error
  18. }
  19. // Manager is an interface representing user and access management
  20. type Manager interface {
  21. // AddUser adds a user with the given username, password and role. The password should be hashed
  22. // before it is stored in a persistence layer.
  23. AddUser(username, password string, role Role) error
  24. // RemoveUser deletes the user with the given username. The function returns nil on success, even
  25. // if the user did not exist in the first place.
  26. RemoveUser(username string) error
  27. // Users returns a list of users. It always also returns the Everyone user ("*").
  28. Users() ([]*User, error)
  29. // User returns the user with the given username if it exists, or ErrNotFound otherwise.
  30. // You may also pass Everyone to retrieve the anonymous user and its Grant list.
  31. User(username string) (*User, error)
  32. // ChangePassword changes a user's password
  33. ChangePassword(username, password string) error
  34. // ChangeRole changes a user's role. When a role is changed from RoleUser to RoleAdmin,
  35. // all existing access control entries (Grant) are removed, since they are no longer needed.
  36. ChangeRole(username string, role Role) error
  37. // AllowAccess adds or updates an entry in th access control list for a specific user. It controls
  38. // read/write access to a topic. The parameter topicPattern may include wildcards (*).
  39. AllowAccess(username string, topicPattern string, read bool, write bool) error
  40. // ResetAccess removes an access control list entry for a specific username/topic, or (if topic is
  41. // empty) for an entire user. The parameter topicPattern may include wildcards (*).
  42. ResetAccess(username string, topicPattern string) error
  43. // DefaultAccess returns the default read/write access if no access control entry matches
  44. DefaultAccess() (read bool, write bool)
  45. }
  46. // User is a struct that represents a user
  47. type User struct {
  48. Name string
  49. Hash string // password hash (bcrypt)
  50. Role Role
  51. Grants []Grant
  52. Language string
  53. }
  54. // Grant is a struct that represents an access control entry to a topic
  55. type Grant struct {
  56. TopicPattern string // May include wildcard (*)
  57. AllowRead bool
  58. AllowWrite bool
  59. }
  60. // Permission represents a read or write permission to a topic
  61. type Permission int
  62. // Permissions to a topic
  63. const (
  64. PermissionRead = Permission(1)
  65. PermissionWrite = Permission(2)
  66. )
  67. // Role represents a user's role, either admin or regular user
  68. type Role string
  69. // User roles
  70. const (
  71. RoleAdmin = Role("admin")
  72. RoleUser = Role("user")
  73. RoleAnonymous = Role("anonymous")
  74. )
  75. // Everyone is a special username representing anonymous users
  76. const (
  77. Everyone = "*"
  78. )
  79. var (
  80. allowedUsernameRegex = regexp.MustCompile(`^[-_.@a-zA-Z0-9]+$`) // Does not include Everyone (*)
  81. allowedTopicPatternRegex = regexp.MustCompile(`^[-_*A-Za-z0-9]{1,64}$`) // Adds '*' for wildcards!
  82. )
  83. // AllowedRole returns true if the given role can be used for new users
  84. func AllowedRole(role Role) bool {
  85. return role == RoleUser || role == RoleAdmin
  86. }
  87. // AllowedUsername returns true if the given username is valid
  88. func AllowedUsername(username string) bool {
  89. return allowedUsernameRegex.MatchString(username)
  90. }
  91. // AllowedTopicPattern returns true if the given topic pattern is valid; this includes the wildcard character (*)
  92. func AllowedTopicPattern(username string) bool {
  93. return allowedTopicPatternRegex.MatchString(username)
  94. }
  95. // Error constants used by the package
  96. var (
  97. ErrUnauthenticated = errors.New("unauthenticated")
  98. ErrUnauthorized = errors.New("unauthorized")
  99. ErrInvalidArgument = errors.New("invalid argument")
  100. ErrNotFound = errors.New("not found")
  101. )