-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathwebpush.go
More file actions
203 lines (183 loc) · 6.13 KB
/
webpush.go
File metadata and controls
203 lines (183 loc) · 6.13 KB
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
package coderd
import (
"database/sql"
"errors"
"net/http"
"net/netip"
"net/url"
"slices"
"strings"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbtime"
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/coderd/httpmw"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/coderd/rbac/policy"
"github.com/coder/coder/v2/coderd/webpush"
"github.com/coder/coder/v2/codersdk"
)
// @Summary Create user webpush subscription
// @ID create-user-webpush-subscription
// @Security CoderSessionToken
// @Accept json
// @Tags Notifications
// @Param request body codersdk.WebpushSubscription true "Webpush subscription"
// @Param user path string true "User ID, name, or me"
// @Router /users/{user}/webpush/subscription [post]
// @Success 204
// @x-apidocgen {"skip": true}
func (api *API) postUserWebpushSubscription(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
user := httpmw.UserParam(r)
var req codersdk.WebpushSubscription
if !httpapi.Read(ctx, rw, r, &req) {
return
}
if err := validateWebpushEndpoint(req.Endpoint); err != nil {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Invalid webpush endpoint.",
Detail: err.Error(),
})
return
}
if err := api.WebpushDispatcher.Test(ctx, req); err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to test webpush subscription",
Detail: err.Error(),
})
return
}
if _, err := api.Database.InsertWebpushSubscription(ctx, database.InsertWebpushSubscriptionParams{
CreatedAt: dbtime.Now(),
UserID: user.ID,
Endpoint: req.Endpoint,
EndpointAuthKey: req.AuthKey,
EndpointP256dhKey: req.P256DHKey,
}); err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to insert push notification subscription.",
Detail: err.Error(),
})
return
}
if invalidator, ok := api.WebpushDispatcher.(webpush.SubscriptionCacheInvalidator); ok {
invalidator.InvalidateUser(user.ID)
}
rw.WriteHeader(http.StatusNoContent)
}
func validateWebpushEndpoint(rawEndpoint string) error {
endpoint, err := url.Parse(rawEndpoint)
if err != nil {
return xerrors.Errorf("parse endpoint URL: %w", err)
}
if !endpoint.IsAbs() {
return xerrors.New("endpoint must be an absolute URL")
}
if endpoint.Scheme != "https" {
return xerrors.New("endpoint URL scheme must be https")
}
if endpoint.Host == "" {
return xerrors.New("endpoint host is required")
}
if endpoint.User != nil {
return xerrors.New("endpoint URL must not include userinfo")
}
hostname := strings.ToLower(endpoint.Hostname())
if hostname == "" {
return xerrors.New("endpoint hostname is required")
}
if hostname == "localhost" || strings.HasSuffix(hostname, ".localhost") {
return xerrors.New("endpoint hostname must not be localhost")
}
if ip, err := netip.ParseAddr(hostname); err == nil &&
(ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast() ||
ip.IsLinkLocalMulticast() || ip.IsMulticast() ||
ip.IsUnspecified()) {
return xerrors.New("endpoint IP must not be private, loopback, link-local, multicast, or unspecified")
}
return nil
}
// @Summary Delete user webpush subscription
// @ID delete-user-webpush-subscription
// @Security CoderSessionToken
// @Accept json
// @Tags Notifications
// @Param request body codersdk.DeleteWebpushSubscription true "Webpush subscription"
// @Param user path string true "User ID, name, or me"
// @Router /users/{user}/webpush/subscription [delete]
// @Success 204
// @x-apidocgen {"skip": true}
func (api *API) deleteUserWebpushSubscription(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
user := httpmw.UserParam(r)
var req codersdk.DeleteWebpushSubscription
if !httpapi.Read(ctx, rw, r, &req) {
return
}
// Return NotFound if the subscription does not exist.
existing, err := api.Database.GetWebpushSubscriptionsByUserID(ctx, user.ID)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to get webpush subscriptions.",
Detail: err.Error(),
})
return
}
if idx := slices.IndexFunc(existing, func(s database.WebpushSubscription) bool {
return s.Endpoint == req.Endpoint
}); idx == -1 {
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
Message: "Webpush subscription not found.",
})
return
}
if err := api.Database.DeleteWebpushSubscriptionByUserIDAndEndpoint(ctx, database.DeleteWebpushSubscriptionByUserIDAndEndpointParams{
UserID: user.ID,
Endpoint: req.Endpoint,
}); err != nil {
if errors.Is(err, sql.ErrNoRows) {
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
Message: "Webpush subscription not found.",
})
return
}
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to delete push notification subscription.",
Detail: err.Error(),
})
return
}
if invalidator, ok := api.WebpushDispatcher.(webpush.SubscriptionCacheInvalidator); ok {
invalidator.InvalidateUser(user.ID)
}
rw.WriteHeader(http.StatusNoContent)
}
// @Summary Send a test push notification
// @ID send-a-test-push-notification
// @Security CoderSessionToken
// @Tags Notifications
// @Param user path string true "User ID, name, or me"
// @Success 204
// @Router /users/{user}/webpush/test [post]
// @x-apidocgen {"skip": true}
func (api *API) postUserPushNotificationTest(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
user := httpmw.UserParam(r)
// We need to authorize the user to send a push notification to themselves.
if !api.Authorize(r, policy.ActionCreate, rbac.ResourceNotificationMessage.WithOwner(user.ID.String())) {
httpapi.Forbidden(rw)
return
}
if err := api.WebpushDispatcher.Dispatch(ctx, user.ID, codersdk.WebpushMessage{
Title: "It's working!",
Body: "You've subscribed to push notifications.",
}); err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to send test notification",
Detail: err.Error(),
})
return
}
rw.WriteHeader(http.StatusNoContent)
}