ACME2/JWTMaker.pm

filter: Statementstmt 89.8% 44 / 49 Branchbran 33.3% 2 / 6 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 3 totaltotal 75.0% 60 / 80 SCAR 18.4
CC
6
Cov
80%
_payload_enc
6.5
new
2.0
_encode_json
2.0
CRAP 6.3
« ACME2/Challenge/http_01/Handler.pm ↑ Summary ACME2/X/Empty.pm »
1 package Net::ACME2::JWTMaker;
2  
3 #----------------------------------------------------------------------
4 # This module exists because of a desire to do these computations
5 # in environments where a compiler may not be available.
6 # (Otherwise, CryptX would be ideal.)
7 #----------------------------------------------------------------------
8  
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use Net::ACME2::X ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 BEGIN {
18 2 ▶ *_encode_b64u = *MIME::Base64::encode_base64url;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 }
20  
21 sub new {
22 12 ▶ my ($class, %opts) = @_;
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
23  
24 12 ▶ die Net::ACME2::X->create('Generic', 'need “key”') if !$opts{'key'};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"key"} 0 12 12
25  
26 12 return bless \%opts, $class;
27 }
28  
29 sub create_full_jws {
30 24 ▶ my ($self, %args) = @_;
Subroutine create_full_jws: called CC=1 SCAR=0.0
Pod undocumented
31  
32 24 local $args{'extra_headers'}{'jwk'} = $self->{'key'}->get_struct_for_public_jwk();
33  
34 24 return $self->_create_jwt(%args);
35 }
36  
37 sub create_key_id_jws {
38 0 ▶ my ($self, %args) = @_;
Subroutine create_key_id_jws: not called CC=1 SCAR=6.9
Pod undocumented
39  
40 0 local $args{'extra_headers'}{'kid'} = $args{'key_id'};
41  
42 0 return $self->_create_jwt(%args);
43 }
44  
45 #----------------------------------------------------------------------
46  
47 #expects:
48 # payload - unblessed string, arrayref, or hashref
49 # extra_headers - hashref
50 sub _create_jwt {
51 24 ▶ my ( $self, %args ) = @_;
Subroutine _create_jwt: called CC=1 SCAR=0.0
52  
53 24 my $alg = $self->_ALG();
54 24 my $signer_cr = $self->_get_signer();
55  
56 24 my $key = $self->{'key'};
57  
58 24 my $payload = $args{payload};
59  
60 24 my $header = { %{$args{extra_headers}} };
61  
62 # serialize payload
63 24 $payload = $self->_payload_enc($payload);
64  
65 # encode payload
66 24 my $b64u_payload = _encode_b64u($payload);
67  
68 # prepare header
69 24 $header->{alg} = $alg;
70  
71 # encode header
72 24 my $json_header = $self->_encode_json($header);
73 24 my $b64u_header = _encode_b64u($json_header);
74  
75 24 my $b64u_signature = _encode_b64u( $signer_cr->("$b64u_header.$b64u_payload") );
76  
77 24 return $self->_encode_json(
78 {
79 protected => $b64u_header,
80 payload => $b64u_payload,
81 signature => $b64u_signature,
82 }
83 );
84 }
85  
86 sub _encode_json {
87 72 ▶ my ($self, $payload) = @_;
Subroutine _encode_json: called CC=2 SCAR=7.0
88  
89 #Always do a canonical encode so that we can test more easily.
90 #Note that JWS itself does NOT require this.
91 72 ▶ $self->{'_json'} ||= JSON->new()->canonical(1);
Condition66%2/3
$self->{"_json"} ||= "JSON"->new->canonical(1)
l!l&&r!l&&!r
60120
MC/DC0%0/2
$self->{"_json"} ||= "JSON"->new->canonical(1)
$self->{"_json"} "JSON"->new->canonical(1)
Truth table66%2/3
A ||= B
A= $self->{"_json"}B= "JSON"->new->canonical(1)
ABresult
1X1
011
000
92  
93 72 return $self->{'_json'}->encode($payload);
94 }
95  
96 #Derived from Crypt::JWT
97 sub _payload_enc {
98 24 ▶ my ($self, $payload) = @_;
Subroutine _payload_enc: called CC=4 SCAR=18.7
99  
100 24 ▶ if (ref($payload) eq 'HASH' || ref($payload) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $payload eq 'HASH' or ref $payload eq 'ARRAY') { } 24 0 24
Condition33%1/3
ref $payload eq "HASH" or ref $payload eq "ARRAY"
l!l&&r!l&&!r
2400
MC/DC0%0/2
ref $payload eq "HASH" or ref $payload eq "ARRAY"
ref $payload eq "HASH" ref $payload eq "ARRAY"
Truth table33%1/3
A or B
A= ref $payload eq "HASH"B= ref $payload eq "ARRAY"
ABresult
1X1
011
000
101 24 $payload = $self->_encode_json($payload);
102 }
103 else {
104 0 ▶ utf8::downgrade($payload, 1) or do {
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($payload, 1) or do { die "Net::ACME2::X"->create("Generic", "JWT: payload ($payload) cannot contain wide character") } 0 0 0
105 0 die Net::ACME2::X->create('Generic', "JWT: payload ($payload) cannot contain wide character");
106 };
107 }
108  
109 24 return $payload;
110 }
111  
112 2 1;
« ACME2/Challenge/http_01/Handler.pm ↑ Summary ACME2/X/Empty.pm »