| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::WebPush; |
|
2
|
4
|
|
|
4
|
|
18012
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
|
4
|
|
|
|
|
13
|
|
|
|
4
|
|
|
|
|
26
|
|
|
3
|
4
|
|
|
4
|
|
773
|
use Mojo::JSON qw(decode_json encode_json); |
|
|
4
|
|
|
|
|
8
|
|
|
|
4
|
|
|
|
|
290
|
|
|
4
|
4
|
|
|
4
|
|
27
|
use Mojo::URL; |
|
|
4
|
|
|
|
|
9
|
|
|
|
4
|
|
|
|
|
44
|
|
|
5
|
4
|
|
|
4
|
|
126
|
use Crypt::PK::ECC; |
|
|
4
|
|
|
|
|
10
|
|
|
|
4
|
|
|
|
|
188
|
|
|
6
|
4
|
|
|
4
|
|
23
|
use MIME::Base64 qw(encode_base64url decode_base64url); |
|
|
4
|
|
|
|
|
8
|
|
|
|
4
|
|
|
|
|
221
|
|
|
7
|
4
|
|
|
4
|
|
2700
|
use Crypt::JWT qw(encode_jwt decode_jwt); |
|
|
4
|
|
|
|
|
132065
|
|
|
|
4
|
|
|
|
|
329
|
|
|
8
|
4
|
|
|
4
|
|
1670
|
use Crypt::RFC8188 qw(ece_encrypt_aes128gcm); |
|
|
4
|
|
|
|
|
160407
|
|
|
|
4
|
|
|
|
|
8916
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
my @MANDATORY_CONF = qw( |
|
13
|
|
|
|
|
|
|
subs_session2user_p |
|
14
|
|
|
|
|
|
|
save_endpoint |
|
15
|
|
|
|
|
|
|
subs_create_p |
|
16
|
|
|
|
|
|
|
subs_read_p |
|
17
|
|
|
|
|
|
|
subs_delete_p |
|
18
|
|
|
|
|
|
|
); |
|
19
|
|
|
|
|
|
|
my @AUTH_CONF = qw(claim_sub ecc_private_key); |
|
20
|
|
|
|
|
|
|
my $DEFAULT_PUSH_HANDLER = <<'EOF'; |
|
21
|
|
|
|
|
|
|
event => { |
|
22
|
|
|
|
|
|
|
var msg = event.data.json(); |
|
23
|
|
|
|
|
|
|
var title = msg.title; |
|
24
|
|
|
|
|
|
|
delete msg.title; |
|
25
|
|
|
|
|
|
|
event.waitUntil(self.registration.showNotification(title, msg)); |
|
26
|
|
|
|
|
|
|
} |
|
27
|
|
|
|
|
|
|
EOF |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub _decode { |
|
30
|
2
|
|
|
2
|
|
56
|
my ($bytes) = @_; |
|
31
|
2
|
|
|
|
|
5
|
my $body = eval { decode_json($bytes) }; |
|
|
2
|
|
|
|
|
8
|
|
|
32
|
|
|
|
|
|
|
# conceal error info like versions from attackers |
|
33
|
2
|
50
|
|
|
|
280
|
return (0, "Malformed request") if $@; |
|
34
|
2
|
|
|
|
|
7
|
(1, $body); |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub _error { |
|
38
|
1
|
|
|
1
|
|
11
|
my ($c, $error) = @_; |
|
39
|
1
|
|
|
|
|
8
|
$c->render(status => 500, json => { errors => [ { message => $error } ] }); |
|
40
|
|
|
|
|
|
|
} |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub _make_route_handler { |
|
43
|
4
|
|
|
4
|
|
24
|
my ($subs_session2user_p, $subs_create_p) = @_; |
|
44
|
|
|
|
|
|
|
sub { |
|
45
|
2
|
|
|
2
|
|
16915
|
my ($c) = @_; |
|
46
|
2
|
|
|
|
|
7
|
my ($decode_ok, $body) = _decode($c->req->body); |
|
47
|
2
|
50
|
|
|
|
21
|
return _error($c, $body) if !$decode_ok; |
|
48
|
2
|
|
|
|
|
5
|
eval { validate_subs_info($body) }; |
|
|
2
|
|
|
|
|
6
|
|
|
49
|
2
|
100
|
|
|
|
522
|
return _error($c, $@) if $@; |
|
50
|
|
|
|
|
|
|
return $subs_session2user_p->($c, $c->session)->then( |
|
51
|
1
|
|
|
|
|
5241
|
sub { $subs_create_p->($c, $_[0], $body) }, |
|
52
|
|
|
|
|
|
|
)->then( |
|
53
|
1
|
|
|
|
|
441
|
sub { $c->render(json => { data => { success => \1 } }) }, |
|
54
|
0
|
|
|
|
|
0
|
sub { _error($c, @_) }, |
|
55
|
1
|
|
|
|
|
6
|
); |
|
56
|
4
|
|
|
|
|
71
|
}; |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub _make_auth_helper { |
|
60
|
4
|
|
|
4
|
|
11
|
my ($app, $conf) = @_; |
|
61
|
4
|
|
50
|
|
|
50
|
my $exp_offset = $conf->{claim_exp_offset} || 86400; |
|
62
|
4
|
|
|
|
|
40
|
my $key = Crypt::PK::ECC->new($conf->{ecc_private_key}); |
|
63
|
4
|
|
|
|
|
24440
|
my $claims_start = { sub => $conf->{claim_sub} }; |
|
64
|
4
|
|
|
|
|
112
|
my $pkey = encode_base64url $key->export_key_raw('public'); |
|
65
|
4
|
|
|
0
|
|
118
|
$app->helper('webpush.public_key' => sub { $pkey }); |
|
|
0
|
|
|
|
|
0
|
|
|
66
|
|
|
|
|
|
|
sub { |
|
67
|
3
|
|
|
3
|
|
1455
|
my ($c, $subs_info) = @_; |
|
68
|
3
|
|
|
|
|
35
|
my $aud = Mojo::URL->new($subs_info->{endpoint})->path(Mojo::Path->new->trailing_slash(0)).''; |
|
69
|
3
|
|
|
|
|
1750
|
my $claims = { aud => $aud, exp => time + $exp_offset, %$claims_start }; |
|
70
|
3
|
|
|
|
|
18
|
my $token = encode_jwt key => $key, alg => 'ES256', payload => $claims; |
|
71
|
3
|
|
|
|
|
9484
|
"vapid t=$token,k=$pkey"; |
|
72
|
4
|
|
|
|
|
1638
|
}; |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub _verify_helper { |
|
76
|
3
|
|
|
3
|
|
9601
|
my ($app, $auth_header_value) = @_; |
|
77
|
3
|
|
|
|
|
13
|
(my $schema, $auth_header_value) = split ' ', $auth_header_value; |
|
78
|
3
|
50
|
|
|
|
14
|
return if $schema ne 'vapid'; |
|
79
|
3
|
|
|
|
|
23
|
my %k2v = map split('=', $_), split ',', $auth_header_value; |
|
80
|
3
|
|
|
|
|
8
|
eval { |
|
81
|
3
|
|
|
|
|
14
|
my $key = Crypt::PK::ECC->new; |
|
82
|
3
|
|
|
|
|
229
|
$key->import_key_raw(decode_base64url($k2v{k}), 'P-256'); |
|
83
|
3
|
|
|
|
|
8815
|
decode_jwt token => $k2v{t}, key => $key, alg => 'ES256', verify_exp => 0; |
|
84
|
|
|
|
|
|
|
}; |
|
85
|
|
|
|
|
|
|
} |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub _encrypt_helper { |
|
88
|
3
|
|
|
3
|
|
7840
|
my ($c, $plaintext, $receiver_key, $auth_key) = @_; |
|
89
|
3
|
50
|
33
|
|
|
28
|
die "Invalid p256dh key specified\n" |
|
90
|
|
|
|
|
|
|
if length($receiver_key) != 65 or $receiver_key !~ /^\x04/; |
|
91
|
3
|
|
|
|
|
19
|
my $onetime_key = Crypt::PK::ECC->new->generate_key('prime256v1'); |
|
92
|
3
|
|
|
|
|
9104
|
ece_encrypt_aes128gcm( |
|
93
|
|
|
|
|
|
|
$plaintext, (undef) x 2, $onetime_key, $receiver_key, $auth_key, |
|
94
|
|
|
|
|
|
|
); |
|
95
|
|
|
|
|
|
|
} |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub _send_helper { |
|
98
|
2
|
|
|
2
|
|
2999
|
my ($c, $message, $user_id, $ttl, $urgency) = @_; |
|
99
|
2
|
|
50
|
|
|
7
|
$ttl ||= 30; |
|
100
|
2
|
|
50
|
|
|
11
|
$urgency ||= 'normal'; |
|
101
|
|
|
|
|
|
|
$c->webpush->read_p($user_id)->then(sub { |
|
102
|
2
|
|
|
2
|
|
1075
|
my ($subs_info) = @_; |
|
103
|
|
|
|
|
|
|
my $body = $c->webpush->encrypt( |
|
104
|
|
|
|
|
|
|
encode_json($message), |
|
105
|
2
|
|
|
|
|
10
|
map decode_base64url($_), @{$subs_info->{keys}}{qw(p256dh auth)} |
|
|
2
|
|
|
|
|
180
|
|
|
106
|
|
|
|
|
|
|
); |
|
107
|
2
|
|
|
|
|
15743
|
my $headers = { |
|
108
|
|
|
|
|
|
|
Authorization => $c->webpush->authorization($subs_info), |
|
109
|
|
|
|
|
|
|
'Content-Length' => length($body), |
|
110
|
|
|
|
|
|
|
'Content-Encoding' => 'aes128gcm', |
|
111
|
|
|
|
|
|
|
TTL => $ttl, |
|
112
|
|
|
|
|
|
|
Urgency => $urgency, |
|
113
|
|
|
|
|
|
|
}; |
|
114
|
2
|
|
|
|
|
13
|
$c->app->ua->post_p($subs_info->{endpoint}, $headers, $body); |
|
115
|
|
|
|
|
|
|
})->then(sub { |
|
116
|
2
|
|
|
2
|
|
85171
|
my ($tx) = @_; |
|
117
|
|
|
|
|
|
|
return $c->webpush->delete_p($user_id)->then(sub { |
|
118
|
1
|
|
|
|
|
329
|
{ data => { success => \1 } } |
|
119
|
2
|
100
|
66
|
|
|
9
|
}) if $tx->res->code == 404 or $tx->res->code == 410; |
|
120
|
1
|
50
|
|
|
|
31
|
return { errors => [ { message => $tx->res->body } ] } |
|
121
|
|
|
|
|
|
|
if $tx->res->code > 399; |
|
122
|
1
|
|
|
|
|
12
|
{ data => { success => \1 } }; |
|
123
|
|
|
|
|
|
|
}, sub { |
|
124
|
0
|
|
|
0
|
|
0
|
{ errors => [ { message => $_[0] } ] } |
|
125
|
2
|
|
|
|
|
20
|
}); |
|
126
|
|
|
|
|
|
|
} |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub register { |
|
129
|
4
|
|
|
4
|
1
|
283
|
my ($self, $app, $conf) = @_; |
|
130
|
4
|
|
|
|
|
26
|
my @config_errors = grep !exists $conf->{$_}, @MANDATORY_CONF; |
|
131
|
4
|
50
|
|
|
|
20
|
die "Missing config keys @config_errors\n" if @config_errors; |
|
132
|
|
|
|
|
|
|
$app->helper('webpush.create_p' => sub { |
|
133
|
4
|
|
|
4
|
|
30878
|
eval { validate_subs_info($_[2]) }; |
|
|
4
|
|
|
|
|
22
|
|
|
134
|
4
|
50
|
|
|
|
16
|
return Mojo::Promise->reject($@) if $@; |
|
135
|
4
|
|
|
|
|
10
|
goto &{ $conf->{subs_create_p} }; |
|
|
4
|
|
|
|
|
25
|
|
|
136
|
4
|
|
|
|
|
54
|
}); |
|
137
|
4
|
|
|
|
|
2046
|
$app->helper('webpush.read_p' => $conf->{subs_read_p}); |
|
138
|
4
|
|
|
|
|
1282
|
$app->helper('webpush.delete_p' => $conf->{subs_delete_p}); |
|
139
|
|
|
|
|
|
|
$app->helper('webpush.authorization' => (grep !$conf->{$_}, @AUTH_CONF) |
|
140
|
0
|
|
|
0
|
|
0
|
? sub { die "Must provide @AUTH_CONF\n" } |
|
141
|
4
|
50
|
|
|
|
1416
|
: _make_auth_helper($app, $conf) |
|
142
|
|
|
|
|
|
|
); |
|
143
|
4
|
|
|
|
|
1635
|
$app->helper('webpush.verify_token' => \&_verify_helper); |
|
144
|
4
|
|
|
|
|
1711
|
$app->helper('webpush.encrypt' => \&_encrypt_helper); |
|
145
|
4
|
|
|
|
|
1824
|
$app->helper('webpush.send_p' => \&_send_helper); |
|
146
|
4
|
|
|
|
|
1980
|
my $r = $app->routes; |
|
147
|
|
|
|
|
|
|
$r->post($conf->{save_endpoint} => _make_route_handler( |
|
148
|
4
|
|
|
|
|
36
|
@$conf{qw(subs_session2user_p subs_create_p)}, |
|
149
|
|
|
|
|
|
|
), 'webpush.save'); |
|
150
|
4
|
|
|
|
|
2064
|
push @{ $app->renderer->classes }, __PACKAGE__; |
|
|
4
|
|
|
|
|
17
|
|
|
151
|
|
|
|
|
|
|
$app->serviceworker->add_event_listener( |
|
152
|
4
|
|
33
|
|
|
74
|
push => $conf->{push_handler} || $DEFAULT_PUSH_HANDLER |
|
153
|
|
|
|
|
|
|
); |
|
154
|
4
|
|
|
|
|
1049
|
$self; |
|
155
|
|
|
|
|
|
|
} |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub validate_subs_info { |
|
158
|
11
|
|
|
11
|
0
|
30971
|
my ($info) = @_; |
|
159
|
11
|
50
|
|
|
|
48
|
die "Expected object\n" if ref $info ne 'HASH'; |
|
160
|
11
|
|
|
|
|
55
|
my @errors = map "no $_", grep !exists $info->{$_}, qw(keys endpoint); |
|
161
|
11
|
|
|
|
|
46
|
push @errors, map "no $_", grep !exists $info->{keys}{$_}, qw(auth p256dh); |
|
162
|
11
|
100
|
|
|
|
90
|
die "Errors found in subscription info: " . join(", ", @errors) . "\n" |
|
163
|
|
|
|
|
|
|
if @errors; |
|
164
|
|
|
|
|
|
|
} |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
1; |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=encoding utf8 |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 NAME |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Mojolicious::Plugin::WebPush - plugin to aid real-time web push |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# Mojolicious::Lite |
|
177
|
|
|
|
|
|
|
my $sw = plugin 'ServiceWorker' => { debug => 1 }; |
|
178
|
|
|
|
|
|
|
my $webpush = plugin 'WebPush' => { |
|
179
|
|
|
|
|
|
|
save_endpoint => '/api/savesubs', |
|
180
|
|
|
|
|
|
|
subs_session2user_p => \&subs_session2user_p, |
|
181
|
|
|
|
|
|
|
subs_create_p => \&subs_create_p, |
|
182
|
|
|
|
|
|
|
subs_read_p => \&subs_read_p, |
|
183
|
|
|
|
|
|
|
subs_delete_p => \&subs_delete_p, |
|
184
|
|
|
|
|
|
|
ecc_private_key => 'vapid_private_key.pem', |
|
185
|
|
|
|
|
|
|
claim_sub => "mailto:admin@example.com", |
|
186
|
|
|
|
|
|
|
}; |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
sub subs_session2user_p { |
|
189
|
|
|
|
|
|
|
my ($c, $session) = @_; |
|
190
|
|
|
|
|
|
|
return Mojo::Promise->reject("Session not logged in") if !$session->{user_id}; |
|
191
|
|
|
|
|
|
|
Mojo::Promise->resolve($session->{user_id}); |
|
192
|
|
|
|
|
|
|
} |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
sub subs_create_p { |
|
195
|
|
|
|
|
|
|
my ($c, $session, $subs_info) = @_; |
|
196
|
|
|
|
|
|
|
app->db->save_subs_p($session->{user_id}, $subs_info); |
|
197
|
|
|
|
|
|
|
} |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
sub subs_read_p { |
|
200
|
|
|
|
|
|
|
my ($c, $user_id) = @_; |
|
201
|
|
|
|
|
|
|
app->db->lookup_subs_p($user_id); |
|
202
|
|
|
|
|
|
|
} |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
sub subs_delete_p { |
|
205
|
|
|
|
|
|
|
my ($c, $user_id) = @_; |
|
206
|
|
|
|
|
|
|
app->db->delete_subs_p($user_id); |
|
207
|
|
|
|
|
|
|
} |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
L is a L plugin. In |
|
212
|
|
|
|
|
|
|
order to function, your app needs to have first installed |
|
213
|
|
|
|
|
|
|
L as shown in the synopsis above. |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head1 METHODS |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
L inherits all methods from |
|
218
|
|
|
|
|
|
|
L and implements the following new ones. |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=head2 register |
|
221
|
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
my $p = $plugin->register(Mojolicious->new, \%conf); |
|
223
|
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
Register plugin in L application, returning the plugin |
|
225
|
|
|
|
|
|
|
object. Takes a hash-ref as configuration, see L for keys. |
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=head1 OPTIONS |
|
228
|
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head2 save_endpoint |
|
230
|
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
Required. The route to be added to the app for the service worker to |
|
232
|
|
|
|
|
|
|
register users for push notification. The handler for that will call |
|
233
|
|
|
|
|
|
|
the L. If success is indicated, it will return JSON: |
|
234
|
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
{ "data": { "success": true } } |
|
236
|
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
If failure: |
|
238
|
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
{ "errors": [ { "message": "The exception reason" } ] } |
|
240
|
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
This will be handled by the provided service worker. In case it is |
|
242
|
|
|
|
|
|
|
required by the app itself, the added route is named C. |
|
243
|
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=head2 subs_session2user_p |
|
245
|
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
Required. The code to be called to look up the user currently identified |
|
247
|
|
|
|
|
|
|
by this session, which returns a promise of the user ID. Must reject |
|
248
|
|
|
|
|
|
|
if no user logged in and that matters. It will be passed parameters: |
|
249
|
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=over |
|
251
|
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=item * |
|
253
|
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
The L object, to correctly identify |
|
255
|
|
|
|
|
|
|
the user. |
|
256
|
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=back |
|
258
|
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=head2 subs_create_p |
|
260
|
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
Required. The code to be called to store users registered for push |
|
262
|
|
|
|
|
|
|
notifications, which must return a promise of a true value if the |
|
263
|
|
|
|
|
|
|
operation succeeds, or reject with a reason. It will be passed parameters: |
|
264
|
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=over |
|
266
|
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
=item * |
|
268
|
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
The ID to correctly identify the user. Please note that you ought to |
|
270
|
|
|
|
|
|
|
allow one person to have several devices with web-push enabled, and to |
|
271
|
|
|
|
|
|
|
design accordingly. |
|
272
|
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=item * |
|
274
|
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
The C hash-ref, needed to push actual messages. |
|
276
|
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=back |
|
278
|
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
=head2 subs_read_p |
|
280
|
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
Required. The code to be called to look up a user registered for push |
|
282
|
|
|
|
|
|
|
notifications. It will be passed parameters: |
|
283
|
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
=over |
|
285
|
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
=item * |
|
287
|
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
The opaque information your app uses to identify the user. |
|
289
|
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
=back |
|
291
|
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
Returns a promise of the C hash-ref. Must reject if |
|
293
|
|
|
|
|
|
|
not found. |
|
294
|
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=head2 subs_delete_p |
|
296
|
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
Required. The code to be called to delete up a user registered for push |
|
298
|
|
|
|
|
|
|
notifications. It will be passed parameters: |
|
299
|
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
=over |
|
301
|
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
=item * |
|
303
|
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
The opaque information your app uses to identify the user. |
|
305
|
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
=back |
|
307
|
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
Returns a promise of the deletion result. Must reject if not found. |
|
309
|
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
=head2 ecc_private_key |
|
311
|
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
A value to be passed to L: a simple scalar is a |
|
313
|
|
|
|
|
|
|
filename, a scalar-ref is the actual key. If not provided, |
|
314
|
|
|
|
|
|
|
L will (obviously) not be able to function. |
|
315
|
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
=head2 claim_sub |
|
317
|
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
A value to be used as the C claim by the L, |
|
319
|
|
|
|
|
|
|
which needs it. Must be either an HTTPS or C URL. |
|
320
|
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
=head2 claim_exp_offset |
|
322
|
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
A value to be added to current time, in seconds, in the C claim |
|
324
|
|
|
|
|
|
|
for L. Defaults to 86400 (24 hours). The maximum |
|
325
|
|
|
|
|
|
|
valid value in RFC 8292 is 86400. |
|
326
|
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
=head2 push_handler |
|
328
|
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
Override the default push-event handler supplied to |
|
330
|
|
|
|
|
|
|
L. The default |
|
331
|
|
|
|
|
|
|
will interpret the message as a JSON object. The key C will be |
|
332
|
|
|
|
|
|
|
the notification title, deleted from that object, then the object will be |
|
333
|
|
|
|
|
|
|
the options passed to C<< .showNotification >>. |
|
334
|
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
See |
|
336
|
|
|
|
|
|
|
L |
|
337
|
|
|
|
|
|
|
for possibilities. |
|
338
|
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
=head1 HELPERS |
|
340
|
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
=head2 webpush.create_p |
|
342
|
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
$c->webpush->create_p($user_id, $subs_info)->then(sub { |
|
344
|
|
|
|
|
|
|
$c->render(json => { data => { success => \1 } }); |
|
345
|
|
|
|
|
|
|
}); |
|
346
|
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
=head2 webpush.read_p |
|
348
|
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
$c->webpush->read_p($user_id)->then(sub { |
|
350
|
|
|
|
|
|
|
$c->render(text => 'Info: ' . to_json(shift)); |
|
351
|
|
|
|
|
|
|
}); |
|
352
|
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
=head2 webpush.delete_p |
|
354
|
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
$c->webpush->delete_p($user_id)->then(sub { |
|
356
|
|
|
|
|
|
|
$c->render(json => { data => { success => \1 } }); |
|
357
|
|
|
|
|
|
|
}); |
|
358
|
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
=head2 webpush.authorization |
|
360
|
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
my $header_value = $c->webpush->authorization($subs_info); |
|
362
|
|
|
|
|
|
|
|
|
363
|
|
|
|
|
|
|
Won't function without L and L, or |
|
364
|
|
|
|
|
|
|
C<$subs_info> having a valid URL to get the base of as the C |
|
365
|
|
|
|
|
|
|
claim. Returns a suitable C header value to send to |
|
366
|
|
|
|
|
|
|
a push service. Valid for a period defined by L. |
|
367
|
|
|
|
|
|
|
but could become so to avoid unnecessary computation. |
|
368
|
|
|
|
|
|
|
|
|
369
|
|
|
|
|
|
|
=head2 webpush.public_key |
|
370
|
|
|
|
|
|
|
|
|
371
|
|
|
|
|
|
|
my $pkey = $c->webpush->public_key; |
|
372
|
|
|
|
|
|
|
|
|
373
|
|
|
|
|
|
|
Gives the app's public VAPID key, calculated from the private key. |
|
374
|
|
|
|
|
|
|
|
|
375
|
|
|
|
|
|
|
=head2 webpush.verify_token |
|
376
|
|
|
|
|
|
|
|
|
377
|
|
|
|
|
|
|
my $bool = $c->webpush->verify_token($authorization_header_value); |
|
378
|
|
|
|
|
|
|
|
|
379
|
|
|
|
|
|
|
Cryptographically verifies a JSON Web Token (JWT), such as generated |
|
380
|
|
|
|
|
|
|
by L. |
|
381
|
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
=head2 webpush.encrypt |
|
383
|
|
|
|
|
|
|
|
|
384
|
|
|
|
|
|
|
use MIME::Base64 qw(decode_base64url); |
|
385
|
|
|
|
|
|
|
my $ciphertext = $c->webpush->encrypt($data_bytes, |
|
386
|
|
|
|
|
|
|
map decode_base64url($_), @{$subscription_info->{keys}}{qw(p256dh auth)} |
|
387
|
|
|
|
|
|
|
); |
|
388
|
|
|
|
|
|
|
|
|
389
|
|
|
|
|
|
|
Returns the data encrypted according to RFC 8188, for the relevant |
|
390
|
|
|
|
|
|
|
subscriber. |
|
391
|
|
|
|
|
|
|
|
|
392
|
|
|
|
|
|
|
=head2 webpush.send_p |
|
393
|
|
|
|
|
|
|
|
|
394
|
|
|
|
|
|
|
my $result_p = $c->webpush->send_p($jsonable_data, $user_id, $ttl, $urgency); |
|
395
|
|
|
|
|
|
|
|
|
396
|
|
|
|
|
|
|
JSON-encodes the given value, encrypts it according to the given user's |
|
397
|
|
|
|
|
|
|
subscription data, adds a VAPID C header, then sends it |
|
398
|
|
|
|
|
|
|
to the relevant web-push endpoint. |
|
399
|
|
|
|
|
|
|
|
|
400
|
|
|
|
|
|
|
Returns a promise of the result, which will be a hash-ref with either a |
|
401
|
|
|
|
|
|
|
C key indicating success, or an C key for an array-ref of |
|
402
|
|
|
|
|
|
|
hash-refs with a C giving reasons. |
|
403
|
|
|
|
|
|
|
|
|
404
|
|
|
|
|
|
|
If the sending gets a status code of 404 or 410, this indicates the |
|
405
|
|
|
|
|
|
|
subscriber has unsubscribed, and L will be used to |
|
406
|
|
|
|
|
|
|
remove the registration. This is considered success. |
|
407
|
|
|
|
|
|
|
|
|
408
|
|
|
|
|
|
|
The C must be one of C, C, C (the default) |
|
409
|
|
|
|
|
|
|
or C. The C defaults to 30 seconds. |
|
410
|
|
|
|
|
|
|
|
|
411
|
|
|
|
|
|
|
=head1 TEMPLATES |
|
412
|
|
|
|
|
|
|
|
|
413
|
|
|
|
|
|
|
Various templates are available for including in the app's templates: |
|
414
|
|
|
|
|
|
|
|
|
415
|
|
|
|
|
|
|
=head2 webpush-askPermission.html.ep |
|
416
|
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
JavaScript functions, also for putting inside a C |
|
446
|
|
|
|
|
|
|
|
|
447
|
|
|
|
|
|
|
Each application must decide when to ask such permission, bearing in |
|
448
|
|
|
|
|
|
|
mind that once permission is refused, it is very difficult for the user |
|
449
|
|
|
|
|
|
|
to change such a refusal. |
|
450
|
|
|
|
|
|
|
|
|
451
|
|
|
|
|
|
|
When it is granted, the JavaScript code will communicate with the |
|
452
|
|
|
|
|
|
|
application, registering the needed information needed to web-push. |
|
453
|
|
|
|
|
|
|
|
|
454
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
455
|
|
|
|
|
|
|
|
|
456
|
|
|
|
|
|
|
L, L, L. |
|
457
|
|
|
|
|
|
|
|
|
458
|
|
|
|
|
|
|
L - command-line control of web-push. |
|
459
|
|
|
|
|
|
|
|
|
460
|
|
|
|
|
|
|
RFC 8292 - Voluntary Application Server Identification (for web push). |
|
461
|
|
|
|
|
|
|
|
|
462
|
|
|
|
|
|
|
L - Encrypted Content-Encoding for HTTP (using C). |
|
463
|
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
L |
|
465
|
|
|
|
|
|
|
|
|
466
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
|
467
|
|
|
|
|
|
|
|
|
468
|
|
|
|
|
|
|
Part of this code is ported from |
|
469
|
|
|
|
|
|
|
L. |
|
470
|
|
|
|
|
|
|
|
|
471
|
|
|
|
|
|
|
=cut |
|
472
|
|
|
|
|
|
|
|
|
473
|
|
|
|
|
|
|
__DATA__ |