| 1 | package OIDC::Client::Utils; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 12 | ▶ | use utf8; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 12 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 12 | ▶ | use Moose::Exporter; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 12 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 12 | ▶ | use MooseX::Params::Validate; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 12 | ▶ | use Carp qw(croak); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 12 | ▶ | use Crypt::PRNG qw(random_bytes_b64u); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 12 | ▶ | use Digest::SHA qw(sha256); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 12 | ▶ | use MIME::Base64 qw(encode_base64url); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | =encoding utf8 | ||||||||||||||
| 14 | |||||||||||||||
| 15 | =head1 NAME | ||||||||||||||
| 16 | |||||||||||||||
| 17 | OIDC::Client::Utils - Utility functions | ||||||||||||||
| 18 | |||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||
| 20 | |||||||||||||||
| 21 | Exports utility functions. | ||||||||||||||
| 22 | |||||||||||||||
| 23 | =cut | ||||||||||||||
| 24 | |||||||||||||||
| 25 | 12 | Moose::Exporter->setup_import_methods(as_is => [qw/get_values_from_space_delimited_string | |||||||||||||
| 26 | reach_data | ||||||||||||||
| 27 | affect_data | ||||||||||||||
| 28 | delete_data | ||||||||||||||
| 29 | generate_state | ||||||||||||||
| 30 | generate_nonce | ||||||||||||||
| 31 | generate_jti | ||||||||||||||
| 32 | generate_code_verifier | ||||||||||||||
| 33 | generate_code_challenge/]); | ||||||||||||||
| 34 | |||||||||||||||
| 35 | |||||||||||||||
| 36 | =head1 FUNCTIONS | ||||||||||||||
| 37 | |||||||||||||||
| 38 | =head2 get_values_from_space_delimited_string( $value ) | ||||||||||||||
| 39 | |||||||||||||||
| 40 | Returns the values (arrayref) from a space-delimited string value. | ||||||||||||||
| 41 | |||||||||||||||
| 42 | =cut | ||||||||||||||
| 43 | |||||||||||||||
| 44 | 24 | ▶ | sub get_values_from_space_delimited_string { | ||||||||||||
|
Subroutine
get_values_from_space_delimited_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 45 | 24 | my ($str) = pos_validated_list(\@_, { isa => 'Str', optional => 0 }); | |||||||||||||
| 46 | 24 | return [ grep { $_ ne '' } split(/\s+/, $str) ]; | |||||||||||||
| 47 | } | ||||||||||||||
| 48 | |||||||||||||||
| 49 | |||||||||||||||
| 50 | =head2 reach_data( $data_tree, \@path, $optional ) | ||||||||||||||
| 51 | |||||||||||||||
| 52 | Simplified data diver. No support for arrayref nodes. | ||||||||||||||
| 53 | |||||||||||||||
| 54 | Tries to find a node under root $data_tree, walking down the tree and choosing subnodes | ||||||||||||||
| 55 | according to values given in $path (which should be an arrayref of scalar values). | ||||||||||||||
| 56 | |||||||||||||||
| 57 | While walking down the tree, if a key doesn't exist, undef is returned if the $optional | ||||||||||||||
| 58 | parameter is true (default value), otherwise an exception is thrown if the $optional parameter | ||||||||||||||
| 59 | is false. | ||||||||||||||
| 60 | |||||||||||||||
| 61 | No autovivification is performed by this function. | ||||||||||||||
| 62 | |||||||||||||||
| 63 | =cut | ||||||||||||||
| 64 | |||||||||||||||
| 65 | 115 | ▶ | sub reach_data { | ||||||||||||
|
Subroutine
reach_data: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 66 | 115 | my ($data_tree, $path, $optional) = pos_validated_list(\@_, { isa => 'HashRef', optional => 0 }, | |||||||||||||
| 67 | { isa => 'ArrayRef[Str]', optional => 0 }, | ||||||||||||||
| 68 | { isa => 'Bool', default => 1 }); | ||||||||||||||
| 69 | 115 | foreach my $key (@$path) { | |||||||||||||
| 70 | 469 | ▶ | ref $data_tree eq 'HASH' or croak("OIDC: not a hashref to reach the value of the '$key' key"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | 468 | ▶ | if (exists $data_tree->{$key}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 72 | 450 | $data_tree = $data_tree->{$key}; | |||||||||||||
| 73 | } | ||||||||||||||
| 74 | elsif ($optional) { | ||||||||||||||
| 75 | 16 | return; | |||||||||||||
| 76 | } | ||||||||||||||
| 77 | else { | ||||||||||||||
| 78 | 2 | croak("OIDC: the '$key' key is not present"); | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | } | ||||||||||||||
| 81 | |||||||||||||||
| 82 | 96 | return $data_tree; | |||||||||||||
| 83 | } | ||||||||||||||
| 84 | |||||||||||||||
| 85 | |||||||||||||||
| 86 | =head2 affect_data( $data_tree, \@path, $value ) | ||||||||||||||
| 87 | |||||||||||||||
| 88 | Walks down the $data_tree and sets the $value to the subnode according to values given | ||||||||||||||
| 89 | in $path (which should be an arrayref of scalar values). | ||||||||||||||
| 90 | |||||||||||||||
| 91 | Arrayref nodes are not supported. | ||||||||||||||
| 92 | |||||||||||||||
| 93 | Autovivification can be performed by this function. | ||||||||||||||
| 94 | |||||||||||||||
| 95 | =cut | ||||||||||||||
| 96 | |||||||||||||||
| 97 | 76 | ▶ | sub affect_data { | ||||||||||||
|
Subroutine
affect_data: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 98 | 76 | my ($data_tree, $path, $value) = pos_validated_list(\@_, { isa => 'HashRef', optional => 0 }, | |||||||||||||
| 99 | { isa => 'ArrayRef[Str]', optional => 0 }, | ||||||||||||||
| 100 | { optional => 0 }); | ||||||||||||||
| 101 | 76 | ▶ | @$path >= 1 or croak(q{OIDC: to affect data, at least one value must be provided in the 'path' arrayref}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 102 | |||||||||||||||
| 103 | 75 | my @path_to_iterate = @$path; | |||||||||||||
| 104 | 75 | my $key_to_assign_value = pop @path_to_iterate; | |||||||||||||
| 105 | |||||||||||||||
| 106 | 75 | foreach my $key (@path_to_iterate) { | |||||||||||||
| 107 | 332 | ▶ | $data_tree = ($data_tree->{$key} //= {}); | ||||||||||||
|
Condition100%2/2
$data_tree->{$key} //= {}
MC/DC100%1/1
$data_tree->{$key} //= {}
$data_tree->{$key}
Truth table100%2/2
A
A= $data_tree->{$key}
| |||||||||||||||
| 108 | 332 | ▶ | ref $data_tree eq 'HASH' or croak("OIDC: the value of the '$key' key is not a hash reference"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 109 | } | ||||||||||||||
| 110 | 74 | $data_tree->{$key_to_assign_value} = $value; | |||||||||||||
| 111 | |||||||||||||||
| 112 | 74 | return; | |||||||||||||
| 113 | } | ||||||||||||||
| 114 | |||||||||||||||
| 115 | |||||||||||||||
| 116 | =head2 delete_data( $data_tree, \@path ) | ||||||||||||||
| 117 | |||||||||||||||
| 118 | Walks down the $data_tree, deletes the key of the last subnode and returns its value | ||||||||||||||
| 119 | according to values given in $path (which should be an arrayref of scalar values). | ||||||||||||||
| 120 | |||||||||||||||
| 121 | Arrayref nodes are not supported. | ||||||||||||||
| 122 | |||||||||||||||
| 123 | While walking down the tree, if a key doesn't exist, undef is returned. | ||||||||||||||
| 124 | |||||||||||||||
| 125 | No autovivification is performed by this function. | ||||||||||||||
| 126 | |||||||||||||||
| 127 | =cut | ||||||||||||||
| 128 | |||||||||||||||
| 129 | 17 | ▶ | sub delete_data { | ||||||||||||
|
Subroutine
delete_data: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 130 | 17 | my ($data_tree, $path) = pos_validated_list(\@_, { isa => 'HashRef', optional => 0 }, | |||||||||||||
| 131 | { isa => 'ArrayRef[Str]', optional => 0 }); | ||||||||||||||
| 132 | 17 | ▶ | @$path >= 1 or croak(q{OIDC: to delete data, at least one value must be provided in the 'path' arrayref}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 133 | |||||||||||||||
| 134 | 16 | my @path_to_iterate = @$path; | |||||||||||||
| 135 | 16 | my $key_to_delete = pop @path_to_iterate; | |||||||||||||
| 136 | |||||||||||||||
| 137 | 16 | foreach my $key (@path_to_iterate) { | |||||||||||||
| 138 | 17 | ▶ | return unless defined $data_tree->{$key}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 139 | 14 | $data_tree = $data_tree->{$key}; | |||||||||||||
| 140 | 14 | ▶ | ref $data_tree eq 'HASH' or croak("OIDC: the value of the '$key' key is not a hash reference"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 141 | } | ||||||||||||||
| 142 | |||||||||||||||
| 143 | 12 | return delete $data_tree->{$key_to_delete}; | |||||||||||||
| 144 | } | ||||||||||||||
| 145 | |||||||||||||||
| 146 | |||||||||||||||
| 147 | =head2 generate_state | ||||||||||||||
| 148 | |||||||||||||||
| 149 | Generates a cryptographically secure OAuth 2.0 C<state> value. | ||||||||||||||
| 150 | Returns a base64url-encoded string of 22 characters. | ||||||||||||||
| 151 | |||||||||||||||
| 152 | =cut | ||||||||||||||
| 153 | |||||||||||||||
| 154 | 4 | ▶ | sub generate_state { | ||||||||||||
|
Subroutine
generate_state: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 155 | 4 | return random_bytes_b64u(16); | |||||||||||||
| 156 | } | ||||||||||||||
| 157 | |||||||||||||||
| 158 | |||||||||||||||
| 159 | =head2 generate_nonce | ||||||||||||||
| 160 | |||||||||||||||
| 161 | Generates a cryptographically secure OpenID Connect C<nonce> value. | ||||||||||||||
| 162 | Returns a base64url-encoded string of 22 characters. | ||||||||||||||
| 163 | |||||||||||||||
| 164 | =cut | ||||||||||||||
| 165 | |||||||||||||||
| 166 | 2 | ▶ | sub generate_nonce { | ||||||||||||
|
Subroutine
generate_nonce: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 167 | 2 | return random_bytes_b64u(16); | |||||||||||||
| 168 | } | ||||||||||||||
| 169 | |||||||||||||||
| 170 | |||||||||||||||
| 171 | =head2 generate_jti | ||||||||||||||
| 172 | |||||||||||||||
| 173 | Generates a cryptographically secure JWT ID (C<jti>) value. | ||||||||||||||
| 174 | Returns a base64url-encoded string of 22 characters. | ||||||||||||||
| 175 | |||||||||||||||
| 176 | =cut | ||||||||||||||
| 177 | |||||||||||||||
| 178 | 2 | ▶ | sub generate_jti { | ||||||||||||
|
Subroutine
generate_jti: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 179 | 2 | return random_bytes_b64u(16); | |||||||||||||
| 180 | } | ||||||||||||||
| 181 | |||||||||||||||
| 182 | |||||||||||||||
| 183 | =head2 generate_code_verifier | ||||||||||||||
| 184 | |||||||||||||||
| 185 | Generates a cryptographically secure random code verifier for PKCE. | ||||||||||||||
| 186 | Returns a base64url-encoded string of 43 characters. | ||||||||||||||
| 187 | |||||||||||||||
| 188 | =cut | ||||||||||||||
| 189 | |||||||||||||||
| 190 | 2 | ▶ | sub generate_code_verifier { | ||||||||||||
|
Subroutine
generate_code_verifier: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 191 | 2 | return random_bytes_b64u(32); | |||||||||||||
| 192 | } | ||||||||||||||
| 193 | |||||||||||||||
| 194 | |||||||||||||||
| 195 | =head2 generate_code_challenge($code_verifier, $code_challenge_method) | ||||||||||||||
| 196 | |||||||||||||||
| 197 | Generates the code challenge from a code verifier and a code challenge method. | ||||||||||||||
| 198 | |||||||||||||||
| 199 | =cut | ||||||||||||||
| 200 | |||||||||||||||
| 201 | 10 | ▶ | sub generate_code_challenge { | ||||||||||||
|
Subroutine
generate_code_challenge: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 202 | 10 | my ($code_verifier, $code_challenge_method) = pos_validated_list(\@_, { isa => 'Str', optional => 0 }, | |||||||||||||
| 203 | { isa => enum([qw(plain S256)]), optional => 0 }); | ||||||||||||||
| 204 | 10 | ▶ | if ($code_challenge_method eq 'plain') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 205 | 3 | return $code_verifier; | |||||||||||||
| 206 | } | ||||||||||||||
| 207 | |||||||||||||||
| 208 | 7 | return encode_base64url(sha256($code_verifier)); | |||||||||||||
| 209 | } | ||||||||||||||
| 210 | |||||||||||||||
| 211 | |||||||||||||||
| 212 | 12 | 1; | |||||||||||||