Debian Codegraph @

server/server_middleware.go

raw
1 package server
2
3 import (
4 "net/http"
5
6 "heckel.io/ntfy/v2/user"
7 "heckel.io/ntfy/v2/util"
8 )
9
10 type contextKey int
11
12 const (
13 contextRateVisitor contextKey = iota + 2586
14 contextTopic
15 contextMatrixPushKey
16 contextVisitorIP // Client IP extracted in maybeAuthenticate; reused by the abuse ban-feed (see ban.Service.Record)
17 )
18
19 func (s *Server) limitRequests(next handleFunc) handleFunc {
20 return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
21 if util.ContainsIP(s.config.VisitorRequestExemptPrefixes, v.ip) {
22 return next(w, r, v)
23 } else if !v.RequestAllowed() {
24 return errHTTPTooManyRequestsLimitRequests
25 }
26 return next(w, r, v)
27 }
28 }
29
30 // limitRequestsWithTopic limits requests with a topic and stores the rate-limiting-subscriber and topic into request.Context
31 func (s *Server) limitRequestsWithTopic(next handleFunc) handleFunc {
32 return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
33 t, err := s.topicFromPath(v, r.URL.Path)
34 if err != nil {
35 return err
36 }
37 vrate := v
38 if rateVisitor := t.RateVisitor(); rateVisitor != nil {
39 vrate = rateVisitor
40 }
41 r = withContext(r, map[contextKey]any{
42 contextRateVisitor: vrate,
43 contextTopic: t,
44 })
45 if util.ContainsIP(s.config.VisitorRequestExemptPrefixes, v.ip) {
46 return next(w, r, v)
47 } else if !vrate.RequestAllowed() {
48 return errHTTPTooManyRequestsLimitRequests
49 }
50 return next(w, r, v)
51 }
52 }
53
54 func (s *Server) ensureWebEnabled(next handleFunc) handleFunc {
55 return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
56 if s.config.WebRoot == "" {
57 return errHTTPNotFound
58 }
59 return next(w, r, v)
60 }
61 }
62
63 func (s *Server) ensureWebPushEnabled(next handleFunc) handleFunc {
64 return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
65 if s.config.WebRoot == "" || s.config.WebPushPublicKey == "" {
66 return errHTTPNotFound
67 }
68 return next(w, r, v)
69 }
70 }
71
72 func (s *Server) ensureUserManager(next handleFunc) handleFunc {
73 return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
74 if s.userManager == nil {
75 return errHTTPNotFound
76 }
77 return next(w, r, v)
78 }
79 }
80
81 func (s *Server) ensureUser(next handleFunc) handleFunc {
82 return s.ensureUserManager(func(w http.ResponseWriter, r *http.Request, v *visitor) error {
83 if v.User() == nil {
84 return errHTTPUnauthorized
85 }
86 return next(w, r, v)
87 })
88 }
89
90 func (s *Server) ensureAdmin(next handleFunc) handleFunc {
91 return s.ensureUserManager(func(w http.ResponseWriter, r *http.Request, v *visitor) error {
92 if !v.User().IsAdmin() {
93 return errHTTPUnauthorized
94 }
95 return next(w, r, v)
96 })
97 }
98
99 func (s *Server) ensureCallsEnabled(next handleFunc) handleFunc {
100 return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
101 if s.config.TwilioAccount == "" || s.userManager == nil {
102 return errHTTPNotFound
103 }
104 return next(w, r, v)
105 }
106 }
107
108 func (s *Server) ensureEmailsEnabled(next handleFunc) handleFunc {
109 return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
110 if s.mailer == nil || s.userManager == nil {
111 return errHTTPNotFound
112 }
113 return next(w, r, v)
114 }
115 }
116
117 func (s *Server) ensurePaymentsEnabled(next handleFunc) handleFunc {
118 return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
119 if s.config.StripeSecretKey == "" || s.stripe == nil {
120 return errHTTPNotFound
121 }
122 return next(w, r, v)
123 }
124 }
125
126 func (s *Server) ensureStripeCustomer(next handleFunc) handleFunc {
127 return s.ensureUser(func(w http.ResponseWriter, r *http.Request, v *visitor) error {
128 if v.User().Billing.StripeCustomerID == "" {
129 return errHTTPBadRequestNotAPaidUser
130 }
131 return next(w, r, v)
132 })
133 }
134
135 func (s *Server) withAccountSync(next handleFunc) handleFunc {
136 return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
137 err := next(w, r, v)
138 if err == nil {
139 s.publishSyncEventAsync(v)
140 }
141 return err
142 }
143 }
144
145 func (s *Server) authorizeTopicWrite(next handleFunc) handleFunc {
146 return s.authorizeTopic(next, user.PermissionWrite)
147 }
148
149 func (s *Server) authorizeTopicRead(next handleFunc) handleFunc {
150 return s.authorizeTopic(next, user.PermissionRead)
151 }
152
153 func (s *Server) authorizeTopic(next handleFunc, perm user.Permission) handleFunc {
154 return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
155 if s.userManager == nil {
156 return next(w, r, v)
157 }
158 topics, _, err := s.topicsFromPath(v, r.URL.Path)
159 if err != nil {
160 return err
161 }
162 u := v.User()
163 for _, t := range topics {
164 if err := s.userManager.Authorize(u, t.ID, perm); err != nil {
165 logvr(v, r).With(t).Err(err).Debug("Access to topic %s not authorized", t.ID)
166 return errHTTPForbidden.With(t)
167 }
168 }
169 return next(w, r, v)
170 }
171 }