IDToken.pm

filter: Statementstmt 100% 75 / 75 Branchbran 100% 20 / 20 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 4 totaltotal 95.4% 125 / 131 CC 14 SCAR 9.2
CC
14
Cov
99%
__ANON__
5.0
encode
4.0
_verify_at_hash
3.0
CRAP 2.5
↑ Summary IDToken/Exception.pm »
1 package Net::OpenID::Connect::IDToken;
2 4 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = "0.03";
7  
8 4 ▶use parent qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use MIME::Base64 qw/encode_base64url/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use JSON::WebToken qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶use Net::OpenID::Connect::IDToken::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Net::OpenID::Connect::IDToken::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 our @EXPORT = qw/encode_id_token decode_id_token/;
18  
19  
20 9 ▶our $JWT_ENCODE = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
21 9 my ($claims, $key, $alg, $extra_headers) = @_;
22 9 JSON::WebToken->encode($claims, $key, $alg, $extra_headers);
23 4 };
24  
25 11 ▶our $JWT_DECODE = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
26 11 my ($id_token, $key, $to_be_verified) = @_;
27 11 JSON::WebToken->decode($id_token, $key, $to_be_verified);
28 4 };
29  
30 1 ▶sub encode_id_token {
Subroutine encode_id_token: called CC=1 SCAR=0.0
Pod undocumented
31 1 __PACKAGE__->encode(@_);
32 }
33  
34 4 ▶sub decode_id_token {
Subroutine decode_id_token: called CC=1 SCAR=0.0
Pod undocumented
35 4 __PACKAGE__->decode(@_);
36 }
37  
38 9 ▶sub encode {
Subroutine encode: called CC=4 SCAR=13.9
Pod undocumented
39 9 my ($class, $claims, $key, $alg, $opts, $extra_headers) = @_;
40 9 ▶ $alg ||= "HS256";
Condition100%2/2
$alg ||= "HS256"
l!l
81
MC/DC100%1/1
$alg ||= "HS256"
$alg
Truth table100%2/2
A
A= $alg
Aresult
11
00
41 9 my $id_token_claims = +{};
42  
43 9 ▶ if ( my $token = $opts->{token} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $opts->{"token"}) 4 5 9
44 4 $id_token_claims->{at_hash} = $class->_generate_token_hash($token, $alg);
45 }
46 9 ▶ if ( my $code = $opts->{code} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $opts->{"code"}) 4 5 9
47 4 $id_token_claims->{c_hash} = $class->_generate_token_hash($code, $alg);
48 }
49  
50 9 return $JWT_ENCODE->(+{ %$claims, %$id_token_claims }, $key, $alg, $extra_headers);
51 }
52  
53 16 ▶sub _generate_token_hash {
Subroutine _generate_token_hash: called CC=2 SCAR=6.9
54 16 my ($class, $token, $alg) = @_;
55 16 my $bits = substr($alg, 2); # 'HS256' -> '256'
56  
57 16 my $sha = Digest::SHA->new($bits);
58 16 ▶ unless ( $sha ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sha) 1 15 16
59 1 Net::OpenID::Connect::IDToken::Exception->throw(
60 code => ERROR_IDTOKEN_INVALID_ALGORITHM,
61 message => sprintf("%s is not supported as SHA-xxx algorithm.", $bits),
62 );
63 }
64 15 $sha->add($token);
65  
66 15 return encode_base64url(substr($sha->digest, 0, $bits / 16));
67 }
68  
69 11 ▶sub decode {
Subroutine decode: called CC=2 SCAR=6.9
Pod undocumented
70 11 my ($class, $id_token, $key, $tokens) = @_;
71  
72 11 ▶ if ( $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key) { } 6 5 11
73 6 ▶ my $tokens_verify_code = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
74 6 my ($header, $claims) = @_;
75  
76 6 ▶ if ( $tokens && $tokens->{token} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens and $tokens->{"token"}) 3 3 6
Condition100%3/3
$tokens and $tokens->{"token"}
!ll&&!rl&&r
123
MC/DC100%2/2
$tokens and $tokens->{"token"}
$tokens $tokens->{"token"}
Truth table100%3/3
A and B
A= $tokensB= $tokens->{"token"}
ABresult
0X0
100
111
77 3 $class->_verify_at_hash($tokens->{token}, $header->{alg}, $claims->{at_hash});
78 }
79 4 ▶ if ( $tokens && $tokens->{code} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens and $tokens->{"code"}) 3 1 4
Condition66%2/3
$tokens and $tokens->{"code"}
!ll&&!rl&&r
103
MC/DC50%1/2
$tokens and $tokens->{"code"}
$tokens $tokens->{"code"}
Truth table66%2/3
A and B
A= $tokensB= $tokens->{"code"}
ABresult
0X0
100
111
80 3 $class->_verify_c_hash($tokens->{code}, $header->{alg}, $claims->{c_hash});
81 }
82  
83 2 return $key;
84 6 };
85 6 return $JWT_DECODE->($id_token, $tokens_verify_code, 1);
86 }
87 else {
88 5 return $JWT_DECODE->($id_token, $key, 0);
89 }
90 }
91  
92 3 ▶sub _verify_at_hash {
Subroutine _verify_at_hash: called CC=3 SCAR=11.0
93 3 my ($class, $access_token, $alg, $at_hash) = @_;
94 3 ▶ unless ( $at_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($at_hash) 1 2 3
95 1 Net::OpenID::Connect::IDToken::Exception->throw(
96 code => ERROR_IDTOKEN_TOKEN_HASH_NOT_FOUND,
97 message => "at_hash not found in given JWT's claims",
98 );
99 }
100 2 my $expected_hash = $class->_generate_token_hash($access_token, $alg);
101 2 ▶ if ( $at_hash ne $expected_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($at_hash ne $expected_hash) 1 1 2
102 1 Net::OpenID::Connect::IDToken::Exception->throw(
103 code => ERROR_IDTOKEN_TOKEN_HASH_INVALID,
104 message => sprintf("at_hash is invalid: got = %s, expected = %s", $at_hash, $expected_hash),
105 );
106 }
107 }
108  
109 3 ▶sub _verify_c_hash {
Subroutine _verify_c_hash: called CC=3 SCAR=11.0
110 3 my ($class, $authorization_code, $alg, $c_hash) = @_;
111 3 ▶ unless ( $c_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c_hash) 1 2 3
112 1 Net::OpenID::Connect::IDToken::Exception->throw(
113 code => ERROR_IDTOKEN_CODE_HASH_NOT_FOUND,
114 message => "c_hash not found in given JWT's claims",
115 );
116 }
117 2 my $expected_hash = $class->_generate_token_hash($authorization_code, $alg);
118 2 ▶ if ( $c_hash ne $expected_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c_hash ne $expected_hash) 1 1 2
119 1 Net::OpenID::Connect::IDToken::Exception->throw(
120 code => ERROR_IDTOKEN_CODE_HASH_INVALID,
121 message => sprintf("c_hash is invalid: got = %s, expected = %s", $c_hash, $expected_hash),
122 );
123 }
124 }
125  
126 4 1;
127 __END__
↑ Summary IDToken/Exception.pm »