2022-12-16 04:07:04 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-01-14 12:43:44 +01:00
|
|
|
"errors"
|
|
|
|
"github.com/stripe/stripe-go/v74"
|
|
|
|
portalsession "github.com/stripe/stripe-go/v74/billingportal/session"
|
|
|
|
"github.com/stripe/stripe-go/v74/checkout/session"
|
|
|
|
"github.com/stripe/stripe-go/v74/subscription"
|
|
|
|
"github.com/stripe/stripe-go/v74/webhook"
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
"heckel.io/ntfy/log"
|
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 (
|
2022-12-29 17:09:45 +01:00
|
|
|
jsonBodyBytesLimit = 4096
|
2023-01-14 12:43:44 +01:00
|
|
|
stripeBodyBytesLimit = 16384
|
2022-12-29 17:09:45 +01:00
|
|
|
subscriptionIDLength = 16
|
2023-01-10 03:53:21 +01:00
|
|
|
createdByAPI = "api"
|
2022-12-29 01:55:11 +01:00
|
|
|
)
|
|
|
|
|
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 15:57:42 +01:00
|
|
|
newAccount, err := 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() {
|
2023-01-06 03:15:10 +01:00
|
|
|
return errHTTPTooManyRequestsLimitAccountCreation
|
2022-12-24 18:10:51 +01:00
|
|
|
}
|
2023-01-10 03:53:21 +01:00
|
|
|
if err := s.userManager.AddUser(newAccount.Username, newAccount.Password, user.RoleUser, createdByAPI); 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
|
|
|
|
}
|
|
|
|
|
2023-01-02 02:42:33 +01:00
|
|
|
func (s *Server) handleAccountGet(w http.ResponseWriter, _ *http.Request, v *visitor) error {
|
2023-01-09 21:40:46 +01:00
|
|
|
info, err := v.Info()
|
2022-12-17 21:17:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-09 21:40:46 +01:00
|
|
|
limits, stats := info.Limits, info.Stats
|
2023-01-12 03:38:10 +01:00
|
|
|
|
2022-12-28 04:14:14 +01:00
|
|
|
response := &apiAccountResponse{
|
2023-01-09 21:40:46 +01:00
|
|
|
Limits: &apiAccountLimits{
|
|
|
|
Basis: string(limits.Basis),
|
|
|
|
Messages: limits.MessagesLimit,
|
|
|
|
MessagesExpiryDuration: int64(limits.MessagesExpiryDuration.Seconds()),
|
|
|
|
Emails: limits.EmailsLimit,
|
|
|
|
Reservations: limits.ReservationsLimit,
|
|
|
|
AttachmentTotalSize: limits.AttachmentTotalSizeLimit,
|
|
|
|
AttachmentFileSize: limits.AttachmentFileSizeLimit,
|
|
|
|
AttachmentExpiryDuration: int64(limits.AttachmentExpiryDuration.Seconds()),
|
|
|
|
},
|
2022-12-19 22:22:13 +01:00
|
|
|
Stats: &apiAccountStats{
|
|
|
|
Messages: stats.Messages,
|
|
|
|
MessagesRemaining: stats.MessagesRemaining,
|
|
|
|
Emails: stats.Emails,
|
|
|
|
EmailsRemaining: stats.EmailsRemaining,
|
2023-01-08 03:04:13 +01:00
|
|
|
Reservations: stats.Reservations,
|
|
|
|
ReservationsRemaining: stats.ReservationsRemaining,
|
2022-12-19 22:22:13 +01:00
|
|
|
AttachmentTotalSize: stats.AttachmentTotalSize,
|
|
|
|
AttachmentTotalSizeRemaining: stats.AttachmentTotalSizeRemaining,
|
|
|
|
},
|
2022-12-17 21:17:52 +01:00
|
|
|
}
|
|
|
|
if v.user != nil {
|
|
|
|
response.Username = v.user.Name
|
|
|
|
response.Role = string(v.user.Role)
|
2023-01-10 03:53:21 +01:00
|
|
|
response.SyncTopic = v.user.SyncTopic
|
2022-12-17 21:17:52 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2023-01-08 03:04:13 +01:00
|
|
|
if v.user.Tier != nil {
|
|
|
|
response.Tier = &apiAccountTier{
|
2023-01-09 21:40:46 +01:00
|
|
|
Code: v.user.Tier.Code,
|
|
|
|
Name: v.user.Tier.Name,
|
|
|
|
Paid: v.user.Tier.Paid,
|
2022-12-18 05:54:19 +01:00
|
|
|
}
|
2022-12-17 21:17:52 +01:00
|
|
|
}
|
2023-01-03 02:08:37 +01:00
|
|
|
reservations, err := s.userManager.Reservations(v.user.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(reservations) > 0 {
|
|
|
|
response.Reservations = make([]*apiAccountReservation, 0)
|
|
|
|
for _, r := range reservations {
|
|
|
|
response.Reservations = append(response.Reservations, &apiAccountReservation{
|
2023-01-03 03:12:42 +01:00
|
|
|
Topic: r.Topic,
|
|
|
|
Everyone: r.Everyone.String(),
|
2023-01-03 02:08:37 +01:00
|
|
|
})
|
2023-01-01 21:21:43 +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-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
|
|
|
|
}
|
|
|
|
|
2023-01-02 02:42:33 +01:00
|
|
|
func (s *Server) handleAccountDelete(w http.ResponseWriter, _ *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 15:57:42 +01:00
|
|
|
newPassword, err := 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
|
|
|
|
}
|
|
|
|
|
2023-01-02 02:42:33 +01:00
|
|
|
func (s *Server) handleAccountTokenIssue(w http.ResponseWriter, _ *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
|
|
|
|
}
|
|
|
|
|
2023-01-02 02:42:33 +01:00
|
|
|
func (s *Server) handleAccountTokenExtend(w http.ResponseWriter, _ *http.Request, v *visitor) error {
|
2022-12-25 17:41:38 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-01-02 02:42:33 +01:00
|
|
|
func (s *Server) handleAccountTokenDelete(w http.ResponseWriter, _ *http.Request, v *visitor) error {
|
2022-12-16 04:07:04 +01:00
|
|
|
// TODO rate limit
|
2022-12-29 01:55:11 +01:00
|
|
|
if v.user.Token == "" {
|
2022-12-29 15:57:42 +01:00
|
|
|
return errHTTPBadRequestNoTokenProvided
|
2022-12-16 04:07:04 +01:00
|
|
|
}
|
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 15:57:42 +01:00
|
|
|
newPrefs, err := 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 15:57:42 +01:00
|
|
|
newSubscription, err := 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 == "" {
|
2022-12-29 17:09:45 +01:00
|
|
|
newSubscription.ID = util.RandomString(subscriptionIDLength)
|
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 {
|
2022-12-29 15:57:42 +01:00
|
|
|
return errHTTPInternalErrorInvalidPath
|
2022-12-26 04:29:55 +01:00
|
|
|
}
|
2022-12-29 15:57:42 +01:00
|
|
|
subscriptionID := matches[1]
|
|
|
|
updatedSubscription, err := readJSONWithLimit[user.Subscription](r.Body, jsonBodyBytesLimit)
|
2022-12-26 04:29:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
matches := accountSubscriptionSingleRegex.FindStringSubmatch(r.URL.Path)
|
|
|
|
if len(matches) != 2 {
|
2022-12-29 15:57:42 +01:00
|
|
|
return errHTTPInternalErrorInvalidPath
|
2022-12-16 04:07:04 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2022-12-29 15:57:42 +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
|
|
|
return nil
|
|
|
|
}
|
2022-12-30 20:20:48 +01:00
|
|
|
|
2023-01-12 16:50:09 +01:00
|
|
|
func (s *Server) handleAccountReservationAdd(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
2023-01-07 15:34:02 +01:00
|
|
|
if v.user != nil && v.user.Role == user.RoleAdmin {
|
|
|
|
return errHTTPBadRequestMakesNoSenseForAdmin
|
|
|
|
}
|
2023-01-12 18:04:18 +01:00
|
|
|
req, err := readJSONWithLimit[apiAccountReservationRequest](r.Body, jsonBodyBytesLimit)
|
2022-12-30 20:20:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !topicRegex.MatchString(req.Topic) {
|
|
|
|
return errHTTPBadRequestTopicInvalid
|
|
|
|
}
|
2023-01-06 16:45:38 +01:00
|
|
|
everyone, err := user.ParsePermission(req.Everyone)
|
|
|
|
if err != nil {
|
|
|
|
return errHTTPBadRequestPermissionInvalid
|
|
|
|
}
|
2023-01-08 03:04:13 +01:00
|
|
|
if v.user.Tier == nil {
|
2023-01-12 16:50:09 +01:00
|
|
|
return errHTTPUnauthorized
|
2023-01-06 03:15:10 +01:00
|
|
|
}
|
2023-01-01 21:21:43 +01:00
|
|
|
if err := s.userManager.CheckAllowAccess(v.user.Name, req.Topic); err != nil {
|
|
|
|
return errHTTPConflictTopicReserved
|
|
|
|
}
|
2023-01-06 16:45:38 +01:00
|
|
|
hasReservation, err := s.userManager.HasReservation(v.user.Name, req.Topic)
|
2023-01-03 03:12:42 +01:00
|
|
|
if err != nil {
|
2023-01-06 16:45:38 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !hasReservation {
|
|
|
|
reservations, err := s.userManager.ReservationsCount(v.user.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-01-08 03:04:13 +01:00
|
|
|
} else if reservations >= v.user.Tier.ReservationsLimit {
|
2023-01-06 16:45:38 +01:00
|
|
|
return errHTTPTooManyRequestsLimitReservations
|
|
|
|
}
|
2023-01-03 03:12:42 +01:00
|
|
|
}
|
2023-01-06 16:45:38 +01:00
|
|
|
owner, username := v.user.Name, v.user.Name
|
2023-01-01 21:21:43 +01:00
|
|
|
if err := s.userManager.AllowAccess(owner, username, req.Topic, true, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-03 03:12:42 +01:00
|
|
|
if err := s.userManager.AllowAccess(owner, user.Everyone, req.Topic, everyone.IsRead(), everyone.IsWrite()); err != nil {
|
2023-01-01 21:21:43 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-12 16:50:09 +01:00
|
|
|
func (s *Server) handleAccountReservationDelete(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
|
|
|
matches := accountReservationSingleRegex.FindStringSubmatch(r.URL.Path)
|
2023-01-01 21:21:43 +01:00
|
|
|
if len(matches) != 2 {
|
|
|
|
return errHTTPInternalErrorInvalidPath
|
|
|
|
}
|
|
|
|
topic := matches[1]
|
|
|
|
if !topicRegex.MatchString(topic) {
|
|
|
|
return errHTTPBadRequestTopicInvalid
|
|
|
|
}
|
2023-01-06 16:45:38 +01:00
|
|
|
authorized, err := s.userManager.HasReservation(v.user.Name, topic)
|
2023-01-03 02:08:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-01-06 16:45:38 +01:00
|
|
|
} else if !authorized {
|
2023-01-01 21:21:43 +01:00
|
|
|
return errHTTPUnauthorized
|
|
|
|
}
|
|
|
|
if err := s.userManager.ResetAccess(v.user.Name, topic); err != nil {
|
2022-12-30 20:20:48 +01:00
|
|
|
return err
|
|
|
|
}
|
2023-01-01 21:21:43 +01:00
|
|
|
if err := s.userManager.ResetAccess(user.Everyone, topic); err != nil {
|
2022-12-30 20:20:48 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
|
|
|
|
return nil
|
|
|
|
}
|
2023-01-14 12:43:44 +01:00
|
|
|
|
|
|
|
func (s *Server) handleAccountCheckoutSessionCreate(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
|
|
|
req, err := readJSONWithLimit[apiAccountTierChangeRequest](r.Body, jsonBodyBytesLimit)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tier, err := s.userManager.Tier(req.Tier)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if tier.StripePriceID == "" {
|
|
|
|
log.Info("Checkout: Downgrading to no tier")
|
|
|
|
return errors.New("not a paid tier")
|
|
|
|
} else if v.user.Billing != nil && v.user.Billing.StripeSubscriptionID != "" {
|
|
|
|
log.Info("Checkout: Changing tier and subscription to %s", tier.Code)
|
|
|
|
|
|
|
|
// Upgrade/downgrade tier
|
|
|
|
sub, err := subscription.Get(v.user.Billing.StripeSubscriptionID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
params := &stripe.SubscriptionParams{
|
|
|
|
CancelAtPeriodEnd: stripe.Bool(false),
|
|
|
|
ProrationBehavior: stripe.String(string(stripe.SubscriptionSchedulePhaseProrationBehaviorCreateProrations)),
|
|
|
|
Items: []*stripe.SubscriptionItemsParams{
|
|
|
|
{
|
|
|
|
ID: stripe.String(sub.Items.Data[0].ID),
|
|
|
|
Price: stripe.String(tier.StripePriceID),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err = subscription.Update(sub.ID, params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
response := &apiAccountCheckoutResponse{}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
|
|
|
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
// Checkout flow
|
|
|
|
log.Info("Checkout: No existing subscription, creating checkout flow")
|
|
|
|
}
|
|
|
|
|
|
|
|
successURL := s.config.BaseURL + accountCheckoutSuccessTemplate
|
|
|
|
var stripeCustomerID *string
|
|
|
|
if v.user.Billing != nil {
|
|
|
|
stripeCustomerID = &v.user.Billing.StripeCustomerID
|
|
|
|
}
|
|
|
|
params := &stripe.CheckoutSessionParams{
|
|
|
|
ClientReferenceID: &v.user.Name, // FIXME Should be user ID
|
|
|
|
Customer: stripeCustomerID,
|
|
|
|
SuccessURL: &successURL,
|
|
|
|
Mode: stripe.String(string(stripe.CheckoutSessionModeSubscription)),
|
|
|
|
LineItems: []*stripe.CheckoutSessionLineItemParams{
|
|
|
|
{
|
|
|
|
Price: stripe.String(tier.StripePriceID),
|
|
|
|
Quantity: stripe.Int64(1),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
sess, err := session.New(params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
response := &apiAccountCheckoutResponse{
|
|
|
|
RedirectURL: sess.URL,
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
|
|
|
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleAccountCheckoutSessionSuccessGet(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
|
|
|
// We don't have a v.user in this endpoint, only a userManager!
|
|
|
|
matches := accountCheckoutSuccessRegex.FindStringSubmatch(r.URL.Path)
|
|
|
|
if len(matches) != 2 {
|
|
|
|
return errHTTPInternalErrorInvalidPath
|
|
|
|
}
|
|
|
|
sessionID := matches[1]
|
|
|
|
// FIXME how do I rate limit this?
|
|
|
|
sess, err := session.Get(sessionID, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("Stripe: %s", err)
|
|
|
|
return errHTTPBadRequestInvalidStripeRequest
|
|
|
|
} else if sess.Customer == nil || sess.Subscription == nil || sess.ClientReferenceID == "" {
|
|
|
|
log.Warn("Stripe: Unexpected session, customer or subscription not found")
|
|
|
|
return errHTTPBadRequestInvalidStripeRequest
|
|
|
|
}
|
|
|
|
sub, err := subscription.Get(sess.Subscription.ID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if sub.Items == nil || len(sub.Items.Data) != 1 || sub.Items.Data[0].Price == nil {
|
|
|
|
log.Error("Stripe: Unexpected subscription, expected exactly one line item")
|
|
|
|
return errHTTPBadRequestInvalidStripeRequest
|
|
|
|
}
|
|
|
|
priceID := sub.Items.Data[0].Price.ID
|
|
|
|
tier, err := s.userManager.TierByStripePrice(priceID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u, err := s.userManager.User(sess.ClientReferenceID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if u.Billing == nil {
|
|
|
|
u.Billing = &user.Billing{}
|
|
|
|
}
|
|
|
|
u.Billing.StripeCustomerID = sess.Customer.ID
|
|
|
|
u.Billing.StripeSubscriptionID = sess.Subscription.ID
|
|
|
|
if err := s.userManager.ChangeBilling(u); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.userManager.ChangeTier(u.Name, tier.Code); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
accountURL := s.config.BaseURL + "/account" // FIXME
|
|
|
|
http.Redirect(w, r, accountURL, http.StatusSeeOther)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleAccountBillingPortalSessionCreate(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
|
|
|
if v.user.Billing == nil {
|
|
|
|
return errHTTPBadRequestNotAPaidUser
|
|
|
|
}
|
|
|
|
params := &stripe.BillingPortalSessionParams{
|
|
|
|
Customer: stripe.String(v.user.Billing.StripeCustomerID),
|
|
|
|
ReturnURL: stripe.String(s.config.BaseURL),
|
|
|
|
}
|
|
|
|
ps, err := portalsession.New(params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
response := &apiAccountBillingPortalRedirectResponse{
|
|
|
|
RedirectURL: ps.URL,
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // FIXME remove this
|
|
|
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleAccountBillingWebhookTrigger(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
|
|
|
// We don't have a v.user in this endpoint, only a userManager!
|
|
|
|
stripeSignature := r.Header.Get("Stripe-Signature")
|
|
|
|
if stripeSignature == "" {
|
|
|
|
return errHTTPBadRequestInvalidStripeRequest
|
|
|
|
}
|
|
|
|
body, err := util.Peek(r.Body, stripeBodyBytesLimit)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if body.LimitReached {
|
|
|
|
return errHTTPEntityTooLargeJSONBody
|
|
|
|
}
|
|
|
|
event, err := webhook.ConstructEvent(body.PeekedBytes, stripeSignature, s.config.StripeWebhookKey)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("Stripe: invalid request: %s", err.Error())
|
|
|
|
return errHTTPBadRequestInvalidStripeRequest
|
|
|
|
} else if event.Data == nil || event.Data.Raw == nil {
|
|
|
|
log.Warn("Stripe: invalid request, data is nil")
|
|
|
|
return errHTTPBadRequestInvalidStripeRequest
|
|
|
|
}
|
|
|
|
log.Info("Stripe: webhook event %s received", event.Type)
|
|
|
|
stripeCustomerID := gjson.GetBytes(event.Data.Raw, "customer")
|
|
|
|
if !stripeCustomerID.Exists() {
|
|
|
|
return errHTTPBadRequestInvalidStripeRequest
|
|
|
|
}
|
|
|
|
switch event.Type {
|
|
|
|
case "checkout.session.completed":
|
|
|
|
// Payment is successful and the subscription is created.
|
|
|
|
// Provision the subscription, save the customer ID.
|
|
|
|
return s.handleAccountBillingWebhookCheckoutCompleted(stripeCustomerID.String(), event.Data.Raw)
|
|
|
|
case "customer.subscription.updated":
|
|
|
|
return s.handleAccountBillingWebhookSubscriptionUpdated(stripeCustomerID.String(), event.Data.Raw)
|
|
|
|
case "invoice.paid":
|
|
|
|
// Continue to provision the subscription as payments continue to be made.
|
|
|
|
// Store the status in your database and check when a user accesses your service.
|
|
|
|
// This approach helps you avoid hitting rate limits.
|
|
|
|
return nil // FIXME
|
|
|
|
case "invoice.payment_failed":
|
|
|
|
// The payment failed or the customer does not have a valid payment method.
|
|
|
|
// The subscription becomes past_due. Notify your customer and send them to the
|
|
|
|
// customer portal to update their payment information.
|
|
|
|
return nil // FIXME
|
|
|
|
default:
|
|
|
|
log.Warn("Stripe: unhandled webhook %s", event.Type)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleAccountBillingWebhookCheckoutCompleted(stripeCustomerID string, event json.RawMessage) error {
|
|
|
|
log.Info("Stripe: checkout completed for customer %s", stripeCustomerID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleAccountBillingWebhookSubscriptionUpdated(stripeCustomerID string, event json.RawMessage) error {
|
|
|
|
status := gjson.GetBytes(event, "status")
|
|
|
|
priceID := gjson.GetBytes(event, "items.data.0.price.id")
|
|
|
|
if !status.Exists() || !priceID.Exists() {
|
|
|
|
return errHTTPBadRequestInvalidStripeRequest
|
|
|
|
}
|
|
|
|
log.Info("Stripe: customer %s: subscription updated to %s, with price %s", stripeCustomerID, status, priceID)
|
|
|
|
u, err := s.userManager.UserByStripeCustomer(stripeCustomerID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tier, err := s.userManager.TierByStripePrice(priceID.String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.userManager.ChangeTier(u.Name, tier.Code); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|