blib/lib/VAPID.pm

filter: Statementstmt 82.9% 161 / 194 Branchbran 63.3% 38 / 60 Conditioncond 45.4% 10 / 22 MC/DCmcdc 21.4% 3 / 14 Subroutinesub 96.1% 25 / 26 Podpod 100% 11 / 11 totaltotal 75.8% 248 / 327 CC 39 SCAR 17.0
CC
39
Cov
76%
build_push_request
9.5
validate_subject
9.3
validate_subscription
9.1
CRAP 5.5
↑ Summary
1 package VAPID;
2 2 ▶use 5.006; use strict; use warnings; our $VERSION = '1.05';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Crypt::JWT qw(encode_jwt); use Crypt::PK::ECC; use URI; use MIME::Base64 qw/encode_base64url decode_base64url/;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Crypt::AuthEnc::GCM qw(gcm_encrypt_authenticate); use Crypt::KeyDerivation qw(hkdf); use Crypt::PRNG qw(random_bytes);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use HTTP::Request; use LWP::UserAgent; use JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use base 'Import::Export';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our (%EX, $DEFAULT_SECONDS, $MAX_DEFAULT_SECONDS);
9  
10 BEGIN {
11 2 ▶ $DEFAULT_SECONDS = 12 * 60 * 60; # 12 hours
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 $MAX_DEFAULT_SECONDS = 24 * 60 * 60; # 24 hours
13 2 %EX = (
14 generate_vapid_keys => [qw/all generate/],
15 generate_future_expiration_timestamp => [qw/all generate/],
16 generate_vapid_header => [qw/all generate/],
17 validate_subject => [qw/all validate/],
18 validate_public_key => [qw/all validate/],
19 validate_private_key => [qw/all validate/],
20 validate_expiration_key => [qw/all validate/],
21 validate_expiration => [qw/all validate/],
22 validate_subscription => [qw/all validate/],
23 encrypt_payload => [qw/all encrypt/],
24 build_push_request => [qw/all push/],
25 send_push_notification => [qw/all push/],
26 );
27 }
28  
29 sub generate_vapid_keys {
30 2 ▶ my $curve = Crypt::PK::ECC->new();
Subroutine generate_vapid_keys: called CC=3 SCAR=12.7
Pod documented
31 2 $curve->generate_key('prime256v1');
32 2 my $priv = $curve->export_key_raw('private');
33 2 my $pub = $curve->export_key_raw('public');
34
35 2 ▶ if (length($priv) < 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $priv < 32) 0 2 2
36 0 my $padding = 32 - length $priv;
37 0 $priv = (0 x $padding) . $priv;
38 }
39
40 2 ▶ if (length($pub) < 65) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $pub < 65) 0 2 2
41 0 my $padding = 65 - length $pub;
42 0 $pub = (0 x $padding) . $pub;
43 }
44  
45 return (
46 2 encode_base64url($pub),
47 encode_base64url($priv)
48 );
49 }
50  
51 sub generate_vapid_header {
52 4 ▶ my ($aud, $subject, $pub, $priv, $expiration, $enc) = @_;
Subroutine generate_vapid_header: called CC=6 SCAR=18.5
Pod documented
53  
54 4 ▶ if (!$aud) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($aud) 0 4 4
55 0 die "No audience could be generated for VAPID.";
56 }
57  
58 4 ▶ if (ref $aud) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $aud) 0 4 4
59 0 die "The audience value must be a string containing the origin of a push service";
60 }
61  
62 4 my $aud_uri = URI->new($aud);
63  
64 4 ▶ if (!$aud_uri->host) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($aud_uri->host) 0 4 4
65 0 die "VAPID audience is not a url.";
66 }
67  
68 4 validate_subject($subject);
69 4 validate_public_key($pub);
70 4 $priv = validate_private_key($priv);
71  
72 4 ▶ if ($expiration) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expiration) { } 2 2 4
73 2 validate_expiration($expiration);
74 } else {
75 2 $expiration = generate_future_expiration_timestamp();
76 }
77  
78 4 my $payload = {
79 aud => $aud,
80 exp => $expiration,
81 sub => $subject
82 };
83  
84 4 my $key = Crypt::PK::ECC->new
85 ->import_key_raw($priv, 'prime256v1')
86 ->export_key_pem('private');
87  
88  
89 4 my $jwt_token = encode_jwt(
90 payload=>$payload,
91 extra_headers => { typ => 'JWT' },
92 alg=>'ES256',
93 key => \$key
94 );
95  
96 4 ▶ return $enc
Branch: 1 branch
BranchTrueFalseTotal
$enc ? : 1 3 4
97 ? {
98 Authorization => "vapit t=${jwt_token}, k=${pub}"
99 }
100 : {
101 Authorization => 'WebPush ' . $jwt_token,
102 'Crypto-Key' => 'p256ecdsa=' . $pub
103 };
104 }
105  
106 sub generate_future_expiration_timestamp {
107 5 ▶ my ($add) = shift;
Subroutine generate_future_expiration_timestamp: called CC=2 SCAR=7.1
Pod documented
108 5 ▶ return time + ($add || $DEFAULT_SECONDS);
Condition66%2/3
$add || $VAPID::DEFAULT_SECONDS
l!l&&r!l&&!r
320
MC/DC0%0/2
$add || $VAPID::DEFAULT_SECONDS
$add $VAPID::DEFAULT_SECONDS
Truth table66%2/3
A || B
A= $addB= $VAPID::DEFAULT_SECONDS
ABresult
1X1
011
000
109 }
110  
111 sub validate_subject {
112 5 ▶ my ($subject) = shift;
Subroutine validate_subject: called CC=5 SCAR=22.3
Pod documented
113
114 5 ▶ if (!$subject) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subject) 0 5 5
115 0 die "No subject passed to validate_subject";
116 }
117  
118 5 ▶ if (ref $subject) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $subject) 0 5 5
119 0 die "The subject value must be a string containing a URL or 'mailto: address.'";
120 }
121  
122 5 ▶ unless ($subject =~ m/^mailto\:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subject =~ /^mailto\:/) 0 5 5
123 0 my $uri = URI->new($subject);
124 0 ▶ if (!$uri->host) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uri->host) 0 0 0
125 0 die "VAPID subject is not a url or mailto: address";
126 }
127 }
128  
129 5 return $subject;
130 }
131  
132 sub validate_public_key {
133 5 ▶ my ($pub) = shift;
Subroutine validate_public_key: called CC=4 SCAR=16.1
Pod documented
134  
135 5 ▶ if (!$pub) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pub) 0 5 5
136 0 die "No public key passed to validate_public_key";
137 }
138  
139 5 ▶ if (ref $pub) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pub) 0 5 5
140 0 die "Vapid public key is must be a URL safe Base 64 encoded string";
141 }
142  
143 5 $pub = decode_base64url($pub);
144  
145 5 ▶ if (length $pub != 65) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $pub != 65) 0 5 5
146 0 die "VAPID public key should be 65 bytes long when decoded.";
147 }
148
149 5 return $pub;
150 }
151  
152 sub validate_private_key {
153 5 ▶ my ($priv) = shift;
Subroutine validate_private_key: called CC=4 SCAR=16.1
Pod documented
154  
155 5 ▶ if (!$priv) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($priv) 0 5 5
156 0 die "No private key passed to validate_private_key";
157 }
158  
159 5 ▶ if (ref $priv) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $priv) 0 5 5
160 0 die "VAPID private key is must be a URL safe Base 64 encoded string";
161 }
162  
163 5 $priv = decode_base64url($priv);
164
165 5 ▶ if (length $priv != 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $priv != 32) 0 5 5
166 0 die "VAPID private key should be 32 bytes long when decoded.";
167 }
168  
169 5 return $priv;
170 }
171  
172 sub validate_expiration {
173 3 ▶ my $expiration = shift;
Subroutine validate_expiration: called CC=4 SCAR=16.6
Pod documented
174  
175 3 ▶ if (!$expiration || $expiration !~ m/^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($expiration and $expiration =~ /^\d+$/) 0 3 3
Condition33%1/3
$expiration and $expiration =~ /^\d+$/
!ll&&!rl&&r
003
MC/DC0%0/2
$expiration and $expiration =~ /^\d+$/
$expiration $expiration =~ /^\d+$/
Truth table33%1/3
A and B
A= $expirationB= $expiration =~ /^\d+$/
ABresult
0X0
100
111
176 0 die "expiration value must be a number";
177 }
178  
179 3 my $max = generate_future_expiration_timestamp($MAX_DEFAULT_SECONDS);
180  
181 3 ▶ if ($expiration >= $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expiration >= $max) 0 3 3
182 0 die "expiration value is greater than maximum of 24 hours";
183 }
184
185 3 return $expiration;
186 }
187  
188 sub validate_subscription {
189 11 ▶ my ($subscription) = @_;
Subroutine validate_subscription: called CC=9 SCAR=22.0
Pod documented
190  
191 11 ▶ if (!$subscription) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subscription) 1 10 11
192 1 die "No subscription passed to validate_subscription";
193 }
194  
195 10 ▶ if (!ref $subscription || ref $subscription ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $subscription or ref $subscription ne "HASH") 1 9 10
Condition66%2/3
not ref $subscription or ref $subscription ne "HASH"
l!l&&r!l&&!r
109
MC/DC50%1/2
not ref $subscription or ref $subscription ne "HASH"
not ref $subscription ref $subscription ne "HASH"
Truth table66%2/3
not(A) or B
A= not ref $subscriptionB= ref $subscription ne "HASH"
ABresult
1X1
011
000
196 1 die "Subscription must be a hash reference";
197 }
198  
199 9 ▶ if (!$subscription->{endpoint}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subscription->{"endpoint"}) 1 8 9
200 1 die "Subscription must have an endpoint";
201 }
202  
203 8 my $uri = URI->new($subscription->{endpoint});
204 8 ▶ if (!$uri->host) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uri->host) 0 8 8
205 0 die "Subscription endpoint is not a valid URL";
206 }
207  
208 8 ▶ if (!$subscription->{keys}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subscription->{"keys"}) 1 7 8
209 1 die "Subscription must have keys";
210 }
211  
212 7 ▶ if (!$subscription->{keys}{p256dh}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subscription->{"keys"}{"p256dh"}) 1 6 7
213 1 die "Subscription must have a p256dh key";
214 }
215  
216 6 ▶ if (!$subscription->{keys}{auth}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subscription->{"keys"}{"auth"}) 1 5 6
217 1 die "Subscription must have an auth key";
218 }
219  
220 5 return $subscription;
221 }
222  
223 sub encrypt_payload {
224 3 ▶ my ($payload, $subscription) = @_;
Subroutine encrypt_payload: called CC=2 SCAR=6.9
Pod documented
225  
226 3 ▶ if (!defined $payload) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $payload) 1 2 3
227 1 die "No payload passed to encrypt_payload";
228 }
229  
230 2 validate_subscription($subscription);
231  
232 2 my $user_public_key = decode_base64url($subscription->{keys}{p256dh});
233 2 my $user_auth = decode_base64url($subscription->{keys}{auth});
234  
235 2 my $salt = random_bytes(16);
236  
237 2 my $local_key = Crypt::PK::ECC->new();
238 2 $local_key->generate_key('prime256v1');
239 2 my $local_public_key = $local_key->export_key_raw('public');
240  
241 2 my $user_key = Crypt::PK::ECC->new();
242 2 $user_key->import_key_raw($user_public_key, 'prime256v1');
243 2 my $shared_secret = $local_key->shared_secret($user_key);
244  
245 2 my $auth_info = "Content-Encoding: auth\x00";
246 2 my $prk = hkdf($shared_secret, $user_auth, 'SHA256', 32, $auth_info);
247  
248 2 my $context = "P-256\x00"
249 . pack('n', length($user_public_key)) . $user_public_key
250 . pack('n', length($local_public_key)) . $local_public_key;
251  
252 2 my $cek_info = "Content-Encoding: aesgcm\x00" . $context;
253 2 my $content_encryption_key = hkdf($prk, $salt, 'SHA256', 16, $cek_info);
254  
255 2 my $nonce_info = "Content-Encoding: nonce\x00" . $context;
256 2 my $nonce = hkdf($prk, $salt, 'SHA256', 12, $nonce_info);
257  
258 2 my $padded_payload = pack('n', 0) . $payload;
259  
260 2 my ($ciphertext, $tag) = gcm_encrypt_authenticate(
261 'AES',
262 $content_encryption_key,
263 $nonce,
264 '',
265 $padded_payload
266 );
267  
268 return {
269 2 ciphertext => $ciphertext . $tag,
270 salt => $salt,
271 local_public_key => $local_public_key
272 };
273 }
274  
275 sub build_push_request {
276 2 ▶ my (%args) = @_;
Subroutine build_push_request: called CC=8 SCAR=22.6
Pod documented
277  
278 2 my $subscription = $args{subscription};
279 2 my $payload = $args{payload};
280 2 my $vapid_public = $args{vapid_public};
281 2 my $vapid_private = $args{vapid_private};
282 2 my $subject = $args{subject};
283 2 ▶ my $ttl = $args{ttl} // 60;
Condition100%2/2
$args{"ttl"} // 60
l!l
11
MC/DC100%1/1
$args{"ttl"} // 60
$args{"ttl"}
Truth table100%2/2
A
A= $args{"ttl"}
Aresult
11
00
284 2 my $expiration = $args{expiration};
285 2 my $enc = $args{enc};
286  
287 2 validate_subscription($subscription);
288  
289 2 my $endpoint = $subscription->{endpoint};
290 2 my $uri = URI->new($endpoint);
291 2 my $audience = $uri->scheme . '://' . $uri->host;
292  
293 2 my $vapid_headers = generate_vapid_header(
294 $audience,
295 $subject,
296 $vapid_public,
297 $vapid_private,
298 $expiration,
299 $enc
300 );
301  
302 2 my $req = HTTP::Request->new(POST => $endpoint);
303 2 $req->header(TTL => $ttl);
304 2 $req->header(Authorization => $vapid_headers->{Authorization});
305  
306 2 ▶ if ($vapid_headers->{'Crypto-Key'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vapid_headers->{'Crypto-Key'}) { } 2 0 2
307 2 my $crypto_key = $vapid_headers->{'Crypto-Key'};
308  
309 2 ▶ if (defined $payload && length $payload) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $payload and length $payload) 1 1 2
Condition66%2/3
defined $payload and length $payload
!ll&&!rl&&r
101
MC/DC50%1/2
defined $payload and length $payload
defined $payload length $payload
Truth table66%2/3
A and B
A= defined $payloadB= length $payload
ABresult
0X0
100
111
310 1 my $encrypted = encrypt_payload($payload, $subscription);
311
312 1 $crypto_key .= ';dh=' . encode_base64url($encrypted->{local_public_key});
313 1 $req->header(Encryption => 'salt=' . encode_base64url($encrypted->{salt}));
314 1 $req->header('Content-Encoding' => 'aesgcm');
315 1 $req->header('Content-Type' => 'application/octet-stream');
316 1 $req->content($encrypted->{ciphertext});
317 }
318  
319 2 $req->header('Crypto-Key' => $crypto_key);
320 } else {
321 0 ▶ if (defined $payload && length $payload) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $payload and length $payload) 0 0 0
Condition0%0/3
defined $payload and length $payload
!ll&&!rl&&r
000
MC/DC0%0/2
defined $payload and length $payload
defined $payload length $payload
Truth table0%0/3
A and B
A= defined $payloadB= length $payload
ABresult
0X0
100
111
322 0 my $encrypted = encrypt_payload($payload, $subscription);
323
324 0 $req->header('Crypto-Key' => 'dh=' . encode_base64url($encrypted->{local_public_key}));
325 0 $req->header(Encryption => 'salt=' . encode_base64url($encrypted->{salt}));
326 0 $req->header('Content-Encoding' => 'aesgcm');
327 0 $req->header('Content-Type' => 'application/octet-stream');
328 0 $req->content($encrypted->{ciphertext});
329 }
330 }
331  
332 2 ▶ $req->header('Content-Length' => length($req->content // ''));
Condition50%1/2
$req->content // ""
l!l
20
MC/DC0%0/1
$req->content // ""
$req->content
Truth table50%1/2
A
A= $req->content
Aresult
11
00
333  
334 2 return $req;
335 }
336  
337 sub send_push_notification {
338 0 ▶ my (%args) = @_;
Subroutine send_push_notification: not called CC=2 SCAR=17.9
Pod documented
339  
340 0 ▶ my $ua = $args{ua} // LWP::UserAgent->new(timeout => 30);
Condition0%0/3
$args{"ua"} // "LWP::UserAgent"->new("timeout", 30)
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"ua"} // "LWP::UserAgent"->new("timeout", 30)
$args{"ua"} "LWP::UserAgent"->new("timeout", 30)
Truth table0%0/3
A // B
A= $args{"ua"}B= "LWP::UserAgent"->new("timeout", 30)
ABresult
1X1
011
000
341  
342 0 my $req = build_push_request(%args);
343 0 my $resp = $ua->request($req);
344  
345 return {
346 0 success => $resp->is_success,
347 status => $resp->code,
348 message => $resp->message,
349 response => $resp
350 };
351 }
352  
353 2 1;
354  
355 __END__
↑ Summary