2022-12-16 04:07:04 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-12-25 17:41:38 +01:00
|
|
|
"heckel.io/ntfy/user"
|
2022-12-16 04:07:04 +01:00
|
|
|
"heckel.io/ntfy/util"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2022-12-29 01:55:11 +01:00
|
|
|
const (
|
|
|
|
jsonBodyBytesLimit = 4096
|
|
|
|
)
|
|
|
|
|
2022-12-16 04:07:04 +01:00
|
|
|
func (s *Server) handleAccountCreate(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
2022-12-25 17:41:38 +01:00
|
|
|
admin := v.user != nil && v.user.Role == user.RoleAdmin
|
2022-12-24 18:10:51 +01:00
|
|
|
if !admin {
|
|
|
|
if !s.config.EnableSignup {
|
|
|
|
return errHTTPBadRequestSignupNotEnabled
|
|
|
|
} else if v.user != nil {
|
|
|
|
return errHTTPUnauthorized // Cannot create account from user context
|
|
|
|
}
|
2022-12-16 04:07:04 +01:00
|
|
|
}
|
2022-12-29 01:55:11 +01:00
|
|
|
newAccount, err := util.ReadJSONWithLimit[apiAccountCreateRequest](r.Body, jsonBodyBytesLimit)
|
2022-12-16 04:07:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-25 17:41:38 +01:00
|
|
|
if existingUser, _ := s.userManager.User(newAccount.Username); existingUser != nil {
|
2022-12-22 03:55:39 +01:00
|
|
|
return errHTTPConflictUserExists
|
|
|
|
}
|
2022-12-24 18:10:51 +01:00
|
|
|
if v.accountLimiter != nil && !v.accountLimiter.Allow() {
|
|
|
|
return errHTTPTooManyRequestsAccountCreateLimit
|
|
|
|
}
|
2022-12-25 17:41:38 +01:00
|
|
|
if err := s.userManager.AddUser(newAccount.Username, newAccount.Password, user.RoleUser); err != nil { // TODO this should return a User
|
2022-12-16 04:07:04 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-17 21:17:52 +01:00
|
|
|
func (s *Server) handleAccountGet(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
2022-12-28 04:14:14 +01:00
|
|
|
stats, err := v.Info()
|
2022-12-17 21:17:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-28 04:14:14 +01:00
|
|
|
response := &apiAccountResponse{
|
2022-12-19 22:22:13 +01:00
|
|
|
Stats: &apiAccountStats{
|
|
|
|
Messages: stats.Messages,
|
|
|
|
MessagesRemaining: stats.MessagesRemaining,
|
|
|
|
Emails: stats.Emails,
|
|
|
|
EmailsRemaining: stats.EmailsRemaining,
|
|
|
|
AttachmentTotalSize: stats.AttachmentTotalSize,
|
|
|
|
AttachmentTotalSizeRemaining: stats.AttachmentTotalSizeRemaining,
|
|
|
|
},
|
|
|
|
Limits: &apiAccountLimits{
|
|
|
|
Basis: stats.Basis,
|
|
|
|
Messages: stats.MessagesLimit,
|
|
|
|
Emails: stats.EmailsLimit,
|
|
|
|
AttachmentTotalSize: stats.AttachmentTotalSizeLimit,
|
|
|
|
AttachmentFileSize: stats.AttachmentFileSizeLimit,
|
|
|
|
},
|
2022-12-17 21:17:52 +01:00
|
|
|
}
|
|
|
|
if v.user != nil {
|
|
|
|
response.Username = v.user.Name
|
|
|
|
response.Role = string(v.user.Role)
|
|
|
|
if v.user.Prefs != nil {
|
|
|
|
if v.user.Prefs.Language != "" {
|
|
|
|
response.Language = v.user.Prefs.Language
|
|
|
|
}
|
|
|
|
if v.user.Prefs.Notification != nil {
|
|
|
|
response.Notification = v.user.Prefs.Notification
|
|
|
|
}
|
|
|
|
if v.user.Prefs.Subscriptions != nil {
|
|
|
|
response.Subscriptions = v.user.Prefs.Subscriptions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v.user.Plan != nil {
|
2022-12-19 22:22:13 +01:00
|
|
|
response.Plan = &apiAccountPlan{
|
2022-12-19 15:59:32 +01:00
|
|
|
Code: v.user.Plan.Code,
|
|
|
|
Upgradable: v.user.Plan.Upgradable,
|
|
|
|
}
|
2022-12-25 17:41:38 +01:00
|
|
|
} else if v.user.Role == user.RoleAdmin {
|
2022-12-20 03:42:36 +01:00
|
|
|
response.Plan = &apiAccountPlan{
|
2022-12-25 17:41:38 +01:00
|
|
|
Code: string(user.PlanUnlimited),
|
2022-12-20 03:42:36 +01:00
|
|
|
Upgradable: false,
|
|
|
|
}
|
2022-12-18 05:54:19 +01:00
|
|
|
} else {
|
2022-12-20 03:42:36 +01:00
|
|
|
response.Plan = &apiAccountPlan{
|
2022-12-25 17:41:38 +01:00
|
|
|
Code: string(user.PlanDefault),
|
2022-12-20 03:42:36 +01:00
|
|
|
Upgradable: true,
|
2022-12-18 05:54:19 +01:00
|
|
|
}
|
2022-12-17 21:17:52 +01:00
|
|
|
}
|
2022-12-20 03:42:36 +01:00
|
|
|
|
2022-12-17 21:17:52 +01:00
|
|
|
} else {
|
2022-12-25 17:41:38 +01:00
|
|
|
response.Username = user.Everyone
|
|
|
|
response.Role = string(user.RoleAnonymous)
|
2022-12-19 22:22:13 +01:00
|
|
|
response.Plan = &apiAccountPlan{
|
2022-12-25 17:41:38 +01:00
|
|
|
Code: string(user.PlanNone),
|
2022-12-19 15:59:32 +01:00
|
|
|
Upgradable: true,
|
|
|
|
}
|
2022-12-17 21:17:52 +01:00
|
|
|
}
|
2022-12-29 01:55:11 +01:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
|
2022-12-17 21:17:52 +01:00
|
|
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-16 04:07:04 +01:00
|
|
|
func (s *Server) handleAccountDelete(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
2022-12-25 17:41:38 +01:00
|
|
|
if err := s.userManager.RemoveUser(v.user.Name); err != nil {
|
2022-12-16 04:07:04 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleAccountPasswordChange(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
2022-12-29 04:16:11 +01:00
|
|
|
newPassword, err := util.ReadJSONWithLimit[apiAccountPasswordChangeRequest](r.Body, jsonBodyBytesLimit)
|
2022-12-16 04:07:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-25 17:41:38 +01:00
|
|
|
if err := s.userManager.ChangePassword(v.user.Name, newPassword.Password); err != nil {
|
2022-12-16 04:07:04 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-25 17:41:38 +01:00
|
|
|
func (s *Server) handleAccountTokenIssue(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
2022-12-16 04:07:04 +01:00
|
|
|
// TODO rate limit
|
2022-12-25 17:41:38 +01:00
|
|
|
token, err := s.userManager.CreateToken(v.user)
|
2022-12-16 04:07:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
|
|
|
|
response := &apiAccountTokenResponse{
|
2022-12-25 17:41:38 +01:00
|
|
|
Token: token.Value,
|
2022-12-28 19:46:18 +01:00
|
|
|
Expires: token.Expires.Unix(),
|
2022-12-25 17:41:38 +01:00
|
|
|
}
|
|
|
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleAccountTokenExtend(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
|
|
|
// TODO rate limit
|
|
|
|
if v.user == nil {
|
|
|
|
return errHTTPUnauthorized
|
|
|
|
} else if v.user.Token == "" {
|
|
|
|
return errHTTPBadRequestNoTokenProvided
|
|
|
|
}
|
|
|
|
token, err := s.userManager.ExtendToken(v.user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
|
|
|
|
response := &apiAccountTokenResponse{
|
|
|
|
Token: token.Value,
|
2022-12-28 19:46:18 +01:00
|
|
|
Expires: token.Expires.Unix(),
|
2022-12-16 04:07:04 +01:00
|
|
|
}
|
|
|
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleAccountTokenDelete(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
|
|
|
// TODO rate limit
|
2022-12-29 01:55:11 +01:00
|
|
|
if v.user.Token == "" {
|
2022-12-16 04:07:04 +01:00
|
|
|
return errHTTPUnauthorized
|
|
|
|
}
|
2022-12-25 17:41:38 +01:00
|
|
|
if err := s.userManager.RemoveToken(v.user); err != nil {
|
2022-12-16 04:07:04 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleAccountSettingsChange(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
2022-12-29 01:55:11 +01:00
|
|
|
newPrefs, err := util.ReadJSONWithLimit[user.Prefs](r.Body, jsonBodyBytesLimit)
|
2022-12-16 04:07:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if v.user.Prefs == nil {
|
2022-12-25 17:41:38 +01:00
|
|
|
v.user.Prefs = &user.Prefs{}
|
2022-12-16 04:07:04 +01:00
|
|
|
}
|
|
|
|
prefs := v.user.Prefs
|
|
|
|
if newPrefs.Language != "" {
|
|
|
|
prefs.Language = newPrefs.Language
|
|
|
|
}
|
|
|
|
if newPrefs.Notification != nil {
|
|
|
|
if prefs.Notification == nil {
|
2022-12-25 17:41:38 +01:00
|
|
|
prefs.Notification = &user.NotificationPrefs{}
|
2022-12-16 04:07:04 +01:00
|
|
|
}
|
|
|
|
if newPrefs.Notification.DeleteAfter > 0 {
|
|
|
|
prefs.Notification.DeleteAfter = newPrefs.Notification.DeleteAfter
|
|
|
|
}
|
|
|
|
if newPrefs.Notification.Sound != "" {
|
|
|
|
prefs.Notification.Sound = newPrefs.Notification.Sound
|
|
|
|
}
|
|
|
|
if newPrefs.Notification.MinPriority > 0 {
|
|
|
|
prefs.Notification.MinPriority = newPrefs.Notification.MinPriority
|
|
|
|
}
|
|
|
|
}
|
2022-12-29 01:55:11 +01:00
|
|
|
if err := s.userManager.ChangeSettings(v.user); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
|
|
|
|
return nil
|
2022-12-16 04:07:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleAccountSubscriptionAdd(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
2022-12-29 01:55:11 +01:00
|
|
|
newSubscription, err := util.ReadJSONWithLimit[user.Subscription](r.Body, jsonBodyBytesLimit)
|
2022-12-16 04:07:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if v.user.Prefs == nil {
|
2022-12-25 17:41:38 +01:00
|
|
|
v.user.Prefs = &user.Prefs{}
|
2022-12-16 04:07:04 +01:00
|
|
|
}
|
|
|
|
newSubscription.ID = "" // Client cannot set ID
|
|
|
|
for _, subscription := range v.user.Prefs.Subscriptions {
|
|
|
|
if newSubscription.BaseURL == subscription.BaseURL && newSubscription.Topic == subscription.Topic {
|
2022-12-26 04:29:55 +01:00
|
|
|
newSubscription = subscription
|
2022-12-16 04:07:04 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if newSubscription.ID == "" {
|
|
|
|
newSubscription.ID = util.RandomString(16)
|
2022-12-26 04:29:55 +01:00
|
|
|
v.user.Prefs.Subscriptions = append(v.user.Prefs.Subscriptions, newSubscription)
|
2022-12-25 17:41:38 +01:00
|
|
|
if err := s.userManager.ChangeSettings(v.user); err != nil {
|
2022-12-16 04:07:04 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-12-26 04:29:55 +01:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
|
2022-12-16 04:07:04 +01:00
|
|
|
if err := json.NewEncoder(w).Encode(newSubscription); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-26 04:29:55 +01:00
|
|
|
func (s *Server) handleAccountSubscriptionChange(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
|
|
|
matches := accountSubscriptionSingleRegex.FindStringSubmatch(r.URL.Path)
|
|
|
|
if len(matches) != 2 {
|
|
|
|
return errHTTPInternalErrorInvalidFilePath // FIXME
|
|
|
|
}
|
2022-12-29 01:55:11 +01:00
|
|
|
updatedSubscription, err := util.ReadJSONWithLimit[user.Subscription](r.Body, jsonBodyBytesLimit)
|
2022-12-26 04:29:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
subscriptionID := matches[1]
|
|
|
|
if v.user.Prefs == nil || v.user.Prefs.Subscriptions == nil {
|
|
|
|
return errHTTPNotFound
|
|
|
|
}
|
|
|
|
var subscription *user.Subscription
|
|
|
|
for _, sub := range v.user.Prefs.Subscriptions {
|
|
|
|
if sub.ID == subscriptionID {
|
|
|
|
sub.DisplayName = updatedSubscription.DisplayName
|
|
|
|
subscription = sub
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if subscription == nil {
|
|
|
|
return errHTTPNotFound
|
|
|
|
}
|
|
|
|
if err := s.userManager.ChangeSettings(v.user); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
|
|
|
|
if err := json.NewEncoder(w).Encode(subscription); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-16 04:07:04 +01:00
|
|
|
func (s *Server) handleAccountSubscriptionDelete(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
|
|
|
if v.user == nil {
|
|
|
|
return errors.New("no user")
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
|
|
|
|
matches := accountSubscriptionSingleRegex.FindStringSubmatch(r.URL.Path)
|
|
|
|
if len(matches) != 2 {
|
|
|
|
return errHTTPInternalErrorInvalidFilePath // FIXME
|
|
|
|
}
|
|
|
|
subscriptionID := matches[1]
|
|
|
|
if v.user.Prefs == nil || v.user.Prefs.Subscriptions == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-12-25 17:41:38 +01:00
|
|
|
newSubscriptions := make([]*user.Subscription, 0)
|
2022-12-16 04:07:04 +01:00
|
|
|
for _, subscription := range v.user.Prefs.Subscriptions {
|
|
|
|
if subscription.ID != subscriptionID {
|
|
|
|
newSubscriptions = append(newSubscriptions, subscription)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(newSubscriptions) < len(v.user.Prefs.Subscriptions) {
|
|
|
|
v.user.Prefs.Subscriptions = newSubscriptions
|
2022-12-25 17:41:38 +01:00
|
|
|
if err := s.userManager.ChangeSettings(v.user); err != nil {
|
2022-12-16 04:07:04 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|