server_account.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package server
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "heckel.io/ntfy/auth"
  6. "heckel.io/ntfy/util"
  7. "net/http"
  8. )
  9. func (s *Server) handleAccountCreate(w http.ResponseWriter, r *http.Request, v *visitor) error {
  10. signupAllowed := s.config.EnableSignup
  11. admin := v.user != nil && v.user.Role == auth.RoleAdmin
  12. if !signupAllowed && !admin {
  13. return errHTTPUnauthorized
  14. }
  15. body, err := util.Peek(r.Body, 4096) // FIXME
  16. if err != nil {
  17. return err
  18. }
  19. defer r.Body.Close()
  20. var newAccount apiAccountCreateRequest
  21. if err := json.NewDecoder(body).Decode(&newAccount); err != nil {
  22. return err
  23. }
  24. if err := s.auth.AddUser(newAccount.Username, newAccount.Password, auth.RoleUser); err != nil { // TODO this should return a User
  25. return err
  26. }
  27. w.Header().Set("Content-Type", "application/json")
  28. w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
  29. // FIXME return something
  30. return nil
  31. }
  32. func (s *Server) handleAccountGet(w http.ResponseWriter, r *http.Request, v *visitor) error {
  33. w.Header().Set("Content-Type", "application/json")
  34. w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
  35. stats, err := v.Stats()
  36. if err != nil {
  37. return err
  38. }
  39. response := &apiAccountSettingsResponse{
  40. Usage: &apiAccountUsageLimits{},
  41. }
  42. if v.user != nil {
  43. response.Username = v.user.Name
  44. response.Role = string(v.user.Role)
  45. if v.user.Prefs != nil {
  46. if v.user.Prefs.Language != "" {
  47. response.Language = v.user.Prefs.Language
  48. }
  49. if v.user.Prefs.Notification != nil {
  50. response.Notification = v.user.Prefs.Notification
  51. }
  52. if v.user.Prefs.Subscriptions != nil {
  53. response.Subscriptions = v.user.Prefs.Subscriptions
  54. }
  55. }
  56. if v.user.Plan != nil {
  57. response.Usage.Basis = "account"
  58. response.Plan = &apiAccountSettingsPlan{
  59. Name: v.user.Plan.Name,
  60. MessagesLimit: v.user.Plan.MessagesLimit,
  61. EmailsLimit: v.user.Plan.EmailsLimit,
  62. AttachmentsBytesLimit: v.user.Plan.AttachmentBytesLimit,
  63. }
  64. } else {
  65. if v.user.Role == auth.RoleAdmin {
  66. response.Usage.Basis = "account"
  67. response.Plan = &apiAccountSettingsPlan{
  68. Name: "Unlimited",
  69. MessagesLimit: 0,
  70. EmailsLimit: 0,
  71. AttachmentsBytesLimit: 0,
  72. }
  73. } else {
  74. response.Usage.Basis = "ip"
  75. response.Plan = &apiAccountSettingsPlan{
  76. Name: "Free",
  77. MessagesLimit: s.config.VisitorRequestLimitBurst,
  78. EmailsLimit: s.config.VisitorEmailLimitBurst,
  79. AttachmentsBytesLimit: s.config.VisitorAttachmentTotalSizeLimit,
  80. }
  81. }
  82. }
  83. } else {
  84. response.Username = auth.Everyone
  85. response.Role = string(auth.RoleAnonymous)
  86. response.Usage.Basis = "account"
  87. response.Plan = &apiAccountSettingsPlan{
  88. Name: "Anonymous",
  89. MessagesLimit: s.config.VisitorRequestLimitBurst,
  90. EmailsLimit: s.config.VisitorEmailLimitBurst,
  91. AttachmentsBytesLimit: s.config.VisitorAttachmentTotalSizeLimit,
  92. }
  93. }
  94. response.Usage.Messages = int(v.requests.Tokens())
  95. response.Usage.AttachmentsBytes = stats.VisitorAttachmentBytesUsed
  96. if err := json.NewEncoder(w).Encode(response); err != nil {
  97. return err
  98. }
  99. return nil
  100. }
  101. func (s *Server) handleAccountDelete(w http.ResponseWriter, r *http.Request, v *visitor) error {
  102. if v.user == nil {
  103. return errHTTPUnauthorized
  104. }
  105. if err := s.auth.RemoveUser(v.user.Name); err != nil {
  106. return err
  107. }
  108. w.Header().Set("Content-Type", "application/json")
  109. w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
  110. // FIXME return something
  111. return nil
  112. }
  113. func (s *Server) handleAccountPasswordChange(w http.ResponseWriter, r *http.Request, v *visitor) error {
  114. if v.user == nil {
  115. return errHTTPUnauthorized
  116. }
  117. body, err := util.Peek(r.Body, 4096) // FIXME
  118. if err != nil {
  119. return err
  120. }
  121. defer r.Body.Close()
  122. var newPassword apiAccountCreateRequest // Re-use!
  123. if err := json.NewDecoder(body).Decode(&newPassword); err != nil {
  124. return err
  125. }
  126. if err := s.auth.ChangePassword(v.user.Name, newPassword.Password); err != nil {
  127. return err
  128. }
  129. w.Header().Set("Content-Type", "application/json")
  130. w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
  131. // FIXME return something
  132. return nil
  133. }
  134. func (s *Server) handleAccountTokenGet(w http.ResponseWriter, r *http.Request, v *visitor) error {
  135. // TODO rate limit
  136. if v.user == nil {
  137. return errHTTPUnauthorized
  138. }
  139. token, err := s.auth.CreateToken(v.user)
  140. if err != nil {
  141. return err
  142. }
  143. w.Header().Set("Content-Type", "application/json")
  144. w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
  145. response := &apiAccountTokenResponse{
  146. Token: token,
  147. }
  148. if err := json.NewEncoder(w).Encode(response); err != nil {
  149. return err
  150. }
  151. return nil
  152. }
  153. func (s *Server) handleAccountTokenDelete(w http.ResponseWriter, r *http.Request, v *visitor) error {
  154. // TODO rate limit
  155. if v.user == nil || v.user.Token == "" {
  156. return errHTTPUnauthorized
  157. }
  158. if err := s.auth.RemoveToken(v.user); err != nil {
  159. return err
  160. }
  161. w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
  162. return nil
  163. }
  164. func (s *Server) handleAccountSettingsChange(w http.ResponseWriter, r *http.Request, v *visitor) error {
  165. if v.user == nil {
  166. return errors.New("no user")
  167. }
  168. w.Header().Set("Content-Type", "application/json")
  169. w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
  170. body, err := util.Peek(r.Body, 4096) // FIXME
  171. if err != nil {
  172. return err
  173. }
  174. defer r.Body.Close()
  175. var newPrefs auth.UserPrefs
  176. if err := json.NewDecoder(body).Decode(&newPrefs); err != nil {
  177. return err
  178. }
  179. if v.user.Prefs == nil {
  180. v.user.Prefs = &auth.UserPrefs{}
  181. }
  182. prefs := v.user.Prefs
  183. if newPrefs.Language != "" {
  184. prefs.Language = newPrefs.Language
  185. }
  186. if newPrefs.Notification != nil {
  187. if prefs.Notification == nil {
  188. prefs.Notification = &auth.UserNotificationPrefs{}
  189. }
  190. if newPrefs.Notification.DeleteAfter > 0 {
  191. prefs.Notification.DeleteAfter = newPrefs.Notification.DeleteAfter
  192. }
  193. if newPrefs.Notification.Sound != "" {
  194. prefs.Notification.Sound = newPrefs.Notification.Sound
  195. }
  196. if newPrefs.Notification.MinPriority > 0 {
  197. prefs.Notification.MinPriority = newPrefs.Notification.MinPriority
  198. }
  199. }
  200. return s.auth.ChangeSettings(v.user)
  201. }
  202. func (s *Server) handleAccountSubscriptionAdd(w http.ResponseWriter, r *http.Request, v *visitor) error {
  203. if v.user == nil {
  204. return errors.New("no user")
  205. }
  206. w.Header().Set("Content-Type", "application/json")
  207. w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
  208. body, err := util.Peek(r.Body, 4096) // FIXME
  209. if err != nil {
  210. return err
  211. }
  212. defer r.Body.Close()
  213. var newSubscription auth.UserSubscription
  214. if err := json.NewDecoder(body).Decode(&newSubscription); err != nil {
  215. return err
  216. }
  217. if v.user.Prefs == nil {
  218. v.user.Prefs = &auth.UserPrefs{}
  219. }
  220. newSubscription.ID = "" // Client cannot set ID
  221. for _, subscription := range v.user.Prefs.Subscriptions {
  222. if newSubscription.BaseURL == subscription.BaseURL && newSubscription.Topic == subscription.Topic {
  223. newSubscription = *subscription
  224. break
  225. }
  226. }
  227. if newSubscription.ID == "" {
  228. newSubscription.ID = util.RandomString(16)
  229. v.user.Prefs.Subscriptions = append(v.user.Prefs.Subscriptions, &newSubscription)
  230. if err := s.auth.ChangeSettings(v.user); err != nil {
  231. return err
  232. }
  233. }
  234. if err := json.NewEncoder(w).Encode(newSubscription); err != nil {
  235. return err
  236. }
  237. return nil
  238. }
  239. func (s *Server) handleAccountSubscriptionDelete(w http.ResponseWriter, r *http.Request, v *visitor) error {
  240. if v.user == nil {
  241. return errors.New("no user")
  242. }
  243. w.Header().Set("Content-Type", "application/json")
  244. w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
  245. matches := accountSubscriptionSingleRegex.FindStringSubmatch(r.URL.Path)
  246. if len(matches) != 2 {
  247. return errHTTPInternalErrorInvalidFilePath // FIXME
  248. }
  249. subscriptionID := matches[1]
  250. if v.user.Prefs == nil || v.user.Prefs.Subscriptions == nil {
  251. return nil
  252. }
  253. newSubscriptions := make([]*auth.UserSubscription, 0)
  254. for _, subscription := range v.user.Prefs.Subscriptions {
  255. if subscription.ID != subscriptionID {
  256. newSubscriptions = append(newSubscriptions, subscription)
  257. }
  258. }
  259. if len(newSubscriptions) < len(v.user.Prefs.Subscriptions) {
  260. v.user.Prefs.Subscriptions = newSubscriptions
  261. if err := s.auth.ChangeSettings(v.user); err != nil {
  262. return err
  263. }
  264. }
  265. return nil
  266. }