-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
270 lines (241 loc) · 5.18 KB
/
handler.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package mock
import (
"crypto/ecdsa"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"github.com/google/uuid"
)
type DeviceToken struct {
Token string
Topic string
Unregistered int64
}
type Push struct {
ID string
Headers http.Header
Payload []byte
Status int
Reason string
Timestamp int64
}
type TokenPublicKeyFunc func(keyID, teamID string) *ecdsa.PublicKey
type DeviceTokenFunc func(token string) *DeviceToken
type PushFunc func(push *Push)
type Handler struct {
TokenPublicKey TokenPublicKeyFunc
DeviceToken DeviceTokenFunc
Push PushFunc
}
func NewHandler(keyFunc TokenPublicKeyFunc, tokenFunc DeviceTokenFunc, pushFunc PushFunc) *Handler {
return &Handler{
TokenPublicKey: keyFunc,
DeviceToken: tokenFunc,
Push: pushFunc,
}
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var id string
headers := make(map[string][]string)
for k, v := range r.Header {
headers[strings.ToLower(k)] = v
}
var payload []byte
notOk := func(status int, reason string, stamp int64) {
h.Push(&Push{
ID: id,
Headers: headers,
Payload: payload,
Status: status,
Reason: reason,
Timestamp: stamp,
})
w.WriteHeader(status)
if stamp > 0 {
fmt.Fprintf(w, `{"reason":"%v","timestamp":%v}`, reason, stamp)
} else {
fmt.Fprintf(w, `{"reason":"%v"}`, reason)
}
}
done := func() {
h.Push(&Push{
ID: id,
Headers: headers,
Payload: payload,
Status: 200,
})
w.WriteHeader(200)
}
// apns-id
if ids, ok := headers["apns-id"]; ok {
if _, err := uuid.Parse(ids[0]); err != nil {
id = strings.ToUpper(uuid.NewString())
} else {
id = ids[0]
}
} else {
id = strings.ToUpper(uuid.NewString())
}
w.Header().Set("apns-id", id)
// :method
if r.Method != http.MethodPost {
notOk(405, "MethodNotAllowed", 0)
return
}
// :path
path := r.URL.Path
if !strings.HasPrefix(path, "/3/device/") {
notOk(404, "BadPath", 0)
return
}
// Authorization
auth := r.Header.Get("authorization")
if len(auth) < 7 {
notOk(403, "MissingProviderToken", 0)
return
}
if !strings.HasPrefix(strings.ToLower(auth[:7]), "bearer ") {
notOk(403, "MissingProviderToken", 0)
return
}
// JWT Token
bearer := strings.TrimSpace(auth[7:])
token, err := DecodeToken(bearer)
if err != nil {
notOk(403, "InvalidProviderToken", 0)
return
}
pub := h.TokenPublicKey(token.KeyID, token.TeamID)
if pub == nil {
notOk(403, "InvalidProviderToken", 0)
return
}
if ok, _ := VerifyJWT(bearer, pub); !ok {
notOk(403, "InvalidProviderToken", 0)
return
}
if token.Expired() {
notOk(403, "ExpiredProviderToken", 0)
return
}
// Payload
if r.ContentLength == 0 {
notOk(400, "PayloadEmpty", 0)
return
}
// apns-id
if ids, ok := headers["apns-id"]; ok {
if _, err := uuid.Parse(id); err != nil {
notOk(400, "BadMessageId", 0)
return
}
if len(ids) > 1 {
notOk(400, "DuplicateHeaders", 0)
return
}
}
// apns-expiration
expirations, ok := headers["apns-expiration"]
if ok {
if _, err := strconv.Atoi(expirations[0]); err != nil {
notOk(400, "BadExpirationDate", 0)
}
if len(expirations) > 1 {
notOk(400, "DuplicateHeaders", 0)
}
}
// apns-priority
priorities, ok := headers["apns-priority"]
if ok {
if _, err := strconv.Atoi(priorities[0]); err != nil {
notOk(400, "BadPriority", 0)
}
if len(priorities) > 1 {
notOk(400, "DuplicateHeaders", 0)
}
}
// apns-collapse-id
collapses, ok := headers["apns-collapse-id"]
if ok {
collapse := collapses[0]
if collapse == "" {
notOk(400, "InvalidCollapseId", 0)
}
if len(collapse) > 64 {
notOk(400, "InvalidCollapseId", 0)
}
if len(collapses) > 1 {
notOk(400, "DuplicateHeaders", 0)
}
}
// apns-push-type
pushTypes, ok := headers["apns-push-type"]
if ok {
ptype := pushTypes[0]
if ptype != "" && ptype != "alert" && ptype != "background" && ptype != "voip" && ptype != "complication" && ptype != "fileprovider" && ptype != "mdm" {
notOk(400, "InvalidPushType", 0)
}
if len(pushTypes) > 1 {
notOk(400, "DuplicateHeaders", 0)
}
}
// Device Token
deviceToken := path[len("/3/device/"):]
if len(deviceToken) != 64 {
notOk(400, "BadDeviceToken", 0)
return
}
// apns-topic
topic := ""
topics, ok := headers["apns-topic"]
if ok {
topic = topics[0]
if topic == "" {
notOk(400, "MissingTopic", 0)
return
}
if len(topics) > 1 {
notOk(400, "DuplicateHeaders", 0)
return
}
} else {
notOk(400, "MissingTopic", 0)
return
}
// Device Token
tokenTopic := h.DeviceToken(deviceToken)
if tokenTopic == nil || tokenTopic.Topic == "" {
notOk(400, "BadDeviceToken", 0)
return
}
if tokenTopic.Topic != topic {
notOk(400, "DeviceTokenNotForTopic", 0)
return
}
if tokenTopic.Unregistered > 0 {
notOk(410, "Unregistered", tokenTopic.Unregistered)
return
}
// Payload size
if strings.HasSuffix(topic, ".voip") {
if r.ContentLength > 5120 {
notOk(413, "PayloadTooLarge", 0)
return
}
} else {
if r.ContentLength > 4096 {
notOk(413, "PayloadTooLarge", 0)
return
}
}
// Body
defer r.Body.Close()
payload, err = io.ReadAll(r.Body)
if err != nil {
notOk(500, "InternalServerError", 0)
return
}
done()
}