1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-11-01 09:31:18 +01:00
ntfy/server/server_middleware.go

133 lines
3.4 KiB
Go
Raw Normal View History

package server
import (
"net/http"
2023-02-22 05:40:15 +01:00
2023-11-17 02:54:58 +01:00
"heckel.io/ntfy/v2/util"
)
2023-02-23 04:26:43 +01:00
type contextKey int
const (
contextRateVisitor contextKey = iota + 2586
contextTopic
2023-03-04 04:22:07 +01:00
contextMatrixPushKey
2023-02-23 04:26:43 +01:00
)
2023-02-08 21:20:44 +01:00
func (s *Server) limitRequests(next handleFunc) handleFunc {
return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
if util.ContainsIP(s.config.VisitorRequestExemptIPAddrs, v.ip) {
return next(w, r, v)
} else if !v.RequestAllowed() {
return errHTTPTooManyRequestsLimitRequests
}
return next(w, r, v)
}
}
2023-02-22 05:40:15 +01:00
// limitRequestsWithTopic limits requests with a topic and stores the rate-limiting-subscriber and topic into request.Context
func (s *Server) limitRequestsWithTopic(next handleFunc) handleFunc {
return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
t, err := s.topicFromPath(r.URL.Path)
if err != nil {
return err
}
2023-02-23 03:00:56 +01:00
vrate := v
2023-02-23 03:33:18 +01:00
if rateVisitor := t.RateVisitor(); rateVisitor != nil {
vrate = rateVisitor
2023-02-22 05:40:15 +01:00
}
2023-02-23 04:26:43 +01:00
r = withContext(r, map[contextKey]any{
contextRateVisitor: vrate,
contextTopic: t,
})
2023-02-22 05:40:15 +01:00
if util.ContainsIP(s.config.VisitorRequestExemptIPAddrs, v.ip) {
return next(w, r, v)
2023-02-23 03:00:56 +01:00
} else if !vrate.RequestAllowed() {
2023-02-22 05:40:15 +01:00
return errHTTPTooManyRequestsLimitRequests
}
return next(w, r, v)
}
}
func (s *Server) ensureWebEnabled(next handleFunc) handleFunc {
return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
2023-05-01 17:58:49 +02:00
if s.config.WebRoot == "" {
return errHTTPNotFound
}
return next(w, r, v)
}
}
func (s *Server) ensureWebPushEnabled(next handleFunc) handleFunc {
return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
if s.config.WebRoot == "" || s.config.WebPushPublicKey == "" {
return errHTTPNotFound
}
return next(w, r, v)
}
}
func (s *Server) ensureUserManager(next handleFunc) handleFunc {
return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
if s.userManager == nil {
return errHTTPNotFound
}
return next(w, r, v)
}
}
func (s *Server) ensureUser(next handleFunc) handleFunc {
return s.ensureUserManager(func(w http.ResponseWriter, r *http.Request, v *visitor) error {
2023-01-29 02:43:06 +01:00
if v.User() == nil {
return errHTTPUnauthorized
}
return next(w, r, v)
})
}
2023-05-13 20:39:31 +02:00
func (s *Server) ensureAdmin(next handleFunc) handleFunc {
return s.ensureUserManager(func(w http.ResponseWriter, r *http.Request, v *visitor) error {
if !v.User().IsAdmin() {
return errHTTPUnauthorized
}
return next(w, r, v)
})
}
2023-05-16 20:15:58 +02:00
func (s *Server) ensureCallsEnabled(next handleFunc) handleFunc {
return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
2023-05-17 17:19:48 +02:00
if s.config.TwilioAccount == "" || s.userManager == nil {
2023-05-16 20:15:58 +02:00
return errHTTPNotFound
}
return next(w, r, v)
}
}
func (s *Server) ensurePaymentsEnabled(next handleFunc) handleFunc {
return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
2023-01-19 05:01:26 +01:00
if s.config.StripeSecretKey == "" || s.stripe == nil {
return errHTTPNotFound
}
return next(w, r, v)
}
}
func (s *Server) ensureStripeCustomer(next handleFunc) handleFunc {
return s.ensureUser(func(w http.ResponseWriter, r *http.Request, v *visitor) error {
2023-01-29 02:43:06 +01:00
if v.User().Billing.StripeCustomerID == "" {
return errHTTPBadRequestNotAPaidUser
}
return next(w, r, v)
})
}
func (s *Server) withAccountSync(next handleFunc) handleFunc {
return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
err := next(w, r, v)
if err == nil {
s.publishSyncEventAsync(v)
}
return err
}
}