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

filter: Statementstmt 38.1% 125 / 328 Branchbran 16.8% 26 / 154 Conditioncond 24.3% 10 / 41 MC/DCmcdc 12.0% 3 / 25 Subroutinesub 73.6% 14 / 19 Podpod 0.0% 0 / 11 totaltotal 30.8% 178 / 578 CC 99 SCAR 59.4
CC
99
Cov
31%
backLogout
812.0
extractFormInfo
523.0
frontLogout
61.4
CRAP 380.2
« blib/lib/Lemonldap/NG/Portal/Lib/OpenID/SREG.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/BasePasswordPolicy.pm »
1 package Lemonldap::NG::Portal::Auth::OpenIDConnect;
2  
3 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use MIME::Base64 qw/encode_base64 decode_base64/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use Scalar::Util qw/looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 40 ▶use Lemonldap::NG::Common::JWT qw(getJWTPayload);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 use Lemonldap::NG::Portal::Main::Constants qw(
9 PE_OK
10 PE_IDPCHOICE
11 PE_OIDC_AUTH_ERROR
12 PE_SENDRESPONSE
13 PE_REDIRECT
14 40 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 40 our $VERSION = '2.22.0';
17  
18 40 extends qw(
19 Lemonldap::NG::Portal::Main::Auth
20 Lemonldap::NG::Portal::Lib::OpenIDConnect
21 );
22  
23 40 with qw(
24 Lemonldap::NG::Portal::Lib::Key
25 );
26  
27 # INTERFACE
28  
29 41 ▶has opList => ( is => 'rw', isa => 'ArrayRef', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
30 40 has path => ( is => 'rw', default => 'oauth2' );
31 40 has endpointsRe => ( is => 'rw' );
32  
33 40 ▶use constant sessionKind => 'OIDC';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 # INITIALIZATION
36  
37 sub init {
38 40 ▶ my $self = shift;
Subroutine init: called CC=12 SCAR=25.9
Pod undocumented
39  
40 40 ▶ return 0 unless ( $self->loadOPs and $self->refreshJWKSdata );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->loadOPs and $self->refreshJWKSdata 0 40 40
Condition33%1/3
$self->loadOPs and $self->refreshJWKSdata
!ll&&!rl&&r
0040
MC/DC0%0/2
$self->loadOPs and $self->refreshJWKSdata
$self->loadOPs $self->refreshJWKSdata
Truth table33%1/3
A and B
A= $self->loadOPsB= $self->refreshJWKSdata
ABresult
0X0
100
111
41 40 my @tab = ( sort keys %{ $self->opMetadata } );
42 40 ▶ unless (@tab) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@tab) 0 40 40
43 0 $self->logger->error("No OP configured");
44 0 return 0;
45 }
46 40 my @list = ();
47  
48 40 foreach (@tab) {
49 42 my $name = $_;
50 $name = $self->opOptions->{$_}->{oidcOPMetaDataOptionsDisplayName}
51 42 ▶ if $self->opOptions->{$_}->{oidcOPMetaDataOptionsDisplayName};
Branch: 1 branch
BranchTrueFalseTotal
if $self->opOptions->{$_}{"oidcOPMetaDataOptionsDisplayName"} 1 41 42
52 42 my $icon = $self->opOptions->{$_}->{oidcOPMetaDataOptionsIcon};
53 my $tooltip =
54 42 ▶ $self->opOptions->{$_}->{oidcOPMetaDataOptionsTooltip} || $name;
Condition66%2/3
$self->opOptions->{$_}{'oidcOPMetaDataOptionsTooltip'} || $name
l!l&&r!l&&!r
1410
MC/DC0%0/2
$self->opOptions->{$_}{'oidcOPMetaDataOptionsTooltip'} || $name
$self->opOptions->{$_}{'oidcOPMetaDataOptionsTooltip'} $name
Truth table66%2/3
A || B
A= $self->opOptions->{$_}{'oidcOPMetaDataOptionsTooltip'}B= $name
ABresult
1X1
011
000
55 my $order = $self->opOptions->{$_}->{oidcOPMetaDataOptionsSortNumber}
56 42 ▶ // 999999;
Condition100%2/2
$self->opOptions->{$_}{"oidcOPMetaDataOptionsSortNumber"} // 999999
l!l
240
MC/DC100%1/1
$self->opOptions->{$_}{"oidcOPMetaDataOptionsSortNumber"} // 999999
$self->opOptions->{$_}{"oidcOPMetaDataOptionsSortNumber"}
Truth table100%2/2
A
A= $self->opOptions->{$_}{"oidcOPMetaDataOptionsSortNumber"}
Aresult
11
00
57 42 my $img_src;
58  
59 42 ▶ if ($icon) {
Branch: 1 branch
BranchTrueFalseTotal
if ($icon) 1 41 42
60 1 ▶ $img_src =
Branch: 1 branch
BranchTrueFalseTotal
$icon =~ m[^https?://] ? : 0 1 1
61 ( $icon =~ m#^https?://# )
62 ? $icon
63 : $self->p->staticPrefix . "/common/" . $icon;
64 }
65  
66 42 push @list,
67 {
68 val => $_,
69 name => $name,
70 title => $tooltip,
71 icon => $img_src,
72 order => $order
73 };
74 }
75  
76 my $re = '^/' . $self->path . '/(?:' . join(
77 '|',
78 map {
79 40 my $s = $self->conf->{$_};
80 80 $s =~ s/#PORTAL#\/*//;
81 80 $s
82 } ( qw(
83 oidcServiceMetaDataFrontChannelURI
84 oidcServiceMetaDataBackChannelURI
85 )
86 )
87 ) . ')(?:[\?/].*)?$';
88  
89 40 $self->endpointsRe(qr/$re/);
90  
91 40 $self->addRouteFromConf(
92 'Auth',
93 oidcServiceMetaDataFrontChannelURI => 'frontLogout',
94 oidcServiceMetaDataBackChannelURI => 'backLogout',
95 oidcServiceMetaDataJWKSURI => 'jwks',
96 );
97 40 $self->addRouteFromConf(
98 'Unauth',
99 oidcServiceMetaDataJWKSURI => 'jwks',
100 oidcServiceMetaDataRpLogoutReturnURI => 'rpLogoutReturn',
101 );
102  
103 @list =
104 sort {
105 40 $a->{order} <=> $b->{order}
106 or $a->{name} cmp $b->{name}
107 or $a->{val} cmp $b->{val}
108 2 ▶ } @list;
Branch: 1 branch
BranchTrueFalseTotal
$Lemonldap::NG::Portal::Auth::OpenIDConnect::a->{"order"} <=> $Lemonldap::NG::Portal::Auth::OpenIDConnect::b->{"order"} or $Lemonldap::NG::Portal::Auth::OpenIDConnect::a->{"name"} cmp $Lemonldap::NG::Portal::Auth::OpenIDConnect::b->{"name"} or $Lemonldap::NG::Portal::Auth::OpenIDConnect::a->{"val"} cmp $Lemonldap::NG::Portal::Auth::OpenIDConnect::b->{"val"} 0 2 2
Condition33%1/3
$Lemonldap::NG::Portal::Auth::OpenIDConnect::a->{"order"} <=> $Lemonldap::NG::Portal::Auth::OpenIDConnect::b->{"order"} or $Lemonldap::NG::Portal::Auth::OpenIDConnect::a->{"name"} cmp $Lemonldap::NG::Portal::Auth::OpenIDConnect::b->{"name"}
l!l&&r!l&&!r
200
MC/DC0%0/2
$Lemonldap::NG::Portal::Auth::OpenIDConnect::a->{"order"} <=> $Lemonldap::NG::Portal::Auth::OpenIDConnect::b->{"order"} or $Lemonldap::NG::Portal::Auth::OpenIDConnect::a->{"name"} cmp $Lemonldap::NG::Portal::Auth::OpenIDConnect::b->{"name"}
$Lemonldap::NG::Portal::Auth::OpenIDConnect::a->{"order"} <=> $Lemonldap::NG::Portal::Auth::OpenIDConnect::b->{"order"} $Lemonldap::NG::Portal::Auth::OpenIDConnect::a->{"name"} cmp $Lemonldap::NG::Portal::Auth::OpenIDConnect::b->{"name"}
Truth table33%1/3
A or B
A= $Lemonldap::NG::Portal::Auth::OpenIDConnect::a->{"order"} <=> $Lemonldap::NG::Portal::Auth::OpenIDConnect::b->{"order"}B= $Lemonldap::NG::Portal::Auth::OpenIDConnect::a->{"name"} cmp $Lemonldap::NG::Portal::Auth::OpenIDConnect::b->{"name"}
ABresult
1X1
011
000
109 40 $self->opList( [@list] );
110 40 return 1;
111 }
112  
113 # RUNNING METHODS
114  
115 sub extractFormInfo {
116 92 ▶ my ( $self, $req ) = @_;
Subroutine extractFormInfo: called CC=39 SCAR=62.6
Pod undocumented
117 92 ▶ if ( $req->uri =~ $self->endpointsRe ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->uri =~ $self->endpointsRe) 1 91 92
118 1 my $path = $req->path;
119 1 my $tmp = $self->path;
120 1 $path =~ s#^/*\Q$tmp\E/*##o;
121 1 $path =~ s#/.*$##;
122 1 ▶ if ( $path eq $self->conf->{oidcServiceMetaDataBackChannelURI} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($path eq $self->conf->{'oidcServiceMetaDataBackChannelURI'}) { } 0 1 1
elsif ($path eq $self->conf->{'oidcServiceMetaDataFrontChannelURI'}) { } 1 0 1
123 0 $req->response( $self->backLogout($req) );
124 0 return PE_SENDRESPONSE;
125 }
126 elsif ( $path eq $self->conf->{oidcServiceMetaDataFrontChannelURI} ) {
127 1 $req->response( $self->searchAndLogout($req) );
128 1 return PE_SENDRESPONSE;
129 }
130 }
131  
132 # Check callback
133 91 ▶ if ( $self->isCallback($req)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isCallback($req) and not $req->param("oidc_callback_processed")) 1 90 91
Condition100%3/3
$self->isCallback($req) and not $req->param("oidc_callback_processed")
!ll&&!rl&&r
8911
MC/DC100%2/2
$self->isCallback($req) and not $req->param("oidc_callback_processed")
$self->isCallback($req) not $req->param("oidc_callback_processed")
Truth table100%3/3
A and not(B)
A= $self->isCallback($req)B= not $req->param("oidc_callback_processed")
ABresult
0X0
100
111
134 and not $req->param('oidc_callback_processed') )
135 {
136 # This makes sure we don't go through the callback code when re-posting
137 # a login form
138 1 $self->p->setHiddenFormValue( $req, "oidc_callback_processed", "1", "",
139 0 );
140  
141 1 $self->logger->debug(
142 'OpenIDConnect callback URI detected: ' . $req->uri );
143  
144 # AuthN Response
145 1 my $state = $req->param('state');
146  
147 # Restore state
148 1 ▶ if ($state) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state) 0 1 1
149 0 ▶ if ( $self->extractState( $req, $state ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->extractState($req, $state)) { } 0 0 0
150 0 $self->logger->debug("State $state extracted");
151 }
152 else {
153 0 $self->userLogger->error("Unable to extract state $state");
154 0 return PE_OIDC_AUTH_ERROR;
155 }
156 }
157  
158 # Get OpenID Provider
159 1 my $op = $req->data->{_oidcOPCurrent};
160  
161 1 ▶ unless ($op) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($op) 1 0 1
162 1 $self->userLogger->error("OpenIDConnect Provider not found");
163 1 return PE_OIDC_AUTH_ERROR;
164 }
165  
166 0 $self->logger->debug("Using OpenIDConnect Provider $op");
167  
168 # Check error
169 0 my $error = $req->param("error");
170 0 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
171 0 my $error_description = $req->param("error_description");
172 0 my $error_uri = $req->param("error_uri");
173  
174 0 $self->logger->error("Error returned by $op Provider: $error");
175 0 ▶ $self->logger->error("Error description: $error_description")
Branch: 1 branch
BranchTrueFalseTotal
if $error_description 0 0 0
176 if $error_description;
177 0 ▶ $self->logger->error("Error URI: $error_uri") if $error_uri;
Branch: 1 branch
BranchTrueFalseTotal
if $error_uri 0 0 0
178  
179 0 return PE_OIDC_AUTH_ERROR;
180 }
181  
182 # Get access_token and id_token
183 0 my $code = $req->param("code");
184  
185 0 my $content =
186 $self->getAuthorizationCodeAccessToken( $req, $op, $code );
187 0 ▶ return PE_OIDC_AUTH_ERROR unless $content;
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 0 0
188  
189 0 my $token_response = $self->decodeTokenResponse($content);
190  
191 0 ▶ unless ($token_response) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($token_response) 0 0 0
192 0 $self->logger->error("Could not decode Token Response: $content");
193 0 return PE_OIDC_AUTH_ERROR;
194 }
195  
196 # Check validity of token response
197 0 ▶ unless ( $self->checkTokenResponseValidity($token_response) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->checkTokenResponseValidity($token_response)) { } 0 0 0
198 0 $self->logger->error("Token response is not valid");
199 0 return PE_OIDC_AUTH_ERROR;
200 }
201 else {
202 0 $self->logger->debug("Token response is valid");
203 }
204  
205 0 my $access_token = $token_response->{access_token};
206 0 my $expires_in = $token_response->{expires_in};
207 0 my $id_token = $self->decryptJwt( $token_response->{id_token} );
208 0 my $refresh_token = $token_response->{refresh_token};
209  
210 0 ▶ undef $expires_in unless looks_like_number($expires_in);
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($expires_in) 0 0 0
211  
212 0 $self->logger->debug("Access token: $access_token");
213 0 ▶ $self->logger->debug(
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
214 "Access token expires in: " . ( $expires_in || "<unknown>" ) );
215 0 $self->logger->debug("ID token: $id_token");
216 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
217 "Refresh token: " . ( $refresh_token || "<none>" ) );
218  
219 # Verify JWT signature
220 0 my $id_token_payload_hash;
221 0 ▶ if ( $self->opOptions->{$op}->{oidcOPMetaDataOptionsCheckJWTSignature} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opOptions->{$op}{'oidcOPMetaDataOptionsCheckJWTSignature'}) { } 0 0 0
222 {
223 0 ▶ unless ( $id_token_payload_hash =
Branch: 1 branch
BranchTrueFalseTotal
unless ($id_token_payload_hash = $self->decodeJWT($id_token, $op)) 0 0 0
224 $self->decodeJWT( $id_token, $op ) )
225 {
226 0 $self->logger->error("JWT signature verification failed");
227 0 $self->logger->error("Failing JWT is: $id_token");
228 0 return PE_OIDC_AUTH_ERROR;
229 }
230 0 $self->logger->debug("JWT signature verified");
231 }
232 else {
233 0 $self->logger->debug("JWT signature check disabled");
234 0 $id_token_payload_hash = getJWTPayload($id_token);
235 }
236  
237 0 ▶ unless ( defined $id_token_payload_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id_token_payload_hash) 0 0 0
238 0 $self->logger->error(
239 "Could not decode incoming ID token: $id_token");
240 0 return PE_OIDC_AUTH_ERROR;
241 }
242  
243 # Call oidcGotIDToken hook
244 0 my $h = $self->p->processHook( $req, 'oidcGotIDToken',
245 $op, $id_token_payload_hash, );
246 0 ▶ return PE_OIDC_AUTH_ERROR if ( $h != PE_OK );
Branch: 1 branch
BranchTrueFalseTotal
if $h != 0 0 0 0
247  
248 # Check validity of Access Token (optional)
249 0 my $at_hash = $id_token_payload_hash->{at_hash};
250 0 ▶ if ($at_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($at_hash) { } 0 0 0
251 0 ▶ unless ( $self->verifyHash( $access_token, $at_hash, $id_token ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->verifyHash($access_token, $at_hash, $id_token)) 0 0 0
252 0 $self->userLogger->error(
253 "Access token hash verification failed");
254 0 return PE_OIDC_AUTH_ERROR;
255 }
256 0 $self->logger->debug("Access token hash verified");
257 }
258 else {
259 0 $self->logger->debug(
260 "No at_hash in ID Token, access token will not be verified");
261 }
262  
263 # Check validity of ID Token
264 0 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->checkIDTokenValidity($op, $id_token_payload_hash, $req->data->{'_oidcNonce'})) { } 0 0 0
265 $self->checkIDTokenValidity(
266 $op, $id_token_payload_hash, $req->data->{_oidcNonce}
267 )
268 )
269 {
270 0 $self->userLogger->error('ID Token not valid');
271 0 return PE_OIDC_AUTH_ERROR;
272 }
273 else {
274 0 $self->logger->debug('ID Token is valid');
275 }
276  
277 # Get sub from ID token
278 0 my $id_token_sub = $id_token_payload_hash->{sub};
279 0 ▶ if ( !$id_token_sub ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id_token_sub) 0 0 0
280 0 $self->logger->error("Could not find sub claim in ID Token");
281 0 return PE_OIDC_AUTH_ERROR;
282 }
283 0 my $user_id = $id_token_sub;
284  
285 # Get user identifier, if different from sub
286 my $user_claim = $self->conf->{oidcOPMetaDataOptions}->{$op}
287 0 ->{oidcOPMetaDataOptionsUserAttribute};
288 0 ▶ if ($user_claim) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_claim) 0 0 0
289 0 $user_id = $id_token_payload_hash->{$user_claim};
290 0 ▶ if ( !$user_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user_id) 0 0 0
291 0 $self->logger->error(
292 "Could not find $user_claim claim in ID Token");
293 0 return PE_OIDC_AUTH_ERROR;
294 }
295 }
296  
297 # Remember ID Token sub
298 0 $req->data->{id_token_sub} = $id_token_sub;
299  
300 # Remember tokens
301 0 $req->data->{access_token} = $access_token;
302 0 ▶ $req->data->{refresh_token} = $refresh_token if $refresh_token;
Branch: 1 branch
BranchTrueFalseTotal
if $refresh_token 0 0 0
303 0 $req->data->{id_token} = $id_token;
304  
305 # Remember sid claim if given
306 $req->data->{op_sid} = $id_token_payload_hash->{sid}
307 0 ▶ if $id_token_payload_hash->{sid};
Branch: 1 branch
BranchTrueFalseTotal
if $id_token_payload_hash->{"sid"} 0 0 0
308  
309 # If access token TTL is given save expiration date
310 # (with security margin)
311 0 ▶ if ($expires_in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expires_in) 0 0 0
312 0 $req->data->{access_token_eol} = time + ( $expires_in * 0.9 );
313 }
314  
315 0 $self->logger->debug( "Found user_id: " . $user_id );
316 0 $req->user($user_id);
317  
318 0 return PE_OK;
319 }
320  
321 # No callback, choose Provider and send authn request
322 90 my $op;
323  
324 90 ▶ unless ( $op = $req->param("idp") ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($op = $req->param("idp")) 90 0 90
325 90 $self->logger->debug("Redirecting user to OP list");
326  
327 # Auto select provider if there is only one
328 90 ▶ if ( @{ $self->opList } == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->opList;} == 1) { } 89 1 90
329 89 $op = $self->opList->[0]->{val};
330 89 $self->logger->debug("Selecting the only defined OP: $op");
331 }
332  
333 else {
334  
335 # Try to use OP resolution rules
336 1 foreach ( keys %{ $self->opRules } ) {
337 0 ▶ my $cond = $self->opRules->{$_} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $cond = $self->opRules->{$_} or next 0 0 0
338 0 ▶ if ( $cond->( $req, $req->sessionInfo ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$cond($req, $req->sessionInfo)) 0 0 0
339 0 $self->logger->debug("OP $_ selected from resolution rule");
340 0 $op = $_;
341 0 last;
342 }
343 }
344  
345 1 ▶ unless ($op) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($op) 1 0 1
346  
347 # display OP list
348 1 $req->data->{list} = $self->opList;
349 1 $req->data->{login} = 1;
350 1 return PE_IDPCHOICE;
351 }
352 }
353 }
354  
355 # Provider is choosen
356 89 $self->logger->debug("OpenID Provider $op choosen");
357  
358 89 $req->data->{_oidcOPCurrent} = $op;
359  
360 # AuthN Request
361 89 $self->logger->debug("Build OpenIDConnect AuthN Request");
362  
363 89 my $nonce;
364 89 ▶ if ( $self->opOptions->{$op}->{oidcOPMetaDataOptionsUseNonce} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opOptions->{$op}{"oidcOPMetaDataOptionsUseNonce"}) 26 63 89
365 26 $nonce = $self->generateNonce();
366 26 $req->data->{_oidcNonce} = $nonce;
367 }
368  
369 # Save state
370 89 my $state = $self->storeState( $req,
371 qw/urldc checkLogins _oidcOPCurrent _oidcNonce/ );
372  
373 # Authorization Code Flow
374 89 my $authorization_request_uri =
375 $self->buildAuthorizationCodeAuthnRequest( $req, $op, $state, $nonce );
376 89 ▶ unless ($authorization_request_uri) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($authorization_request_uri) 88 1 89
377 88 return PE_OIDC_AUTH_ERROR;
378 }
379  
380 1 $req->urldc($authorization_request_uri);
381  
382 1 $self->logger->debug( "Redirect user to " . $req->{urldc} );
383 1 $req->continue(1);
384 1 $req->steps( [] );
385  
386 1 return PE_OK;
387 }
388  
389 sub isCallback {
390 91 ▶ my ( $self, $req ) = @_;
Subroutine isCallback: called CC=1 SCAR=0.0
Pod undocumented
391 91 return $req->param( $self->conf->{oidcRPCallbackGetParam} );
392 }
393  
394 sub authenticate {
395 0 ▶ return PE_OK;
Subroutine authenticate: not called CC=1 SCAR=6.9
Pod undocumented
396 }
397  
398 sub setAuthSessionInfo {
399 0 ▶ my ( $self, $req ) = @_;
Subroutine setAuthSessionInfo: not called CC=6 SCAR=37.4
Pod undocumented
400 0 my $op = $req->data->{_oidcOPCurrent};
401  
402 0 $req->{sessionInfo}->{authenticationLevel} = $self->conf->{oidcAuthnLevel};
403  
404 0 $req->{sessionInfo}->{_oidc_OP} = $op;
405 $req->{sessionInfo}->{_oidc_access_token} =
406 0 $req->data->{access_token};
407  
408 0 ▶ if ( $req->data->{refresh_token} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->data->{"refresh_token"}) 0 0 0
409 $req->{sessionInfo}->{_oidc_refresh_token} =
410 0 $req->data->{refresh_token};
411 }
412  
413 0 ▶ if ( $req->data->{access_token_eol} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->data->{"access_token_eol"}) 0 0 0
414 $req->{sessionInfo}->{_oidc_access_token_eol} =
415 0 $req->data->{access_token_eol};
416 }
417  
418 0 ▶ if ( $req->data->{op_sid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->data->{"op_sid"}) 0 0 0
419 0 $req->{sessionInfo}->{_oidc_sid} = $req->data->{op_sid};
420 }
421  
422 0 ▶ if ( $req->data->{id_token_sub} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->data->{"id_token_sub"}) 0 0 0
423 0 $req->{sessionInfo}->{_oidc_sub} = $req->data->{id_token_sub};
424 }
425  
426 # Keep ID Token in session
427 my $store_IDToken =
428 0 $self->opOptions->{$op}->{oidcOPMetaDataOptionsStoreIDToken};
429 0 ▶ if ($store_IDToken) {
Branch: 1 branch
BranchTrueFalseTotal
if ($store_IDToken) { } 0 0 0
430 0 $self->logger->debug("Store ID Token in session");
431 0 $req->{sessionInfo}->{_oidc_id_token} = $req->data->{id_token};
432 }
433 else {
434 0 $self->logger->debug("ID Token will not be stored in session");
435 }
436  
437 0 return PE_OK;
438 }
439  
440 sub authLogout {
441 0 ▶ my ( $self, $req ) = @_;
Subroutine authLogout: not called CC=5 SCAR=34.0
Pod undocumented
442  
443 0 ▶ return PE_OK if $req->data->{oidcSkipLogout};
Branch: 1 branch
BranchTrueFalseTotal
if $req->data->{"oidcSkipLogout"} 0 0 0
444  
445 0 my $op = $req->{sessionInfo}->{_oidc_OP};
446  
447 # Find endession endpoint
448 my $endsession_endpoint =
449 0 $self->opMetadata->{$op}->{conf}->{end_session_endpoint};
450  
451 0 my $client_id = $self->opOptions->{$op}->{oidcOPMetaDataOptionsClientID};
452  
453 0 ▶ if ($endsession_endpoint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($endsession_endpoint) { } 0 0 0
454 my $logout_url = $self->p->buildUrl( $req->portal, $self->path,
455 0 $self->conf->{oidcServiceMetaDataRpLogoutReturnURI} );
456  
457 0 my $state;
458 0 ▶ if ( my $next_url = ( $req->{issuerNextUrl} || $req->urldc ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next_url = $req->{"issuerNextUrl"} || $req->urldc) 0 0 0
Condition0%0/3
$req->{'issuerNextUrl'} || $req->urldc
l!l&&r!l&&!r
000
MC/DC0%0/2
$req->{'issuerNextUrl'} || $req->urldc
$req->{'issuerNextUrl'} $req->urldc
Truth table0%0/3
A || B
A= $req->{'issuerNextUrl'}B= $req->urldc
ABresult
1X1
011
000
459 0 $state = $self->state_ott->createToken( { next_url => $next_url } );
460 }
461  
462 $req->urldc(
463 $self->buildLogoutRequest(
464 $endsession_endpoint, $req->{sessionInfo}->{_oidc_id_token},
465 0 $logout_url, $state, $client_id,
466 )
467 );
468  
469 0 $self->logger->debug(
470 "OpenID Connect logout to $op will be done on " . $req->urldc );
471 }
472 else {
473 0 $self->logger->debug("No end session endpoint found for $op");
474 }
475 0 return PE_OK;
476 }
477  
478 sub rpLogoutReturn {
479 0 ▶ my ( $self, $req ) = @_;
Subroutine rpLogoutReturn: not called CC=3 SCAR=24.8
Pod undocumented
480  
481 0 my $url = $self->p->buildUrl( $req->portal, { logout => 1 } );
482  
483 0 ▶ if ( my $state = $req->param('state') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $state = $req->param("state")) 0 0 0
484  
485 0 $state = $self->state_ott->getToken($state);
486 0 ▶ if ($state) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state) { } 0 0 0
487 0 $url = $state->{next_url};
488 }
489 else {
490 0 $self->logger->warn("Post logout state is no longer valid");
491 }
492 }
493  
494 0 $req->urldc($url);
495 0 return $self->p->do( $req, [ [ returnPE => PE_REDIRECT ] ] );
496 }
497  
498 sub getDisplayType {
499 87 ▶ return "logo";
Subroutine getDisplayType: called CC=1 SCAR=0.0
Pod undocumented
500 }
501  
502 sub searchAndLogout {
503 1 ▶ my ( $self, $req ) = @_;
Subroutine searchAndLogout: called CC=1 SCAR=0.0
Pod undocumented
504 1 $self->userLogger->info(
505 'Front-channel logout request called without cookie');
506 1 $req->data->{frontLogoutSearch} = 1;
507 1 return $self->frontLogout($req);
508 }
509  
510 sub frontLogout {
511 1 ▶ my ( $self, $req ) = @_;
Subroutine frontLogout: called CC=12 SCAR=41.2
Pod undocumented
512  
513 # No double disconnect
514 1 $req->data->{oidcSkipLogout} = 1;
515  
516 # Response should be displayed in an iframe
517 1 $req->frame(1);
518  
519 # TODO check iss and sid
520 1 my $iss = $req->param('iss');
521 1 my $sid = $req->param('sid');
522 1 my $badRequest = 0;
523 1 my $logName = 'OIDC FC Logout';
524  
525 1 my $img = $self->conf->{staticPrefix} . '/common/icons/ok.png';
526 1 my $frame = qq'<html><body><img src="$img"></body></html>';
527 1 my $response = [
528 200,
529 [
530 'Content-Type' => 'text/html',
531 'Content-Length' => length($frame)
532 ],
533 [$frame]
534 ];
535  
536 # LLNG requires sid and iss
537 1 ▶ unless ( $iss and $sid ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($iss and $sid) { } 0 1 1
if (do { $req->data->{'frontLogoutSearch'} }) { } 0 0 0
Condition33%1/3
$iss and $sid
!ll&&!rl&&r
100
MC/DC0%0/2
$iss and $sid
$iss $sid
Truth table33%1/3
A and B
A= $issB= $sid
ABresult
0X0
100
111
538 1 $self->userLogger->error(
539 'OIDC Front-Channel-Logout called without sid and iss');
540 1 $badRequest++;
541 }
542 elsif ( $req->data->{frontLogoutSearch} ) {
543 0 my %store = %{ $self->conf->{globalStorageOptions} };
544 0 $store{backend} = $self->conf->{globalStorage};
545 0 my $oidcSessions =
546 Lemonldap::NG::Common::Apache::Session->searchOn( \%store,
547 '_oidc_sid', $sid );
548 0 my @sessionToDelete = keys %$oidcSessions;
549 0 foreach (@sessionToDelete) {
550  
551 # searchOn() returns sessions indexed by their storage ID, then
552 # it is required to set hashStore to 0
553 0 ▶ if ( my $as = $self->p->getApacheSession( $_, hashStore => 0, ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $as = $self->p->getApacheSession($_, "hashStore", 0)) 0 0 0
554 0 ▶ if ( $self->p->_deleteSession( $req, $as, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->p->_deleteSession($req, $as, 1)) { } 0 0 0
555 0 $self->logger->debug(
556 "$logName: session $_ deleted from global storage");
557 }
558 else {
559 0 $self->logger->error(
560 "$logName: unable to delete session $_");
561 0 $self->logger->error( $as->error );
562 }
563 }
564 }
565 0 return $response;
566 }
567 else {
568 0 ▶ unless ( $req->userData->{_oidc_sid} and $req->userData->{_oidc_OP} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->userData->{'_oidc_sid'} and $req->userData->{'_oidc_OP'}) { } 0 0 0
Condition0%0/3
$req->userData->{"_oidc_sid"} and $req->userData->{"_oidc_OP"}
!ll&&!rl&&r
000
MC/DC0%0/2
$req->userData->{"_oidc_sid"} and $req->userData->{"_oidc_OP"}
$req->userData->{"_oidc_sid"} $req->userData->{"_oidc_OP"}
Truth table0%0/3
A and B
A= $req->userData->{"_oidc_sid"}B= $req->userData->{"_oidc_OP"}
ABresult
0X0
100
111
569 0 $self->logger->error(
570 "$logName: key _oidc_sid not stored in session");
571 0 $badRequest++;
572 }
573 else {
574 0 ▶ if ( $sid ne $req->userData->{_oidc_sid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sid ne $req->userData->{"_oidc_sid"}) 0 0 0
575 0 $self->userLogger->error(
576 'OIDC Front-Channel-Logout: sid mismatch');
577 0 $badRequest++;
578 }
579 }
580 }
581 1 ▶ if ($badRequest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($badRequest) 1 0 1
582 1 return $self->p->sendError( $req, 'Bad OIDC Logout Request', 200 );
583 }
584  
585 # Ignore beforeLogout errors
586 0 $req->steps( $self->p->beforeLogout );
587 0 my $res = $self->p->process($req);
588  
589 0 $req->data->{_continueLogout} = 1;
590 0 $req->response($response);
591 0 ▶ return $self->p->do(
Branch: 1 branch
BranchTrueFalseTotal
$res ? : 0 0 0
592 $req,
593 [
594 'authLogout', 'deleteSession',
595 [ returnPE => ( $res ? $res : PE_SENDRESPONSE ) ]
596 ]
597 );
598 }
599  
600 # Implements https://openid.net/specs/openid-connect-backchannel-1_0.html
601 sub backLogout {
602 0 ▶ my ( $self, $req ) = @_;
Subroutine backLogout: not called CC=28 SCAR=67.0
Pod undocumented
603 0 my $logName = 'OIDC BC Logout';
604  
605 # REQUIRED CHECKS
606  
607 # Back channel requires POST
608 0 ▶ return $self->p->sendError( $req, 'Only POST allowed', 400 )
Branch: 1 branch
BranchTrueFalseTotal
if $req->method ne "POST" 0 0 0
609 if $req->method ne 'POST';
610  
611 # Back channel requires logout_token
612 0 my $content = $req->body_parameters;
613 return $self->p->sendError( $req, 'logout_token not found', 400 )
614 0 ▶ unless $content and $content->{logout_token};
Branch: 1 branch
BranchTrueFalseTotal
unless $content and $content->{"logout_token"} 0 0 0
Condition0%0/3
$content and $content->{"logout_token"}
!ll&&!rl&&r
000
MC/DC0%0/2
$content and $content->{"logout_token"}
$content $content->{"logout_token"}
Truth table0%0/3
A and B
A= $contentB= $content->{"logout_token"}
ABresult
0X0
100
111
615  
616 # Decode token
617 0 my $logoutToken = $self->decryptJwt( $content->{logout_token} );
618 0 ▶ my $payload = getJWTPayload($logoutToken)
Branch: 1 branch
BranchTrueFalseTotal
my $payload = getJWTPayload($logoutToken) or return $self->p->sendError($req, "Could not decode logout token", 400) 0 0 0
619 or
620 return $self->p->sendError( $req, 'Could not decode logout token', 400 );
621  
622 # TODO: validate "alg" header => prohibit "none" value
623  
624 # Required fields
625 0 foreach (qw(iss aud iat jti events)) {
626 return $self->p->sendError( $req, "Missing $_", 400 )
627 0 ▶ unless $payload->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $payload->{$_} 0 0 0
628 }
629 return $self->p->sendError( $req, 'Missing sub/sid', 400 )
630 0 ▶ unless $payload->{sub} or $payload->{sid};
Branch: 1 branch
BranchTrueFalseTotal
unless $payload->{"sub"} or $payload->{"sid"} 0 0 0
Condition0%0/3
$payload->{"sub"} or $payload->{"sid"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$payload->{"sub"} or $payload->{"sid"}
$payload->{"sub"} $payload->{"sid"}
Truth table0%0/3
A or B
A= $payload->{"sub"}B= $payload->{"sid"}
ABresult
1X1
011
000
631  
632 # Get iss and found related OP
633 0 my $op;
634 0 foreach ( keys %{ $self->opMetadata } ) {
635 0 ▶ if ( $self->opMetadata->{$_}->{conf}->{issuer} eq $payload->{iss} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opMetadata->{$_}{"conf"}{"issuer"} eq $payload->{"iss"}) 0 0 0
636 0 $op = $_;
637 0 last;
638 }
639 }
640 0 ▶ return $self->p->sendError( $req, 'Issuer not found', 400 ) unless $op;
Branch: 1 branch
BranchTrueFalseTotal
unless $op 0 0 0
641  
642 # Verify signature
643 0 ▶ return $self->p->sendError( $req, 'Bad signature', 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->decodeJWT($logoutToken, $op) 0 0 0
644 unless $self->decodeJWT( $logoutToken, $op );
645  
646 # Verify audience
647 return $self->p->error( $req, 'Bad aud', 400 )
648 0 ▶ unless ref $payload->{aud} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $payload->{"aud"} eq "ARRAY" 0 0 0
649 return $self->p->error( $req, 'Client ID not found in audience array', 400 )
650 unless
651 0 grep { $_ eq $self->opOptions->{$op}->{oidcOPMetaDataOptionsClientID} }
652 0 ▶ @{ $payload->{aud} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $self->opOptions->{$op}{"oidcOPMetaDataOptionsClientID"};} @{$$payload{"aud"};} 0 0 0
653  
654 # Verify iat
655 return $self->p->error( $req, 'Token too old', 400 )
656 unless $payload->{iat} +
657 0 ▶ ( $self->opOptions->{$op}->{oidcOPMetaDataOptionsIDTokenMaxAge} // 30 ) >
Branch: 1 branch
BranchTrueFalseTotal
unless $payload->{"iat"} + ($self->opOptions->{$op}{"oidcOPMetaDataOptionsIDTokenMaxAge"} // 30) > &CORE::GLOBAL::time() 0 0 0
Condition0%0/2
$self->opOptions->{$op}{"oidcOPMetaDataOptionsIDTokenMaxAge"} // 30
l!l
00
MC/DC0%0/1
$self->opOptions->{$op}{"oidcOPMetaDataOptionsIDTokenMaxAge"} // 30
$self->opOptions->{$op}{"oidcOPMetaDataOptionsIDTokenMaxAge"}
Truth table0%0/2
A
A= $self->opOptions->{$op}{"oidcOPMetaDataOptionsIDTokenMaxAge"}
Aresult
11
00
658 time;
659  
660 # Verify events
661 return $self->p->error( $req, 'Bad events', 400 )
662 unless ref $payload->{events} eq 'HASH'
663 and $payload->{events}->{ $self->BACKCHANNEL_EVENTSKEY() }
664 0 ▶ and ref $payload->{events}->{ $self->BACKCHANNEL_EVENTSKEY() } eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $payload->{"events"} eq "HASH" and $payload->{"events"}{$self->BACKCHANNEL_EVENTSKEY} and ref $payload->{"events"}{$self->BACKCHANNEL_EVENTSKEY} eq "HASH" 0 0 0
Condition0%0/6
ref $payload->{"events"} eq "HASH" and $payload->{"events"}{$self->BACKCHANNEL_EVENTSKEY} and ref $payload->{"events"}{$self->BACKCHANNEL_EVENTSKEY} eq "HASH"
!ll&&!rl&&r
000
ref $payload->{"events"} eq "HASH" and $payload->{"events"}{$self->BACKCHANNEL_EVENTSKEY}
!ll&&!rl&&r
000
MC/DC0%0/3
ref $payload->{"events"} eq "HASH" and $payload->{"events"}{$self->BACKCHANNEL_EVENTSKEY} and ref $payload->{"events"}{$self->BACKCHANNEL_EVENTSKEY} eq "HASH"
ref $payload->{"events"} eq "HASH" $payload->{"events"}{$self->BACKCHANNEL_EVENTSKEY} ref $payload->{"events"}{$self->BACKCHANNEL_EVENTSKEY} eq "HASH"
Truth table0%0/4
A and B and C
A= ref $payload->{"events"} eq "HASH"B= $payload->{"events"}{$self->BACKCHANNEL_EVENTSKEY}C= ref $payload->{"events"}{$self->BACKCHANNEL_EVENTSKEY} eq "HASH"
ABCresult
0XX0
10X0
1100
1111
665  
666 # Verify nonce
667 0 ▶ return $self->p->error( $req, 'Bad request', 400 ) if $payload->{nonce};
Branch: 1 branch
BranchTrueFalseTotal
if $payload->{"nonce"} 0 0 0
668  
669 # Now request is valid, even if session isn't found, response will be 200
670  
671 # Find session
672 0 my $uid = $payload->{sub};
673 0 my $sid = $payload->{sid};
674 0 my %store = %{ $self->conf->{globalStorageOptions} };
675 0 $store{backend} = $self->conf->{globalStorage};
676 0 my @userSessions;
677 0 ▶ if ($sid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sid) { } 0 0 0
678 0 my $oidcSessions =
679 Lemonldap::NG::Common::Apache::Session->searchOn( \%store,
680 '_oidc_sid', $sid );
681 0 @userSessions = keys %$oidcSessions;
682 }
683 else {
684 0 my $oidcSessions =
685 Lemonldap::NG::Common::Apache::Session->searchOn( \%store,
686 '_oidc_sub', $uid );
687 @userSessions =
688 grep {
689 0 $oidcSessions->{$_}->{_oidc_OP}
690 0 ▶ and $oidcSessions->{$_}->{_oidc_OP} eq $op;
Branch: 1 branch
BranchTrueFalseTotal
$oidcSessions->{$_}{"_oidc_OP"} and $oidcSessions->{$_}{"_oidc_OP"} eq $op 0 0 0
691 } keys %$oidcSessions;
692 }
693 0 ▶ if (@userSessions) {
Branch: 1 branch
BranchTrueFalseTotal
if (@userSessions) { } 0 0 0
694 0 ▶ if ($#userSessions) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#userSessions) 0 0 0
695 0 $self->logger->warn(
696 "$logName: more than one link found between $uid and $op, cleanong all"
697 );
698 }
699 0 foreach (@userSessions) {
700  
701 # searchOn() returns sessions indexed by their storage ID, then
702 # it is required to set hashStore to 0
703 0 ▶ if ( my $as = $self->p->getApacheSession( $_, hashStore => 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $as = $self->p->getApacheSession($_, 'hashStore', 0)) { } 0 0 0
704 0 ▶ if ( $self->p->_deleteSession( $req, $as, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->p->_deleteSession($req, $as, 1)) { } 0 0 0
705 0 $self->logger->debug(
706 "$logName: session $_ deleted from global storage");
707 }
708 else {
709 0 $self->logger->error(
710 "$logName: unable to delete session $_");
711 0 $self->logger->error( $as->error );
712 }
713 }
714 else {
715 0 $self->logger->info(
716 "$logName: session $_ for user $uid already deleted");
717 }
718 }
719 }
720 else {
721 0 $self->logger->warn("$logName: no session found for $uid");
722 }
723  
724 0 return [ 200, [], [] ];
725 }
726  
727 40 1;
« blib/lib/Lemonldap/NG/Portal/Lib/OpenID/SREG.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/BasePasswordPolicy.pm »