/
push.go
150 lines (126 loc) · 3.6 KB
/
push.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
package apns
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/RobotsAndPencils/buford/payload"
"github.com/RobotsAndPencils/buford/push"
"github.com/go-kit/kit/endpoint"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/micromdm/micromdm/pkg/httputil"
)
type pushOpts struct {
expiration time.Time
}
// WithExpiration sets the expiration of the APNS message.
// Apple will retry delivery until this time. The default behavior only tries once.
func WithExpiration(t time.Time) PushOption {
return func(opt *pushOpts) {
opt.expiration = t
}
}
// PushOption adds optional parameters to the Push method.
type PushOption func(*pushOpts)
func (svc *PushService) Push(ctx context.Context, deviceUDID string, opts ...PushOption) (string, error) {
// loop through defaults and apply user provided overrides
var opt pushOpts
for _, optFn := range opts {
optFn(&opt)
}
headers := &push.Headers{}
if !opt.expiration.IsZero() {
headers.Expiration = opt.expiration
}
info, err := svc.store.PushInfo(ctx, deviceUDID)
if err != nil {
return "", errors.Wrap(err, "retrieving PushInfo by UDID")
}
p := payload.MDM{Token: info.PushMagic}
valid := push.IsDeviceTokenValid(info.Token)
if !valid {
return "", errors.New("invalid push token")
}
jsonPayload, err := json.Marshal(p)
if err != nil {
return "", errors.Wrap(err, "marshalling push notification payload")
}
result, err := svc.pushsvc.Push(info.Token, headers, jsonPayload)
if err != nil && strings.HasSuffix(err.Error(), "remote error: tls: internal error") {
// TODO: yuck, error substring searching. see:
// https://github.com/micromdm/micromdm/issues/150
return result, errors.Wrap(err, "push error: possibly expired or invalid APNs certificate")
}
return result, err
}
type pushRequest struct {
UDID string
expireAt time.Time
}
type pushResponse struct {
Status string `json:"status,omitempty"`
ID string `json:"push_notification_id,omitempty"`
Err error `json:"error,omitempty"`
}
func (r pushResponse) Failed() error { return r.Err }
func decodePushRequest(ctx context.Context, r *http.Request) (interface{}, error) {
vars := mux.Vars(r)
udid, ok := vars["udid"]
if !ok {
return 0, errors.New("apns: bad route")
}
v := r.URL.Query()
exp, err := expiration(v.Get("expiration"))
if err != nil {
return nil, err
}
return pushRequest{
UDID: udid,
expireAt: exp,
}, nil
}
func expiration(s string) (time.Time, error) {
if s == "" {
return time.Time{}, nil
}
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return time.Time{}, fmt.Errorf("parsing expiration time: %s", err)
}
return time.Unix(i, 0).UTC(), nil
}
func decodePushResponse(_ context.Context, r *http.Response) (interface{}, error) {
var resp pushResponse
err := httputil.DecodeJSONResponse(r, &resp)
return resp, err
}
func MakePushEndpoint(svc Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(pushRequest)
var opts []PushOption
if !req.expireAt.IsZero() {
opts = append(opts, WithExpiration(req.expireAt))
}
id, err := svc.Push(ctx, req.UDID, opts...)
if err != nil {
return pushResponse{Err: err, Status: "failure"}, nil
}
return pushResponse{Status: "success", ID: id}, nil
}
}
func (mw loggingMiddleware) Push(ctx context.Context, udid string, opts ...PushOption) (id string, err error) {
defer func(begin time.Time) {
_ = mw.logger.Log(
"method", "Push",
"udid", udid,
"err", err,
"took", time.Since(begin),
)
}(time.Now())
id, err = mw.next.Push(ctx, udid, opts...)
return
}