types.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Package user deals with authentication and authorization against topics
  2. package user
  3. import (
  4. "errors"
  5. "regexp"
  6. )
  7. type Auther interface {
  8. // Authenticate checks username and password and returns a user if correct. The method
  9. // returns in constant-ish time, regardless of whether the user exists or the password is
  10. // correct or incorrect.
  11. Authenticate(username, password string) (*User, error)
  12. // Authorize returns nil if the given user has access to the given topic using the desired
  13. // permission. The user param may be nil to signal an anonymous user.
  14. Authorize(user *User, topic string, perm Permission) error
  15. }
  16. // User is a struct that represents a user
  17. type User struct {
  18. Name string
  19. Hash string // password hash (bcrypt)
  20. Token string // Only set if token was used to log in
  21. Role Role
  22. Grants []Grant
  23. Prefs *Prefs
  24. Plan *Plan
  25. Stats *Stats
  26. }
  27. type Token struct {
  28. Value string
  29. Expires int64
  30. }
  31. type Prefs struct {
  32. Language string `json:"language,omitempty"`
  33. Notification *NotificationPrefs `json:"notification,omitempty"`
  34. Subscriptions []*Subscription `json:"subscriptions,omitempty"`
  35. }
  36. type PlanCode string
  37. const (
  38. PlanUnlimited = PlanCode("unlimited")
  39. PlanDefault = PlanCode("default")
  40. PlanNone = PlanCode("none")
  41. )
  42. type Plan struct {
  43. Code string `json:"name"`
  44. Upgradable bool `json:"upgradable"`
  45. MessagesLimit int64 `json:"messages_limit"`
  46. EmailsLimit int64 `json:"emails_limit"`
  47. AttachmentFileSizeLimit int64 `json:"attachment_file_size_limit"`
  48. AttachmentTotalSizeLimit int64 `json:"attachment_total_size_limit"`
  49. }
  50. type Subscription struct {
  51. ID string `json:"id"`
  52. BaseURL string `json:"base_url"`
  53. Topic string `json:"topic"`
  54. DisplayName string `json:"display_name"`
  55. }
  56. type NotificationPrefs struct {
  57. Sound string `json:"sound,omitempty"`
  58. MinPriority int `json:"min_priority,omitempty"`
  59. DeleteAfter int `json:"delete_after,omitempty"`
  60. }
  61. type Stats struct {
  62. Messages int64
  63. Emails int64
  64. }
  65. // Grant is a struct that represents an access control entry to a topic
  66. type Grant struct {
  67. TopicPattern string // May include wildcard (*)
  68. AllowRead bool
  69. AllowWrite bool
  70. }
  71. // Permission represents a read or write permission to a topic
  72. type Permission int
  73. // Permissions to a topic
  74. const (
  75. PermissionRead = Permission(1)
  76. PermissionWrite = Permission(2)
  77. )
  78. // Role represents a user's role, either admin or regular user
  79. type Role string
  80. // User roles
  81. const (
  82. RoleAdmin = Role("admin")
  83. RoleUser = Role("user")
  84. RoleAnonymous = Role("anonymous")
  85. )
  86. // Everyone is a special username representing anonymous users
  87. const (
  88. Everyone = "*"
  89. )
  90. var (
  91. allowedUsernameRegex = regexp.MustCompile(`^[-_.@a-zA-Z0-9]+$`) // Does not include Everyone (*)
  92. allowedTopicPatternRegex = regexp.MustCompile(`^[-_*A-Za-z0-9]{1,64}$`) // Adds '*' for wildcards!
  93. )
  94. // AllowedRole returns true if the given role can be used for new users
  95. func AllowedRole(role Role) bool {
  96. return role == RoleUser || role == RoleAdmin
  97. }
  98. // AllowedUsername returns true if the given username is valid
  99. func AllowedUsername(username string) bool {
  100. return allowedUsernameRegex.MatchString(username)
  101. }
  102. // AllowedTopicPattern returns true if the given topic pattern is valid; this includes the wildcard character (*)
  103. func AllowedTopicPattern(username string) bool {
  104. return allowedTopicPatternRegex.MatchString(username)
  105. }
  106. // Error constants used by the package
  107. var (
  108. ErrUnauthenticated = errors.New("unauthenticated")
  109. ErrUnauthorized = errors.New("unauthorized")
  110. ErrInvalidArgument = errors.New("invalid argument")
  111. ErrNotFound = errors.New("not found")
  112. )