blib/lib/Acme/JWT.pm

filter: Statementstmt 86.6% 91 / 105 Branchbran 53.8% 14 / 26 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 85.0% 17 / 20 Podpod 0.0% 0 / 6 totaltotal 75.9% 123 / 162 CC 15 SCAR 13.3
CC
15
Cov
79%
decode
9.6
encode
4.1
sign
3.0
CRAP 3.8
↑ Summary
1 package Acme::JWT;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 our $VERSION = '0.04';
5  
6 2 ▶use JSON qw/decode_json encode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use MIME::Base64 qw/encode_base64url decode_base64url/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Digest::SHA qw/hmac_sha256 hmac_sha384 hmac_sha512/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Crypt::OpenSSL::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our $has_sha2;
13 2 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.1
14 2 $has_sha2 = 0;
15 2 ▶ if (UNIVERSAL::can('Crypt::OpenSSL::RSA', 'use_sha512_hash')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can("Crypt::OpenSSL::RSA", "use_sha512_hash")) 2 0 2
16 2 $has_sha2 = 1;
17 }
18 }
19  
20 2 ▶sub encode {
Subroutine encode: called CC=4 SCAR=14.1
Pod undocumented
21 2 my $self = shift;
22 2 my ($payload, $key, $algorithm) = @_;
23 2 ▶ unless (defined($algorithm)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $algorithm) 1 1 2
24 1 $algorithm = 'HS256';
25 }
26 2 ▶ unless ($algorithm) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($algorithm) 0 2 2
27 0 $algorithm = 'none';
28 }
29 2 my $segments = [];
30 2 my $header = {
31 typ => 'JWT',
32 alg => $algorithm,
33 };
34 2 push(@$segments, encode_base64url(encode_json($header)));
35 2 push(@$segments, encode_base64url(encode_json($payload)));
36 2 my $signing_input = join('.', @$segments);
37 2 ▶ unless ($algorithm eq 'none') {
Branch: 1 branch
BranchTrueFalseTotal
if ($algorithm eq 'none') { } 0 2 2
38 2 my $signature = $self->sign($algorithm, $key, $signing_input);
39 1 push(@$segments, encode_base64url($signature));
40 } else {
41 0 push(@$segments, '');
42 }
43 1 return join('.', @$segments);
44 }
45  
46 1 ▶sub decode {
Subroutine decode: called CC=8 SCAR=22.6
Pod undocumented
47 1 my $self = shift;
48 1 my ($jwt, $key, $verify) = @_;
49 1 ▶ unless (defined($verify)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $verify) 1 0 1
50 1 $verify = 1;
51 }
52 1 my $segments = [split(/\./, $jwt)];
53 1 ▶ die 'Not enough or to many segments' unless (@$segments == 2 or @$segments == 3);
Branch: 1 branch
BranchTrueFalseTotal
unless @$segments == 2 or @$segments == 3 0 1 1
Condition33%1/3
@$segments == 2 or @$segments == 3
l!l&&r!l&&!r
010
MC/DC0%0/2
@$segments == 2 or @$segments == 3
@$segments == 2 @$segments == 3
Truth table33%1/3
A or B
A= @$segments == 2B= @$segments == 3
ABresult
1X1
011
000
54 1 my ($header_segment, $payload_segment, $crypt_segment) = @$segments;
55 1 my $signing_input = join('.', $header_segment, $payload_segment);
56 1 my $header;
57 my $payload;
58 1 my $signature;
59 1 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=7.1
60 1 $header = decode_json(decode_base64url($header_segment));
61 1 $payload = decode_json(decode_base64url($payload_segment));
62 1 ▶ $signature = decode_base64url($crypt_segment) if ($verify);
Branch: 1 branch
BranchTrueFalseTotal
if $verify 1 0 1
63 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 0 warn $_;
65 1 };
66 1 ▶ if ($verify) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verify) 1 0 1
67 1 my $algo = $header->{alg};
68 1 ▶ my $hmac = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
69 1 my ($algo, $key, $signing_input, $signature) = @_;
70 1 $signature eq $self->sign_hmac($algo, $key, $signing_input);
71 1 };
72 0 ▶ my $verify_method = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 0 my ($algo, $key, $signing_input, $signature) = @_;
74 0 $self->verify_rsa($algo, $key, $signing_input, $signature);
75 1 };
76 1 my $algorithm = {
77 HS256 => $hmac,
78 HS384 => $hmac,
79 HS512 => $hmac,
80 };
81  
82 1 ▶ if ($has_sha2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Acme::JWT::has_sha2) 1 0 1
83 1 $algorithm = {
84 %$algorithm,
85 (
86 RS256 => $verify_method,
87 RS384 => $verify_method,
88 RS512 => $verify_method,
89 ),
90 };
91 }
92 1 ▶ if (exists($algorithm->{$algo})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $algorithm->{$algo}) { } 1 0 1
93 1 ▶ unless ($algorithm->{$algo}->($algo, $key, $signing_input, $signature)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($algorithm->{$algo}->($algo, $key, $signing_input, $signature)) 0 1 1
94 0 die 'Signature verifacation failed';
95 }
96 } else {
97 0 die 'Algorithm not supported';
98 }
99 }
100 1 return $payload;
101 }
102  
103 2 ▶sub sign {
Subroutine sign: called CC=3 SCAR=11.1
Pod undocumented
104 2 my $self = shift;
105 2 my ($algo, $key, $signing_input) = @_;
106 1 ▶ my $hmac = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
107 1 my ($algo, $key, $signing_input) = @_;
108 1 $self->sign_hmac($algo, $key, $signing_input);
109 2 };
110 1 ▶ my $rsa = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
111 1 my ($algo, $key, $signing_input) = @_;
112 1 $self->sign_rsa($algo, $key, $signing_input);
113 2 };
114 2 my $algorithm = {
115 HS256 => $hmac,
116 HS384 => $hmac,
117 HS512 => $hmac,
118 };
119 2 ▶ if ($has_sha2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Acme::JWT::has_sha2) 2 0 2
120 2 $algorithm = {
121 %$algorithm,
122 (
123 RS256 => $rsa,
124 RS384 => $rsa,
125 RS512 => $rsa,
126 ),
127 };
128 }
129 2 ▶ unless (exists($algorithm->{$algo})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $algorithm->{$algo}) 0 2 2
130 0 die 'Unsupported signing method';
131 }
132 2 $algorithm->{$algo}->($algo, $key, $signing_input);
133 }
134  
135 1 ▶sub sign_rsa {
Subroutine sign_rsa: called CC=1 SCAR=0.0
Pod undocumented
136 1 my $self = shift;
137 1 my ($algo, $key, $msg) = @_;
138 1 $algo =~ s/\D+//;
139 1 my $private_key = Crypt::OpenSSL::RSA->new_private_key($key);
140 1 $private_key->can("use_sha${algo}_hash")->($private_key);
141 1 $private_key->sign($msg);
142 }
143  
144 0 ▶sub verify_rsa {
Subroutine verify_rsa: not called CC=1 SCAR=6.9
Pod undocumented
145 0 my $self = shift;
146 0 my ($algo, $key, $signing_input, $signature) = @_;
147 0 $algo =~ s/\D+//;
148 0 my $public_key = Crypt::OpenSSL::RSA->new_public_key($key);
149 0 $public_key->can("use_sha${algo}_hash")->($public_key);
150 0 $public_key->verify($signing_input, $signature);
151 }
152  
153 2 ▶sub sign_hmac {
Subroutine sign_hmac: called CC=1 SCAR=0.0
Pod undocumented
154 2 my $self = shift;
155 2 my ($algo, $key, $msg) = @_;
156 2 $algo =~ s/\D+//;
157 2 my $method = $self->can("hmac_sha$algo");
158 2 $method->($msg, $key);
159 }
160  
161 2 1;
162 __END__
↑ Summary