blib/lib/JSON/WebToken.pm

filter: Statementstmt 99.1% 117 / 118 Branchbran 97.8% 45 / 46 Conditioncond 92.0% 23 / 25 MC/DCmcdc 87.5% 14 / 16 Subroutinesub 100% 22 / 22 Podpod 71.4% 5 / 7 totaltotal 96.5% 226 / 234 SCAR 35.0
CC
33
Cov
98%
decode
17.0
encode
6.0
_ensure_class_loaded
6.0
CRAP 33.0
↑ Summary Build »
1 package JSON::WebToken;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 our $VERSION = '0.09';
8  
9 8 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use JSON qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use MIME::Base64 qw(encode_base64 decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use Module::Runtime qw(use_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 8 ▶use JSON::WebToken::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use JSON::WebToken::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 8 our @EXPORT = qw(encode_jwt decode_jwt);
20  
21 8 our $ALGORITHM_MAP = {
22 # for JWS
23 HS256 => 'HMAC',
24 HS384 => 'HMAC',
25 HS512 => 'HMAC',
26 RS256 => 'RSA',
27 RS384 => 'RSA',
28 RS512 => 'RSA',
29 # ES256 => 'EC',
30 # ES384 => 'EC',
31 # ES512 => 'EC',
32 none => 'NONE',
33  
34 # for JWE
35 RSA1_5 => 'RSA',
36 # 'RSA-OAEP' => 'OAEP',
37 # A128KW => '',
38 # A256KW => '',
39 dir => 'NONE',
40 # 'ECDH-ES' => '',
41 # 'ECDH-ES+A128KW' => '',
42 # 'ECDH-ES+A256KW' => '',
43  
44 # for JWK
45 # EC => 'EC',
46 RSA => 'RSA',
47 };
48  
49 #our $ENCRIPTION_ALGORITHM_MAP = {
50 # 'A128CBC+HS256' => 'AES_CBC',
51 # 'A256CBC+HS512' => 'AES_CBC',
52 # A128GCM => '',
53 # A256GCM => '',
54 #};
55  
56 sub encode {
57 20 ▶ my ($class, $claims, $secret, $algorithm, $extra_headers) = @_;
Subroutine encode: called CC=6 SCAR=17.9
Pod documented
58 20 ▶ unless (ref $claims eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $claims eq "HASH") 2 18 20
59 2 JSON::WebToken::Exception->throw(
60 code => ERROR_JWT_INVALID_PARAMETER,
61 message => 'Usage: JSON::WebToken->encode(\%claims [, $secret, $algorithm, \%$extra_headers ])',
62 );
63 }
64  
65 18 ▶ $algorithm ||= 'HS256';
Condition100%2/2
$algorithm ||= "HS256"
l!l
108
MC/DC100%1/1
$algorithm ||= "HS256"
$algorithm
Truth table100%2/2
A
A= $algorithm
Aresult
11
00
66 18 ▶ $extra_headers ||= {};
Condition100%2/2
$extra_headers ||= {}
l!l
117
MC/DC100%1/1
$extra_headers ||= {}
$extra_headers
Truth table100%2/2
A
A= $extra_headers
Aresult
11
00
67  
68 18 my $header = {
69 # typ parameter is OPTIONAL ("JWT" or "urn:ietf:params:oauth:token-type:jwt")
70 # typ => 'JWT',
71 alg => $algorithm,
72 %$extra_headers,
73 };
74  
75 18 $algorithm = $header->{alg};
76 18 ▶ if ($algorithm ne 'none' && !defined $secret) {
Branch: 1 branch
BranchTrueFalseTotal
if ($algorithm ne "none" and not defined $secret) 1 17 18
Condition100%3/3
$algorithm ne "none" and not defined $secret
!ll&&!rl&&r
3141
MC/DC100%2/2
$algorithm ne "none" and not defined $secret
$algorithm ne "none" not defined $secret
Truth table100%3/3
A and not(B)
A= $algorithm ne "none"B= not defined $secret
ABresult
0X0
100
111
77 1 JSON::WebToken::Exception->throw(
78 code => ERROR_JWT_MISSING_SECRET,
79 message => 'secret must be specified',
80 );
81 }
82  
83 17 my $header_segment = encode_base64url(encode_json $header);
84 17 my $claims_segment = encode_base64url(encode_json $claims);
85 17 my $signature_input = join '.', $header_segment, $claims_segment;
86  
87 17 my $signature = $class->_sign($algorithm, $signature_input, $secret);
88  
89 16 return join '.', $signature_input, encode_base64url($signature);
90 }
91  
92 sub encode_jwt {
93 18 ▶ local $Carp::CarpLevel = $Carp::CarpLevel + 1;
Subroutine encode_jwt: called CC=1 SCAR=0.0
Pod documented
94 18 __PACKAGE__->encode(@_);
95 }
96  
97 sub decode {
98 20 ▶ my ($class, $jwt, $secret, $verify_signature, $accept_algorithm_none) = @_;
Subroutine decode: called CC=17 SCAR=28.3
Pod documented
99 20 ▶ unless (defined $jwt) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $jwt) 1 19 20
100 1 JSON::WebToken::Exception->throw(
101 code => ERROR_JWT_INVALID_PARAMETER,
102 message => 'Usage: JSON::WebToken->decode($jwt [, $secret, $verify_signature, $accept_algorithm_none ])',
103 );
104 }
105  
106 19 ▶ $verify_signature = 1 unless defined $verify_signature;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $verify_signature 10 9 19
107 19 ▶ if ($verify_signature && !defined $secret) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verify_signature and not defined $secret) 1 18 19
Condition100%3/3
$verify_signature and not defined $secret
!ll&&!rl&&r
1171
MC/DC100%2/2
$verify_signature and not defined $secret
$verify_signature not defined $secret
Truth table100%3/3
A and not(B)
A= $verify_signatureB= not defined $secret
ABresult
0X0
100
111
108 1 JSON::WebToken::Exception->throw(
109 code => ERROR_JWT_MISSING_SECRET,
110 message => 'secret must be specified',
111 );
112 }
113  
114 18 my $segments = [ split '\.', $jwt ];
115 18 ▶ unless (@$segments >= 2 && @$segments <= 4) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$segments >= 2 and @$segments <= 4) 2 16 18
Condition100%3/3
@$segments >= 2 and @$segments <= 4
!ll&&!rl&&r
1116
MC/DC100%2/2
@$segments >= 2 and @$segments <= 4
@$segments >= 2 @$segments <= 4
Truth table100%3/3
A and B
A= @$segments >= 2B= @$segments <= 4
ABresult
0X0
100
111
116 2 JSON::WebToken::Exception->throw(
117 code => ERROR_JWT_INVALID_SEGMENT_COUNT,
118 message => "Not enough or too many segments by $jwt",
119 );
120 }
121  
122 16 my ($header_segment, $claims_segment, $crypto_segment) = @$segments;
123 16 my $signature_input = join '.', $header_segment, $claims_segment;
124  
125 16 my ($header, $claims, $signature);
126 16 eval {
127 16 $header = decode_json decode_base64url($header_segment);
128 15 $claims = decode_json decode_base64url($claims_segment);
129 15 ▶ $signature = decode_base64url($crypto_segment) if $header->{alg} ne 'none' && $verify_signature;
Branch: 1 branch
BranchTrueFalseTotal
if $header->{"alg"} ne "none" and $verify_signature 11 4 15
Condition100%3/3
$header->{"alg"} ne "none" and $verify_signature
!ll&&!rl&&r
3111
MC/DC100%2/2
$header->{"alg"} ne "none" and $verify_signature
$header->{"alg"} ne "none" $verify_signature
Truth table100%3/3
A and B
A= $header->{"alg"} ne "none"B= $verify_signature
ABresult
0X0
100
111
130 };
131 16 ▶ if (my $e = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 1 15 16
132 1 JSON::WebToken::Exception->throw(
133 code => ERROR_JWT_INVALID_SEGMENT_ENCODING,
134 message => 'Invalid segment encoding',
135 );
136 }
137  
138 15 ▶ return $claims unless $verify_signature;
Branch: 1 branch
BranchTrueFalseTotal
unless $verify_signature 1 14 15
139  
140 # https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-37#section-3.6
141 14 ▶ if ( $header->{alg} eq 'none' && ! $accept_algorithm_none ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header->{"alg"} eq "none" and not $accept_algorithm_none) 1 13 14
Condition100%3/3
$header->{"alg"} eq "none" and not $accept_algorithm_none
!ll&&!rl&&r
1121
MC/DC100%2/2
$header->{"alg"} eq "none" and not $accept_algorithm_none
$header->{"alg"} eq "none" not $accept_algorithm_none
Truth table100%3/3
A and not(B)
A= $header->{"alg"} eq "none"B= not $accept_algorithm_none
ABresult
0X0
100
111
142 1 JSON::WebToken::Exception->throw(
143 code => ERROR_JWT_UNACCEPTABLE_ALGORITHM,
144 message => 'Algorithm "none" is not acceptable by default',
145 );
146 }
147  
148 13 ▶ if (ref $secret eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $secret eq "CODE") 2 11 13
149 2 $secret = $secret->($header, $claims);
150 }
151  
152 13 my $algorithm = $header->{alg};
153 13 ▶ if ($algorithm eq 'none' and $crypto_segment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($algorithm eq "none" and $crypto_segment) 1 12 13
Condition100%3/3
$algorithm eq "none" and $crypto_segment
!ll&&!rl&&r
1111
MC/DC100%2/2
$algorithm eq "none" and $crypto_segment
$algorithm eq "none" $crypto_segment
Truth table100%3/3
A and B
A= $algorithm eq "none"B= $crypto_segment
ABresult
0X0
100
111
154 1 JSON::WebToken::Exception->throw(
155 code => ERROR_JWT_UNWANTED_SIGNATURE,
156 message => 'Signature must be the empty string when alg is none',
157 );
158 }
159  
160 12 ▶ unless ($class->_verify($algorithm, $signature_input, $secret, $signature)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_verify($algorithm, $signature_input, $secret, $signature)) 1 11 12
161 1 JSON::WebToken::Exception->throw(
162 code => ERROR_JWT_INVALID_SIGNATURE,
163 message => "Invalid signature by $signature",
164 );
165 }
166  
167 11 return $claims;
168 }
169  
170 sub decode_jwt {
171 18 ▶ local $Carp::CarpLevel = $Carp::CarpLevel + 1;
Subroutine decode_jwt: called CC=1 SCAR=0.0
Pod documented
172 18 __PACKAGE__->decode(@_);
173 }
174  
175 sub add_signing_algorithm {
176 2 ▶ my ($class, $algorithm, $signing_class) = @_;
Subroutine add_signing_algorithm: called CC=3 SCAR=13.3
Pod documented
177 2 ▶ unless ($algorithm && $signing_class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($algorithm and $signing_class) 0 2 2
Condition33%1/3
$algorithm and $signing_class
!ll&&!rl&&r
002
MC/DC0%0/2
$algorithm and $signing_class
$algorithm $signing_class
Truth table33%1/3
A and B
A= $algorithmB= $signing_class
ABresult
0X0
100
111
178 0 JSON::WebToken::Exception->throw(
179 code => ERROR_JWT_INVALID_PARAMETER,
180 message => 'Usage: JSON::WebToken->add_signing_algorithm($algorithm, $signing_class)',
181 );
182 }
183 2 $ALGORITHM_MAP->{$algorithm} = $signing_class;
184 }
185  
186 sub _sign {
187 17 ▶ my ($class, $algorithm, $message, $secret) = @_;
Subroutine _sign: called CC=2 SCAR=6.9
188 17 ▶ return '' if $algorithm eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $algorithm eq "none" 3 14 17
189  
190 14 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
191 14 $class->_ensure_class_loaded($algorithm)->sign($algorithm, $message, $secret);
192 }
193  
194 sub _verify {
195 12 ▶ my ($class, $algorithm, $message, $secret, $signature) = @_;
Subroutine _verify: called CC=2 SCAR=6.9
196 12 ▶ return 1 if $algorithm eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $algorithm eq "none" 1 11 12
197  
198 11 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
199 11 $class->_ensure_class_loaded($algorithm)->verify($algorithm, $message, $secret, $signature);
200 }
201  
202 8 my (%class_loaded, %alg_to_class);
203 sub _ensure_class_loaded {
204 25 ▶ my ($class, $algorithm) = @_;
Subroutine _ensure_class_loaded: called CC=6 SCAR=17.9
205 25 ▶ return $alg_to_class{$algorithm} if $alg_to_class{$algorithm};
Branch: 1 branch
BranchTrueFalseTotal
if $alg_to_class{$algorithm} 14 11 25
206  
207 11 my $klass = $ALGORITHM_MAP->{$algorithm};
208 11 ▶ unless ($klass) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($klass) 1 10 11
209 1 JSON::WebToken::Exception->throw(
210 code => ERROR_JWT_NOT_SUPPORTED_SIGNING_ALGORITHM,
211 message => "`$algorithm` is Not supported siging algorithm",
212 );
213 }
214  
215 10 ▶ my $signing_class = $klass =~ s/^\+// ? $klass : "JSON::WebToken::Crypt::$klass";
Branch: 1 branch
BranchTrueFalseTotal
$klass =~ s/^\+// ? : 1 9 10
216 10 ▶ return $signing_class if $class_loaded{$signing_class};
Branch: 1 branch
BranchTrueFalseTotal
if $class_loaded{$signing_class} 4 6 10
217  
218 6 ▶ use_module $signing_class unless $class->_is_inner_package($signing_class);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_is_inner_package($signing_class) 4 2 6
219  
220 6 $class_loaded{$signing_class} = 1;
221 6 $alg_to_class{$algorithm} = $signing_class;
222  
223 6 return $signing_class;
224 }
225  
226 sub _is_inner_package {
227 6 ▶ my ($class, $klass) = @_;
Subroutine _is_inner_package: called CC=2 SCAR=6.9
228 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
229 6 ▶ %{ "$klass\::" } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
%{"${klass}::";} ? : 2 4 6
230 }
231  
232 ####################################################
233 # Taken from newer MIME::Base64
234 # In order to support older version of MIME::Base64
235 ####################################################
236 sub encode_base64url {
237 50 ▶ my $e = encode_base64(shift, "");
Subroutine encode_base64url: called CC=1 SCAR=0.0
Pod undocumented
238 50 $e =~ s/=+\z//;
239 50 $e =~ tr[+/][-_];
240 50 return $e;
241 }
242  
243 sub decode_base64url {
244 42 ▶ my $s = shift;
Subroutine decode_base64url: called CC=2 SCAR=6.9
Pod undocumented
245 42 $s =~ tr[-_][+/];
246 42 ▶ $s .= '=' while length($s) % 4;
Branch: 1 branch
BranchTrueFalseTotal
length($s) % 4 and do { $s .= '=' } 45 42 87
247 42 return decode_base64($s);
248 }
249  
250 8 1;
251 __END__
↑ Summary Build »