Use readJSONWithLimit for web push sub/unsub

pull/751/head
nimbleghost 2023-05-31 18:30:20 +02:00
parent 4648f83669
commit 0c25425346
1 changed files with 3 additions and 7 deletions

View File

@ -9,8 +9,7 @@ import (
)
func (s *Server) handleTopicWebPushSubscribe(w http.ResponseWriter, r *http.Request, v *visitor) error {
var sub webPushSubscribePayload
err := json.NewDecoder(r.Body).Decode(&sub)
sub, err := readJSONWithLimit[webPushSubscribePayload](r.Body, jsonBodyBytesLimit, false)
if err != nil || sub.BrowserSubscription.Endpoint == "" || sub.BrowserSubscription.Keys.P256dh == "" || sub.BrowserSubscription.Keys.Auth == "" {
return errHTTPBadRequestWebPushSubscriptionInvalid
}
@ -19,17 +18,14 @@ func (s *Server) handleTopicWebPushSubscribe(w http.ResponseWriter, r *http.Requ
if err != nil {
return err
}
if err = s.webPush.AddSubscription(topic.ID, v.MaybeUserID(), sub); err != nil {
if err = s.webPush.AddSubscription(topic.ID, v.MaybeUserID(), *sub); err != nil {
return err
}
return s.writeJSON(w, newSuccessResponse())
}
func (s *Server) handleTopicWebPushUnsubscribe(w http.ResponseWriter, r *http.Request, _ *visitor) error {
var payload webPushUnsubscribePayload
err := json.NewDecoder(r.Body).Decode(&payload)
payload, err := readJSONWithLimit[webPushUnsubscribePayload](r.Body, jsonBodyBytesLimit, false)
if err != nil {
return errHTTPBadRequestWebPushSubscriptionInvalid
}