server_account.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. Basis: "ip",
  42. },
  43. }
  44. if v.user != nil {
  45. response.Username = v.user.Name
  46. response.Role = string(v.user.Role)
  47. if v.user.Prefs != nil {
  48. if v.user.Prefs.Language != "" {
  49. response.Language = v.user.Prefs.Language
  50. }
  51. if v.user.Prefs.Notification != nil {
  52. response.Notification = v.user.Prefs.Notification
  53. }
  54. if v.user.Prefs.Subscriptions != nil {
  55. response.Subscriptions = v.user.Prefs.Subscriptions
  56. }
  57. }
  58. if v.user.Plan != nil {
  59. response.Usage.Basis = "account"
  60. response.Plan = &apiAccountSettingsPlan{
  61. Name: v.user.Plan.Name,
  62. MessagesLimit: v.user.Plan.MessagesLimit,
  63. EmailsLimit: v.user.Plan.EmailsLimit,
  64. AttachmentsBytesLimit: v.user.Plan.AttachmentBytesLimit,
  65. }
  66. }
  67. } else {
  68. response.Username = auth.Everyone
  69. response.Role = string(auth.RoleAnonymous)
  70. }
  71. response.Usage.AttachmentsBytes = stats.VisitorAttachmentBytesUsed
  72. if err := json.NewEncoder(w).Encode(response); err != nil {
  73. return err
  74. }
  75. return nil
  76. }
  77. func (s *Server) handleAccountDelete(w http.ResponseWriter, r *http.Request, v *visitor) error {
  78. if v.user == nil {
  79. return errHTTPUnauthorized
  80. }
  81. if err := s.auth.RemoveUser(v.user.Name); err != nil {
  82. return err
  83. }
  84. w.Header().Set("Content-Type", "application/json")
  85. w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
  86. // FIXME return something
  87. return nil
  88. }
  89. func (s *Server) handleAccountPasswordChange(w http.ResponseWriter, r *http.Request, v *visitor) error {
  90. if v.user == nil {
  91. return errHTTPUnauthorized
  92. }
  93. body, err := util.Peek(r.Body, 4096) // FIXME
  94. if err != nil {
  95. return err
  96. }
  97. defer r.Body.Close()
  98. var newPassword apiAccountCreateRequest // Re-use!
  99. if err := json.NewDecoder(body).Decode(&newPassword); err != nil {
  100. return err
  101. }
  102. if err := s.auth.ChangePassword(v.user.Name, newPassword.Password); err != nil {
  103. return err
  104. }
  105. w.Header().Set("Content-Type", "application/json")
  106. w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
  107. // FIXME return something
  108. return nil
  109. }
  110. func (s *Server) handleAccountTokenGet(w http.ResponseWriter, r *http.Request, v *visitor) error {
  111. // TODO rate limit
  112. if v.user == nil {
  113. return errHTTPUnauthorized
  114. }
  115. token, err := s.auth.CreateToken(v.user)
  116. if err != nil {
  117. return err
  118. }
  119. w.Header().Set("Content-Type", "application/json")
  120. w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
  121. response := &apiAccountTokenResponse{
  122. Token: token,
  123. }
  124. if err := json.NewEncoder(w).Encode(response); err != nil {
  125. return err
  126. }
  127. return nil
  128. }
  129. func (s *Server) handleAccountTokenDelete(w http.ResponseWriter, r *http.Request, v *visitor) error {
  130. // TODO rate limit
  131. if v.user == nil || v.user.Token == "" {
  132. return errHTTPUnauthorized
  133. }
  134. if err := s.auth.RemoveToken(v.user); err != nil {
  135. return err
  136. }
  137. w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
  138. return nil
  139. }
  140. func (s *Server) handleAccountSettingsChange(w http.ResponseWriter, r *http.Request, v *visitor) error {
  141. if v.user == nil {
  142. return errors.New("no user")
  143. }
  144. w.Header().Set("Content-Type", "application/json")
  145. w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
  146. body, err := util.Peek(r.Body, 4096) // FIXME
  147. if err != nil {
  148. return err
  149. }
  150. defer r.Body.Close()
  151. var newPrefs auth.UserPrefs
  152. if err := json.NewDecoder(body).Decode(&newPrefs); err != nil {
  153. return err
  154. }
  155. if v.user.Prefs == nil {
  156. v.user.Prefs = &auth.UserPrefs{}
  157. }
  158. prefs := v.user.Prefs
  159. if newPrefs.Language != "" {
  160. prefs.Language = newPrefs.Language
  161. }
  162. if newPrefs.Notification != nil {
  163. if prefs.Notification == nil {
  164. prefs.Notification = &auth.UserNotificationPrefs{}
  165. }
  166. if newPrefs.Notification.DeleteAfter > 0 {
  167. prefs.Notification.DeleteAfter = newPrefs.Notification.DeleteAfter
  168. }
  169. if newPrefs.Notification.Sound != "" {
  170. prefs.Notification.Sound = newPrefs.Notification.Sound
  171. }
  172. if newPrefs.Notification.MinPriority > 0 {
  173. prefs.Notification.MinPriority = newPrefs.Notification.MinPriority
  174. }
  175. }
  176. return s.auth.ChangeSettings(v.user)
  177. }
  178. func (s *Server) handleAccountSubscriptionAdd(w http.ResponseWriter, r *http.Request, v *visitor) error {
  179. if v.user == nil {
  180. return errors.New("no user")
  181. }
  182. w.Header().Set("Content-Type", "application/json")
  183. w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
  184. body, err := util.Peek(r.Body, 4096) // FIXME
  185. if err != nil {
  186. return err
  187. }
  188. defer r.Body.Close()
  189. var newSubscription auth.UserSubscription
  190. if err := json.NewDecoder(body).Decode(&newSubscription); err != nil {
  191. return err
  192. }
  193. if v.user.Prefs == nil {
  194. v.user.Prefs = &auth.UserPrefs{}
  195. }
  196. newSubscription.ID = "" // Client cannot set ID
  197. for _, subscription := range v.user.Prefs.Subscriptions {
  198. if newSubscription.BaseURL == subscription.BaseURL && newSubscription.Topic == subscription.Topic {
  199. newSubscription = *subscription
  200. break
  201. }
  202. }
  203. if newSubscription.ID == "" {
  204. newSubscription.ID = util.RandomString(16)
  205. v.user.Prefs.Subscriptions = append(v.user.Prefs.Subscriptions, &newSubscription)
  206. if err := s.auth.ChangeSettings(v.user); err != nil {
  207. return err
  208. }
  209. }
  210. if err := json.NewEncoder(w).Encode(newSubscription); err != nil {
  211. return err
  212. }
  213. return nil
  214. }
  215. func (s *Server) handleAccountSubscriptionDelete(w http.ResponseWriter, r *http.Request, v *visitor) error {
  216. if v.user == nil {
  217. return errors.New("no user")
  218. }
  219. w.Header().Set("Content-Type", "application/json")
  220. w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
  221. matches := accountSubscriptionSingleRegex.FindStringSubmatch(r.URL.Path)
  222. if len(matches) != 2 {
  223. return errHTTPInternalErrorInvalidFilePath // FIXME
  224. }
  225. subscriptionID := matches[1]
  226. if v.user.Prefs == nil || v.user.Prefs.Subscriptions == nil {
  227. return nil
  228. }
  229. newSubscriptions := make([]*auth.UserSubscription, 0)
  230. for _, subscription := range v.user.Prefs.Subscriptions {
  231. if subscription.ID != subscriptionID {
  232. newSubscriptions = append(newSubscriptions, subscription)
  233. }
  234. }
  235. if len(newSubscriptions) < len(v.user.Prefs.Subscriptions) {
  236. v.user.Prefs.Subscriptions = newSubscriptions
  237. if err := s.auth.ChangeSettings(v.user); err != nil {
  238. return err
  239. }
  240. }
  241. return nil
  242. }