This repository has been archived by the owner on Dec 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
cert_apns.go
237 lines (203 loc) · 6.19 KB
/
cert_apns.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
// Copyright 2015-present Oursky Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package push
import (
"crypto/tls"
"crypto/x509"
"encoding/asn1"
"encoding/json"
"errors"
"net/http"
"time"
"net"
"golang.org/x/net/http2"
"github.com/sirupsen/logrus"
"github.com/skygeario/buford/push"
"github.com/skygeario/skygear-server/pkg/server/skydb"
)
type certBasedAPNSPusher struct {
APNSPusher
// Function to obtain a skydb connection
connOpener func() (skydb.Conn, error)
topic string
conn skydb.Conn
service pushService
failed chan failedNotification
}
// parseCertificateLeaf parses the provided TLS certificate for its
// leaf certificate. Returns an error if the leaf certificate cannot be found.
func parseCertificateLeaf(certificate *tls.Certificate) error {
if certificate.Leaf != nil {
return nil
}
if len(certificate.Certificate) > 0 {
x509Cert, err := x509.ParseCertificate(certificate.Certificate[0])
if err != nil {
return err
}
certificate.Leaf = x509Cert
return nil
}
return errors.New("push/apns: provided APNS certificate does not contain leaf")
}
// findDefaultAPNSTopic returns the APNS topic in the TLS certificate.
//
// The Subject of leaf certificate should contains the UID, which we can
// use as the topic for APNS. The topic is usually the same as the
// application bundle // identifier.
//
// Returns the topic name, and an error if an error occurring finding the topic
// name.
func findDefaultAPNSTopic(certificate tls.Certificate) (string, error) {
if certificate.Leaf == nil {
err := parseCertificateLeaf(&certificate)
if err != nil {
return "", err
}
}
// Loop over the subject names array to look for UID
uidObjectIdentifier := asn1.ObjectIdentifier([]int{0, 9, 2342, 19200300, 100, 1, 1})
for _, attr := range certificate.Leaf.Subject.Names {
if uidObjectIdentifier.Equal(attr.Type) {
switch value := attr.Value.(type) {
case string:
return value, nil
}
break
}
}
return "", errors.New("push/apns: cannot find UID in APNS certificate subject name")
}
// newHTTPClient returns a properly configured http client
func newHTTPClientWithCert(cert tls.Certificate, keepalive int) (*http.Client, error) {
config := &tls.Config{
Certificates: []tls.Certificate{cert},
}
config.BuildNameToCertificate()
transport := &http.Transport{
TLSClientConfig: config,
DialContext: (&net.Dialer{
KeepAlive: time.Duration(keepalive) * time.Second,
DualStack: true,
}).DialContext,
}
if err := http2.ConfigureTransport(transport); err != nil {
return nil, err
}
return &http.Client{Transport: transport}, nil
}
// NewCertBasedAPNSPusher returns a new APNSPusher from content of certificate
// and private key as string
func NewCertBasedAPNSPusher(
connOpener func() (skydb.Conn, error),
gatewayType GatewayType,
cert string,
key string,
keepalive int,
) (APNSPusher, error) {
certificate, err := tls.X509KeyPair([]byte(cert), []byte(key))
if err != nil {
return nil, err
}
topic, err := findDefaultAPNSTopic(certificate)
if err != nil {
return nil, err
}
client, err := newHTTPClientWithCert(certificate, keepalive)
if err != nil {
return nil, err
}
service, err := newPushService(client, gatewayType)
if err != nil {
return nil, err
}
return &certBasedAPNSPusher{
connOpener: connOpener,
service: service,
topic: topic,
}, nil
}
// Start setups the pusher and starts it
func (pusher *certBasedAPNSPusher) Start() {
conn, err := pusher.connOpener()
if err != nil {
log.Errorf("push/apns: failed to open skydb.Conn, abort feedback retrival: %v\n", err)
return
}
pusher.conn = conn
pusher.failed = make(chan failedNotification)
go func() {
checkFailedNotifications(pusher)
}()
}
// Stop stops and cleans up the pusher
func (pusher *certBasedAPNSPusher) Stop() {
close(pusher.failed)
pusher.failed = nil
}
// Send sends a notification to the device identified by the
// specified device
func (pusher *certBasedAPNSPusher) Send(m Mapper, device skydb.Device) error {
logger := log.WithFields(logrus.Fields{
"deviceToken": device.Token,
"deviceID": device.ID,
"deviceTopic": device.Topic,
})
if m == nil {
logger.Warn("Cannot send push notification with nil data.")
return errors.New("push/apns: push notification has no data")
}
apnsMap, ok := m.Map()["apns"].(map[string]interface{})
if !ok {
return errors.New("push/apns: payload has no apns dictionary")
}
serializedPayload, err := json.Marshal(apnsMap)
if err != nil {
return err
}
headers := push.Headers{
Topic: pusher.topic,
}
apnsid, err := pusher.service.Push(device.Token, &headers, serializedPayload)
if err != nil {
if pushError, ok := err.(*push.Error); ok && pushError != nil {
// We recognize the error, and that error comes from APNS
pushLogger := logger.WithFields(logrus.Fields{
"apnsErrorReason": pushError.Reason,
"apnsErrorStatus": pushError.Status,
"apnsErrorTimestamp": pushError.Timestamp,
})
if pushError.Status == http.StatusGone ||
pushError.Reason.Error() == "BadDeviceToken" {
pushLogger.Info("push/apns: device token is no longer valid")
queueFailedNotification(pusher, device.Token, *pushError)
} else {
pushLogger.Error("push/apns: failed to send push notification")
}
return err
}
logger.Errorf("Failed to send push notification: %s", err)
return err
}
logger.WithFields(logrus.Fields{
"apnsID": apnsid,
}).Info("push/apns: push notification is sent")
return nil
}
func (pusher certBasedAPNSPusher) getFailedNotificationChannel() chan failedNotification {
return pusher.failed
}
func (pusher certBasedAPNSPusher) deleteDeviceToken(token string, beforeTime time.Time) error {
return pusher.conn.DeleteDevicesByToken(token, beforeTime)
}