-
Notifications
You must be signed in to change notification settings - Fork 0
/
validators.go
53 lines (46 loc) · 1.51 KB
/
validators.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package fcm
import (
"fmt"
"net/http"
"strings"
"github.com/savannahghi/errorcodeutil"
"github.com/savannahghi/firebasetools"
"github.com/savannahghi/serverutils"
)
// ValidateFCMData checks that the supplied FCM data does not use re
func ValidateFCMData(data map[string]string) error {
if data != nil {
fcmReservedWords := []string{"from", "notification", "message_type"}
for _, reservedWord := range fcmReservedWords {
_, present := data[reservedWord]
if present {
return fmt.Errorf("invalid use of FCM reserved word: %s", reservedWord)
}
}
fcmReservedPrefixes := []string{"gcm", "google"}
for _, reservedPrefix := range fcmReservedPrefixes {
for k := range data {
if strings.HasPrefix(k, reservedPrefix) {
return fmt.Errorf("illegal FCM prefix: %s", reservedPrefix)
}
}
}
}
return nil
}
// ValidateSendNotificationPayload checks that the request payload supplied in the indicated request are valid
func ValidateSendNotificationPayload(w http.ResponseWriter, r *http.Request) (*firebasetools.SendNotificationPayload, error) {
payload := &firebasetools.SendNotificationPayload{}
serverutils.DecodeJSONToTargetStruct(w, r, payload)
if payload.RegistrationTokens == nil {
err := fmt.Errorf("can't send FCM notifications to nil registration tokens")
errorcodeutil.ReportErr(w, err, http.StatusBadRequest)
return nil, err
}
err := ValidateFCMData(payload.Data)
if err != nil {
errorcodeutil.ReportErr(w, err, http.StatusBadRequest)
return nil, err
}
return payload, nil
}