blib/lib/Lemonldap/NG/Portal/Lib/OpenIDConnect.pm

filter: Statementstmt 58.5% 723 / 1235 Branchbran 38.6% 235 / 608 Conditioncond 39.0% 87 / 223 MC/DCmcdc 14.1% 18 / 127 Subroutinesub 78.7% 100 / 127 Podpod 46.8% 37 / 79 totaltotal 50.0% 1200 / 2399 CC 411 SCAR 43.0
CC
411
Cov
51%
checkIDTokenValidity
380.0
decodeJWT
380.0
getEndPointAuthenticationCredentials
335.5
CRAP 73.9
« blib/lib/Lemonldap/NG/Portal/Lib/LDAP.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/CrowdSecAgent.pm »
1 ## @file
2 # Common OpenID Connect functions
3  
4 ## @class
5 # Common OpenID Connect functions
6 package Lemonldap::NG::Portal::Lib::OpenIDConnect;
7  
8 54 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 54 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 54 ▶use Crypt::URandom;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 54 ▶use Crypt::OpenSSL::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 54 ▶use Crypt::OpenSSL::X509;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 54 ▶use Crypt::JWT qw(encode_jwt decode_jwt);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 54 ▶use Digest::SHA qw/sha1 hmac_sha256_base64 sha256 sha384 sha512 sha256_base64/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 54 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 54 ▶use Lemonldap::NG::Common::FormEncode;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 54 ▶use Lemonldap::NG::Common::OpenIDConnect::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 54 ▶use Lemonldap::NG::Common::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 use Lemonldap::NG::Common::JWT
20 54 ▶ qw(getAccessTokenSessionId getJWTPayload getJWTHeader getJWTSignature getJWTSignedData);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 use MIME::Base64
22 54 ▶ qw/encode_base64 decode_base64 encode_base64url decode_base64url/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 54 ▶use Scalar::Util qw/looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 54 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 54 ▶use URI::Escape qw(uri_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 54 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 use Lemonldap::NG::Portal::Main::Constants
29 54 ▶ qw(PE_OK PE_REDIRECT PE_ERROR portalConsts);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 54 our $VERSION = '2.23.0';
32  
33 54 use constant oidcErrorLevel => {
34 server_error => 'error',
35 invalid_request => 'warn',
36 consent_required => 'notice',
37 54 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 # PROPERTIES
40  
41 154 ▶has opAttributes => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
42 154 ▶has opMetadata => ( is => 'rw', default => sub { {} }, );
Subroutine __ANON__: called CC=1 SCAR=0.0
43 154 ▶has opOptions => ( is => 'rw', default => sub { {} }, );
Subroutine __ANON__: called CC=1 SCAR=0.0
44 154 ▶has opRules => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
45 154 ▶has rpAttributes => ( is => 'rw', default => sub { {} }, );
Subroutine __ANON__: called CC=1 SCAR=0.0
46 154 ▶has rpMacros => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
47 154 ▶has rpOptions => ( is => 'rw', default => sub { {} }, );
Subroutine __ANON__: called CC=1 SCAR=0.0
48 154 ▶has rpRules => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
49 154 ▶has rpLevelRules => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
50 154 ▶has rpScopes => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
51 154 ▶has rpScopeRules => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
52 154 ▶has rpEncKey => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
53 154 ▶has rpSigKey => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
54  
55 # Deprecated names, remove in 3.0
56 54 *oidcOPList = \&opMetadata;
57 54 *oidcRPList = \&rpOptions;
58 54 *spMacros = \&rpMacros;
59 54 *spRules = \&rpRules;
60 54 *spScopeRules = \&rpScopeRules;
61  
62 # return LWP::UserAgent object
63 has ua => (
64 is => 'rw',
65 lazy => 1,
66 builder => sub {
67 13 ▶ my $ua = Lemonldap::NG::Common::UserAgent->new( $_[0]->{conf} );
Subroutine __ANON__: called CC=1 SCAR=0.0
68 13 $ua->env_proxy();
69 13 return $ua;
70 }
71 54 );
72  
73 has state_ott => (
74 is => 'rw',
75 lazy => 1,
76 default => sub {
77 44 ▶ my $ott = $_[0]->{p}->loadModule('::Lib::OneTimeToken');
Subroutine __ANON__: called CC=2 SCAR=7.6
78 $ott->timeout( $_[0]->conf->{oidcRPStateTimeout}
79 44 ▶ || $_[0]->conf->{timeout} );
Condition33%1/3
$_[0]->conf->{'oidcRPStateTimeout'} || $_[0]->conf->{'timeout'}
l!l&&r!l&&!r
4400
MC/DC0%0/2
$_[0]->conf->{'oidcRPStateTimeout'} || $_[0]->conf->{'timeout'}
$_[0]->conf->{'oidcRPStateTimeout'} $_[0]->conf->{'timeout'}
Truth table33%1/3
A || B
A= $_[0]->conf->{'oidcRPStateTimeout'}B= $_[0]->conf->{'timeout'}
ABresult
1X1
011
000
80 44 return $ott;
81 }
82 54 );
83  
84 # Calculate conditional plugins to be loaded if required by a dynamic RP
85 has _rpConditionalPlugins => (
86 is => 'ro',
87 lazy => 1,
88 default => sub {
89 52 ▶ my %map;
Subroutine __ANON__: called CC=3 SCAR=11.0
90 52 ▶ for ( my $i = 0 ; $i < @Lemonldap::NG::Portal::Main::pList ; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @Lemonldap::NG::Portal::Main::pList) 2132 52 2184
91 2132 ▶ if ( $Lemonldap::NG::Portal::Main::pList[$i] =~
Branch: 1 branch
BranchTrueFalseTotal
if ($Lemonldap::NG::Portal::Main::pList[$i] =~ m[^or::oidcRPMetaDataOptions/\*/(.+)$]) 208 1924 2132
92 m{^or::oidcRPMetaDataOptions/\*/(.+)$} )
93 {
94 208 $map{$1} = $Lemonldap::NG::Portal::Main::pList[ $i + 1 ];
95 }
96 }
97 52 return \%map;
98 }
99 54 );
100  
101 # METHODS
102  
103 # Load OpenID Connect Providers and JWKS data
104 # @param no_cache Disable cache use
105 # @return boolean result
106 sub loadOPs {
107 93 ▶ my ($self) = @_;
Subroutine loadOPs: called CC=8 SCAR=24.8
Pod documented
108  
109 # Check cache
110 # Check presence of at least one identity provider in configuration
111 93 ▶ unless ( $self->conf->{oidcOPMetaDataJSON}
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->conf->{"oidcOPMetaDataJSON"} and keys %{$self->conf->{"oidcOPMetaDataJSON"};}) 0 93 93
Condition33%1/3
$self->conf->{"oidcOPMetaDataJSON"} and keys %{$self->conf->{"oidcOPMetaDataJSON"};}
!ll&&!rl&&r
0093
MC/DC0%0/2
$self->conf->{"oidcOPMetaDataJSON"} and keys %{$self->conf->{"oidcOPMetaDataJSON"};}
$self->conf->{"oidcOPMetaDataJSON"} keys %{$self->conf->{"oidcOPMetaDataJSON"};}
Truth table33%1/3
A and B
A= $self->conf->{"oidcOPMetaDataJSON"}B= keys %{$self->conf->{"oidcOPMetaDataJSON"};}
ABresult
0X0
100
111
112 93 and keys %{ $self->conf->{oidcOPMetaDataJSON} } )
113 {
114 0 $self->logger->warn(
115 "No OpenID Connect Provider found in configuration");
116 0 return 1;
117 }
118  
119 # Extract JSON data
120 93 foreach ( keys %{ $self->conf->{oidcOPMetaDataJSON} } ) {
121 my $op_conf =
122 97 $self->decodeJSON( $self->conf->{oidcOPMetaDataJSON}->{$_} );
123 97 ▶ if ($op_conf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op_conf) { } 97 0 97
124 97 $self->opMetadata->{$_}->{conf} = $op_conf;
125 $self->opMetadata->{$_}->{jwks} =
126 97 $self->decodeJSON( $self->conf->{oidcOPMetaDataJWKS}->{$_} );
127 }
128 else {
129 0 $self->logger->warn("Could not parse OIDC metadata for $_");
130 }
131 }
132  
133 # Set rule
134 93 foreach ( keys %{ $self->conf->{oidcOPMetaDataOptions} } ) {
135 $self->opAttributes->{$_} =
136 97 $self->conf->{oidcOPMetaDataExportedVars}->{$_};
137 97 $self->opOptions->{$_} = $self->conf->{oidcOPMetaDataOptions}->{$_};
138 my $cond =
139 97 $self->opOptions->{$_}->{oidcOPMetaDataOptionsResolutionRule};
140 97 ▶ if ( length $cond ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $cond) 0 97 97
141 0 my $rule_sub =
142 $self->p->buildRule( $cond, "OIDC provider resolution" );
143 0 ▶ if ($rule_sub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_sub) 0 0 0
144 0 $self->opRules->{$_} = $rule_sub;
145 }
146 }
147 }
148  
149 93 return 1;
150 }
151  
152 # Load a single RP from LLNG configuration
153 sub load_rp_from_llng_conf {
154 63 ▶ my ( $self, $rp ) = @_;
Subroutine load_rp_from_llng_conf: called CC=1 SCAR=0.0
Pod undocumented
155  
156 return $self->load_rp(
157 confKey => $rp,
158 extraClaims => $self->conf->{oidcRPMetaDataOptionsExtraClaims}->{$rp},
159 options => $self->conf->{oidcRPMetaDataOptions}->{$rp},
160 macros => $self->conf->{oidcRPMetaDataMacros}->{$rp},
161 scopeRules => $self->conf->{oidcRPMetaDataScopeRules}->{$rp},
162 63 attributes => $self->conf->{oidcRPMetaDataExportedVars}->{$rp},
163 );
164 }
165  
166 sub load_rp {
167 63 ▶ my ( $self, %config ) = @_;
Subroutine load_rp: called CC=46 SCAR=45.9
Pod undocumented
168 63 my $rp = $config{confKey};
169  
170 # Check if RP is activated
171 63 ▶ unless ( $config{options}->{oidcRPMetaDataOptionsActivation} // 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config{"options"}{"oidcRPMetaDataOptionsActivation"} // 1) 1 62 63
Condition100%2/2
$config{"options"}{"oidcRPMetaDataOptionsActivation"} // 1
l!l
261
MC/DC100%1/1
$config{"options"}{"oidcRPMetaDataOptionsActivation"} // 1
$config{"options"}{"oidcRPMetaDataOptionsActivation"}
Truth table100%2/2
A
A= $config{"options"}{"oidcRPMetaDataOptionsActivation"}
Aresult
11
00
172 1 $self->logger->debug("Processing $rp: RP Deactivated");
173 1 return 0;
174 }
175  
176 62 my $valid = 1;
177  
178 # Handle scopes
179 # this HAS to be a deep copy of the DEFAULT_SCOPES hashref!
180 62 my $scope_values = { %{ DEFAULT_SCOPES() } };
181  
182 # Additional claims
183 62 my $extraClaims = $config{extraClaims};
184 62 ▶ if ($extraClaims) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extraClaims) 2 60 62
185 2 $self->logger->debug("Processing extra claims for RP $rp...");
186 2 foreach my $scope ( keys %$extraClaims ) {
187 2 $self->logger->debug("Processing scope value $scope for RP $rp...");
188 2 my @extraAttributes = split( /\s/, $extraClaims->{$scope} );
189 2 $scope_values->{$scope} = \@extraAttributes;
190 }
191 }
192  
193 # Access rule
194 62 my $rule = $config{options}->{oidcRPMetaDataOptionsRule};
195 62 ▶ if ( length $rule ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rule) 3 59 62
196 3 $self->logger->debug("Processing access rule for RP $rp...");
197 3 $rule = $self->p->buildRule( $rule, "access rule for RP $rp" );
198 3 ▶ unless ($rule) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rule) 0 3 3
199 0 $valid = 0;
200 }
201 }
202  
203 # Required authentication level rule
204 my $levelrule =
205 $config{options}->{oidcRPMetaDataOptionsAuthnLevel}
206 || $self->conf->{defaultAuthnLevel}
207 62 ▶ || 0;
Condition40%2/5
$config{'options'}{'oidcRPMetaDataOptionsAuthnLevel'} || $self->conf->{'defaultAuthnLevel'} || 0
l!l
062
$config{"options"}{"oidcRPMetaDataOptionsAuthnLevel"} or $self->conf->{"defaultAuthnLevel"}
l!l&&r!l&&!r
0062
MC/DC0%0/2
$config{'options'}{'oidcRPMetaDataOptionsAuthnLevel'} || $self->conf->{'defaultAuthnLevel'} || 0
$config{"options"}{"oidcRPMetaDataOptionsAuthnLevel"} $self->conf->{"defaultAuthnLevel"}
Truth table33%1/3
A or B
A= $config{"options"}{"oidcRPMetaDataOptionsAuthnLevel"}B= $self->conf->{"defaultAuthnLevel"}
ABresult
1X1
011
000
208 62 $levelrule = $self->p->buildRule( $levelrule,
209 "Required authentication level rule for RP $rp" );
210 62 ▶ unless ($levelrule) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($levelrule) 0 62 62
211 0 $valid = 0;
212 }
213  
214 # Load per-RP macros
215 62 my $macros = $config{macros};
216 62 my $compiledMacros = {};
217 62 for my $macroAttr ( keys %{$macros} ) {
218 12 my $macroRule = $macros->{$macroAttr};
219 12 ▶ if ( length $macroRule ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $macroRule) 12 0 12
220 12 $self->logger->debug("Processing macros for RP $rp...");
221 12 $macroRule = $self->p->HANDLER->substitute($macroRule);
222 12 ▶ if ( $macroRule = $self->p->HANDLER->buildSub($macroRule) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($macroRule = $self->p->HANDLER->buildSub($macroRule)) { } 12 0 12
223 12 $compiledMacros->{$macroAttr} = $macroRule;
224 }
225 else {
226 $self->logger->error(
227 "Unable to build macro $macroAttr for RP $rp:"
228 0 . $self->p->HANDLER->tsv->{jail}->error );
229 0 $valid = 0;
230 }
231 }
232 }
233  
234 # Load per-RP dynamic scopes
235 62 my $scope_rules = $config{scopeRules};
236 62 my $compiled_scope_rules = {};
237 62 for my $scopeName ( keys %{$scope_rules} ) {
238 0 my $scopeRule = $scope_rules->{$scopeName};
239 0 ▶ if ( length $scopeRule ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $scopeRule) 0 0 0
240 0 $self->logger->debug("Processing dynamic scopes for RP $rp...");
241 0 $scopeRule = $self->p->HANDLER->substitute($scopeRule);
242 0 ▶ if ( $scopeRule = $self->p->HANDLER->buildSub($scopeRule) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scopeRule = $self->p->HANDLER->buildSub($scopeRule)) { } 0 0 0
243 0 $compiled_scope_rules->{$scopeName} = $scopeRule;
244 }
245 else {
246 $self->logger->error(
247 "Unable to build scope $scopeName for RP $rp:"
248 0 . $self->p->HANDLER->tsv->{jail}->error );
249 0 $valid = 0;
250 }
251 }
252 }
253 62 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($valid and $config{"options"}{"oidcRPMetaDataOptionsJwksUri"} || $config{"options"}{"oidcRPMetaDataOptionsJwks"}) 12 50 62
Condition50%3/6
$valid and $config{"options"}{"oidcRPMetaDataOptionsJwksUri"} || $config{"options"}{"oidcRPMetaDataOptionsJwks"}
!ll&&!rl&&r
0620
$config{"options"}{"oidcRPMetaDataOptionsJwksUri"} or $config{"options"}{"oidcRPMetaDataOptionsJwks"}
l!l&&r!l&&!r
12050
MC/DC0%0/3
$valid and $config{"options"}{"oidcRPMetaDataOptionsJwksUri"} || $config{"options"}{"oidcRPMetaDataOptionsJwks"}
$valid $config{"options"}{"oidcRPMetaDataOptionsJwksUri"} $config{"options"}{"oidcRPMetaDataOptionsJwks"}
Truth table25%1/4
A and B or C
A= $validB= $config{"options"}{"oidcRPMetaDataOptionsJwksUri"}C= $config{"options"}{"oidcRPMetaDataOptionsJwks"}
ABCresult
0XX0
1000
11X1
1011
254 $valid
255 and ( $config{options}->{oidcRPMetaDataOptionsJwksUri}
256 or $config{options}->{oidcRPMetaDataOptionsJwks} )
257 )
258 {
259 12 $self->logger->debug("Processing JWKS options for RP $rp");
260 12 my $jwks = $config{options}->{oidcRPMetaDataOptionsJwks};
261 12 ▶ $jwks = $self->decodeJSON($jwks) if $jwks and not ref $jwks;
Branch: 1 branch
BranchTrueFalseTotal
if $jwks and not ref $jwks 2 10 12
Condition66%2/3
$jwks and not ref $jwks
!ll&&!rl&&r
1002
MC/DC50%1/2
$jwks and not ref $jwks
$jwks not ref $jwks
Truth table66%2/3
A and not(B)
A= $jwksB= not ref $jwks
ABresult
0X0
100
111
262  
263 12 ▶ if ( !$jwks
Branch: 1 branch
BranchTrueFalseTotal
if (not $jwks and my $url = $config{"options"}{"oidcRPMetaDataOptionsJwksUri"}) 10 2 12
Condition66%2/3
not $jwks and my $url = $config{"options"}{"oidcRPMetaDataOptionsJwksUri"}
!ll&&!rl&&r
2010
MC/DC50%1/2
not $jwks and my $url = $config{"options"}{"oidcRPMetaDataOptionsJwksUri"}
not $jwks my $url = $config{"options"}{"oidcRPMetaDataOptionsJwksUri"}
Truth table66%2/3
not(A) and B
A= not $jwksB= my $url = $config{"options"}{"oidcRPMetaDataOptionsJwksUri"}
ABresult
0X0
100
111
264 and my $url = $config{options}->{oidcRPMetaDataOptionsJwksUri} )
265 {
266 10 $self->logger->debug("Fetching JWKS URL: $url for RP $rp");
267 10 my $resp = $self->ua->get($url);
268 10 ▶ if ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->is_success) { } 10 0 10
269 10 my $content = $self->decodeJSON( $resp->decoded_content );
270 10 ▶ if ( $content and ref($content) eq 'HASH' and $content->{keys} )
Branch: 1 branch
BranchTrueFalseTotal
if ($content and ref $content eq 'HASH' and $content->{'keys'}) { } 10 0 10
Condition33%2/6
$content and ref $content eq "HASH" and $content->{"keys"}
!ll&&!rl&&r
0010
$content and ref $content eq "HASH"
!ll&&!rl&&r
0010
MC/DC0%0/3
$content and ref $content eq "HASH" and $content->{"keys"}
$content ref $content eq "HASH" $content->{"keys"}
Truth table25%1/4
A and B and C
A= $contentB= ref $content eq "HASH"C= $content->{"keys"}
ABCresult
0XX0
10X0
1100
1111
271 {
272 10 $jwks = $content;
273 }
274 else {
275 0 $self->logger->error("Invalid response from $url");
276 0 $valid = 0;
277 }
278 }
279 else {
280 0 $self->logger->error( "Unable to fetch RP keys from $url: "
281 . $resp->status_line );
282 0 $valid = 0;
283 }
284 }
285 12 ▶ if ( $jwks and ref($jwks) eq 'HASH' and $jwks->{keys} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jwks and ref $jwks eq 'HASH' and $jwks->{'keys'}) { } 12 0 12
Condition33%2/6
$jwks and ref $jwks eq "HASH" and $jwks->{"keys"}
!ll&&!rl&&r
0012
$jwks and ref $jwks eq "HASH"
!ll&&!rl&&r
0012
MC/DC0%0/3
$jwks and ref $jwks eq "HASH" and $jwks->{"keys"}
$jwks ref $jwks eq "HASH" $jwks->{"keys"}
Truth table25%1/4
A and B and C
A= $jwksB= ref $jwks eq "HASH"C= $jwks->{"keys"}
ABCresult
0XX0
10X0
1100
1111
286 12 $self->logger->debug("Processing JWKS document for RP $rp");
287 12 my %keys;
288 my %validKeys;
289 12 foreach my $key ( sort @{ $jwks->{keys} } ) {
290 14 my $type = lc( $key->{use} );
291 14 ▶ next unless $type =~ /^(?:enc|sig)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /^(?:enc|sig)$/ 0 14 14
292 $key->{alg} = 'RSA-OAEP'
293 14 ▶ if !$key->{alg} and $key->{kty} eq 'RSA';
Branch: 1 branch
BranchTrueFalseTotal
if not $key->{"alg"} and $key->{"kty"} eq "RSA" 8 6 14
Condition100%3/3
not $key->{"alg"} and $key->{"kty"} eq "RSA"
!ll&&!rl&&r
428
MC/DC100%2/2
not $key->{"alg"} and $key->{"kty"} eq "RSA"
not $key->{"alg"} $key->{"kty"} eq "RSA"
Truth table100%3/3
not(A) and B
A= not $key->{"alg"}B= $key->{"kty"} eq "RSA"
ABresult
0X0
100
111
294 $key->{alg} = 'ES256'
295 14 ▶ if !$key->{alg} and $key->{kty} eq 'EC';
Branch: 1 branch
BranchTrueFalseTotal
if not $key->{"alg"} and $key->{"kty"} eq "EC" 2 12 14
Condition66%2/3
not $key->{"alg"} and $key->{"kty"} eq "EC"
!ll&&!rl&&r
1202
MC/DC50%1/2
not $key->{"alg"} and $key->{"kty"} eq "EC"
not $key->{"alg"} $key->{"kty"} eq "EC"
Truth table66%2/3
not(A) and B
A= not $key->{"alg"}B= $key->{"kty"} eq "EC"
ABresult
0X0
100
111
296 14 ▶ if ( $type eq 'sig' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "sig") 10 4 14
297 10 push @{ $validKeys{sig} }, $key;
298 }
299 14 ▶ if ( $key->{alg} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key->{'alg'}) { } 14 0 14
300 14 ▶ $keys{ $key->{alg} } ||= $key;
Condition33%1/3
$keys{$key->{"alg"}} ||= $key
l!l&&r!l&&!r
0140
MC/DC0%0/2
$keys{$key->{"alg"}} ||= $key
$keys{$key->{"alg"}} $key
Truth table33%1/3
A ||= B
A= $keys{$key->{"alg"}}B= $key
ABresult
1X1
011
000
301 }
302 else {
303 0 $self->logger->warn('Unable to find "alg" field in RP key');
304 }
305 }
306 12 foreach my $alg ( @{&ENC_ALG_SUPPORTED} ) {
307 60 ▶ if ( $keys{$alg} and $keys{$alg}->{use} eq 'enc' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keys{$alg} and $keys{$alg}{"use"} eq "enc") 4 56 60
Condition100%3/3
$keys{$alg} and $keys{$alg}{"use"} eq "enc"
!ll&&!rl&&r
4884
MC/DC100%2/2
$keys{$alg} and $keys{$alg}{"use"} eq "enc"
$keys{$alg} $keys{$alg}{"use"} eq "enc"
Truth table100%3/3
A and B
A= $keys{$alg}B= $keys{$alg}{"use"} eq "enc"
ABresult
0X0
100
111
308 4 $self->logger->debug(
309 "Found encryption key with algorith $alg");
310 4 $validKeys{enc}{$alg} = $keys{$alg};
311 4 last;
312 }
313 }
314 12 ▶ unless (%validKeys) {
Branch: 1 branch
BranchTrueFalseTotal
if (%validKeys) { } 12 0 12
315 0 $self->logger->error(
316 "Unable to find a supported key for RP $rp");
317 0 $valid = 0;
318 }
319 else {
320 $self->rpEncKey->{$rp} = { keys => $validKeys{enc} }
321 12 ▶ if $validKeys{enc};
Branch: 1 branch
BranchTrueFalseTotal
if $validKeys{"enc"} 4 8 12
322 $self->rpSigKey->{$rp} = { keys => $validKeys{sig} }
323 12 ▶ if $validKeys{sig};
Branch: 1 branch
BranchTrueFalseTotal
if $validKeys{"sig"} 8 4 12
324 }
325 }
326 else {
327 0 $self->logger->error('Malformed JWKS document');
328 0 $valid = 0;
329 }
330 }
331 62 ▶ if ($valid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valid) { } 62 0 62
332 62 $self->logger->debug(" -> RP $rp is valid");
333  
334 # Register RP
335 62 $self->rpOptions->{$rp} = $config{options};
336 62 $self->rpAttributes->{$rp} = $config{attributes};
337 62 $self->rpScopes->{$rp} = $scope_values;
338 62 $self->rpMacros->{$rp} = $compiledMacros;
339 62 $self->rpScopeRules->{$rp} = $compiled_scope_rules;
340 62 $self->rpRules->{$rp} = $rule;
341 62 $self->rpLevelRules->{$rp} = $levelrule;
342  
343 # Ensure plugins required by this RP's options are loaded (#3587)
344 62 $self->_loadConditionalPlugins( $config{options} );
345  
346 62 return 1;
347 }
348 else {
349 0 $self->logger->debug(" -> RP $rp is NOT valid");
350 0 $self->logger->error(
351 "Relying Party $rp has errors and will be ignored");
352 }
353 0 return 0;
354 }
355  
356 # Plugins that must be loaded when specific RP options are enabled (#3587)
357 # Built from @Lemonldap::NG::Portal::Main::pList entries matching
358 # or::oidcRPMetaDataOptions/*/...
359 sub _loadConditionalPlugins {
360 62 ▶ my ( $self, $options ) = @_;
Subroutine _loadConditionalPlugins: called CC=5 SCAR=16.2
361  
362 62 ▶ while ( my ( $opt, $plugin ) = each %{ $self->_rpConditionalPlugins } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->_rpConditionalPlugins;}) 248 62 310
363 248 ▶ next unless $options->{$opt};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{$opt} 241 7 248
364 7 ▶ my $full =
Branch: 1 branch
BranchTrueFalseTotal
$plugin =~ /^::/ ? : 7 0 7
365 ( $plugin =~ /^::/ ) ? "Lemonldap::NG::Portal$plugin" : $plugin;
366 7 ▶ next if $self->p->loadedModules->{$full};
Branch: 1 branch
BranchTrueFalseTotal
if $self->p->loadedModules->{$full} 0 7 7
367 7 $self->logger->info(
368 "Dynamically loading plugin $plugin for RP option $opt");
369 7 $self->p->loadPlugin($plugin);
370 }
371 }
372  
373 # Refresh JWKS data if needed
374 # @param no_cache Disable cache update
375 # @return boolean result
376 sub refreshJWKSdata {
377 47 ▶ my ($self) = @_;
Subroutine refreshJWKSdata: called CC=4 SCAR=15.5
Pod documented
378  
379 47 ▶ unless ( $self->conf->{oidcOPMetaDataJSON}
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->conf->{"oidcOPMetaDataJSON"} and keys %{$self->conf->{"oidcOPMetaDataJSON"};}) 0 47 47
Condition33%1/3
$self->conf->{"oidcOPMetaDataJSON"} and keys %{$self->conf->{"oidcOPMetaDataJSON"};}
!ll&&!rl&&r
0047
MC/DC0%0/2
$self->conf->{"oidcOPMetaDataJSON"} and keys %{$self->conf->{"oidcOPMetaDataJSON"};}
$self->conf->{"oidcOPMetaDataJSON"} keys %{$self->conf->{"oidcOPMetaDataJSON"};}
Truth table33%1/3
A and B
A= $self->conf->{"oidcOPMetaDataJSON"}B= keys %{$self->conf->{"oidcOPMetaDataJSON"};}
ABresult
0X0
100
111
380 47 and keys %{ $self->conf->{oidcOPMetaDataJSON} } )
381 {
382 0 $self->logger->debug(
383 "No OpenID Provider configured, JWKS data will not be refreshed");
384 0 return 1;
385 }
386  
387 47 foreach ( keys %{ $self->conf->{oidcOPMetaDataJSON} } ) {
388 49 $self->refreshJWKSdataForOp($_);
389 }
390 47 return 1;
391 }
392  
393 sub refreshJWKSdataForOp {
394 49 ▶ my ( $self, $op, $force ) = @_;
Subroutine refreshJWKSdataForOp: called CC=7 SCAR=19.8
Pod undocumented
395  
396 49 $self->logger->debug("Attempting to refresh JWKS data for $op");
397  
398 # Refresh JWKS data if
399 # 1/ oidcOPMetaDataOptionsJWKSTimeout > 0
400 # 2/ jwks_uri defined in metadata
401  
402 my $jwksTimeout =
403 49 $self->opOptions->{$op}->{oidcOPMetaDataOptionsJWKSTimeout};
404 49 my $jwksUri = $self->opMetadata->{$op}->{conf}->{jwks_uri};
405  
406 49 ▶ unless ($jwksUri) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($jwksUri) 44 5 49
407 44 $self->logger->debug("No JWKS URI defined for $op, skipping...");
408 44 return;
409 }
410  
411 5 ▶ if ( !$force ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($force) 5 0 5
412 5 ▶ unless ($jwksTimeout) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($jwksTimeout) 3 2 5
413 3 $self->logger->debug(
414 "No JWKS refresh timeout defined for $op, skipping...");
415 3 return;
416 }
417  
418 2 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opMetadata->{$op}{"jwks"}{"time"} and $self->opMetadata->{$op}{"jwks"}{"time"} + $jwksTimeout > &CORE::GLOBAL::time()) 0 2 2
Condition33%1/3
$self->opMetadata->{$op}{"jwks"}{"time"} and $self->opMetadata->{$op}{"jwks"}{"time"} + $jwksTimeout > &CORE::GLOBAL::time()
!ll&&!rl&&r
200
MC/DC0%0/2
$self->opMetadata->{$op}{"jwks"}{"time"} and $self->opMetadata->{$op}{"jwks"}{"time"} + $jwksTimeout > &CORE::GLOBAL::time()
$self->opMetadata->{$op}{"jwks"}{"time"} $self->opMetadata->{$op}{"jwks"}{"time"} + $jwksTimeout > &CORE::GLOBAL::time()
Truth table33%1/3
A and B
A= $self->opMetadata->{$op}{"jwks"}{"time"}B= $self->opMetadata->{$op}{"jwks"}{"time"} + $jwksTimeout > &CORE::GLOBAL::time()
ABresult
0X0
100
111
419 $self->opMetadata->{$op}->{jwks}->{time}
420 && (
421 $self->opMetadata->{$op}->{jwks}->{time} + $jwksTimeout > time )
422 )
423 {
424 0 $self->logger->debug("JWKS data still valid for $op, skipping...");
425 0 return;
426 }
427 }
428  
429 2 $self->logger->debug("Refresh JWKS data for $op from $jwksUri");
430  
431 2 my $response = $self->ua->get($jwksUri);
432  
433 2 ▶ if ( $response->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 1 1 2
434 1 $self->logger->warn(
435 "Unable to get JWKS data for $op from $jwksUri: "
436 . $response->message );
437 1 $self->logger->debug(
438 "JWKS response received: " . $response->as_string );
439 1 return;
440 }
441  
442 1 my $content = $self->decodeJSON( $response->decoded_content );
443  
444 1 $self->opMetadata->{$op}->{jwks} = $content;
445 1 $self->opMetadata->{$op}->{jwks}->{time} = time;
446  
447 1 return 1;
448 }
449  
450 # Compute callback URI
451 # @return String Callback URI
452 sub getCallbackUri {
453 7 ▶ my ( $self, $req ) = @_;
Subroutine getCallbackUri: called CC=1 SCAR=0.0
Pod documented
454  
455 7 my $callback_get_param = $self->conf->{oidcRPCallbackGetParam};
456 7 my $callback_uri =
457 $self->p->buildUrl( $req->portal, { $callback_get_param => 1 } );
458  
459 7 $self->logger->debug("OpenIDConnect Callback URI: $callback_uri");
460 7 return $callback_uri;
461 }
462  
463 # Build Authentication Request URI for Authorization Code Flow
464 # @param op OpenIP Provider configuration key
465 # @param state State
466 # return String Authentication Request URI
467 sub buildAuthorizationCodeAuthnRequest {
468 127 ▶ my ( $self, $req, $op, $state, $nonce ) = @_;
Subroutine buildAuthorizationCodeAuthnRequest: called CC=28 SCAR=37.2
Pod documented
469 my $authMode =
470 127 $self->opOptions->{$op}->{oidcOPMetaDataOptionsAuthnEndpointAuthMethod};
471  
472 my $authorize_uri =
473 127 $self->opMetadata->{$op}->{conf}->{authorization_endpoint};
474  
475 127 ▶ unless ($authorize_uri) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($authorize_uri) 120 7 127
476 120 $self->logger->error(
477 "Could not build Authorize request: no
478 'authorization_endpoint'" . " in JSON metadata for OP $op"
479 );
480 120 return undef;
481 }
482 7 my $client_id = $self->opOptions->{$op}->{oidcOPMetaDataOptionsClientID};
483 7 my $scope = $self->opOptions->{$op}->{oidcOPMetaDataOptionsScope};
484 7 my $response_type = "code";
485 7 my $redirect_uri = $self->getCallbackUri($req);
486 7 my $display = $self->opOptions->{$op}->{oidcOPMetaDataOptionsDisplay};
487 7 my $prompt = $self->opOptions->{$op}->{oidcOPMetaDataOptionsPrompt};
488 7 my $max_age = $self->opOptions->{$op}->{oidcOPMetaDataOptionsMaxAge};
489 7 my $ui_locales = $self->opOptions->{$op}->{oidcOPMetaDataOptionsUiLocales};
490 7 my $acr_values = $self->opOptions->{$op}->{oidcOPMetaDataOptionsAcrValues};
491 7 my $login_hint = $req->data->{suggestedLogin};
492  
493 7 ▶ my $authorize_request_oauth2_params = {
Branch: 3 branches
BranchTrueFalseTotal
defined $state ? : 7 0 7
defined $nonce ? : 6 1 7
defined $login_hint ? : 0 7 7
494 response_type => $response_type,
495 client_id => $client_id,
496 scope => $scope,
497 redirect_uri => $redirect_uri,
498 ( defined $state ? ( state => $state ) : () ),
499 ( defined $nonce ? ( nonce => $nonce ) : () ),
500 ( defined $login_hint ? ( login_hint => $login_hint ) : () ),
501 };
502  
503 7 my %custom_request_params;
504 7 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->opOptions->{$op}{"oidcOPMetaDataOptionsAuthEndpointExtraParams"} or {};}) 2 7 9
505 my ( $key, $value ) = each %{
506 $self->opOptions->{$op}
507 ->{oidcOPMetaDataOptionsAuthEndpointExtraParams} || {}
508 9 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
$self->opOptions->{$op}{"oidcOPMetaDataOptionsAuthEndpointExtraParams"} or {} 6 3 9
509 )
510 {
511  
512 # If the value looks like JSON, decode it
513 2 ▶ if ( $value =~ /^[\[\{]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^[\[\{]/) 1 1 2
514 1 my $try_json = eval { from_json($value) };
515 1 ▶ if ( defined($try_json) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $try_json) { } 1 0 1
516 1 $value = $try_json;
517 }
518 else {
519 0 $self->logger->debug(
520 "Unable to decode value for request parameter $key"
521 . " as JSON: $@" );
522 }
523 }
524 2 $custom_request_params{$key} = $value;
525 }
526  
527 7 ▶ my $authorize_request_params = {
Branch: 5 branches
BranchTrueFalseTotal
$display ? : 1 6 7
$prompt ? : 1 6 7
$max_age ? : 3 4 7
defined $ui_locales && length $ui_locales ? : 1 6 7
defined $acr_values && length $acr_values ? : 3 4 7
Condition100%6/6
defined $ui_locales and length $ui_locales
!ll&&!rl&&r
511
defined $acr_values and length $acr_values
!ll&&!rl&&r
313
MC/DC100%2/2
defined $ui_locales and length $ui_locales
defined $ui_locales length $ui_locales
MC/DC100%2/2
defined $acr_values and length $acr_values
defined $acr_values length $acr_values
Truth table100%3/3
A and B
A= defined $ui_localesB= length $ui_locales
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= defined $acr_valuesB= length $acr_values
ABresult
0X0
100
111
528 %$authorize_request_oauth2_params,
529 ( $display ? ( display => $display ) : () ),
530 ( $prompt ? ( prompt => $prompt ) : () ),
531  
532 # MaxAge is defined as an int type in LLNG config,
533 # so 0 means undefined
534 ( $max_age ? ( max_age => $max_age ) : () ),
535 (
536 defined($ui_locales)
537 && length($ui_locales) ? ( ui_locales => $ui_locales ) : ()
538 ),
539 (
540 defined($acr_values)
541 && length($acr_values) ? ( acr_values => $acr_values ) : ()
542 ),
543 %custom_request_params,
544 };
545  
546 # Call oidcGenerateAuthenticationRequest
547 7 my $h = $self->p->processHook(
548 $req, 'oidcGenerateAuthenticationRequest',
549 $op, $authorize_request_params,
550 );
551 7 ▶ return if ( $h != PE_OK );
Branch: 1 branch
BranchTrueFalseTotal
if $h != 0 0 7 7
552  
553 7 ▶ if ( $authMode and $authMode =~ /^jw(?:s|e)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($authMode and $authMode =~ /^jw(?:s|e)$/) 0 7 7
Condition33%1/3
$authMode and $authMode =~ /^jw(?:s|e)$/
!ll&&!rl&&r
700
MC/DC0%0/2
$authMode and $authMode =~ /^jw(?:s|e)$/
$authMode $authMode =~ /^jw(?:s|e)$/
Truth table33%1/3
A and B
A= $authModeB= $authMode =~ /^jw(?:s|e)$/
ABresult
0X0
100
111
554  
555 # Save hook changes if any
556 $authorize_request_oauth2_params->{$_} = $authorize_request_params->{$_}
557 0 foreach ( keys %$authorize_request_oauth2_params );
558 0 my $aud = $authorize_uri;
559 0 $aud =~ s#^(https://[^/]*).*?$#$1#;
560 my $jwt = $self->createJWTForOP( {
561 iss => $client_id,
562 aud => $aud,
563 jti => $self->generateNonce,
564 exp => time + $self->conf->{shortTokenTTL},
565 iat => time,
566 %$authorize_request_params,
567 },
568 $self->opOptions->{$op}
569 0 ▶ ->{oidcOPMetaDataOptionsAuthnEndpointAuthSigAlg} || 'RS256',
Condition0%0/2
$self->opOptions->{$op}{'oidcOPMetaDataOptionsAuthnEndpointAuthSigAlg'} || 'RS256'
l!l
00
MC/DC0%0/1
$self->opOptions->{$op}{'oidcOPMetaDataOptionsAuthnEndpointAuthSigAlg'} || 'RS256'
$self->opOptions->{$op}{'oidcOPMetaDataOptionsAuthnEndpointAuthSigAlg'}
Truth table0%0/2
A
A= $self->opOptions->{$op}{'oidcOPMetaDataOptionsAuthnEndpointAuthSigAlg'}
Aresult
11
00
570 $op
571 );
572 0 ▶ if ($jwt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jwt) { } 0 0 0
573 0 $authorize_request_params =
574 { %$authorize_request_oauth2_params, request => $jwt };
575 0 ▶ if ( $authMode eq 'jwe' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($authMode eq "jwe") 0 0 0
576 0 $self->logger->error('jwe mode not yet implemented');
577 }
578 }
579 else {
580 0 $self->logger->error(
581 'Unable to generate JWT, continue with unauthenticated query');
582 }
583 }
584  
585 # Make sure request params all contain string data, since hooks or extra
586 # claims may have inserted a complex object
587  
588 7 my %flat_authorize_request_params;
589 7 ▶ while ( my ( $key, $value ) = each %$authorize_request_params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$authorize_request_params) 52 7 59
590 52 ▶ if ( ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 1 51 52
591 1 my $try_value = eval { to_json($value) };
592 1 my $eval_error = $@;
593 1 ▶ if ( defined($try_value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $try_value) { } 1 0 1
594 1 $value = $try_value;
595 }
596 else {
597 0 $value = undef;
598 0 $self->logger->warn(
599 "Could not add $key to Authorization request: $eval_error");
600 }
601 }
602  
603 52 ▶ $flat_authorize_request_params{$key} = $value if defined($value);
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 52 0 52
604 }
605  
606 7 ▶ my $authn_uri =
Branch: 1 branch
BranchTrueFalseTotal
$authorize_uri =~ /\?/ ? : 0 7 7
607 $authorize_uri
608 . ( $authorize_uri =~ /\?/ ? '&' : '?' )
609 . build_urlencoded(%flat_authorize_request_params);
610  
611 7 $self->logger->debug(
612 "OpenIDConnect Authorization Code Flow Authn Request: $authn_uri");
613  
614 7 return $authn_uri;
615 }
616  
617 sub isResponseModeAllowed {
618 14 ▶ my ( $self, $flow, $response_mode ) = @_;
Subroutine isResponseModeAllowed: called CC=3 SCAR=13.3
Pod undocumented
619  
620 # Query encoding can only be used for authorization code flow
621 # cf oauth-v2-multiple-response-types-1_0.html
622 14 ▶ if ( $response_mode and $response_mode eq "query" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response_mode and $response_mode eq "query") 0 14 14
Condition33%1/3
$response_mode and $response_mode eq "query"
!ll&&!rl&&r
1400
MC/DC0%0/2
$response_mode and $response_mode eq "query"
$response_mode $response_mode eq "query"
Truth table33%1/3
A and B
A= $response_modeB= $response_mode eq "query"
ABresult
0X0
100
111
623 0 return ( $flow eq "authorizationcode" );
624 }
625  
626 # Fragment or Form Post are OK for all types
627 # cf oauth-v2-form-post-response-mode-1_0.html
628 14 return 1;
629 }
630  
631 # Build OpenID Connect response
632 # This method does not check if the response mode is allowed for the current
633 # grant type. Use isResponseModeAllowed for that
634 sub sendOidcResponse {
635 13 ▶ my ( $self, $req, $flow, $response_mode, $redirect_uri, $response_params )
Subroutine sendOidcResponse: called CC=5 SCAR=19.7
Pod undocumented
636 = @_;
637  
638 # RFC 9207: Add iss parameter to prevent mix-up attacks
639 13 $response_params->{iss} = $self->get_issuer($req);
640  
641 13 ▶ $response_mode //= $self->getDefaultResponseModeForFlow($flow);
Condition33%1/3
$response_mode //= $self->getDefaultResponseModeForFlow($flow)
l!l&&r!l&&!r
0130
MC/DC0%0/2
$response_mode //= $self->getDefaultResponseModeForFlow($flow)
$response_mode $self->getDefaultResponseModeForFlow($flow)
Truth table33%1/3
A //= B
A= $response_modeB= $self->getDefaultResponseModeForFlow($flow)
ABresult
1X1
011
000
642  
643 13 ▶ if ( $response_mode eq "query" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($response_mode eq 'query') { } 12 1 13
elsif ($response_mode eq 'fragment') { } 1 0 1
elsif ($response_mode eq 'form_post') { } 0 0 0
644 12 return $self->sendQueryResponse( $req, $redirect_uri,
645 $response_params );
646 }
647 elsif ( $response_mode eq "fragment" ) {
648 1 return $self->sendFragmentResponse( $req, $redirect_uri,
649 $response_params );
650 }
651 elsif ( $response_mode eq "form_post" ) {
652 0 return $self->sendFormPostResponse( $req, $redirect_uri,
653 $response_params );
654 }
655 else {
656 0 $self->logger->error("Unknown response_mode $response_mode");
657 0 return PE_ERROR;
658 }
659 }
660  
661 sub getDefaultResponseModeForFlow {
662 13 ▶ my ( $self, $flow ) = @_;
Subroutine getDefaultResponseModeForFlow: called CC=1 SCAR=0.0
Pod undocumented
663 13 my %def_flows = (
664 authorizationcode => "query",
665 implicit => "fragment",
666 hybrid => "fragment",
667 );
668 13 return $def_flows{$flow};
669 }
670  
671 sub sendQueryResponse {
672 12 ▶ my ( $self, $req, $redirect_uri, $response_params ) = @_;
Subroutine sendQueryResponse: called CC=1 SCAR=0.0
Pod undocumented
673 12 my $response_uri =
674 $self->getQueryResponse( $redirect_uri, $response_params );
675 12 return $self->_redirectToUrl( $req, $response_uri );
676 }
677  
678 sub sendFragmentResponse {
679 1 ▶ my ( $self, $req, $redirect_uri, $response_params ) = @_;
Subroutine sendFragmentResponse: called CC=1 SCAR=0.0
Pod undocumented
680 1 my $response_uri =
681 $self->getFragmentResponse( $redirect_uri, $response_params );
682 1 return $self->_redirectToUrl( $req, $response_uri );
683 }
684  
685 sub getQueryResponse {
686 12 ▶ my ( $self, $redirect_uri, $response_params ) = @_;
Subroutine getQueryResponse: called CC=2 SCAR=6.9
Pod undocumented
687 12 my $uri = URI->new($redirect_uri);
688 12 for ( keys %$response_params ) {
689 48 $uri->query_param( $_, $response_params->{$_} );
690 }
691 12 return $uri;
692 }
693  
694 sub getFragmentResponse {
695 1 ▶ my ( $self, $redirect_uri, $response_params ) = @_;
Subroutine getFragmentResponse: called CC=2 SCAR=6.9
Pod undocumented
696 1 my $uri = URI->new($redirect_uri);
697  
698 # Use a temporary URL so we can use QueryParam features to build the fragment
699 1 my $tmp = URI->new;
700 1 $tmp->query( $uri->fragment );
701 1 for ( keys %$response_params ) {
702 8 $tmp->query_param( $_, $response_params->{$_} );
703 }
704  
705 1 $uri->fragment( $tmp->query );
706  
707 1 return $uri;
708 }
709  
710 sub sendFormPostResponse {
711 0 ▶ my ( $self, $req, $redirect_uri, $response_params ) = @_;
Subroutine sendFormPostResponse: not called CC=1 SCAR=6.9
Pod undocumented
712  
713 0 $self->p->clearHiddenFormValue($req);
714 0 $req->postUrl($redirect_uri);
715 0 $req->postFields($response_params);
716 0 $req->steps( ['autoPost'] );
717 0 return PE_OK;
718 }
719  
720 sub _redirectToUrl {
721 13 ▶ my ( $self, $req, $response_url ) = @_;
Subroutine _redirectToUrl: called CC=1 SCAR=0.0
722  
723 # We must clear hidden form fields saved from the request (#2085)
724 13 $self->p->clearHiddenFormValue($req);
725 13 $self->logger->debug("Redirect user to $response_url");
726 13 $req->urldc($response_url);
727  
728 13 return PE_REDIRECT;
729 }
730  
731 # Build Authentication Response URI for Authorization Code Flow
732 # DEPRECATED, remove in 3.0, use sendOidcResponse instead
733 # @param redirect_uri Redirect URI
734 # @param code Code
735 # @param state State
736 # @param session_state Session state
737 # return String Authentication Response URI
738 sub buildAuthorizationCodeAuthnResponse {
739 0 ▶ my ( $self, $redirect_uri, $code, $state, $session_state ) = @_;
Subroutine buildAuthorizationCodeAuthnResponse: not called CC=3 SCAR=24.8
Pod documented
740  
741 0 ▶ return $self->getQueryResponse(
Branch: 2 branches
BranchTrueFalseTotal
$state ? : 0 0 0
$session_state ? : 0 0 0
742 $redirect_uri,
743 {
744 code => $code,
745 ( $state ? ( state => $state ) : () ),
746 ( $session_state ? ( session_state => $session_state ) : () )
747 }
748 );
749 }
750  
751 # Build Authentication Response URI for Implicit Flow
752 # DEPRECATED, remove in 3.0, use sendOidcResponse instead
753 # @param redirect_uri Redirect URI
754 # @param access_token Access token
755 # @param id_token ID token
756 # @param expires_in Expiration of access token
757 # @param state State
758 # @param session_state Session state
759 # return String Authentication Response URI
760 sub buildImplicitAuthnResponse {
761 0 ▶ my ( $self, $redirect_uri, $access_token, $id_token, $expires_in,
Subroutine buildImplicitAuthnResponse: not called CC=6 SCAR=37.4
Pod documented
762 $state, $session_state, $scope )
763 = @_;
764  
765 0 ▶ return $self->getFragmentResponse(
Branch: 5 branches
BranchTrueFalseTotal
$access_token ? : 0 0 0
$expires_in ? : 0 0 0
$state ? : 0 0 0
$scope ? : 0 0 0
$session_state ? : 0 0 0
766 $redirect_uri,
767 {
768 id_token => $id_token,
769 (
770 $access_token
771 ? ( token_type => 'bearer', access_token => $access_token )
772 : ()
773 ),
774 ( $expires_in ? ( expires_in => $expires_in ) : () ),
775 ( $state ? ( state => $state ) : () ),
776 ( $scope ? ( scope => $scope ) : () ),
777 ( $session_state ? ( session_state => $session_state ) : () )
778 }
779 );
780 }
781  
782 # Build Authentication Response URI for Hybrid Flow
783 # DEPRECATED, remove in 3.0, use sendOidcResponse instead
784 # @param redirect_uri Redirect URI
785 # @param code Code
786 # @param access_token Access token
787 # @param id_token ID token
788 # @param expires_in Expiration of access token
789 # @param state State
790 # @param session_state Session state
791 # return String Authentication Response URI
792 sub buildHybridAuthnResponse {
793 my (
794 0 ▶ $self, $redirect_uri, $code, $access_token, $id_token,
Subroutine buildHybridAuthnResponse: not called CC=7 SCAR=40.3
Pod documented
795 $expires_in, $state, $session_state, $scope
796 ) = @_;
797  
798 0 ▶ return $self->getFragmentResponse(
Branch: 6 branches
BranchTrueFalseTotal
$access_token ? : 0 0 0
$id_token ? : 0 0 0
$expires_in ? : 0 0 0
$state ? : 0 0 0
$scope ? : 0 0 0
$session_state ? : 0 0 0
799 $redirect_uri,
800 {
801 code => $code,
802 (
803 $access_token
804 ? ( token_type => 'bearer', access_token => $access_token )
805 : ()
806 ),
807 (
808 $id_token ? ( id_token => $id_token )
809 : ()
810 ),
811 ( $expires_in ? ( expires_in => $expires_in ) : () ),
812 ( $state ? ( state => $state ) : () ),
813 ( $scope ? ( scope => $scope ) : () ),
814 ( $session_state ? ( session_state => $session_state ) : () )
815 }
816 );
817 }
818  
819 sub getAccessTokenFromTokenEndpoint {
820 0 ▶ my ( $self, $req, $op, $grant_type, $grant_options ) = @_;
Subroutine getAccessTokenFromTokenEndpoint: not called CC=13 SCAR=52.0
Pod undocumented
821  
822 0 ▶ $grant_options ||= {};
Condition0%0/2
$grant_options ||= {}
l!l
00
MC/DC0%0/1
$grant_options ||= {}
$grant_options
Truth table0%0/2
A
A= $grant_options
Aresult
11
00
823  
824 0 my $client_id = $self->opOptions->{$op}->{oidcOPMetaDataOptionsClientID};
825 my $client_secret =
826 0 $self->opOptions->{$op}->{oidcOPMetaDataOptionsClientSecret};
827 my $access_token_uri =
828 0 $self->opMetadata->{$op}->{conf}->{token_endpoint};
829  
830 0 ▶ unless ($access_token_uri) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($access_token_uri) 0 0 0
831 0 $self->logger->error(
832 "Could not build Token request: no
833 'token_endpoint'" . " in JSON metadata for OP $op"
834 );
835 0 return 0;
836 }
837  
838 my $auth_method =
839 $self->opOptions->{$op}->{oidcOPMetaDataOptionsTokenEndpointAuthMethod}
840 0 ▶ || 'client_secret_post';
Condition0%0/2
$self->opOptions->{$op}{'oidcOPMetaDataOptionsTokenEndpointAuthMethod'} || 'client_secret_post'
l!l
00
MC/DC0%0/1
$self->opOptions->{$op}{'oidcOPMetaDataOptionsTokenEndpointAuthMethod'} || 'client_secret_post'
$self->opOptions->{$op}{'oidcOPMetaDataOptionsTokenEndpointAuthMethod'}
Truth table0%0/2
A
A= $self->opOptions->{$op}{'oidcOPMetaDataOptionsTokenEndpointAuthMethod'}
Aresult
11
00
841  
842 0 ▶ unless ( $auth_method =~
Branch: 1 branch
BranchTrueFalseTotal
unless ($auth_method =~ /^(?:client_secret_(?:(?:pos|jw)t|basic)|private_key_jwt)$/) 0 0 0
843 /^(?:client_secret_(?:(?:pos|jw)t|basic)|private_key_jwt)$/ )
844 {
845 0 $self->logger->error(
846 "Bad authentication method on token endpoint for OP $op");
847 0 return 0;
848 }
849  
850 $self->logger->debug(
851 0 "Using auth method $auth_method to token endpoint $access_token_uri");
852  
853 0 my $response;
854 my $token_request_params = {
855 grant_type => $grant_type,
856 0 %{$grant_options}
857 };
858  
859 # Call oidcGenerateTokenRequest
860 0 my $h = $self->p->processHook( $req, 'oidcGenerateTokenRequest',
861 $op, $token_request_params );
862 0 ▶ return 0 if ( $h != PE_OK );
Branch: 1 branch
BranchTrueFalseTotal
if $h != 0 0 0 0
863  
864 0 ▶ if ( $auth_method eq "client_secret_basic" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth_method eq 'client_secret_basic') { } 0 0 0
865 0 $response = $self->ua->post(
866 $access_token_uri, $token_request_params,
867 "Authorization" => "Basic "
868 . encode_base64( "$client_id:$client_secret", '' ),
869 "Content-Type" => 'application/x-www-form-urlencoded',
870 );
871 }
872 else {
873 0 ▶ if ( $auth_method eq "client_secret_post" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($auth_method eq 'client_secret_post') { } 0 0 0
elsif ($auth_method =~ /^(?:client_secret|private_key)_jwt$/) { } 0 0 0
874 0 $token_request_params->{client_id} = $client_id;
875 0 $token_request_params->{client_secret} = $client_secret;
876 }
877 elsif ( $auth_method =~ /^(?:client_secret|private_key)_jwt$/ ) {
878  
879 my $alg = $self->opOptions->{$op}
880 0 ->{oidcOPMetaDataOptionsTokenEndpointAuthSigAlg};
881 0 ▶ if ( !$alg ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($alg) 0 0 0
882 0 my $default_signing_key_type =
883 $self->_getKeyType(
884 $self->get_public_key("default-oidc-sig") );
885  
886 0 ▶ $alg =
Branch: 2 branches
BranchTrueFalseTotal
$auth_method eq 'client_secret_jwt' ? : 0 0 0
$default_signing_key_type eq 'EC' ? : 0 0 0
887 $auth_method eq 'client_secret_jwt' ? 'HS256'
888 : $default_signing_key_type eq 'EC' ? 'ES256'
889 : 'RS256';
890 }
891 0 my $time = time;
892 my $jws = $self->createJWTForOP( {
893 iss => $client_id,
894 sub => $client_id,
895 aud => $access_token_uri,
896 jti => $self->generateNonce,
897 exp => $time + $self->conf->{shortTokenTTL},
898 0 iat => $time,
899 },
900 $alg, $op
901 );
902 0 $token_request_params->{client_id} = $client_id;
903 $token_request_params->{client_assertion_type} =
904 0 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer';
905 0 $token_request_params->{client_assertion} = $jws;
906 }
907 else {
908 0 $self->logger->error("Unknown auth method $auth_method");
909 }
910 0 $response = $self->ua->post( $access_token_uri, $token_request_params,
911 "Content-Type" => 'application/x-www-form-urlencoded' );
912 }
913  
914 0 $self->logger->debug(
915 "Token request sent: " . $response->request()->as_string );
916  
917 0 ▶ if ( $response->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 0 0 0
918 0 $self->logger->error(
919 "Bad token response from $op, grant_type: $grant_type, error: "
920 . $response->message );
921 0 $self->logger->debug(
922 "Token response received: " . $response->as_string );
923 0 return 0;
924 }
925 0 return $response->decoded_content;
926 }
927  
928 # Get Token response with authorization code
929 # @param op OpenIP Provider configuration key
930 # @param code Code
931 # @param auth_method Authentication Method (optional)
932 # return String Token response decoded content
933 sub getAuthorizationCodeAccessToken {
934 0 ▶ my ( $self, $req, $op, $code ) = @_;
Subroutine getAuthorizationCodeAccessToken: not called CC=1 SCAR=6.9
Pod documented
935  
936 0 my $redirect_uri = $self->getCallbackUri($req);
937  
938 0 return $self->getAccessTokenFromTokenEndpoint( $req, $op,
939 "authorization_code",
940 { code => $code, redirect_uri => $redirect_uri } );
941 }
942  
943 # Check validity of Token Response
944 # return boolean 1 if the response is valid, 0 else
945 sub checkTokenResponseValidity {
946 0 ▶ my ( $self, $json ) = @_;
Subroutine checkTokenResponseValidity: not called CC=3 SCAR=24.8
Pod documented
947  
948 # token_type MUST be Bearer
949 0 ▶ unless ( $json->{token_type} =~ /^Bearer$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json->{"token_type"} =~ /^Bearer$/i) 0 0 0
950 $self->logger->error(
951 0 "Token type is " . $json->{token_type} . " but must be Bearer" );
952 0 return 0;
953 }
954  
955 # id_token MUST be present
956 0 ▶ unless ( $json->{id_token} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json->{"id_token"}) 0 0 0
957 0 $self->logger->error("No id_token");
958 0 return 0;
959 }
960  
961 0 return 1;
962 }
963  
964 # Check validity of ID Token
965 # return boolean 1 if the token is valid, 0 else
966 sub checkIDTokenValidity {
967 0 ▶ my ( $self, $op, $id_token, $state_nonce ) = @_;
Subroutine checkIDTokenValidity: not called CC=19 SCAR=59.4
Pod undocumented
968  
969 0 my $client_id = $self->opOptions->{$op}->{oidcOPMetaDataOptionsClientID};
970 0 my $acr_values = $self->opOptions->{$op}->{oidcOPMetaDataOptionsAcrValues};
971 0 my $max_age = $self->opOptions->{$op}->{oidcOPMetaDataOptionsMaxAge};
972 my $id_token_max_age =
973 0 $self->opOptions->{$op}->{oidcOPMetaDataOptionsIDTokenMaxAge};
974 0 my $use_nonce = $self->opOptions->{$op}->{oidcOPMetaDataOptionsUseNonce};
975  
976 # Check issuer
977 0 ▶ unless ( $id_token->{iss} eq $self->opMetadata->{$op}->{conf}->{issuer} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id_token->{"iss"} eq $self->opMetadata->{$op}{"conf"}{"issuer"}) 0 0 0
978 0 $self->logger->error("Issuer mismatch");
979 0 return 0;
980 }
981  
982 # Check audience
983 0 ▶ if ( ref $id_token->{aud} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $id_token->{'aud'}) { } 0 0 0
984 0 my @audience = @{ $id_token->{aud} };
985 0 ▶ unless ( grep { $_ eq $client_id } @audience ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $client_id;} @audience) 0 0 0
986 0 $self->logger->error("Client ID not found in audience array");
987 0 return 0;
988 }
989  
990 0 ▶ if ( $#audience > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#audience > 1) 0 0 0
991 0 ▶ unless ( $id_token->{azp} eq $client_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id_token->{"azp"} eq $client_id) 0 0 0
992 0 $self->logger->error(
993 "More than one audience, and azp not equal to client ID");
994 0 return 0;
995 }
996 }
997 }
998 else {
999 0 ▶ unless ( $id_token->{aud} eq $client_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id_token->{"aud"} eq $client_id) 0 0 0
1000 0 $self->logger->error("Audience mismatch");
1001 0 return 0;
1002 }
1003 }
1004  
1005 # Check time
1006 0 ▶ unless ( time < $id_token->{exp} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&CORE::GLOBAL::time() < $id_token->{"exp"}) 0 0 0
1007 0 $self->logger->error("ID token expired");
1008 0 return 0;
1009 }
1010  
1011 # Check iat
1012 0 my $iat = $id_token->{iat};
1013 0 ▶ if ($id_token_max_age) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id_token_max_age) 0 0 0
1014 0 ▶ unless ( $iat + $id_token_max_age > time ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($iat + $id_token_max_age > &CORE::GLOBAL::time()) 0 0 0
1015 0 $self->logger->error(
1016 "ID token too old (Max age: $id_token_max_age)");
1017 0 return 0;
1018 }
1019 }
1020  
1021 # Check nonce
1022 0 ▶ if ($use_nonce) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_nonce) 0 0 0
1023 0 my $id_token_nonce = $id_token->{nonce};
1024 0 ▶ unless ($id_token_nonce) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id_token_nonce) { } 0 0 0
1025 0 $self->logger->error("Nonce was not returned by OP $op");
1026 0 return 0;
1027 }
1028 else {
1029 # Get nonce session
1030 0 ▶ unless ( $id_token_nonce eq $state_nonce ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id_token_nonce eq $state_nonce) 0 0 0
1031 0 $self->logger->error(
1032 "Nonce $id_token_nonce verification failed, expected $state_nonce"
1033 );
1034 0 return 0;
1035 }
1036 }
1037 }
1038  
1039 # Check acr
1040 0 my $acr = $id_token->{acr};
1041 0 ▶ if ($acr_values) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acr_values) 0 0 0
1042 0 ▶ unless ($acr) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($acr) 0 0 0
1043 0 $self->logger->error("ACR was not returned by OP $op");
1044 0 return 0;
1045 }
1046 0 ▶ unless ( grep { $_ eq $acr } split( /[\s,]+/, $acr_values ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $acr;} split(/[\s,]+/, $acr_values, 0)) 0 0 0
1047 0 $self->logger->error(
1048 "ACR $acr not listed in request ACR values ($acr_values)");
1049 0 return 0;
1050 }
1051 }
1052  
1053 # Check auth_time
1054 0 my $auth_time = $id_token->{auth_time};
1055 0 ▶ if ($max_age) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_age) 0 0 0
1056 0 ▶ unless ($auth_time) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($auth_time) 0 0 0
1057 0 $self->logger->error("Auth time was not returned by OP $op");
1058 0 return 0;
1059 }
1060 0 ▶ if ( time > $auth_time + $max_age ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&CORE::GLOBAL::time() > $auth_time + $max_age) 0 0 0
1061 0 $self->userLogger->error(
1062 "Authentication time ($auth_time) is too old (Max age: $max_age)"
1063 );
1064 0 return 0;
1065 }
1066 }
1067  
1068 0 return 1;
1069 }
1070  
1071 # Returns the current OP and a valid Access token
1072 sub getUserInfoParams {
1073 0 ▶ my ( $self, $req ) = @_;
Subroutine getUserInfoParams: not called CC=2 SCAR=17.9
Pod undocumented
1074  
1075 0 my $op = $req->data->{_oidcOPCurrent};
1076  
1077 0 ▶ if ($op) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op) { } 0 0 0
1078  
1079 # We are in the middle of an auth process,
1080 # access token has just been fetched already
1081 0 my $access_token = $req->data->{access_token};
1082 0 return ( $op, $access_token );
1083 }
1084 else {
1085 # Get OP and access token from existing session (refresh)
1086 0 return $self->getUserInfoParamsFromSession($req);
1087 }
1088 }
1089  
1090 sub getUserInfoParamsFromSession {
1091 0 ▶ my ( $self, $req ) = @_;
Subroutine getUserInfoParamsFromSession: not called CC=3 SCAR=24.8
Pod undocumented
1092 0 my $op = $req->userData->{_oidc_OP};
1093  
1094 # Save current OP, we will need it for setSessionInfo & friends
1095 0 $req->data->{_oidcOPCurrent} = $op;
1096  
1097 0 ▶ if ($op) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op) { } 0 0 0
1098 0 my $access_token = $req->userData->{_oidc_access_token};
1099 0 my $access_token_eol = $req->userData->{_oidc_access_token_eol};
1100 0 ▶ if ($access_token_eol) {
Branch: 1 branch
BranchTrueFalseTotal
if ($access_token_eol) { } 0 0 0
1101 0 return $self->refreshAccessTokenIfExpired( $req, $op );
1102 }
1103 else {
1104 # We don't know the TTL for this access token,
1105 # so we can only hope that it works
1106 0 return ( $op, $access_token );
1107 }
1108 }
1109 else {
1110 0 $self->logger->warn("No OP found in session");
1111 0 return ( $op, undef );
1112 }
1113 }
1114  
1115 sub refreshAccessTokenIfExpired {
1116 0 ▶ my ( $self, $req, $op, $session ) = @_;
Subroutine refreshAccessTokenIfExpired: not called CC=3 SCAR=24.8
Pod undocumented
1117  
1118 # Handle unauthenticated OIDC calls
1119 0 ▶ my $data = $session ? $session->data : $req->userData;
Branch: 1 branch
BranchTrueFalseTotal
$session ? : 0 0 0
1120  
1121 0 my $access_token = $data->{_oidc_access_token};
1122 0 my $access_token_eol = $data->{_oidc_access_token_eol};
1123 0 ▶ if ( time < $access_token_eol ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&CORE::GLOBAL::time() < $access_token_eol) { } 0 0 0
1124  
1125 # Access Token is still valid, return it
1126 0 return ( $op, $access_token );
1127 }
1128 else {
1129 # Refresh Access Token
1130 0 return ( $op, $self->refreshAccessToken( $req, $op, $session ) );
1131 }
1132 }
1133  
1134 sub refreshAccessToken {
1135 0 ▶ my ( $self, $req, $op, $session ) = @_;
Subroutine refreshAccessToken: not called CC=11 SCAR=48.8
Pod undocumented
1136  
1137 # Handle unauthenticated OIDC calls
1138 0 ▶ my $data = $session ? $session->data : $req->userData;
Branch: 1 branch
BranchTrueFalseTotal
$session ? : 0 0 0
1139 0 ▶ my $session_id = $session ? $session->id : $req->id;
Branch: 1 branch
BranchTrueFalseTotal
$session ? : 0 0 0
1140 0 my $refresh_token = $data->{_oidc_refresh_token};
1141  
1142 0 ▶ if ($refresh_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refresh_token) { } 0 0 0
1143  
1144 0 my $content =
1145 $self->getAccessTokenFromTokenEndpoint( $req, $op, 'refresh_token',
1146 { refresh_token => $refresh_token } );
1147  
1148 0 ▶ if ($content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content) { } 0 0 0
1149 0 my $token_response = $self->decodeTokenResponse($content);
1150 0 ▶ if ($token_response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_response) { } 0 0 0
1151  
1152 0 my $access_token = $token_response->{access_token};
1153 0 my $expires_in = $token_response->{expires_in};
1154 0 my $refresh_token = $token_response->{refresh_token};
1155  
1156 0 ▶ undef $expires_in unless looks_like_number($expires_in);
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($expires_in) 0 0 0
1157  
1158 0 $self->logger->debug("Access token: $access_token");
1159 0 ▶ $self->logger->debug( "Access token expires in: "
Condition0%0/2
$expires_in || '<unknown>'
l!l
00
MC/DC0%0/1
$expires_in || '<unknown>'
$expires_in
Truth table0%0/2
A
A= $expires_in
Aresult
11
00
1160 . ( $expires_in || "<unknown>" ) );
1161 0 ▶ $self->logger->debug(
Condition0%0/2
$refresh_token || '<none>'
l!l
00
MC/DC0%0/1
$refresh_token || '<none>'
$refresh_token
Truth table0%0/2
A
A= $refresh_token
Aresult
11
00
1162 "Refresh token: " . ( $refresh_token || "<none>" ) );
1163  
1164 0 my $updateSession;
1165  
1166 # Remember tokens
1167 0 $updateSession->{_oidc_access_token} = $access_token;
1168 0 ▶ $updateSession->{_oidc_refresh_token} = $refresh_token
Branch: 1 branch
BranchTrueFalseTotal
if $refresh_token 0 0 0
1169 if $refresh_token;
1170  
1171 # If access token TTL is given save expiration date
1172 # (with security margin)
1173 0 ▶ if ($expires_in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expires_in) 0 0 0
1174 $updateSession->{_oidc_access_token_eol} =
1175 0 time + ( $expires_in * 0.9 );
1176 }
1177  
1178 0 $self->p->updateSession( $req, $updateSession, $session_id );
1179  
1180 0 return ($access_token);
1181 }
1182 else {
1183 0 $self->logger->warn("Could not decode Token Response for $op");
1184 0 return undef;
1185 }
1186 }
1187 else {
1188 0 $self->logger->warn("Could not fetch new Access Token for $op");
1189 0 return undef;
1190 }
1191 }
1192 else {
1193 0 $self->logger->warn("No Refresh Token was found for $op");
1194 0 return undef;
1195 }
1196 }
1197  
1198 # Get UserInfo response
1199 # return String UserInfo response decoded content
1200 sub getUserInfo {
1201 1 ▶ my ( $self, $op, $access_token ) = @_;
Subroutine getUserInfo: called CC=5 SCAR=21.4
Pod documented
1202  
1203 my $userinfo_uri =
1204 1 $self->opMetadata->{$op}->{conf}->{userinfo_endpoint};
1205  
1206 1 ▶ unless ($userinfo_uri) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($userinfo_uri) 0 1 1
1207 0 $self->logger->error("UserInfo URI not found in $op configuration");
1208 0 return 0;
1209 }
1210  
1211 $self->logger->debug(
1212 1 "Request UserInfo on $userinfo_uri with access token $access_token");
1213  
1214 1 my $response = $self->ua->get( $userinfo_uri,
1215 "Authorization" => "Bearer $access_token" );
1216  
1217 1 ▶ if ( $response->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 0 1 1
1218 0 $self->logger->error( "Bad UserInfo response: " . $response->message );
1219 0 $self->logger->debug(
1220 "UserInfo response received: " . $response->as_string );
1221 0 return 0;
1222 }
1223  
1224 1 my $userinfo_content = $response->decoded_content;
1225  
1226 1 $self->logger->debug("UserInfo received: $userinfo_content");
1227  
1228 1 my $content_type = $response->header('Content-Type');
1229 1 ▶ if ( $content_type =~ /json/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($content_type =~ /json/) { } 1 0 1
elsif ($content_type =~ /jwt/) { } 0 0 0
1230 1 return $self->decodeUserInfo($userinfo_content);
1231 }
1232 elsif ( $content_type =~ /jwt/ ) {
1233 0 my $jwt = $self->decryptJwt($userinfo_content);
1234 0 return $self->decodeJWT( $jwt, $op );
1235 }
1236 else {
1237 0 $self->logger->error('Bad UserInfo Content-Type');
1238 0 $self->logger->debug( "UserInfo response received with"
1239 . " invalid Content-Type ($content_type)" );
1240 0 return 0;
1241 }
1242 }
1243  
1244 # Convert JSON to HashRef
1245 # @return HashRef JSON decoded content
1246 sub decodeJSON {
1247 212 ▶ my ( $self, $json ) = @_;
Subroutine decodeJSON: called CC=3 SCAR=11.3
Pod documented
1248 212 my $json_hash;
1249  
1250 212 eval { $json_hash = from_json( $json, { allow_nonref => 1 } ); };
1251 212 ▶ return undef if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 4 208 212
1252 208 ▶ unless ( ref $json_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $json_hash) 0 208 208
1253 0 $self->logger->error("Wanted a JSON object, got: $json_hash");
1254 0 return undef;
1255 }
1256  
1257 208 return $json_hash;
1258 }
1259  
1260 sub decodeTokenResponse {
1261 0 ▶ return decodeJSON(@_);
Subroutine decodeTokenResponse: not called CC=1 SCAR=6.9
Pod undocumented
1262 }
1263  
1264 sub decodeUserInfo {
1265 1 ▶ return decodeJSON(@_);
Subroutine decodeUserInfo: called CC=1 SCAR=0.0
Pod undocumented
1266 }
1267  
1268 # Create a new Authorization Code
1269 # @param info hashref of session info
1270 # @return new Lemonldap::NG::Common::Session object
1271  
1272 sub newAuthorizationCode {
1273 13 ▶ my ( $self, $rp, $info ) = @_;
Subroutine newAuthorizationCode: called CC=2 SCAR=8.1
Pod documented
1274  
1275 return $self->getOpenIDConnectSession(
1276 undef,
1277 "authorization_code",
1278 ttl => $self->rpOptions->{$rp}
1279 ->{oidcRPMetaDataOptionsAuthorizationCodeExpiration}
1280 || $self->conf->{oidcServiceAuthorizationCodeExpiration},
1281 13 ▶ info => $info
Condition33%1/3
$self->rpOptions->{$rp}{'oidcRPMetaDataOptionsAuthorizationCodeExpiration'} || $self->conf->{'oidcServiceAuthorizationCodeExpiration'}
l!l&&r!l&&!r
0130
MC/DC0%0/2
$self->rpOptions->{$rp}{'oidcRPMetaDataOptionsAuthorizationCodeExpiration'} || $self->conf->{'oidcServiceAuthorizationCodeExpiration'}
$self->rpOptions->{$rp}{'oidcRPMetaDataOptionsAuthorizationCodeExpiration'} $self->conf->{'oidcServiceAuthorizationCodeExpiration'}
Truth table33%1/3
A || B
A= $self->rpOptions->{$rp}{'oidcRPMetaDataOptionsAuthorizationCodeExpiration'}B= $self->conf->{'oidcServiceAuthorizationCodeExpiration'}
ABresult
1X1
011
000
1282 );
1283 }
1284  
1285 # Get existing Authorization Code
1286 # @param id
1287 # @return new Lemonldap::NG::Common::Session object
1288  
1289 sub getAuthorizationCode {
1290 11 ▶ my ( $self, $id ) = @_;
Subroutine getAuthorizationCode: called CC=1 SCAR=0.0
Pod documented
1291  
1292 11 return $self->getOpenIDConnectSession( $id, "authorization_code" );
1293 }
1294  
1295 # Create a new Access Token
1296 # @param req current request
1297 # @param scope access token scope
1298 # @param rp configuration key of the RP this token is being made for
1299 # @param sessionInfo. Hashref of session info OR session ID for lazy fetching
1300 # @param info hashref of access token session info (offline vs online)
1301 # @return new Lemonldap::NG::Common::Session object
1302  
1303 sub newAccessToken {
1304 16 ▶ my ( $self, $req, $rp, $scope, $sessionInfo, $info ) = @_;
Subroutine newAccessToken: called CC=5 SCAR=16.3
Pod documented
1305 16 my $client_id = $self->rpOptions->{$rp}->{oidcRPMetaDataOptionsClientID};
1306 my $at_info = {
1307 scope => $scope,
1308 rp => $rp,
1309 client_id => $client_id,
1310 iat => time,
1311 nbf => time,
1312 aud => $self->getAudiences($rp),
1313 16 %{$info},
1314 };
1315 my $ttl =
1316 $self->rpOptions->{$rp}->{oidcRPMetaDataOptionsAccessTokenExpiration}
1317 16 ▶ || $self->conf->{oidcServiceAccessTokenExpiration};
Condition66%2/3
$self->rpOptions->{$rp}{'oidcRPMetaDataOptionsAccessTokenExpiration'} || $self->conf->{'oidcServiceAccessTokenExpiration'}
l!l&&r!l&&!r
970
MC/DC0%0/2
$self->rpOptions->{$rp}{'oidcRPMetaDataOptionsAccessTokenExpiration'} || $self->conf->{'oidcServiceAccessTokenExpiration'}
$self->rpOptions->{$rp}{'oidcRPMetaDataOptionsAccessTokenExpiration'} $self->conf->{'oidcServiceAccessTokenExpiration'}
Truth table66%2/3
A || B
A= $self->rpOptions->{$rp}{'oidcRPMetaDataOptionsAccessTokenExpiration'}B= $self->conf->{'oidcServiceAccessTokenExpiration'}
ABresult
1X1
011
000
1318 16 my $session = $self->getOpenIDConnectSession(
1319 undef, "access_token",
1320 ttl => $ttl,
1321 info => $at_info,
1322 );
1323  
1324 16 ▶ if ($session) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session) { } 16 0 16
1325 16 ▶ if ( $self->_wantJWT($rp) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_wantJWT($rp)) { } 4 12 16
1326 4 my $at_jwt =
1327 $self->makeJWT( $req, $session->id, $sessionInfo, $at_info,
1328 $ttl );
1329 4 ▶ return undef unless $at_jwt;
Branch: 1 branch
BranchTrueFalseTotal
unless $at_jwt 0 4 4
1330 $at_jwt = $self->encryptToken(
1331 $rp,
1332 $at_jwt,
1333 $self->rpOptions->{$rp}
1334 ->{oidcRPMetaDataOptionsIdTokenEncKeyMgtAlg},
1335 $self->rpOptions->{$rp}
1336 ->{oidcRPMetaDataOptionsIdTokenEncContentEncAlg},
1337 4 );
1338 4 $at_info->{sha256_hash} = $self->createHash( $at_jwt, 256 );
1339 4 $self->updateToken( $session->id, $at_info );
1340 4 return $at_jwt;
1341 }
1342 else {
1343 12 my $user = $sessionInfo->{ $self->conf->{whatToTrace} };
1344 12 $self->auditLog(
1345 $req,
1346 code => "ISSUER_OIDC_ACCESS_TOKEN",
1347 rp => $rp,
1348 message =>
1349 ("Access Token for $user generated for $rp with TTL $ttl"),
1350 user => $user,
1351 ttl => $ttl,
1352 );
1353 12 return $session->id;
1354 }
1355 }
1356 else {
1357 0 return undef;
1358 }
1359 }
1360  
1361 sub _wantJWT {
1362 16 ▶ my ( $self, $rp ) = @_;
Subroutine _wantJWT: called CC=1 SCAR=0.0
1363 16 return $self->rpOptions->{$rp}->{oidcRPMetaDataOptionsAccessTokenJWT};
1364 }
1365  
1366 sub makeJWT {
1367 4 ▶ my ( $self, $req, $id, $sessionInfo, $at_info, $ttl ) = @_;
Subroutine makeJWT: called CC=8 SCAR=20.8
Pod undocumented
1368  
1369 4 my $rp = $at_info->{rp};
1370 4 my $exp = $ttl + time;
1371 4 my $client_id = $self->rpOptions->{$rp}->{oidcRPMetaDataOptionsClientID};
1372  
1373 4 my $access_token_payload = {
1374 iss => $self->get_issuer($req), # Issuer Identifier
1375 exp => $exp, # Expiration
1376 aud => $self->getAudiences($rp), # Audience
1377 jti => $id, # Access Token session ID
1378 sid => $self->getSidFromSession( $rp, $sessionInfo ), # Session id
1379 };
1380 $access_token_payload->{$_} = $at_info->{$_}
1381 4 foreach (qw(scope client_id iat));
1382  
1383 my $claims =
1384 4 $self->buildUserInfoResponseFromData( $req, $at_info->{scope}, $rp,
1385 $sessionInfo );
1386  
1387 4 my $sub = $access_token_payload->{sub} = delete $claims->{sub};
1388 4 my %extra_attr;
1389  
1390 # Release claims, or only sub
1391 4 ▶ if ( $self->rpOptions->{$rp}->{oidcRPMetaDataOptionsAccessTokenClaims} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rpOptions->{$rp}{"oidcRPMetaDataOptionsAccessTokenClaims"}) 2 2 4
1392 2 foreach ( keys %$claims ) {
1393 8 ▶ unless ( $access_token_payload->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($access_token_payload->{$_}) 6 2 8
1394 6 $extra_attr{$_} = $access_token_payload->{$_} = $claims->{$_};
1395 }
1396 }
1397 }
1398  
1399 4 my $extra_headers = { typ => "at+JWT" };
1400  
1401 # Call hook to let the user modify payload
1402 4 my $h = $self->p->processHook( $req, 'oidcGenerateAccessToken',
1403 $access_token_payload, $rp, $extra_headers );
1404 4 ▶ return undef if ( $h != PE_OK );
Branch: 1 branch
BranchTrueFalseTotal
if $h != 0 0 4 4
1405  
1406 # Get signature algorithm
1407 4 my $alg = $self->getSignAlg($rp);
1408  
1409 4 my $jwt =
1410 $self->createJWT( $access_token_payload, $alg, $rp, $extra_headers );
1411 4 ▶ return undef unless $jwt;
Branch: 1 branch
BranchTrueFalseTotal
unless $jwt 0 4 4
1412  
1413 4 ▶ my $attr_str = (
Branch: 1 branch
BranchTrueFalseTotal
%extra_attr ? : 2 2 4
1414 %extra_attr
1415 ? ( " with attributes " . join( ',', sort( keys %extra_attr ) ) )
1416 : ''
1417 );
1418  
1419 4 my $user = $sessionInfo->{ $self->conf->{whatToTrace} };
1420 4 $self->auditLog(
1421 $req,
1422 code => "ISSUER_OIDC_ACCESS_TOKEN",
1423 rp => $rp,
1424 message => (
1425 "Access Token for $user generated for $rp as $sub with TTL $ttl$attr_str"
1426 ),
1427 user => $user,
1428 ttl => $ttl,
1429 oidc_sub => $sub,
1430 attributes => \%extra_attr,
1431 );
1432 4 return $jwt;
1433 }
1434  
1435 # Get an session from the supplied Access Token
1436 # @param id
1437 # @return new Lemonldap::NG::Common::Session object
1438 sub getAccessToken {
1439 15 ▶ my ( $self, $access_token ) = @_;
Subroutine getAccessToken: called CC=5 SCAR=16.4
Pod documented
1440  
1441 15 my $id = getAccessTokenSessionId($access_token);
1442 15 ▶ return unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 15 15
1443  
1444 15 my $session = $self->getOpenIDConnectSession( $id, "access_token" );
1445 15 ▶ return undef unless $session;
Branch: 1 branch
BranchTrueFalseTotal
unless $session 5 10 15
1446  
1447 10 my $stored_hash = $session->{data}->{sha256_hash};
1448 10 ▶ if ($stored_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stored_hash) { } 4 6 10
1449 4 my $incoming_hash = $self->createHash( $access_token, 256 );
1450 4 ▶ if ( $stored_hash eq $incoming_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stored_hash eq $incoming_hash) { } 4 0 4
1451 4 return $session;
1452 }
1453 else {
1454 0 $self->logger->error(
1455 "Incoming Access token hash $incoming_hash "
1456 . "does not match stored hash $stored_hash. "
1457 . "The access token might have been tampered with." );
1458 0 return undef;
1459 }
1460 }
1461 else {
1462 6 return $session;
1463 }
1464 }
1465  
1466 # Create a new Refresh Token
1467 # @param info hashref of session info
1468 # @return new Lemonldap::NG::Common::Session object
1469  
1470 sub newRefreshToken {
1471 4 ▶ my ( $self, $rp, $info, $offline ) = @_;
Subroutine newRefreshToken: called CC=3 SCAR=13.3
Pod documented
1472 my $ttl =
1473 $offline
1474 ? (
1475 $self->rpOptions->{$rp}->{oidcRPMetaDataOptionsOfflineSessionExpiration}
1476 || $self->conf->{oidcServiceOfflineSessionExpiration} )
1477 4 ▶ : $self->conf->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
$offline ? : 0 4 4
Condition0%0/3
$self->rpOptions->{$rp}{"oidcRPMetaDataOptionsOfflineSessionExpiration"} or $self->conf->{"oidcServiceOfflineSessionExpiration"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->rpOptions->{$rp}{"oidcRPMetaDataOptionsOfflineSessionExpiration"} or $self->conf->{"oidcServiceOfflineSessionExpiration"}
$self->rpOptions->{$rp}{"oidcRPMetaDataOptionsOfflineSessionExpiration"} $self->conf->{"oidcServiceOfflineSessionExpiration"}
Truth table0%0/3
A or B
A= $self->rpOptions->{$rp}{"oidcRPMetaDataOptionsOfflineSessionExpiration"}B= $self->conf->{"oidcServiceOfflineSessionExpiration"}
ABresult
1X1
011
000
1478 4 $info->{_oidcRtUpdate} = time;
1479  
1480 4 return $self->getOpenIDConnectSession(
1481 undef, "refresh_token",
1482 ttl => $ttl,
1483 info => $info
1484 );
1485 }
1486  
1487 # Get existing Refresh Token
1488 # @param id
1489 # @return new Lemonldap::NG::Common::Session object
1490  
1491 sub getRefreshToken {
1492 4 ▶ my ( $self, $id, $raw ) = @_;
Subroutine getRefreshToken: called CC=2 SCAR=7.2
Pod documented
1493  
1494 4 ▶ return $self->getOpenIDConnectSession(
Branch: 1 branch
BranchTrueFalseTotal
$raw ? : 0 4 4
1495 $id, "refresh_token",
1496 noCache => 1,
1497 ( $raw ? ( hashStore => 0 ) : () )
1498 );
1499 }
1500  
1501 sub updateRefreshToken {
1502 2 ▶ my ( $self, $id, $infos ) = @_;
Subroutine updateRefreshToken: called CC=1 SCAR=0.0
Pod undocumented
1503 2 $infos->{_oidcRtUpdate} = time;
1504 2 return $self->updateToken( $id, $infos );
1505 }
1506  
1507 sub updateToken {
1508 6 ▶ my ( $self, $id, $infos ) = @_;
Subroutine updateToken: called CC=2 SCAR=7.9
Pod undocumented
1509  
1510 my $oidcSession = Lemonldap::NG::Common::Session->new( {
1511 $self->_storeOpts(),
1512 cacheModule => $self->conf->{localSessionStorage},
1513 cacheModuleOptions => $self->conf->{localSessionStorageOptions},
1514 hashStore => $self->conf->{hashedSessionStore},
1515 6 id => $id,
1516 info => $infos,
1517 }
1518 );
1519  
1520 6 ▶ if ( $oidcSession->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oidcSession->error) 0 6 6
1521 0 $self->userLogger->warn("OpenIDConnect session $id not found");
1522 0 return undef;
1523 }
1524  
1525 6 return $oidcSession;
1526 }
1527  
1528 # Try to recover the OpenID Connect session corresponding to id and return session
1529 # If id is set to undef, return a new session
1530 # @return Lemonldap::NG::Common::Session object
1531 sub getOpenIDConnectSession {
1532 63 ▶ my $self = shift;
Subroutine getOpenIDConnectSession: called CC=18 SCAR=36.1
Pod documented
1533 63 my $id = shift;
1534 63 my $type = shift;
1535  
1536 63 ▶ my $type_log = $type || "session";
Condition50%1/2
$type || 'session'
l!l
630
MC/DC0%0/1
$type || 'session'
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
1537  
1538 # Check old method signature ($id, $type, $ttl, $info)
1539 63 ▶ my %opts =
Branch: 1 branch
BranchTrueFalseTotal
$_[0] && $_[0] =~ /^\d+$/ || $_[1] && ref $_[1] ? : 0 63 63
Condition55%5/9
$_[0] and $_[0] =~ /^\d+$/ or $_[1] and ref $_[1]
l!l&&r!l&&!r
0063
$_[0] and $_[0] =~ /^\d+$/
!ll&&!rl&&r
26370
$_[1] and ref $_[1]
!ll&&!rl&&r
26370
MC/DC0%0/4
$_[0] and $_[0] =~ /^\d+$/ or $_[1] and ref $_[1]
$_[0] $_[0] =~ /^\d+$/ $_[1] ref $_[1]
Truth table28%2/7
A and B or C and D
A= $_[0]B= $_[0] =~ /^\d+$/C= $_[1]D= ref $_[1]
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
1540 ( ( $_[0] and $_[0] =~ /^\d+$/ ) or ( $_[1] and ref $_[1] ) )
1541 ? ( ttl => $_[0], info => $_[1] )
1542 : (@_);
1543  
1544 63 ▶ $opts{ttl} ||= $self->conf->{timeout};
Condition66%2/3
$opts{"ttl"} ||= $self->conf->{"timeout"}
l!l&&r!l&&!r
33300
MC/DC0%0/2
$opts{"ttl"} ||= $self->conf->{"timeout"}
$opts{"ttl"} $self->conf->{"timeout"}
Truth table66%2/3
A ||= B
A= $opts{"ttl"}B= $self->conf->{"timeout"}
ABresult
1X1
011
000
1545  
1546 my $oidcSession = Lemonldap::NG::Common::Session->new( {
1547 $self->_storeOpts(),
1548 (
1549 $opts{noCache} ? ()
1550 : (
1551 cacheModule => $self->conf->{localSessionStorage},
1552 cacheModuleOptions =>
1553 $self->conf->{localSessionStorageOptions}
1554 )
1555 ),
1556 hashStore => $opts{hashStore} // $self->conf->{hashedSessionStore},
1557 id => $id,
1558 kind => $self->sessionKind,
1559 (
1560 $opts{info}
1561 ? (
1562 info => {
1563 _type => $type,
1564 _utime => time + $opts{ttl} - $self->conf->{timeout},
1565 63 ▶ %{ $opts{info} }
Branch: 2 branches
BranchTrueFalseTotal
$opts{'noCache'} ? : 4 59 63
$opts{'info'} ? : 33 30 63
Condition33%1/3
$opts{"hashStore"} // $self->conf->{"hashedSessionStore"}
l!l&&r!l&&!r
0063
MC/DC0%0/2
$opts{"hashStore"} // $self->conf->{"hashedSessionStore"}
$opts{"hashStore"} $self->conf->{"hashedSessionStore"}
Truth table33%1/3
A // B
A= $opts{"hashStore"}B= $self->conf->{"hashedSessionStore"}
ABresult
1X1
011
000
1566 }
1567 )
1568 : ()
1569 ),
1570 }
1571 );
1572  
1573 63 ▶ if ( $oidcSession->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oidcSession->error) 0 63 63
1574 0 ▶ if ($id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id) { } 0 0 0
1575 0 $self->userLogger->warn("OpenIDConnect $type_log $id not found");
1576 }
1577 else {
1578 0 $self->logger->error(
1579 "Unable to create new OpenIDConnect $type_log");
1580 0 $self->logger->error( $oidcSession->error );
1581 }
1582 0 return undef;
1583 }
1584  
1585 63 ▶ if ( $id and $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id and $type) 30 33 63
Condition66%2/3
$id and $type
!ll&&!rl&&r
33030
MC/DC50%1/2
$id and $type
$id $type
Truth table66%2/3
A and B
A= $idB= $type
ABresult
0X0
100
111
1586 30 my $storedType = $oidcSession->{data}->{_type};
1587  
1588 # Only check if a type is set in DB, for backward compatibility
1589 30 ▶ if ( $storedType and $type ne $storedType ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($storedType and $type ne $storedType) 0 30 30
Condition33%1/3
$storedType and $type ne $storedType
!ll&&!rl&&r
0300
MC/DC0%0/2
$storedType and $type ne $storedType
$storedType $type ne $storedType
Truth table33%1/3
A and B
A= $storedTypeB= $type ne $storedType
ABresult
0X0
100
111
1590 $self->logger->error( "Wrong OpenID session type: "
1591 . $oidcSession->{data}->{_type}
1592 0 . ". Expected: "
1593 . $type );
1594 0 return undef;
1595 }
1596  
1597 # Make sure the token is still valid, we already compensated for
1598 # different TTLs when storing _utime
1599 30 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (&CORE::GLOBAL::time() > $oidcSession->{"data"}{"_utime"} + $self->conf->{"timeout"}) 5 25 30
1600 time > ( $oidcSession->{data}->{_utime} + $self->conf->{timeout} ) )
1601 {
1602 5 $self->logger->notice("OpenIDConnect $type_log $id has expired");
1603 5 return undef;
1604 }
1605 }
1606  
1607 # Make sure the token is still valid, we already compensated for
1608 # different TTLs when storing _utime
1609 58 ▶ if ( time > ( $oidcSession->{data}->{_utime} + $self->conf->{timeout} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&CORE::GLOBAL::time() > $oidcSession->{"data"}{"_utime"} + $self->conf->{"timeout"}) 0 58 58
1610 0 $self->logger->notice("OpenIDConnect $type_log $id has expired");
1611 0 return undef;
1612 }
1613  
1614 58 return $oidcSession;
1615 }
1616  
1617 sub getSignAlg {
1618 4 ▶ my ( $self, $rp ) = @_;
Subroutine getSignAlg: called CC=3 SCAR=13.5
Pod undocumented
1619 my $alg =
1620 4 $self->rpOptions->{$rp}->{oidcRPMetaDataOptionsAccessTokenSignAlg};
1621 4 ▶ if ( !$alg ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($alg) 0 4 4
1622 0 my $default_signing_key_type =
1623 $self->_getKeyType( $self->get_public_key("default-oidc-sig") );
1624 0 ▶ $alg = ( $default_signing_key_type eq 'EC' ? 'ES256' : 'RS256' );
Branch: 1 branch
BranchTrueFalseTotal
$default_signing_key_type eq 'EC' ? : 0 0 0
1625 }
1626 4 $self->logger->debug("Access Token signature algorithm: $alg");
1627 4 return $alg;
1628 }
1629  
1630 # Store information in state database and return
1631 # corresponding session_id
1632 # @return State Session ID
1633 sub storeState {
1634 123 ▶ my ( $self, $req, @data ) = @_;
Subroutine storeState: called CC=5 SCAR=16.2
Pod documented
1635  
1636 # check if there are data to store
1637 123 my $infos;
1638 123 foreach (@data) {
1639 492 ▶ $infos->{state}->{$_} = $req->{$_} if $req->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $req->{$_} 0 492 492
1640 492 ▶ $infos->{state}->{"data_$_"} = $req->data->{$_} if $req->data->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $req->data->{$_} 160 332 492
1641 }
1642 123 ▶ return unless ($infos);
Branch: 1 branch
BranchTrueFalseTotal
unless $infos 0 123 123
1643  
1644 # Session type
1645 123 $infos->{_type} = "state";
1646  
1647 # Create state session and store infos
1648 123 return $self->state_ott->createToken($infos);
1649 }
1650  
1651 # Extract state information into $req
1652 sub extractState {
1653 0 ▶ my ( $self, $req, $state ) = @_;
Subroutine extractState: not called CC=8 SCAR=42.8
Pod documented
1654  
1655 0 ▶ return 0 unless $state;
Branch: 1 branch
BranchTrueFalseTotal
unless $state 0 0 0
1656  
1657 # Open state session
1658 0 my $stateSession = $self->state_ott->getToken($state);
1659  
1660 0 ▶ return 0 unless $stateSession;
Branch: 1 branch
BranchTrueFalseTotal
unless $stateSession 0 0 0
1661 0 ▶ return 0 unless $stateSession->{_type} eq "state";
Branch: 1 branch
BranchTrueFalseTotal
unless $stateSession->{"_type"} eq "state" 0 0 0
1662 0 ▶ return 0 unless $stateSession->{state};
Branch: 1 branch
BranchTrueFalseTotal
unless $stateSession->{"state"} 0 0 0
1663  
1664 # Push values in $self
1665 0 foreach ( keys %{ $stateSession->{state} } ) {
1666 0 my $tmp = $stateSession->{state}->{$_};
1667 0 ▶ if (s/^data_//) {
Branch: 2 branches
BranchTrueFalseTotal
if (s/^data_//) { } 0 0 0
elsif ($req->can($_)) { } 0 0 0
1668 0 $req->data->{$_} = $tmp;
1669 }
1670 elsif ( $req->can($_) ) {
1671 0 $req->$_($tmp);
1672 }
1673 else {
1674 0 $self->logger->warn("Unknown request property $_, skipping");
1675 }
1676 }
1677  
1678 0 return 1;
1679 }
1680  
1681 # Check signature of a JWT
1682 # @return boolean 1 if signature is verified, 0 else
1683 sub decodeJWT {
1684 0 ▶ my ( $self, $jwt, $op, $rp ) = @_;
Subroutine decodeJWT: not called CC=19 SCAR=59.4
Pod undocumented
1685  
1686 0 $self->logger->debug("Verification of JWT signature: $jwt");
1687  
1688 # Extract JWT parts
1689 0 my $jwt_header = getJWTHeader($jwt);
1690 0 my $signed_data = getJWTSignedData($jwt);
1691 0 my $signature = getJWTSignature($jwt);
1692  
1693 # Get signature algorithm
1694 0 my $alg = $jwt_header->{alg};
1695  
1696 0 $self->logger->debug("JWT signature algorithm: $alg");
1697  
1698 0 ▶ if ( $alg eq "none" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alg eq "none") 0 0 0
1699  
1700 # If none alg, signature should be empty
1701 0 ▶ if ($signature) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signature) 0 0 0
1702 0 $self->logger->debug( "Signature "
1703 . $signature
1704 . " is present but algorithm is 'none'" );
1705 0 return;
1706 }
1707 $self->logger->debug(
1708 0 "JWT algorithm is 'none', signature cannot be verified");
1709 0 return;
1710 }
1711  
1712 0 my $jwks;
1713 0 ▶ if ($op) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op) { } 0 0 0
1714  
1715 # Always refresh JWKS if timeout has elapsed
1716 0 $self->refreshJWKSdataForOp($op);
1717  
1718 0 my $kid = $jwt_header->{kid};
1719  
1720 # If the JWT is signed by an unknown kid, force a refresh
1721 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($kid and not $self->_kid_found_in_jwks($kid, $self->opMetadata->{$op}{"jwks"})) 0 0 0
Condition0%0/3
$kid and not $self->_kid_found_in_jwks($kid, $self->opMetadata->{$op}{"jwks"})
!ll&&!rl&&r
000
MC/DC0%0/2
$kid and not $self->_kid_found_in_jwks($kid, $self->opMetadata->{$op}{"jwks"})
$kid not $self->_kid_found_in_jwks($kid, $self->opMetadata->{$op}{"jwks"})
Truth table0%0/3
A and not(B)
A= $kidB= not $self->_kid_found_in_jwks($kid, $self->opMetadata->{$op}{"jwks"})
ABresult
0X0
100
111
1722 $kid
1723 and !$self->_kid_found_in_jwks(
1724 $kid, $self->opMetadata->{$op}->{jwks}
1725 )
1726 )
1727 {
1728 0 $self->logger->debug(
1729 "Key ID $kid not found in current JWKS, forcing JWKS refresh");
1730 0 $self->refreshJWKSdataForOp( $op, 1 );
1731 }
1732  
1733 0 $jwks = $self->opMetadata->{$op}->{jwks};
1734 }
1735 else {
1736 0 $jwks = $self->rpSigKey->{$rp};
1737 }
1738  
1739 0 ▶ unless ( $alg =~ /^HS/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($alg =~ /^HS/) 0 0 0
1740 0 ▶ unless ($jwks) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($jwks) 0 0 0
1741 0 $self->logger->error(
1742 "Cannot verify $alg signature: no JWKS data found");
1743 0 return;
1744 }
1745 0 ▶ unless ($jwks->{keys}
Branch: 1 branch
BranchTrueFalseTotal
unless ($jwks->{"keys"} and ref $jwks->{"keys"} eq "ARRAY" and @{$jwks->{"keys"};}) 0 0 0
Condition0%0/6
$jwks->{"keys"} and ref $jwks->{"keys"} eq "ARRAY" and @{$jwks->{"keys"};}
!ll&&!rl&&r
000
$jwks->{"keys"} and ref $jwks->{"keys"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
$jwks->{"keys"} and ref $jwks->{"keys"} eq "ARRAY" and @{$jwks->{"keys"};}
$jwks->{"keys"} ref $jwks->{"keys"} eq "ARRAY" @{$jwks->{"keys"};}
Truth table0%0/4
A and B and C
A= $jwks->{"keys"}B= ref $jwks->{"keys"} eq "ARRAY"C= @{$jwks->{"keys"};}
ABCresult
0XX0
10X0
1100
1111
1746 and ref( $jwks->{keys} ) eq 'ARRAY'
1747 0 and @{ $jwks->{keys} } )
1748 {
1749 0 $self->logger->error('Malformed JWKS, I need {"keys":[..keys..]}');
1750 0 return;
1751 }
1752 }
1753  
1754 # Choosing keys
1755 # - if algorithm is HS{digits}, the key is the ClientSecret
1756 # - if JWS has a "kid" field in its header, use it (then replace the
1757 # "key" arg of Crypt::JWT by "kid_keys" and give the whole JWKS)
1758 # - else we try the first available key of jwks document
1759 0 my @keyArgs;
1760 0 ▶ if ( $alg =~ /^HS/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($alg =~ /^HS/) { } 0 0 0
elsif ($jwt_header->{'kid'}) { } 0 0 0
1761 0 $self->logger->debug("Alg is $alg, using secret as key");
1762 @keyArgs = ( [
1763 key => $op
1764 ? $self->opOptions->{$op}->{oidcOPMetaDataOptionsClientSecret}
1765 : $self->rpOptions->{$rp}->{oidcRPMetaDataOptionsClientSecret}
1766 0 ▶ ]
Branch: 1 branch
BranchTrueFalseTotal
$op ? : 0 0 0
1767 );
1768 }
1769 elsif ( $jwt_header->{kid} ) {
1770 0 $self->logger->debug(
1771 "'kid' found in JWT header, using the whole JWKS doc as 'kid_keys'"
1772 );
1773 0 @keyArgs = ( [ kid_keys => $jwks ] );
1774 }
1775 else {
1776 $self->logger->debug(
1777 "No 'kid' found in JWT header, will try all keys found in JWKS doc ("
1778 0 . @{ $jwks->{keys} }
1779 . ' key(s))' );
1780 0 @keyArgs = map { [ key => $_ ] } @{ $jwks->{keys} };
1781 }
1782  
1783 0 my $error = [];
1784 0 my $content;
1785 0 foreach my $keyArg (@keyArgs) {
1786  
1787 # JSON decoding is done here because #2748
1788 0 $content = eval {
1789 0 JSON::from_json(
1790 decode_jwt( token => $jwt, @$keyArg, decode_payload => 0 ) );
1791 };
1792 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
1793 0 $error = [ "Unable to verify JWT: $@", "Jwt was: $jwt" ];
1794 }
1795 else {
1796 0 $error = [];
1797 0 last;
1798 }
1799 }
1800 0 ▶ if (@$error) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$error) 0 0 0
1801 0 $self->logger->error($_) foreach @$error;
1802 0 return;
1803 }
1804 0 ▶ return wantarray ? ( $content, $alg ) : $content;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1805 }
1806  
1807 sub _kid_found_in_jwks {
1808 0 ▶ my ( $self, $kid, $jwks ) = @_;
Subroutine _kid_found_in_jwks: not called CC=5 SCAR=34.0
1809  
1810 0 ▶ return 0 if !$kid;
Branch: 1 branch
BranchTrueFalseTotal
unless $kid 0 0 0
1811  
1812 0 ▶ my @keys = $jwks ? @{ $jwks->{keys} // [] } : ();
Branch: 1 branch
BranchTrueFalseTotal
$jwks ? : 0 0 0
Condition0%0/2
$jwks->{"keys"} // []
l!l
00
MC/DC0%0/1
$jwks->{"keys"} // []
$jwks->{"keys"}
Truth table0%0/2
A
A= $jwks->{"keys"}
Aresult
11
00
1813  
1814 0 ▶ my @found = grep { $_->{kid} and $_->{kid} eq $kid } @keys;
Branch: 1 branch
BranchTrueFalseTotal
$_->{"kid"} and $_->{"kid"} eq $kid 0 0 0
1815  
1816 0 return @found > 0;
1817 }
1818  
1819 ### HERE
1820  
1821 # Check value hash
1822 # @param value Value
1823 # @param hash Hash
1824 # @param id_token ID Token
1825 # @return boolean 1 if hash is verified, 0 else
1826 sub verifyHash {
1827 0 ▶ my ( $self, $value, $hash, $id_token ) = @_;
Subroutine verifyHash: not called CC=4 SCAR=30.0
Pod documented
1828  
1829 0 $self->logger->debug("Verification of value $value with hash $hash");
1830  
1831 0 my $jwt_header = getJWTHeader($id_token);
1832  
1833 # Get signature algorithm
1834 0 my $alg = $jwt_header->{alg};
1835  
1836 0 $self->logger->debug("ID Token signature algorithm: $alg");
1837  
1838 0 ▶ if ( $alg eq "none" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alg eq "none") 0 0 0
1839  
1840 # Not supported
1841 0 $self->logger->debug("Cannot check hash without signature algorithm");
1842 0 return 0;
1843 }
1844  
1845 0 ▶ if ( $alg =~ /(?:\w{2})(\d{3})/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alg =~ /(?:\w{2})(\d{3})/) 0 0 0
1846  
1847 # Hash Level
1848 0 my $hash_level = $1;
1849  
1850 0 $self->logger->debug("Use SHA $hash_level to check hash");
1851  
1852 0 my $cHash = $self->createHash( $value, $hash_level );
1853  
1854 # Compare values
1855 0 ▶ unless ( $cHash eq $hash ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cHash eq $hash) 0 0 0
1856 0 $self->logger->debug("Hash $hash not equal to hash $cHash");
1857 0 return 0;
1858 }
1859 0 return 1;
1860 }
1861  
1862 # Other algorithms not managed
1863 0 $self->logger->debug("Algorithm $alg not known");
1864  
1865 0 return 0;
1866 }
1867  
1868 # Create Hash
1869 # @param value Value to hash
1870 # @param hash_level SHA Hash level
1871 # @return String hash
1872 sub createHash {
1873 23 ▶ my ( $self, $value, $hash_level ) = @_;
Subroutine createHash: called CC=4 SCAR=13.9
Pod documented
1874  
1875 23 $self->logger->debug("Use SHA $hash_level to hash $value");
1876  
1877 23 my $hash;
1878  
1879 23 ▶ if ( $hash_level eq "256" ) { $hash = sha256($value); }
Branch: 1 branch
BranchTrueFalseTotal
if ($hash_level eq 256) 15 8 23
1880 23 ▶ if ( $hash_level eq "384" ) { $hash = sha384($value); }
Branch: 1 branch
BranchTrueFalseTotal
if ($hash_level eq 384) 0 23 23
1881 23 ▶ if ( $hash_level eq "512" ) { $hash = sha512($value); }
Branch: 1 branch
BranchTrueFalseTotal
if ($hash_level eq 512) 8 15 23
1882  
1883 23 $hash = substr( $hash, 0, length($hash) / 2 );
1884 23 $hash = encode_base64url( $hash, "" );
1885  
1886 23 return $hash;
1887 }
1888  
1889 # Create error redirection
1890 # @param redirect_url Redirection URL
1891 # @param error Error code
1892 # @param error_description Human-readable ASCII encoded text description of the error
1893 # @param error_uri URI of a web page that includes additional information about the error
1894 # @param state OAuth 2.0 state value
1895 # @param fragment Set to true to return fragment component
1896 # @return void
1897  
1898 sub returnRedirectError {
1899 0 ▶ my ( $self, $req, $redirect_url, $error, $error_description,
Subroutine returnRedirectError: not called CC=8 SCAR=42.8
Pod undocumented
1900 $error_uri, $state, $fragment )
1901 = @_;
1902  
1903 0 ▶ my $reason = $error_description ? ": $error_description" : "";
Branch: 1 branch
BranchTrueFalseTotal
$error_description ? : 0 0 0
1904 $self->auditLog(
1905 $req,
1906 code => "ISSUER_OIDC_LOGIN_FAILED",
1907 message => ( "OIDC login failed" . $reason ),
1908 ( $error_description ? ( reason => $error_description ) : () ),
1909 oauth_error => $error,
1910 portal_error => portalConsts->{PE_REDIRECT},
1911 user => $req->sessionInfo->{ $self->conf->{whatToTrace} },
1912 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$error_description ? : 0 0 0
1913  
1914 0 ▶ my $urldc =
Branch: 5 branches
BranchTrueFalseTotal
$fragment ? : 0 0 0
$redirect_url =~ /\?/ ? : 0 0 0
defined $error_description ? : 0 0 0
defined $error_uri ? : 0 0 0
defined $state ? : 0 0 0
1915 $redirect_url
1916 . ( $fragment ? '#' : $redirect_url =~ /\?/ ? '&' : '?' )
1917 . build_urlencoded(
1918 error => $error,
1919 (
1920 defined $error_description
1921 ? ( error_description => $error_description )
1922 : ()
1923 ),
1924 ( defined $error_uri ? ( error_uri => $error_uri ) : () ),
1925 ( defined $state ? ( state => $state ) : () )
1926 );
1927 0 $req->urldc($urldc);
1928 0 return PE_REDIRECT;
1929 }
1930  
1931 #sub returnJSONStatus {
1932 #my ( $self, $req, $content, $status_code ) = @_;
1933 # replace this call by $self->p->sendJSONresponse($req,$content,code=>$status_code)
1934  
1935 #sub returnJSONError {
1936 #my ( $self, $error ) = @_;
1937 #replace this by $self->p->sendError($req, $error,400);
1938 sub sendOIDCError {
1939 3 ▶ my ( $self, $req, $err, $code, $description ) = @_;
Subroutine sendOIDCError: called CC=3 SCAR=11.7
Pod undocumented
1940 3 ▶ $code ||= 500;
Condition50%1/2
$code ||= 500
l!l
30
MC/DC0%0/1
$code ||= 500
$code
Truth table50%1/2
A
A= $code
Aresult
11
00
1941  
1942 3 ▶ return $self->sendJSONresponse(
Branch: 1 branch
BranchTrueFalseTotal
$description ? : 0 3 3
1943 $req,
1944 {
1945 error => $err,
1946 ( $description ? ( error_description => $description ) : () ),
1947 },
1948  
1949 code => $code
1950 );
1951 }
1952  
1953 #sub returnJSON {
1954 #my ( $self, $content ) = @_;
1955 #replace this call by $self->p->sendJSONresponse($req,$content)
1956  
1957 # Return Bearer error
1958 # @param error_code Error code
1959 # @param error_message Error message
1960 # @return GI response
1961 sub returnBearerError {
1962 5 ▶ my ( $self, $req, $error_code, $error_message ) = @_;
Subroutine returnBearerError: called CC=1 SCAR=0.0
Pod documented
1963  
1964 5 my $res = $self->p->sendBinaryResponse(
1965 $req, '',
1966 code => 401,
1967 headers => [
1968 'WWW-Authenticate' =>
1969 "error=$error_code,error_description=$error_message"
1970 ]
1971 );
1972  
1973 5 $self->p->setCorsHeaderFromConfig($res);
1974  
1975 5 return $res;
1976 }
1977  
1978 sub invalidClientResponse {
1979 0 ▶ my ( $self, $req ) = @_;
Subroutine invalidClientResponse: not called CC=3 SCAR=24.8
Pod undocumented
1980 0 ▶ if ( $req->authorization ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->authorization) { } 0 0 0
1981 0 my ($method) = $req->authorization =~ qw/^(\w+) /;
1982 0 ▶ if ($method) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method) 0 0 0
1983 0 $req->respHeaders( [ 'WWW-Authenticate' => $method ] );
1984 0 return $self->sendOIDCError( $req, 'invalid_client', 401 );
1985 }
1986 }
1987 else {
1988 0 return $self->sendOIDCError( $req, 'invalid_client', 400 );
1989 }
1990 }
1991  
1992 sub checkEndPointAuthenticationCredentials {
1993 18 ▶ my ( $self, $req ) = @_;
Subroutine checkEndPointAuthenticationCredentials: called CC=11 SCAR=33.0
Pod undocumented
1994  
1995 # Check authentication
1996 18 my ( $client_id, $client_secret, $method ) =
1997 $self->getEndPointAuthenticationCredentials($req);
1998  
1999 18 ▶ $self->logger->debug(
Condition50%1/2
$method || 'undefined'
l!l
180
MC/DC0%0/1
$method || 'undefined'
$method
Truth table50%1/2
A
A= $method
Aresult
11
00
2000 'Authentication method: ' . ( $method || 'undefined' ) );
2001  
2002 18 ▶ unless ($client_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($client_id) 0 18 18
2003 0 $self->logger->error(
2004 "No authentication provided to get token, or authentication type not supported"
2005 );
2006 0 return undef;
2007 }
2008  
2009 # Verify that client_id is registered in configuration
2010 18 my $rp = $self->getRP($client_id);
2011  
2012 18 ▶ unless ($rp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rp) { } 18 0 18
2013 0 $self->userLogger->error(
2014 "No registered Relying Party found with client_id $client_id");
2015 0 return undef;
2016 }
2017 else {
2018 18 $self->logger->debug("Client id $client_id match Relying Party $rp");
2019 }
2020  
2021 # Check client_secret
2022 18 ▶ if ( $self->rpOptions->{$rp}->{oidcRPMetaDataOptionsPublic} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rpOptions->{$rp}{'oidcRPMetaDataOptionsPublic'}) { } 0 18 18
2023 0 $self->logger->debug(
2024 "Relying Party $rp is public, do not check client secret");
2025 }
2026 else {
2027 18 ▶ if ( $method eq "none" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "none") 0 18 18
2028 0 $self->logger->error(
2029 "Relying Party $rp is confidential but no known method was used"
2030 . " to authenticate on token endpoint" );
2031 0 return undef;
2032 }
2033 18 ▶ if ( $method =~ /^client_secret_(?:basic|post)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^client_secret_(?:basic|post)$/) 18 0 18
2034 18 ▶ unless ($client_secret) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($client_secret) 0 18 18
2035 0 $self->logger->error(
2036 "Relying Party $rp is confidential but no client secret was provided to authenticate on token endpoint"
2037 );
2038 0 return undef;
2039 }
2040 18 ▶ unless ( $client_secret eq
Branch: 1 branch
BranchTrueFalseTotal
unless ($client_secret eq $self->rpOptions->{$rp}{"oidcRPMetaDataOptionsClientSecret"}) 0 18 18
2041 $self->rpOptions->{$rp}->{oidcRPMetaDataOptionsClientSecret} )
2042 {
2043 0 $self->logger->error("Wrong credentials for $rp");
2044 0 return undef;
2045 }
2046 }
2047  
2048 18 ▶ if ( $self->rpOptions->{$rp}->{oidcRPMetaDataOptionsAuthMethod} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rpOptions->{$rp}{"oidcRPMetaDataOptionsAuthMethod"}) 0 18 18
2049 0 ▶ unless ( $method eq
Branch: 1 branch
BranchTrueFalseTotal
unless ($method eq $self->rpOptions->{$rp}{"oidcRPMetaDataOptionsAuthMethod"}) 0 0 0
2050 $self->rpOptions->{$rp}->{oidcRPMetaDataOptionsAuthMethod} )
2051 {
2052 0 $self->logger->error("Wrong authentication method for $rp");
2053 0 return undef;
2054 }
2055 }
2056 }
2057 $self->p->HANDLER->set_user( $req,
2058 18 $self->rpOptions->{$rp}->{oidcRPMetaDataOptionsClientID} );
2059 18 return ( $rp, $method );
2060 }
2061  
2062 # Get Client ID and Client Secret
2063 # @return array (client_id, client_secret)
2064 sub getEndPointAuthenticationCredentials {
2065 18 ▶ my ( $self, $req ) = @_;
Subroutine getEndPointAuthenticationCredentials: called CC=29 SCAR=58.2
Pod documented
2066 18 my ( $client_id, $client_secret, $scheme );
2067  
2068 18 my $authorization = $req->authorization;
2069 18 ▶ if ( $authorization and $authorization =~ m#^Basic ([[:alnum:]+/=]+)#i ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($authorization and $authorization =~ m[^Basic ([[:alnum:]+/=]+)]i) { } 16 2 18
elsif (my $atype = $req->param('client_assertion_type') and my $jws = $req->param('client_assertion') and my $_clientId = $req->param('client_id')) { } 0 2 2
elsif ($req->param('client_id') and $req->body_parameters->{'client_secret'}) { } 2 0 2
elsif ($req->param('client_id')) { } 0 0 0
Condition66%2/3
$authorization and $authorization =~ m[^Basic ([[:alnum:]+/=]+)]i
!ll&&!rl&&r
2016
MC/DC50%1/2
$authorization and $authorization =~ m[^Basic ([[:alnum:]+/=]+)]i
$authorization $authorization =~ m[^Basic ([[:alnum:]+/=]+)]i
Truth table66%2/3
A and B
A= $authorizationB= $authorization =~ m[^Basic ([[:alnum:]+/=]+)]i
ABresult
0X0
100
111
2070 16 $scheme = 'client_secret_basic';
2071 16 $self->logger->debug("Method client_secret_basic used");
2072 16 eval {
2073 16 ( $client_id, $client_secret ) =
2074 split( ':', decode_base64($1), 2 );
2075 };
2076 16 ▶ $self->logger->error("Bad authentication header: $@") if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 16 16
2077  
2078 # Unescape clientId and clientSecret to be compliant with RFC 6749
2079 # https://datatracker.ietf.org/doc/html/rfc6749
2080 16 $client_id = uri_unescape($client_id);
2081 16 $client_secret = uri_unescape($client_secret);
2082  
2083 # Using multiple methods is an error
2084 16 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($req->param("client_id") and $req->param("client_secret") or $req->param("client_assertion") and $req->param("client_assertion_type")) 0 16 16
Condition44%4/9
$req->param("client_id") and $req->param("client_secret") or $req->param("client_assertion") and $req->param("client_assertion_type")
l!l&&r!l&&!r
0016
$req->param("client_id") and $req->param("client_secret")
!ll&&!rl&&r
1420
$req->param("client_assertion") and $req->param("client_assertion_type")
!ll&&!rl&&r
1600
MC/DC0%0/4
$req->param("client_id") and $req->param("client_secret") or $req->param("client_assertion") and $req->param("client_assertion_type")
$req->param("client_id") $req->param("client_secret") $req->param("client_assertion") $req->param("client_assertion_type")
Truth table0%0/7
A and B or C and D
A= $req->param("client_id")B= $req->param("client_secret")C= $req->param("client_assertion")D= $req->param("client_assertion_type")
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
2085 ( $req->param('client_id') and $req->param('client_secret') )
2086 or ( $req->param('client_assertion')
2087 and $req->param('client_assertion_type') )
2088 )
2089 {
2090 0 $self->logger->error("Multiple client authentication methods used");
2091 0 ( $client_id, $client_secret ) = ( undef, undef );
2092 }
2093 }
2094  
2095 # JWS authentication
2096 ▶ elsif ( my $atype = $req->param('client_assertion_type')
Condition33%2/6
my $atype = $req->param("client_assertion_type") and my $jws = $req->param("client_assertion") and my $_clientId = $req->param("client_id")
!ll&&!rl&&r
200
my $atype = $req->param("client_assertion_type") and my $jws = $req->param("client_assertion")
!ll&&!rl&&r
200
MC/DC0%0/3
my $atype = $req->param("client_assertion_type") and my $jws = $req->param("client_assertion") and my $_clientId = $req->param("client_id")
my $atype = $req->param("client_assertion_type") my $jws = $req->param("client_assertion") my $_clientId = $req->param("client_id")
Truth table25%1/4
A and B and C
A= my $atype = $req->param("client_assertion_type")B= my $jws = $req->param("client_assertion")C= my $_clientId = $req->param("client_id")
ABCresult
0XX0
10X0
1100
1111
2097 and my $jws = $req->param('client_assertion')
2098 and my $_clientId = $req->param('client_id') )
2099 {
2100 # Type must be 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer'
2101 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($atype eq 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer') { } 0 0 0
2102 $atype eq 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer' )
2103 {
2104 # JWS token must contain iss, sub and iss must be equal to usb
2105 0 my $payload = getJWTPayload($jws);
2106 0 ▶ if ( $payload
Branch: 1 branch
BranchTrueFalseTotal
if ($payload and ref $payload eq 'HASH' and $payload->{'iss'} and $payload->{'sub'} and $payload->{'iss'} eq $payload->{'sub'} and $payload->{'iss'} eq $_clientId) { } 0 0 0
Condition0%0/15
$payload and ref $payload eq "HASH" and $payload->{"iss"} and $payload->{"sub"} and $payload->{"iss"} eq $payload->{"sub"} and $payload->{"iss"} eq $_clientId
!ll&&!rl&&r
000
$payload and ref $payload eq "HASH" and $payload->{"iss"} and $payload->{"sub"} and $payload->{"iss"} eq $payload->{"sub"}
!ll&&!rl&&r
000
$payload and ref $payload eq "HASH" and $payload->{"iss"} and $payload->{"sub"}
!ll&&!rl&&r
000
$payload and ref $payload eq "HASH" and $payload->{"iss"}
!ll&&!rl&&r
000
$payload and ref $payload eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/6
$payload and ref $payload eq "HASH" and $payload->{"iss"} and $payload->{"sub"} and $payload->{"iss"} eq $payload->{"sub"} and $payload->{"iss"} eq $_clientId
$payload ref $payload eq "HASH" $payload->{"iss"} $payload->{"sub"} $payload->{"iss"} eq $payload->{"sub"} $payload->{"iss"} eq $_clientId
Truth table0%0/7
A and B and C and D and E and F
A= $payloadB= ref $payload eq "HASH"C= $payload->{"iss"}D= $payload->{"sub"}E= $payload->{"iss"} eq $payload->{"sub"}F= $payload->{"iss"} eq $_clientId
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
2107 and ( ref($payload) eq 'HASH' )
2108 and $payload->{iss}
2109 and $payload->{sub}
2110 and $payload->{iss} eq $payload->{sub}
2111 and $payload->{iss} eq $_clientId )
2112 {
2113 # client_id must match to a known relying party
2114 0 my $rp = $self->getRP($_clientId);
2115 0 ▶ if ($rp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rp) { } 0 0 0
2116  
2117 # RP must have a signature key registered
2118 # (key may be the secret for HS* alg)
2119 0 ▶ if ( $self->rpSigKey->{$rp}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rpSigKey->{$rp} or $self->rpOptions->{$rp}{'oidcRPMetaDataOptionsClientSecret'}) { } 0 0 0
Condition0%0/3
$self->rpSigKey->{$rp} or $self->rpOptions->{$rp}{"oidcRPMetaDataOptionsClientSecret"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->rpSigKey->{$rp} or $self->rpOptions->{$rp}{"oidcRPMetaDataOptionsClientSecret"}
$self->rpSigKey->{$rp} $self->rpOptions->{$rp}{"oidcRPMetaDataOptionsClientSecret"}
Truth table0%0/3
A or B
A= $self->rpSigKey->{$rp}B= $self->rpOptions->{$rp}{"oidcRPMetaDataOptionsClientSecret"}
ABresult
1X1
011
000
2120 or $self->rpOptions->{$rp}
2121 ->{oidcRPMetaDataOptionsClientSecret} )
2122 {
2123  
2124 # Signature must be valid
2125 0 my ( $jwt, $alg ) =
2126 $self->decodeJWT( $jws, undef, $rp );
2127 0 ▶ if ($jwt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jwt) { } 0 0 0
2128  
2129 0 ▶ $scheme =
Branch: 1 branch
BranchTrueFalseTotal
$alg =~ /^HS/i ? : 0 0 0
2130 $alg =~ /^HS/i
2131 ? 'client_secret_jwt'
2132 : 'private_key_jwt';
2133  
2134 # Token must be time-valid
2135 0 ▶ if ( $jwt->{aud} and $jwt->{exp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jwt->{'aud'} and $jwt->{'exp'}) { } 0 0 0
Condition0%0/3
$jwt->{"aud"} and $jwt->{"exp"}
!ll&&!rl&&r
000
MC/DC0%0/2
$jwt->{"aud"} and $jwt->{"exp"}
$jwt->{"aud"} $jwt->{"exp"}
Truth table0%0/3
A and B
A= $jwt->{"aud"}B= $jwt->{"exp"}
ABresult
0X0
100
111
2136 0 ▶ if ( time < $jwt->{exp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&CORE::GLOBAL::time() < $jwt->{'exp'}) { } 0 0 0
2137 0 $self->logger->debug("JWS is valid");
2138  
2139 # Then export the client_id !
2140 0 $client_id = $_clientId;
2141 }
2142 else {
2143 0 $self->logger->error('JWS expired');
2144 }
2145 }
2146 else {
2147 0 $self->logger->error(
2148 'Bad JWS content (missing aud or exp)');
2149 }
2150 }
2151 else {
2152 0 $self->logger->error('Bad JWS signature');
2153 }
2154 }
2155 else {
2156 0 $self->logger->error("No signature key found for $rp");
2157 }
2158 }
2159 else {
2160 0 $self->logger->error(
2161 "Unable to find any RP with client_id=$_clientId");
2162 }
2163 }
2164 else {
2165 0 $self->logger->error("Bad JWS payload: $jws");
2166 }
2167 }
2168 else {
2169 0 $self->logger->error("Unsuported client_assertion_type $atype");
2170 }
2171 }
2172 ▶ elsif ( $req->param('client_id')
Condition33%1/3
$req->param("client_id") and $req->body_parameters->{"client_secret"}
!ll&&!rl&&r
002
MC/DC0%0/2
$req->param("client_id") and $req->body_parameters->{"client_secret"}
$req->param("client_id") $req->body_parameters->{"client_secret"}
Truth table33%1/3
A and B
A= $req->param("client_id")B= $req->body_parameters->{"client_secret"}
ABresult
0X0
100
111
2173 and $req->body_parameters->{client_secret} )
2174 {
2175 2 $scheme = 'client_secret_post';
2176 2 $self->logger->debug("Method client_secret_post used");
2177 2 $client_id = $req->param('client_id');
2178 2 $client_secret = $req->param('client_secret');
2179 }
2180 elsif ( $req->param('client_id') ) {
2181 0 $scheme = 'none';
2182 0 $self->logger->debug("Method none used");
2183 0 $client_id = $req->param('client_id');
2184 }
2185  
2186 18 return ( $client_id, $client_secret, $scheme );
2187 }
2188  
2189 # Get Access Token
2190 # @return access_token
2191 sub getEndPointAccessToken {
2192 13 ▶ my ( $self, $req ) = @_;
Subroutine getEndPointAccessToken: called CC=5 SCAR=19.3
Pod documented
2193 13 my ( $access_token, $method );
2194  
2195 13 my $authorization = $req->authorization;
2196 13 ▶ if ( $authorization and $authorization =~ /^Bearer ([\w\-\.]+)/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($authorization and $authorization =~ /^Bearer ([\w\-\.]+)/i) { } 13 0 13
elsif ($access_token = $req->param('access_token')) { } 0 0 0
Condition33%1/3
$authorization and $authorization =~ /^Bearer ([\w\-\.]+)/i
!ll&&!rl&&r
0013
MC/DC0%0/2
$authorization and $authorization =~ /^Bearer ([\w\-\.]+)/i
$authorization $authorization =~ /^Bearer ([\w\-\.]+)/i
Truth table33%1/3
A and B
A= $authorizationB= $authorization =~ /^Bearer ([\w\-\.]+)/i
ABresult
0X0
100
111
2197 13 $self->logger->debug("Bearer access token");
2198 13 $access_token = $1;
2199 13 $method = 'header';
2200 }
2201 elsif ( $access_token = $req->param('access_token') ) {
2202 0 $self->logger->debug("GET/POST access token");
2203 0 $method = 'param';
2204 }
2205  
2206 13 ▶ return wantarray ? ( $access_token, $method ) : $access_token;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 13 0 13
2207 }
2208  
2209 # DEPRECATED, remove in 3.0, use getAttributeListFromScopeValue instead
2210 sub getAttributesListFromClaim {
2211 0 ▶ my ( $self, $rp, $scope_value ) = @_;
Subroutine getAttributesListFromClaim: not called CC=1 SCAR=6.9
Pod documented
2212 0 return $self->getAttributesListFromScopeValue( $rp, $scope_value );
2213 }
2214  
2215 # Return list of attributes authorized for a claim
2216 # @param rp RP name
2217 # @param claim Claim
2218 # @return arrayref attributes list
2219 sub getAttributesListFromScopeValue {
2220 29 ▶ my ( $self, $rp, $scope_value ) = @_;
Subroutine getAttributesListFromScopeValue: called CC=1 SCAR=0.0
Pod undocumented
2221 29 return $self->rpScopes->{$rp}->{$scope_value};
2222 }
2223  
2224 # Return granted scopes for this request
2225 # @param req current request
2226 # @param req selected RP
2227 # @param scope requested scope
2228 sub getScope {
2229 15 ▶ my ( $self, $req, $rp, $scope ) = @_;
Subroutine getScope: called CC=10 SCAR=28.9
Pod undocumented
2230  
2231 15 my @scope_values = split( /\s+/, $scope );
2232  
2233 # Clean up unknown scopes
2234 15 ▶ if ( $self->conf->{oidcServiceAllowOnlyDeclaredScopes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"oidcServiceAllowOnlyDeclaredScopes"}) 3 12 15
2235 my @known_scopes = (
2236 3 ▶ keys( %{ $self->rpScopeRules->{$rp} || {} } ),
Branch: 1 branch
BranchTrueFalseTotal
$self->rpScopeRules->{$rp} or {} 0 3 3
2237 3 ▶ keys( %{ $self->rpScopes->{$rp} || {} } ),
Branch: 1 branch
BranchTrueFalseTotal
$self->rpScopes->{$rp} or {} 0 3 3
2238 'openid', 'offline_access',
2239 );
2240 3 my @scope_values_tmp;
2241 3 for my $scope_value (@scope_values) {
2242 4 ▶ if ( grep { $_ eq $scope_value } @known_scopes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $scope_value;} @known_scopes) { } 4 0 4
2243 4 push @scope_values_tmp, $scope_value;
2244 }
2245 else {
2246 0 $self->logger->warn(
2247 "Unknown scope $scope_value requested for service $rp");
2248 }
2249 }
2250 3 @scope_values = @scope_values_tmp;
2251 }
2252  
2253 # If this RP has dynamic scopes
2254 15 ▶ if ( $self->rpScopeRules->{$rp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rpScopeRules->{$rp}) 15 0 15
2255  
2256 # Add dynamic scopes
2257 15 for my $dynamicScope ( keys %{ $self->rpScopeRules->{$rp} } ) {
2258  
2259 # Set a magic "$requested" variable that contains true if the
2260 # scope was requested by the application
2261 0 my $requested = grep { $_ eq $dynamicScope } @scope_values;
2262 0 my $attributes = { %{ $req->userData }, requested => $requested };
2263  
2264 # If scope is granted by the rule
2265 0 ▶ if ( $self->rpScopeRules->{$rp}->{$dynamicScope}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rpScopeRules->{$rp}{$dynamicScope}->($req, $attributes)) { } 0 0 0
2266 ->( $req, $attributes ) )
2267 {
2268 # Add to list
2269 0 ▶ unless ( grep { $_ eq $dynamicScope } @scope_values ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $dynamicScope;} @scope_values) 0 0 0
2270 0 push @scope_values, $dynamicScope;
2271 }
2272  
2273 }
2274  
2275 # Else make sure it is not granted
2276 else {
2277 0 @scope_values = grep { $_ ne $dynamicScope } @scope_values;
2278 }
2279 }
2280 }
2281  
2282 15 $self->p->processHook( $req, 'oidcResolveScope', \@scope_values, $rp );
2283  
2284 15 my $scope_str = join( ' ', @scope_values );
2285 15 $self->logger->debug("Resolved scopes: $scope_str");
2286 15 return $scope_str;
2287 }
2288  
2289 # Return Hash of UserInfo data
2290 # @param scope OIDC scope
2291 # @param rp Internal Relying Party identifier
2292 # @param user_session_id User session identifier
2293 # @return hashref UserInfo data
2294 sub buildUserInfoResponseFromId {
2295 0 ▶ my ( $self, $req, $scope, $rp, $user_session_id ) = @_;
Subroutine buildUserInfoResponseFromId: not called CC=2 SCAR=17.9
Pod documented
2296 0 my $session = $self->p->getApacheSession($user_session_id);
2297  
2298 0 ▶ return undef unless ($session);
Branch: 1 branch
BranchTrueFalseTotal
unless $session 0 0 0
2299 0 return buildUserInfoResponse( $self, $req, $scope, $rp, $session );
2300 }
2301  
2302 # Return Hash of UserInfo data
2303 # @param scope OIDC scope
2304 # @param rp Internal Relying Party identifier
2305 # @param session SSO or offline session
2306 # @return hashref UserInfo data
2307 sub buildUserInfoResponse {
2308 8 ▶ my ( $self, $req, $scope, $rp, $session ) = @_;
Subroutine buildUserInfoResponse: called CC=1 SCAR=0.0
Pod documented
2309 8 return $self->buildUserInfoResponseFromData( $req, $scope, $rp,
2310 $session->data );
2311 }
2312  
2313 sub _addAttributeToResponse {
2314 250 ▶ my ( $self, $req, $data, $userinfo_response, $rp, $attribute ) = @_;
Subroutine _addAttributeToResponse: called CC=8 SCAR=20.9
2315 my @attrConf = split /;/,
2316 250 ▶ ( $self->rpAttributes->{$rp}->{$attribute} || "" );
Condition100%2/2
$self->rpAttributes->{$rp}{$attribute} || ''
l!l
56194
MC/DC100%1/1
$self->rpAttributes->{$rp}{$attribute} || ''
$self->rpAttributes->{$rp}{$attribute}
Truth table100%2/2
A
A= $self->rpAttributes->{$rp}{$attribute}
Aresult
11
00
2317 250 my $session_key = $attrConf[0];
2318 250 ▶ if ($session_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session_key) 56 194 250
2319 56 ▶ my $type = $attrConf[1] || 'string';
Condition50%1/2
$attrConf[1] || 'string'
l!l
056
MC/DC0%0/1
$attrConf[1] || 'string'
$attrConf[1]
Truth table50%1/2
A
A= $attrConf[1]
Aresult
11
00
2320 56 ▶ my $array = $attrConf[2] || 'auto';
Condition50%1/2
$attrConf[2] || 'auto'
l!l
056
MC/DC0%0/1
$attrConf[2] || 'auto'
$attrConf[2]
Truth table50%1/2
A
A= $attrConf[2]
Aresult
11
00
2321  
2322 56 my $session_value;
2323  
2324 # Lookup attribute in macros first
2325 56 ▶ if ( $self->rpMacros->{$rp}->{$session_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rpMacros->{$rp}{$session_key}) { } 8 48 56
2326 $session_value =
2327 8 $self->rpMacros->{$rp}->{$session_key}->( $req, $data );
2328  
2329 # If not found, search in session
2330 }
2331 else {
2332 48 $session_value = $data->{$session_key};
2333 }
2334  
2335 # Handle empty values, arrays, type, etc.
2336 56 $session_value =
2337 $self->_formatValue( $session_value, $type, $array,
2338 $attribute, $req->user );
2339  
2340 # From this point on, do NOT touch $session_value
2341 # or you will break the variable's type.
2342  
2343 # Only release claim if it has a value
2344 56 ▶ if ( defined $session_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $session_value) 56 0 56
2345  
2346 # If this attribute is a standardized subkey (address)
2347 56 ▶ if ( COMPLEX_CLAIM->{$attribute} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ({country => 'address', formatted => 'address', locality => 'address', postal_code => 'address', region => 'address', street_address => 'address'}->{$attribute}) { } 2 54 56
2348 2 my $superkey = COMPLEX_CLAIM->{$attribute};
2349 2 $userinfo_response->{$superkey}->{$attribute} = $session_value;
2350 }
2351 else {
2352 54 $userinfo_response->{$attribute} = $session_value;
2353 }
2354 }
2355 }
2356 }
2357  
2358 # Return Hash of UserInfo data
2359 # @param scope OIDC scope
2360 # @param rp Internal Relying Party identifier
2361 # @param sessionInfo hash of session data
2362 # @return hashref UserInfo data
2363 sub buildUserInfoResponseFromData {
2364 17 ▶ my ( $self, $req, $scope, $rp, $session_data ) = @_;
Subroutine buildUserInfoResponseFromData: called CC=10 SCAR=23.2
Pod undocumented
2365 17 my $userinfo_response = {};
2366  
2367 my $data = {
2368 17 %{$session_data},
2369 _clientId => $self->rpOptions->{$rp}->{oidcRPMetaDataOptionsClientID},
2370 17 _clientConfKey => $rp,
2371 _scope => $scope,
2372 };
2373 17 my $user_id = $self->getUserIDForRP( $req, $rp, $data );
2374  
2375 17 $self->logger->debug("Found corresponding user: $user_id");
2376  
2377 17 $userinfo_response->{sub} = $user_id;
2378 17 $userinfo_response->{sid} = $self->getSidFromSession( $rp, $session_data );
2379  
2380 # By default, release all exported attributes
2381 17 ▶ if ( $self->conf->{oidcServiceIgnoreScopeForClaims} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{'oidcServiceIgnoreScopeForClaims'}) { } 2 15 17
2382 2 ▶ for my $attribute ( keys %{ $self->rpAttributes->{$rp} || {} } ) {
Branch: 1 branch
BranchTrueFalseTotal
$self->rpAttributes->{$rp} or {} 0 2 2
2383 12 $self->_addAttributeToResponse( $req, $data, $userinfo_response,
2384 $rp, $attribute );
2385 }
2386  
2387 # Else, iterate through scopes to find allowed attributes
2388 }
2389 else {
2390 15 foreach my $scope_value ( split( /\s/, $scope ) ) {
2391 44 ▶ next if ( $scope_value eq "openid" );
Branch: 1 branch
BranchTrueFalseTotal
if $scope_value eq "openid" 15 29 44
2392 29 $self->logger->debug(
2393 "Get attributes linked to scope value $scope_value");
2394 29 my $list =
2395 $self->getAttributesListFromScopeValue( $rp, $scope_value );
2396 $self->logger->debug(
2397 29 ▶ "-> found attributes: " . join( " ", @{ $list || [] } ) );
Branch: 1 branch
BranchTrueFalseTotal
$list or [] 0 29 29
2398 29 ▶ next unless $list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 0 29 29
2399 29 foreach my $attribute (@$list) {
2400 238 $self->_addAttributeToResponse( $req, $data,
2401 $userinfo_response, $rp, $attribute );
2402 }
2403 }
2404 }
2405  
2406 17 my $h = $self->p->processHook( $req, 'oidcGenerateUserInfoResponse',
2407 $userinfo_response, $rp, $data );
2408 17 ▶ return {} if ( $h != PE_OK );
Branch: 1 branch
BranchTrueFalseTotal
if $h != 0 0 17 17
2409  
2410 17 return $userinfo_response;
2411 }
2412  
2413 sub _formatValue {
2414 56 ▶ my ( $self, $session_value, $type, $array, $attribute, $user ) = @_;
Subroutine _formatValue: called CC=4 SCAR=15.5
2415  
2416 # If $session_value is not a scalar, return it as is
2417 56 ▶ unless ( ref($session_value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $session_value) 56 0 56
2418 56 ▶ if ( defined $session_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $session_value) 56 0 56
2419  
2420 # Empty strings or lists are invalid values
2421 56 ▶ if ( length($session_value) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $session_value > 0) { } 56 0 56
2422  
2423 # Format value for JSON output: multi valuation, JSON type...
2424 56 my $separator = $self->conf->{multiValuesSeparator};
2425 56 return $self->_applyType( $session_value, $separator, $type,
2426 $array, $attribute, $user );
2427 }
2428 else {
2429 0 return undef;
2430 }
2431 }
2432 }
2433 0 return $session_value;
2434 }
2435  
2436 sub _applyType {
2437 56 ▶ my ( $self, $session_value, $separator, $type, $array, $attribute, $user )
Subroutine _applyType: called CC=7 SCAR=31.5
2438 = @_;
2439  
2440 # Array style handling
2441 # In auto array mode, split as array only if there are multiple values
2442 56 ▶ if ( $array eq "auto" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($array eq 'auto') { } 56 0 56
elsif ($array eq 'always') { } 0 0 0
2443 56 ▶ if ( $session_value and $session_value =~ /$separator/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session_value and $session_value =~ /$separator/) { } 0 56 56
Condition33%1/3
$session_value and $session_value =~ /$separator/
!ll&&!rl&&r
0560
MC/DC0%0/2
$session_value and $session_value =~ /$separator/
$session_value $session_value =~ /$separator/
Truth table33%1/3
A and B
A= $session_valueB= $session_value =~ /$separator/
ABresult
0X0
100
111
2444 $session_value = [
2445 0 map { $self->_forceType( $_, $type ) }
2446 split( $separator, $session_value )
2447 ];
2448 }
2449 else {
2450 56 $session_value = $self->_forceType( $session_value, $type );
2451 }
2452  
2453 # In always array mode, always split (even on empty values)
2454 }
2455 elsif ( $array eq "always" ) {
2456 $session_value = [
2457 0 map { $self->_forceType( $_, $type ) }
2458 split( $separator, $session_value )
2459 ];
2460 }
2461  
2462 # In never array mode, return the string as-is
2463 else {
2464 # No type coaxing is possible on a flattened string
2465 0 ▶ if ( $session_value =~ /$separator/ and $type ne "string" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session_value =~ /$separator/ and $type ne 'string') { } 0 0 0
Condition0%0/3
$session_value =~ /$separator/ and $type ne "string"
!ll&&!rl&&r
000
MC/DC0%0/2
$session_value =~ /$separator/ and $type ne "string"
$session_value =~ /$separator/ $type ne "string"
Truth table0%0/3
A and B
A= $session_value =~ /$separator/B= $type ne "string"
ABresult
0X0
100
111
2466 0 $self->logger->warn( "Cannot force type of value $session_value"
2467 . " for attribute $attribute of user "
2468 . $user
2469 . " because it is multi-valued. "
2470 . "Use auto or always as array type for this attribute" );
2471 }
2472 else {
2473 0 $session_value = $self->_forceType( $session_value, $type );
2474 }
2475 }
2476  
2477 56 return $session_value;
2478 }
2479  
2480 sub _forceType {
2481 56 ▶ my ( $self, $val, $type ) = @_;
Subroutine _forceType: called CC=4 SCAR=16.1
2482  
2483 # Boolean
2484 56 ▶ return ( $val ? JSON::true : JSON::false ) if ( $type eq "bool" );
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "bool" 0 56 56
$val ? : 0 0 0
2485  
2486 # Coax into int
2487 56 ▶ return ( $val + 0 ) if ( $type eq "int" );
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "int" 0 56 56
2488  
2489 # Coax into string
2490 56 return ( $val . "" );
2491 }
2492  
2493 # Return JWT
2494 # @param payload JWT content
2495 # @param alg Signature algorithm
2496 # @param rp Internal Relying Party identifier
2497 # @return String jwt JWT
2498  
2499 sub createJWT {
2500 18 ▶ my ( $self, $payload, $alg, $rp, $extra_headers_or_type ) = @_;
Subroutine createJWT: called CC=3 SCAR=12.7
Pod documented
2501  
2502 18 my $extra_headers = $extra_headers_or_type;
2503  
2504 # Compatibility with old signature
2505 18 ▶ if ( !ref($extra_headers_or_type) and $extra_headers_or_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $extra_headers_or_type and $extra_headers_or_type) 0 18 18
Condition33%1/3
not ref $extra_headers_or_type and $extra_headers_or_type
!ll&&!rl&&r
1800
MC/DC0%0/2
not ref $extra_headers_or_type and $extra_headers_or_type
not ref $extra_headers_or_type $extra_headers_or_type
Truth table33%1/3
not(A) and B
A= not ref $extra_headers_or_typeB= $extra_headers_or_type
ABresult
0X0
100
111
2506 0 $extra_headers = { typ => $extra_headers_or_type };
2507 }
2508  
2509 18 return $self->_createJWT( $payload, $alg, $rp, $extra_headers );
2510 }
2511  
2512 sub createJWTForOP {
2513 0 ▶ my ( $self, $payload, $alg, $op, $extra_headers_or_type ) = @_;
Subroutine createJWTForOP: not called CC=3 SCAR=24.8
Pod undocumented
2514  
2515 0 my $extra_headers = $extra_headers_or_type;
2516  
2517 # Compatibility with old signature
2518 0 ▶ if ( !ref($extra_headers_or_type) and $extra_headers_or_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $extra_headers_or_type and $extra_headers_or_type) 0 0 0
Condition0%0/3
not ref $extra_headers_or_type and $extra_headers_or_type
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $extra_headers_or_type and $extra_headers_or_type
not ref $extra_headers_or_type $extra_headers_or_type
Truth table0%0/3
not(A) and B
A= not ref $extra_headers_or_typeB= $extra_headers_or_type
ABresult
0X0
100
111
2519 0 $extra_headers = { typ => $extra_headers_or_type };
2520 }
2521  
2522 0 return $self->_createJWT( $payload, $alg, $op, $extra_headers, 1 );
2523 }
2524  
2525 sub _createJWT {
2526 18 ▶ my ( $self, $payload, $alg, $partner, $extra_headers, $isRp ) = @_;
Subroutine _createJWT: called CC=17 SCAR=33.2
2527  
2528 18 my @keyArg;
2529 18 ▶ $extra_headers //= {};
Condition50%1/2
$extra_headers //= {}
l!l
180
MC/DC0%0/1
$extra_headers //= {}
$extra_headers
Truth table50%1/2
A
A= $extra_headers
Aresult
11
00
2530  
2531 # Set Cript::JWT arguments depending on "alg"
2532 # a) "none"
2533 18 ▶ if ( $alg eq 'none' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($alg eq 'none') { } 0 18 18
elsif ($alg =~ /^HS/) { } 8 10 18
2534 0 @keyArg = ( allow_none => 1 );
2535 }
2536  
2537 # b) HMAC algorithms, key is the client secret
2538 elsif ( $alg =~ /^HS/ ) {
2539  
2540 # Sign with client secret
2541 my $client_secret =
2542 $isRp
2543 ? $self->opOptions->{$partner}->{oidcOPMetaDataOptionsClientSecret}
2544 8 ▶ : $self->rpOptions->{$partner}->{oidcRPMetaDataOptionsClientSecret};
Branch: 1 branch
BranchTrueFalseTotal
$isRp ? : 0 8 8
2545 8 ▶ unless ($client_secret) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($client_secret) 0 8 8
2546 0 $self->logger->error(
2547 "Algorithm $alg needs a Client Secret to sign JWT");
2548 0 return;
2549 }
2550 8 @keyArg = ( key => $client_secret );
2551 }
2552  
2553 # c) asymetric algorithms
2554 else {
2555 my $key_list =
2556 $isRp
2557 ? $self->opOptions->{$partner}->{oidcOPMetaDataOptionsSigningKey}
2558 10 ▶ : $self->rpOptions->{$partner}->{oidcRPMetaDataOptionsSigningKey};
Branch: 1 branch
BranchTrueFalseTotal
$isRp ? : 0 10 10
2559 10 ▶ $key_list ||= $self->conf->{oidcServiceSignatureKey};
Condition33%1/3
$key_list ||= $self->conf->{"oidcServiceSignatureKey"}
l!l&&r!l&&!r
0100
MC/DC0%0/2
$key_list ||= $self->conf->{"oidcServiceSignatureKey"}
$key_list $self->conf->{"oidcServiceSignatureKey"}
Truth table33%1/3
A ||= B
A= $key_listB= $self->conf->{"oidcServiceSignatureKey"}
ABresult
1X1
011
000
2560 10 my $key_id = ( split( /\s*,\s*/, $key_list ) )[0];
2561 10 my $priv_key = $self->get_private_key($key_id);
2562  
2563 10 ▶ unless ($priv_key) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($priv_key) 0 10 10
2564 0 $self->logger->error(
2565 "Algorithm $alg needs a Private Key to sign JWT");
2566 0 return;
2567 }
2568  
2569 10 my $key_type = $self->_getKeyType($priv_key);
2570 10 my $required_key_type = $self->_getRequiredKeyTypeForAlg($alg);
2571 10 ▶ if ( $required_key_type and $key_type ne $required_key_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($required_key_type and $key_type ne $required_key_type) 0 10 10
Condition33%1/3
$required_key_type and $key_type ne $required_key_type
!ll&&!rl&&r
0100
MC/DC0%0/2
$required_key_type and $key_type ne $required_key_type
$required_key_type $key_type ne $required_key_type
Truth table33%1/3
A and B
A= $required_key_typeB= $key_type ne $required_key_type
ABresult
0X0
100
111
2572 0 $self->logger->error(
2573 "Algorithm $alg needs a $required_key_type key to sign JWT,"
2574 . " but key $key_id is $key_type" );
2575 0 return;
2576 }
2577  
2578 10 @keyArg = ( key => \$priv_key->{private}, );
2579  
2580 10 ▶ if ( $priv_key->{external_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($priv_key->{"external_id"}) 1 9 10
2581 1 $extra_headers->{kid} = $priv_key->{external_id};
2582 }
2583 10 my $key_info = $self->getCertInfo( $priv_key->{public} );
2584 10 ▶ if ( $key_info->{x5t} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_info->{"x5t"}) 10 0 10
2585 10 $extra_headers->{x5t} = $key_info->{x5t};
2586 }
2587 }
2588  
2589 my $noTyp =
2590 $isRp
2591 ? $self->opOptions->{$partner}->{oidcOPMetaDataOptionsNoJwtHeader}
2592 18 ▶ : $self->rpOptions->{$partner}->{oidcRPMetaDataOptionsNoJwtHeader};
Branch: 1 branch
BranchTrueFalseTotal
$isRp ? : 0 18 18
2593 18 ▶ if ($noTyp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($noTyp) { } 3 15 18
2594 3 delete $extra_headers->{typ};
2595 }
2596 else {
2597 15 ▶ $extra_headers->{typ} ||= 'JWT';
Condition50%1/2
$extra_headers->{"typ"} ||= "JWT"
l!l
150
MC/DC0%0/1
$extra_headers->{"typ"} ||= "JWT"
$extra_headers->{"typ"}
Truth table50%1/2
A
A= $extra_headers->{"typ"}
Aresult
11
00
2598 }
2599  
2600 # Encode payload here due to #2748
2601 18 my $jwt = eval {
2602 18 encode_jwt(
2603 payload => to_json($payload),
2604 alg => $alg,
2605 extra_headers => $extra_headers,
2606 @keyArg,
2607 );
2608 };
2609 18 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 18 18
2610 0 $self->logger->error("Unable to build JWT: $@");
2611 0 return;
2612 }
2613 18 return $jwt;
2614 }
2615  
2616 # Return ID Token
2617 # @param payload ID Token content
2618 # @param rp Internal Relying Party identifier
2619 # @return String id_token ID Token as JWT
2620 sub createIDToken {
2621 14 ▶ my ( $self, $req, $payload, $rp, $sessionData ) = @_;
Subroutine createIDToken: called CC=3 SCAR=11.1
Pod documented
2622  
2623 # Get signature algorithm
2624 14 my $alg = $self->rpOptions->{$rp}->{oidcRPMetaDataOptionsIDTokenSignAlg};
2625 14 $self->logger->debug("ID Token signature algorithm: $alg");
2626  
2627 14 my $extra_headers = { typ => "JWT" };
2628 14 my $h = $self->p->processHook( $req, 'oidcGenerateIDToken', $payload, $rp,
2629 $sessionData, $extra_headers );
2630 14 ▶ return undef if ( $h != PE_OK );
Branch: 1 branch
BranchTrueFalseTotal
if $h != 0 0 14 14
2631  
2632 14 my $id_token = $self->createJWT( $payload, $alg, $rp, $extra_headers );
2633 14 ▶ return undef unless $id_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $id_token 0 14 14
2634  
2635 return $self->encryptToken(
2636 $rp,
2637 $id_token,
2638 $self->rpOptions->{$rp}->{oidcRPMetaDataOptionsAccessTokenEncKeyMgtAlg},
2639 $self->rpOptions->{$rp}
2640 ->{oidcRPMetaDataOptionsAccessTokenEncContentEncAlg},
2641 14 );
2642 }
2643  
2644 # Return flow type
2645 # @param response_type Response type
2646 # @return String flow
2647 sub getFlowType {
2648 14 ▶ my ( $self, $response_type ) = @_;
Subroutine getFlowType: called CC=1 SCAR=0.0
Pod documented
2649  
2650 return {
2651 "code" => "authorizationcode",
2652 "id_token" => "implicit",
2653 "id_token token" => "implicit",
2654 "code id_token" => "hybrid",
2655 "code token" => "hybrid",
2656 "code id_token token" => "hybrid",
2657 14 }->{$response_type};
2658 }
2659  
2660 # Return sub field of an ID Token
2661 # @param id_token ID Token
2662 # @return String sub
2663 sub getIDTokenSub {
2664 0 ▶ my ( $self, $id_token ) = @_;
Subroutine getIDTokenSub: not called CC=1 SCAR=6.9
Pod documented
2665 0 my $payload = getJWTPayload($id_token);
2666 0 return $payload->{sub};
2667 }
2668  
2669 # Return JWKS representation of a key
2670 # @param key Raw key
2671 # @return HashRef JWKS key
2672 sub key2jwks {
2673 30 ▶ my ( $self, $key, $type ) = @_;
Subroutine key2jwks: called CC=3 SCAR=11.0
Pod documented
2674  
2675 30 ▶ if ( $type and $type eq 'EC' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type and $type eq 'EC') { } 7 23 30
Condition100%3/3
$type and $type eq "EC"
!ll&&!rl&&r
1227
MC/DC100%2/2
$type and $type eq "EC"
$type $type eq "EC"
Truth table100%3/3
A and B
A= $typeB= $type eq "EC"
ABresult
0X0
100
111
2676 7 require Crypt::PK::ECC;
2677 7 my $eck = Crypt::PK::ECC->new();
2678 7 $eck->import_key( \$key );
2679 7 return $eck->export_key_jwk( 'public', 1 );
2680 }
2681 else {
2682 23 require Crypt::PK::RSA;
2683 23 my $pk = Crypt::PK::RSA->new();
2684 23 $pk->import_key( \$key );
2685 23 return $pk->export_key_jwk( 'public', 1 );
2686 }
2687 }
2688  
2689 # Return X.509 data if public key is a certificate
2690 # @param key public key or certificate
2691 # @return HashRef of JWK attributes
2692 sub getCertInfo {
2693 39 ▶ my ( $self, $key ) = @_;
Subroutine getCertInfo: called CC=2 SCAR=6.9
Pod undocumented
2694  
2695 39 ▶ if ( $key =~ /CERTIFICATE/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /CERTIFICATE/) { } 31 8 39
2696 31 my $x509 = Crypt::OpenSSL::X509->new_from_string( $key,
2697 Crypt::OpenSSL::X509::FORMAT_PEM );
2698 31 my $der = $x509->as_string(Crypt::OpenSSL::X509::FORMAT_ASN1);
2699 31 my $hash = sha1($der);
2700 return {
2701 # Caution, x5c is B64, x5t is B64URL, this is not a mistake
2702 31 x5c => [ encode_base64( $der, '' ) ],
2703 x5t => encode_base64url($hash),
2704 };
2705  
2706 }
2707 else {
2708 8 return {};
2709 }
2710 }
2711  
2712 ### JWKS ENDPOINT
2713  
2714 # Keys to display in jwks endpoint:
2715 # Signature:
2716 # - current, new and old key to permit to clients to verify all JWT emitted
2717 # during 3 weeks
2718 # Encryption:
2719 # - only the current key. Old encryption key is kept to permit to
2720 # Auth::OPenIDCOnnect to decrypt all JWE emitted by Issuer but no
2721 # need to display any other key
2722 sub _buildJwk {
2723 83 ▶ my ( $self, $type, $key ) = @_;
Subroutine _buildJwk: called CC=5 SCAR=16.1
2724 83 ▶ return unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 54 29 83
2725  
2726 29 my $publicKeyOrCert = $key->{public};
2727 29 my $keyId = $key->{external_id};
2728 29 my $keytype = $self->_getKeyType($key);
2729 return $publicKeyOrCert
2730 ? {
2731 kty => $keytype,
2732 use => lc($type),
2733 (
2734 $type eq 'Enc'
2735 ? ( alg => $self->conf->{oidcServiceEncAlgorithmAlg} )
2736 : ()
2737 ),
2738 ( $keyId ? ( kid => $keyId ) : () ),
2739 29 %{ $self->key2jwks( $publicKeyOrCert, $keytype ) },
2740 29 ▶ %{ $self->getCertInfo($publicKeyOrCert) },
Branch: 3 branches
BranchTrueFalseTotal
$publicKeyOrCert ? : 29 0 29
$type eq 'Enc' ? : 3 26 29
$keyId ? : 26 3 29
2741 }
2742 : ();
2743 }
2744  
2745 # Handle jwks endpoint
2746 sub jwks {
2747 22 ▶ my ( $self, $req ) = @_;
Subroutine jwks: called CC=9 SCAR=22.3
Pod undocumented
2748 22 ▶ $req->data->{dropCsp} = 1 if $self->conf->{oidcDropCspHeaders};
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{"oidcDropCspHeaders"} 0 22 22
2749 22 $self->logger->debug("URL detected as an OpenID Connect JWKS URL");
2750  
2751 22 my $jwks = { keys => [] };
2752 22 my $client_id = $req->param('client_id');
2753  
2754 22 my $list_sig_keys;
2755 22 ▶ if ($client_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client_id) 1 21 22
2756 1 my $rp = $self->getRP($client_id);
2757 1 ▶ if ($rp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rp) 1 0 1
2758 $list_sig_keys =
2759 1 $self->rpOptions->{$rp}->{oidcRPMetaDataOptionsSigningKey};
2760 }
2761 }
2762 22 ▶ $list_sig_keys ||= $self->conf->{oidcServiceSignatureKey} || "";
Condition60%3/5
$list_sig_keys ||= $self->conf->{"oidcServiceSignatureKey"} || ""
l!l&&r!l&&!r
1210
$self->conf->{'oidcServiceSignatureKey'} || ''
l!l
210
MC/DC0%0/2
$list_sig_keys ||= $self->conf->{"oidcServiceSignatureKey"} || ""
$list_sig_keys $self->conf->{'oidcServiceSignatureKey'}
Truth table66%2/3
A ||= B
A= $list_sig_keysB= $self->conf->{'oidcServiceSignatureKey'}
ABresult
1X1
011
000
2763  
2764 22 for my $sig_key ( split( /\s*,\s*/, $list_sig_keys ) ) {
2765 61 push @{ $jwks->{keys} },
2766 $self->_buildJwk( "Sig", $self->get_public_key($sig_key) );
2767 }
2768  
2769 22 ▶ for my $enc_key (
Condition50%1/2
$self->conf->{'oidcServiceEncryptionKey'} || ''
l!l
220
MC/DC0%0/1
$self->conf->{'oidcServiceEncryptionKey'} || ''
$self->conf->{'oidcServiceEncryptionKey'}
Truth table50%1/2
A
A= $self->conf->{'oidcServiceEncryptionKey'}
Aresult
11
00
2770 split( /\s*,\s*/, ( $self->conf->{oidcServiceEncryptionKey} || "" ) ) )
2771 {
2772 22 push @{ $jwks->{keys} },
2773 $self->_buildJwk( "Enc", $self->get_public_key($enc_key) );
2774 }
2775  
2776 22 $self->logger->debug("Send JWKS response sent");
2777 22 return $self->p->sendJSONresponse( $req, $jwks );
2778 }
2779  
2780 # Build Logout Request URI
2781 # @param redirect_uri Redirect URI
2782 # @param id_token_hint ID Token
2783 # @param post_logout_redirect_uri Callback URI
2784 # @param state State
2785 # return String Logout URI
2786 sub buildLogoutRequest {
2787 0 ▶ my ( $self, $redirect_uri, @args ) = @_;
Subroutine buildLogoutRequest: not called CC=5 SCAR=34.0
Pod documented
2788  
2789 0 my @tab = (qw(id_token_hint post_logout_redirect_uri state client_id));
2790 0 my @prms;
2791 0 ▶ for ( my $i = 0 ; $i < @tab ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @tab) 0 0 0
2792 0 ▶ push @prms, $tab[$i], $args[$i]
Branch: 1 branch
BranchTrueFalseTotal
if defined $args[$i] 0 0 0
2793 if defined( $args[$i] );
2794 }
2795 0 my $response_url = $redirect_uri;
2796 0 ▶ $response_url .=
Branch: 2 branches
BranchTrueFalseTotal
if @prms 0 0 0
$response_url =~ /\?/ ? : 0 0 0
2797 ( $response_url =~ /\?/ ? '&' : '?' ) . build_urlencoded(@prms)
2798 if (@prms);
2799 0 return $response_url;
2800 }
2801  
2802 # Build Logout Response URI
2803 # @param redirect_uri Redirect URI
2804 # @param state State
2805 # return String Logout URI
2806 sub buildLogoutResponse {
2807 0 ▶ my ( $self, $redirect_uri, $state ) = @_;
Subroutine buildLogoutResponse: not called CC=3 SCAR=24.8
Pod documented
2808  
2809 0 my $response_url = $redirect_uri;
2810  
2811 0 ▶ if ($state) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state) 0 0 0
2812 0 ▶ $response_url .= ( $redirect_uri =~ /\?/ ? '&' : '?' );
Branch: 1 branch
BranchTrueFalseTotal
$redirect_uri =~ /\?/ ? : 0 0 0
2813 0 $response_url .= build_urlencoded( state => $state );
2814 }
2815  
2816 0 return URI->new($response_url)->as_string;
2817 }
2818  
2819 # Create session_state parameter
2820 # @param session_id Session ID
2821 # @param client_id Client ID
2822 # return String Session state
2823 sub createSessionState {
2824 13 ▶ my ( $self, $session_id, $client_id ) = @_;
Subroutine createSessionState: called CC=2 SCAR=6.9
Pod undocumented
2825  
2826 my $salt =
2827 13 encode_base64url( $self->conf->{cipher}->encrypt($client_id) );
2828 13 my $data = $client_id . " " . $session_id . " " . $salt;
2829  
2830 13 my $hash = sha256_base64($data);
2831 13 ▶ while ( length($hash) % 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($hash) % 4) 13 13 26
2832 13 $hash .= '=';
2833 }
2834  
2835 13 my $session_state = $hash . "." . $salt;
2836  
2837 13 return $session_state;
2838 }
2839  
2840 # Get request JWT from request uri
2841 # @param request_uri request uri
2842 # return String request JWT
2843 sub getRequestJWT {
2844 0 ▶ my ( $self, $request_uri ) = @_;
Subroutine getRequestJWT: not called CC=2 SCAR=17.9
Pod undocumented
2845  
2846 0 my $response = $self->ua->get($request_uri);
2847  
2848 0 ▶ if ( $response->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 0 0 0
2849 0 $self->logger->error( "Unable to get request JWT on $request_uri: "
2850 . $response->message );
2851 0 $self->logger->debug(
2852 "Request JWT response received: " . $response->as_string );
2853 0 return;
2854 }
2855  
2856 0 return $response->decoded_content;
2857 }
2858  
2859 sub addRouteFromConf {
2860 208 ▶ my ( $self, $type, %subs ) = @_;
Subroutine addRouteFromConf: called CC=5 SCAR=16.7
Pod documented
2861  
2862 # avoid a warning in logs when route is already defined
2863 208 ▶ my $getter = { "Auth" => "authRoutes", "Unauth" => "unAuthRoutes" }->{$type}
Condition50%1/2
{'Auth', 'authRoutes', 'Unauth', 'unAuthRoutes'}->{$type} || "${type}Routes"
l!l
2080
MC/DC0%0/1
{'Auth', 'authRoutes', 'Unauth', 'unAuthRoutes'}->{$type} || "${type}Routes"
{'Auth', 'authRoutes', 'Unauth', 'unAuthRoutes'}->{$type}
Truth table50%1/2
A
A= {'Auth', 'authRoutes', 'Unauth', 'unAuthRoutes'}->{$type}
Aresult
11
00
2864 || "${type}Routes";
2865  
2866 208 my $adder = "add${type}Route";
2867 208 foreach ( keys %subs ) {
2868 976 my $sub = $subs{$_};
2869 976 my $path = $self->conf->{$_};
2870 976 ▶ unless ($path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path) 0 976 976
2871 0 $self->logger->error("$_ parameter not defined");
2872 0 next;
2873 }
2874  
2875 # Avoid warning if loading modules twice
2876 976 ▶ next if $self->p->$getter->{GET}->{ $self->path }->{$path};
Branch: 1 branch
BranchTrueFalseTotal
if $self->p->$getter->{"GET"}{$self->path}{$path} 8 968 976
2877  
2878 968 $self->$adder(
2879 $self->path => { $path => $sub },
2880 [ 'GET', 'POST' ]
2881 );
2882 }
2883 }
2884  
2885 # Validate PKCE code challenge with given code challenge method
2886 # @param code_verifier
2887 # @param code_challenge
2888 # @param code_challenge_method
2889 # @return boolean 1 if challenge succeed, 0 else
2890 sub validatePKCEChallenge {
2891 11 ▶ my ( $self, $code_verifier, $code_challenge, $code_challenge_method ) = @_;
Subroutine validatePKCEChallenge: called CC=9 SCAR=40.3
Pod documented
2892  
2893 11 ▶ unless ($code_challenge) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code_challenge) 11 0 11
2894  
2895 # RFC 9700 section 2.1.1: Mitigate PKCE downgrade attacks
2896 # Reject code_verifier if no code_challenge was sent during authorization
2897 11 ▶ if ($code_verifier) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code_verifier) 0 11 11
2898 0 $self->userLogger->error(
2899 "PKCE downgrade attack: code_verifier provided "
2900 . "but no code_challenge was sent during authorization" );
2901 0 return 0;
2902 }
2903 11 $self->logger->debug("PKCE was not requested by the RP");
2904 11 return 1;
2905 }
2906  
2907 0 ▶ unless ($code_verifier) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code_verifier) 0 0 0
2908 0 $self->logger->error("PKCE required but no code verifier provided");
2909 0 return 0;
2910 }
2911  
2912 0 $self->logger->debug("PKCE code verifier received: $code_verifier");
2913  
2914 0 ▶ if ( !$code_challenge_method or $code_challenge_method eq "plain" ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $code_challenge_method or $code_challenge_method eq 'plain') { } 0 0 0
elsif ($code_challenge_method eq 'S256') { } 0 0 0
Condition0%0/3
not $code_challenge_method or $code_challenge_method eq "plain"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $code_challenge_method or $code_challenge_method eq "plain"
not $code_challenge_method $code_challenge_method eq "plain"
Truth table0%0/3
not(A) or B
A= not $code_challenge_methodB= $code_challenge_method eq "plain"
ABresult
1X1
011
000
2915 0 ▶ if ( $code_verifier eq $code_challenge ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code_verifier eq $code_challenge) { } 0 0 0
2916 0 $self->logger->debug("PKCE challenge validated (plain method)");
2917 0 return 1;
2918 }
2919 else {
2920 0 $self->logger->error("PKCE challenge failed (plain method)");
2921 0 return 0;
2922 }
2923 }
2924  
2925 elsif ( $code_challenge_method eq "S256" ) {
2926 0 my $code_verifier_hashed = encode_base64url( sha256($code_verifier) );
2927 0 ▶ if ( $code_verifier_hashed eq $code_challenge ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code_verifier_hashed eq $code_challenge) { } 0 0 0
2928 0 $self->logger->debug("PKCE challenge validated (S256 method)");
2929 0 return 1;
2930 }
2931 else {
2932 0 $self->logger->error("PKCE challenge failed (S256 method)");
2933 0 return 0;
2934 }
2935 }
2936  
2937 else {
2938 0 $self->logger->error("PKCE challenge method not valid");
2939 0 return 0;
2940 }
2941  
2942 0 return 0;
2943 }
2944  
2945 sub force_id_claims {
2946 14 ▶ my ( $self, $rp ) = @_;
Subroutine force_id_claims: called CC=1 SCAR=0.0
Pod undocumented
2947 14 return $self->rpOptions->{$rp}->{oidcRPMetaDataOptionsIDTokenForceClaims};
2948 }
2949  
2950 # https://openid.net/specs/openid-connect-core-1_0.html#IDToken
2951 # Audience(s) that this ID Token is intended for. It MUST contain the OAuth 2.0
2952 # client_id of the Relying Party as an audience value. It MAY also contain
2953 # identifiers for other audiences. In the general case, the aud value is an
2954 # array of case sensitive strings. In the common special case when there is one
2955 # audience, the aud value MAY be a single case sensitive string.
2956 sub getAudiences {
2957 34 ▶ my ( $self, $rp ) = @_;
Subroutine getAudiences: called CC=2 SCAR=6.9
Pod undocumented
2958  
2959 34 my $client_id = $self->rpOptions->{$rp}->{oidcRPMetaDataOptionsClientID};
2960 my @addAudiences = split /\s+/,
2961 ( $self->rpOptions->{$rp}->{oidcRPMetaDataOptionsAdditionalAudiences}
2962 34 ▶ || '' );
Condition100%2/2
$self->rpOptions->{$rp}{'oidcRPMetaDataOptionsAdditionalAudiences'} || ''
l!l
1024
MC/DC100%1/1
$self->rpOptions->{$rp}{'oidcRPMetaDataOptionsAdditionalAudiences'} || ''
$self->rpOptions->{$rp}{'oidcRPMetaDataOptionsAdditionalAudiences'}
Truth table100%2/2
A
A= $self->rpOptions->{$rp}{'oidcRPMetaDataOptionsAdditionalAudiences'}
Aresult
11
00
2963  
2964 34 my $result = [$client_id];
2965 34 push @{$result}, @addAudiences;
2966  
2967 34 return $result;
2968 }
2969  
2970 # Returns the main attribute (sub) to use for this RP
2971 # It can be a session attribute, or per-RP macro
2972 sub getUserIDForRP {
2973 45 ▶ my ( $self, $req, $rp, $data ) = @_;
Subroutine getUserIDForRP: called CC=4 SCAR=14.1
Pod undocumented
2974  
2975 # Use pre-computed sub if available (stored in online refresh tokens)
2976 45 ▶ return $data->{_oidc_sub} if $data->{_oidc_sub};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"_oidc_sub"} 0 45 45
2977  
2978 my $user_id_attribute =
2979 $self->rpOptions->{$rp}->{oidcRPMetaDataOptionsUserIDAttr}
2980 45 ▶ || $self->conf->{whatToTrace};
Condition66%2/3
$self->rpOptions->{$rp}{'oidcRPMetaDataOptionsUserIDAttr'} || $self->conf->{'whatToTrace'}
l!l&&r!l&&!r
4410
MC/DC0%0/2
$self->rpOptions->{$rp}{'oidcRPMetaDataOptionsUserIDAttr'} || $self->conf->{'whatToTrace'}
$self->rpOptions->{$rp}{'oidcRPMetaDataOptionsUserIDAttr'} $self->conf->{'whatToTrace'}
Truth table66%2/3
A || B
A= $self->rpOptions->{$rp}{'oidcRPMetaDataOptionsUserIDAttr'}B= $self->conf->{'whatToTrace'}
ABresult
1X1
011
000
2981  
2982 # If the main attribute is a SP macro, resolve it
2983 # else, get it directly from session data
2984 return $self->rpMacros->{$rp}->{$user_id_attribute}
2985 ? $self->rpMacros->{$rp}->{$user_id_attribute}->( $req, $data )
2986 45 ▶ : $data->{$user_id_attribute};
Branch: 1 branch
BranchTrueFalseTotal
$self->rpMacros->{$rp}{$user_id_attribute} ? : 4 41 45
2987 }
2988  
2989 # Return storage options
2990 sub _storeOpts {
2991 71 ▶ my ($self) = @_;
Subroutine _storeOpts: called CC=2 SCAR=7.1
2992 my $storage =
2993 $self->conf->{oidcStorage}
2994 ? {
2995 storageModule => $self->conf->{oidcStorage},
2996 storageModuleOptions => $self->conf->{oidcStorageOptions},
2997 }
2998 : {
2999 storageModule => $self->conf->{globalStorage},
3000 storageModuleOptions => $self->conf->{globalStorageOptions},
3001 71 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$self->conf->{'oidcStorage'} ? : 71 0 71
3002 71 return %$storage;
3003 }
3004  
3005 sub generateNonce {
3006 37 ▶ my ($self) = @_;
Subroutine generateNonce: called CC=1 SCAR=0.0
Pod undocumented
3007 37 return encode_base64url( Crypt::URandom::urandom(16) );
3008 }
3009  
3010 sub getSidFromSession {
3011 46 ▶ my ( $self, $rp, $sessionInfo ) = @_;
Subroutine getSidFromSession: called CC=2 SCAR=8.1
Pod undocumented
3012 return $sessionInfo->{_oidc_sid}
3013 || Digest::SHA::hmac_sha256_base64(
3014 46 ▶ $sessionInfo->{_session_id} . ':' . $rp );
Condition33%1/3
$sessionInfo->{'_oidc_sid'} || &Digest::SHA::hmac_sha256_base64($sessionInfo->{'_session_id'} . ':' . $rp)
l!l&&r!l&&!r
0460
MC/DC0%0/2
$sessionInfo->{'_oidc_sid'} || &Digest::SHA::hmac_sha256_base64($sessionInfo->{'_session_id'} . ':' . $rp)
$sessionInfo->{'_oidc_sid'} &Digest::SHA::hmac_sha256_base64($sessionInfo->{'_session_id'} . ':' . $rp)
Truth table33%1/3
A || B
A= $sessionInfo->{'_oidc_sid'}B= &Digest::SHA::hmac_sha256_base64($sessionInfo->{'_session_id'} . ':' . $rp)
ABresult
1X1
011
000
3015 }
3016  
3017 sub decryptJwt {
3018 0 ▶ my ( $self, $jwt ) = @_;
Subroutine decryptJwt: not called CC=6 SCAR=37.4
Pod undocumented
3019 0 my @count = split /\./, $jwt;
3020 0 ▶ if ( $#count == 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#count == 4) 0 0 0
3021 0 my $key = $self->conf->{oidcServicePrivateKeyEnc};
3022 0 $self->logger->debug("Receive an encrypted JWT: $jwt");
3023 0 ▶ unless ($key) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key) 0 0 0
3024 0 $self->logger->error('Receive an encrypted JWT but no key defined');
3025 0 return $jwt;
3026 }
3027  
3028 0 my $tmp;
3029 0 eval { $tmp = decode_jwt( token => $jwt, key => \$key, ); };
3030 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
3031 0 ▶ if ( $key = $self->conf->{oidcServiceOldPrivateKeyEnc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key = $self->conf->{"oidcServiceOldPrivateKeyEnc"}) 0 0 0
3032 0 eval { $tmp = decode_jwt( token => $jwt, key => \$key, ); };
3033 }
3034 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
3035 0 $self->logger->error( 'Unable to decrypt JWE: ' . $@ );
3036 0 return undef;
3037 }
3038 }
3039 0 $jwt = $tmp;
3040 0 $self->logger->debug("Decrypted JWT: $jwt");
3041 }
3042 0 return $jwt;
3043 }
3044  
3045 sub _getKeyType {
3046 42 ▶ my ( $self, $key ) = @_;
Subroutine _getKeyType: called CC=3 SCAR=11.0
3047  
3048 42 require Crypt::PK::RSA;
3049  
3050 42 my $rsa_pub = eval { Crypt::PK::RSA->new( \( $key->{public} ) ) };
3051 42 ▶ if ($rsa_pub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rsa_pub) 35 7 42
3052 35 return "RSA";
3053 }
3054  
3055 7 require Crypt::PK::ECC;
3056  
3057 7 my $ecc_pub = eval { Crypt::PK::ECC->new( \( $key->{public} ) ) };
3058 7 ▶ if ($ecc_pub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ecc_pub) 7 0 7
3059 7 return "EC";
3060 }
3061  
3062 0 return;
3063 }
3064  
3065 sub _getRequiredKeyTypeForAlg {
3066 10 ▶ my ( $self, $alg ) = @_;
Subroutine _getRequiredKeyTypeForAlg: called CC=3 SCAR=15.1
3067 10 ▶ if ( $alg =~ m/^(?:R|P)S/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($alg =~ /^(?:R|P)S/) { } 10 0 10
elsif ($alg =~ /^ES/) { } 0 0 0
3068 10 return "RSA";
3069 }
3070 elsif ( $alg =~ /^ES/ ) {
3071 0 return "EC";
3072 }
3073 0 return;
3074 }
3075  
3076 54 1;
3077  
3078 __END__
3079  
3080 =head1 NAME
3081  
3082 =encoding utf8
3083  
3084 Lemonldap::NG::Portal::Lib::OpenIDConnect - Common OpenIDConnect functions
3085  
3086 =head1 SYNOPSIS
3087  
3088 use Lemonldap::NG::Portal::Lib::OpenIDConnect;
3089  
3090 =head1 DESCRIPTION
3091  
3092 This module contains common methods for OpenIDConnect authentication
3093 and user information loading
3094  
3095 =head1 METHODS
3096  
3097 =head2 loadOPs
3098  
3099 Load OpenID Connect Providers and JWKS data
3100  
3101 =head2 loadRPs
3102  
3103 Load OpenID Connect Relying Parties
3104  
3105 =head2 refreshJWKSdata
3106  
3107 Refresh JWKS data if needed
3108  
3109 =head2 getRP
3110  
3111 Get Relying Party corresponding to a Client ID
3112  
3113 =head2 getCallbackUri
3114  
3115 Compute callback URI
3116  
3117 =head2 buildAuthorizationCodeAuthnRequest
3118  
3119 Build Authentication Request URI for Authorization Code Flow
3120  
3121 =head2 buildAuthorizationCodeAuthnResponse
3122  
3123 Build Authentication Response URI for Authorization Code Flow
3124  
3125 =head2 buildImplicitAuthnResponse
3126  
3127 Build Authentication Response URI for Implicit Flow
3128  
3129 =head2 buildHybridAuthnResponse
3130  
3131 Build Authentication Response URI for Hybrid Flow
3132  
3133 =head2 getAuthorizationCodeAccessToken
3134  
3135 Get Token response with authorization code
3136  
3137 =head2 checkTokenResponseValidity
3138  
3139 Check validity of Token Response
3140  
3141 =head2 getUserInfo
3142  
3143 Get UserInfo response
3144  
3145 =head2 decodeJSON
3146  
3147 Convert JSON to HashRef
3148  
3149 =head2 newAuthorizationCode
3150  
3151 Generate new Authorization Code session
3152  
3153 =head2 newAccessToken
3154  
3155 Generate new Access Token session
3156  
3157 =head2 newRefreshToken
3158  
3159 Generate new Refresh Token session
3160  
3161 =head2 getAuthorizationCode
3162  
3163 Get existing Authorization Code session
3164  
3165 =head2 getAccessToken
3166  
3167 Get existing Access Token session
3168  
3169 =head2 getRefreshToken
3170  
3171 Get existing Refresh Token session
3172  
3173 =head2 getOpenIDConnectSession
3174  
3175 Try to recover the OpenID Connect session corresponding to id and return session
3176  
3177 =head2 storeState
3178  
3179 Store information in state database and return
3180  
3181 =head2 extractState
3182  
3183 Extract state information into $self
3184  
3185 =head2 verifyJWTSignature
3186  
3187 Check signature of a JWT
3188  
3189 =head2 verifyHash
3190  
3191 Check value hash
3192  
3193 =head2 createHash
3194  
3195 Create Hash
3196  
3197 =head2 returnBearerError
3198  
3199 Return Bearer error
3200  
3201 =head2 getEndPointAuthenticationCredentials
3202  
3203 Get Client ID and Client Secret
3204  
3205 =head2 getEndPointAccessToken
3206  
3207 Get Access Token
3208  
3209 =head2 getAttributesListFromClaim
3210  
3211 Return list of attributes authorized for a claim
3212  
3213 =head2 buildUserInfoResponseFromId
3214  
3215 Return Hash of UserInfo data from session ID
3216  
3217 =head2 buildUserInfoResponse
3218  
3219 Return Hash of UserInfo data from session object
3220  
3221 =head2 createJWT
3222  
3223 Return JWT
3224  
3225 =head2 createIDToken
3226  
3227 Return ID Token
3228  
3229 =head2 getFlowType
3230  
3231 Return flow type
3232  
3233 =head2 getIDTokenSub
3234  
3235 Return sub field of an ID Token
3236  
3237 =head2 getJWTJSONData
3238  
3239 Return payload of a JWT as Hash ref
3240  
3241 =head2 key2jwks
3242  
3243 Return JWKS representation of a key
3244  
3245 =head2 buildLogoutRequest
3246  
3247 Build Logout Request URI
3248  
3249 =head2 buildLogoutResponse
3250  
3251 Build Logout Response URI
3252  
3253 =head2 addRouteFromConf
3254  
3255 Build a Lemonldap::NG::Common::PSGI::Router route from OIDC configuration
3256 attribute
3257  
3258 =head2 validatePKCEChallenge
3259  
3260 Validate PKCE code challenge with given code challenge method
3261  
3262 =head1 SEE ALSO
3263  
3264 L<Lemonldap::NG::Portal::AuthOpenIDConnect>, L<Lemonldap::NG::Portal::UserDBOpenIDConnect>
3265  
3266 =head1 AUTHORS
3267  
3268 =over
3269  
3270 =item LemonLDAP::NG team L<http://lemonldap-ng.org/team>
3271  
3272 =back
3273  
3274 =head1 BUG REPORT
3275  
3276 Use OW2 system to report bug or ask for features:
3277 L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
3278  
3279 =head1 DOWNLOAD
3280  
3281 Lemonldap::NG is available at
3282 L<https://lemonldap-ng.org/download>
3283  
3284 =head1 COPYRIGHT AND LICENSE
3285  
3286 See COPYING file for details.
3287  
3288 This library is free software; you can redistribute it and/or modify
3289 it under the terms of the GNU General Public License as published by
3290 the Free Software Foundation; either version 2, or (at your option)
3291 any later version.
3292  
3293 This program is distributed in the hope that it will be useful,
3294 but WITHOUT ANY WARRANTY; without even the implied warranty of
3295 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3296 GNU General Public License for more details.
3297  
3298 You should have received a copy of the GNU General Public License
3299 along with this program. If not, see L<http://www.gnu.org/licenses/>.
3300  
3301 =cut
« blib/lib/Lemonldap/NG/Portal/Lib/LDAP.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/CrowdSecAgent.pm »