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

filter: Statementstmt 24.6% 17 / 69 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 71.4% 5 / 7 Podpod 0.0% 0 / 2 totaltotal 18.1% 22 / 121 CC 18 SCAR 55.5
CC
18
Cov
16%
tokenExchange
342.0
__ANON__
2.0
BEGIN
1.0
CRAP 256.8
« blib/lib/Lemonldap/NG/Portal/Plugins/MailPasswordReset.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/DecryptValue.pm »
1 package Lemonldap::NG::Portal::Lib::OIDCTokenExchange;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 use Lemonldap::NG::Portal::Main::Constants qw(
5 PE_SENDRESPONSE
6 PE_OK
7 1 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 extends 'Lemonldap::NG::Portal::Main::Plugin';
11  
12 1 our $VERSION = '2.20.0';
13  
14 # INTERFACE
15 1 ▶use constant hook => { oidcGotTokenExchange => 'tokenExchange' };
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 has oidc => (
18 is => 'ro',
19 lazy => 1,
20 default => sub {
21 $_[0]
22 0 ▶ ->p->loadedModules->{'Lemonldap::NG::Portal::Issuer::OpenIDConnect'};
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 }
24 1 );
25  
26 2 ▶sub init { 1 }
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
27  
28 # MAIN METHOD
29 #
30 # Important note: only PE_OK and PE_RESPONSE are used here because:
31 # - oidcGotTokenExchange only accept PE_SENDRESPONSE has positive response
32 # - PE_OK permits to continue to check other plugins
33 sub tokenExchange {
34 0 ▶ my ( $self, $req, $rp ) = @_;
Subroutine tokenExchange: not called CC=18 SCAR=58.3
Pod undocumented
35  
36 0 ▶ unless ( $self->oidc ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->oidc) 0 0 0
37 0 $self->logger->error('Authentication is not OIDC, aborting');
38 0 return PE_OK;
39 }
40  
41 0 $self->logger->debug("Matrix Refresh Token exchange for $rp");
42  
43 # 1. Validate request
44 #
45 # Note that client_id and grant_type value are verified by
46 # ::/lib::OpenIDConnect
47  
48 0 my $clientId = $req->param('client_id'); # already validated
49 0 my $subjectToken = $req->param('subject_token');
50 0 ▶ unless ($subjectToken) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subjectToken) 0 0 0
51 0 $self->userLogger->error('Token exchange called without subject_token');
52 0 return PE_OK;
53 }
54  
55 # Even if these fields are required in Oauth2, it seems that it is not
56 # required by Keycloak..
57  
58 0 my @types;
59 0 foreach my $name (qw(subject_token_type requested_token_type)) {
60 0 my $val = $req->param($name);
61 0 ▶ if ($val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val) 0 0 0
62 0 ▶ unless ($val =~ s/^urn:ietf:params:oauth:token-type://
Branch: 1 branch
BranchTrueFalseTotal
unless ($val =~ s/^urn:ietf:params:oauth:token-type:// and $val =~ /^(?:(?:refresh|access|id)_token|saml[12])$/) 0 0 0
Condition0%0/3
$val =~ s/^urn:ietf:params:oauth:token-type:// and $val =~ /^(?:(?:refresh|access|id)_token|saml[12])$/
!ll&&!rl&&r
000
MC/DC0%0/2
$val =~ s/^urn:ietf:params:oauth:token-type:// and $val =~ /^(?:(?:refresh|access|id)_token|saml[12])$/
$val =~ s/^urn:ietf:params:oauth:token-type:// $val =~ /^(?:(?:refresh|access|id)_token|saml[12])$/
Truth table0%0/3
A and B
A= $val =~ s/^urn:ietf:params:oauth:token-type://B= $val =~ /^(?:(?:refresh|access|id)_token|saml[12])$/
ABresult
0X0
100
111
63 and $val =~ /^(?:(?:refresh|access|id)_token|saml[12])$/ )
64 {
65 0 $self->logger->info(
66 "Malformed $name: " . $req->param('subject_token_type') );
67 0 return PE_OK;
68 }
69 }
70 0 push @types, $val;
71 }
72  
73 # 2. Check for audience and authorization
74 0 my $targetClientId = $req->param('audience');
75 0 ▶ my $target = { audience => $req->param('audience') // undef, };
Condition0%0/2
$req->param("audience") // undef
l!l
00
MC/DC0%0/1
$req->param("audience") // undef
$req->param("audience")
Truth table0%0/2
A
A= $req->param("audience")
Aresult
11
00
76 0 ▶ if ( $target->{audience} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target->{'audience'}) { } 0 0 0
77 0 ▶ if ( $target->{audience} eq $clientId ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target->{'audience'} eq $clientId) { } 0 0 0
78 0 $target->{rp} = $rp;
79 }
80 else {
81 my ($res) = grep {
82 $self->oidc->rpOptions->{$_}->{oidcRPMetaDataOptionsClientID}
83 0 eq $targetClientId
84 0 } keys %{ $self->oidc->rpOptions };
85 0 $target->{rp} = $res;
86 }
87 }
88 else {
89 0 $target->{rp} = $rp;
90 }
91  
92 0 ▶ unless ( $self->validateAudience( $req, $rp, $target, $types[1] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->validateAudience($req, $rp, $target, $types[1])) 0 0 0
93 0 $self->logger->debug( ref($self) . " refused to validate audience" );
94 0 return PE_OK;
95 }
96  
97 # 3. Use subclass getUid() to get user ID
98 0 my $uid = $self->getUid( $req, $rp, $subjectToken, $types[0] );
99 0 ▶ return PE_OK unless $uid;
Branch: 1 branch
BranchTrueFalseTotal
unless $uid 0 0 0
100  
101 # 4. Create refresh_token
102  
103 0 my $opts = { $self->oidc->_storeOpts() };
104  
105 # 3.1. Create a new refresh_token if missing
106 0 $req->user($uid);
107 0 $req->steps( [
108 'getUser', 'setAuthSessionInfo',
109 'setSessionInfo', $self->p->groupsAndMacros,
110 'setLocalGroups'
111 ]
112 );
113 0 ▶ if ( my $error = $self->p->process($req) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $self->p->process($req)) 0 0 0
114 0 ▶ if ( $error == PE_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error == 0) 0 0 0
115 $self->userLogger->warn(
116 'Token exchange requested for an invalid user ('
117 . $req->{user}
118 0 . ")" );
119 }
120 0 $self->logger->error("Unable to find user $uid");
121 0 return PE_OK;
122 }
123  
124 my $refreshToken = $self->oidc->newRefreshToken(
125 $rp,
126 {
127 0 ▶ %{ $req->sessionInfo },
Condition0%0/5
$req->param('scope') || 'openid'
l!l
00
$target->{'audience'} || $self->oidc->rpOptions->{$rp}{'oidcRPMetaDataOptionsClientID'}
l!l&&r!l&&!r
000
MC/DC0%0/1
$req->param('scope') || 'openid'
$req->param('scope')
MC/DC0%0/2
$target->{'audience'} || $self->oidc->rpOptions->{$rp}{'oidcRPMetaDataOptionsClientID'}
$target->{'audience'} $self->oidc->rpOptions->{$rp}{'oidcRPMetaDataOptionsClientID'}
Truth table0%0/2
A
A= $req->param('scope')
Aresult
11
00
Truth table0%0/3
A || B
A= $target->{'audience'}B= $self->oidc->rpOptions->{$rp}{'oidcRPMetaDataOptionsClientID'}
ABresult
1X1
011
000
128 scope => $req->param('scope') || 'openid',
129 client_id => $target->{audience}
130 || $self->oidc->rpOptions->{$rp}->{oidcRPMetaDataOptionsClientID},
131 0 _session_uid => $uid,
132 grant_type => $req->param('grant_type'),
133 },
134 );
135 0 ▶ unless ($refreshToken) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($refreshToken) 0 0 0
136 0 return PE_OK;
137 }
138  
139 # 5. Use normal method to create new access_token from refresh_token
140 #
141 # TODO: remote may want a refresh_token
142  
143 0 ▶ my $res = $self->oidc->_handleRefreshTokenGrant( $req, $target->{rp} || $rp,
Condition0%0/3
$target->{'rp'} || $rp
l!l&&r!l&&!r
000
MC/DC0%0/2
$target->{'rp'} || $rp
$target->{'rp'} $rp
Truth table0%0/3
A || B
A= $target->{'rp'}B= $rp
ABresult
1X1
011
000
144 $refreshToken->id, $targetClientId );
145  
146 # Insert refresh_token in response
147 0 my $tmp = JSON::from_json( $res->[2]->[0] );
148 0 $tmp->{refresh_token} = $refreshToken->id;
149 0 $res->[2]->[0] = JSON::to_json($tmp);
150  
151 0 $req->response($res);
152  
153 0 return PE_SENDRESPONSE;
154 }
155  
156 1 1;
157 __END__
158  
159 =pod
160  
161 =encoding utf8
162  
163 =head1 NAME
164  
165 Lemonldap::NG::Portal::Lib::OIDCTokenExchange - Base class for building OpenID
166 Connect token exchange systems.
167  
168 =head1 SYNOPSIS
169  
170 use Mouse
171 extends 'Lemonldap::NG::Portal::Lib::OIDCTokenExchange';
172
173 sub validateAudience {
174 my ( $self, $req, $rp, $target, $requestedTokenType ) = @_;
175 #
176 # verify and update if needed:
177 # * $target->{audience}
178 # * $target->{rp}
179 #
180 return 1;
181 }
182
183 sub getUid {
184 my ( $self, $req, $rp, $subjectToken, $subjectTokenType ) = @_;
185 #
186 # verify subjectToken
187 #
188 return 1;
189 }
190  
191 =head1 DESCRIPTION
192  
193 When L<Lemonldap::NG|https://lemonldap-ng.org> detects a
194 L<Oauth2 token exchange|https://datatracker.ietf.org/doc/html/rfc8693> request,
195 it searches for a plugin able to respond. If no one returns a valid response,
196 it rejects the requests.
197  
198 B<Lemonldap::NG::Portal::Lib::OIDCTokenExchange> permits one to build such
199 plugin by just writing two methods. Of course you need then to load the module
200 for example using L<Enabling custom plugin|https://lemonldap-ng.org/documentation/latest/plugincustom.html#enabling-your-plugin>.
201  
202 =head2 Methods to write
203  
204 =head3 validateAudience
205  
206 The goal of C<validateAudience()> is to validate the requested B<audience>.
207  
208 If a non-null value is returned, then the request is accepted and Lemonldap::NG
209 will build new C<access_token>, C<id_token> and C<refresh_token> using the
210 values included into C<$target> hash.
211  
212 If a null value is returned, Lemonldap::NG will try the next plugin.
213  
214 Parameters:
215  
216 =over
217  
218 =item * B<$req>, the L<Lemonldap::NG::Portal::Main::Request> object
219  
220 =item * B<$rp>, the internal LLNG name of the Relying Party which pushed the request
221  
222 =item * B<$target>, a hash value with 2 keys:
223  
224 =over
225  
226 =item * B<audience>, the requested audience
227  
228 =item * B<rp>: if B<Lemonldap::NG> found a known Relying Party which Client-ID
229 matches with requested audience, its name is put here, else this value is
230 undefined.
231  
232 =back
233  
234 This value can be modified inside C<validateAudience> and will be used to generate
235 the new C<access_token>.
236  
237 =item * B<$requestedTokenType>, the type of the requested token. This value is always
238 one of:
239  
240 =over
241  
242 =item * B<access_token>
243  
244 =item * B<refresh_token>
245  
246 =item * B<id_token>
247  
248 =item * B<saml1>
249  
250 =item * B<saml2>
251  
252 =item * I<undef>
253  
254 =back
255  
256 =back
257  
258 =head3 getUid
259  
260 C<getUid()> is a boolean method to validate the token given in the request.
261  
262 If a non-null value is returned, then the request is accepted. Else Lemonldap::NG
263 will try the next plugin.
264  
265 Parameters:
266  
267 =over
268  
269 =item * B<$req>, the L<Lemonldap::NG::Portal::Main::Request> object
270  
271 =item * B<$rp>, the internal LLNG name of the Relying Party which pushed the request
272  
273 =item * B<$subjectToken>, the token given in the request
274  
275 =item * B<$subjectTokenType>, the type of the given token. This value is always
276 one of:
277  
278 =over
279  
280 =item * B<access_token>
281  
282 =item * B<refresh_token>
283  
284 =item * B<id_token>
285  
286 =item * B<saml1>
287  
288 =item * B<saml2>
289  
290 =item * I<undef>
291  
292 =back
293  
294 =back
295  
296 =head1 AUTHORS
297  
298 =over
299  
300 =item * LemonLDAP::NG team L<http://lemonldap-ng.org/team>
301  
302 =back
303  
304 =head1 BUG REPORT
305  
306 Use OW2 system to report bug or ask for features:
307 L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
308  
309 =head1 DOWNLOAD
310  
311 Lemonldap::NG is available at
312 L<https://lemonldap-ng.org/download>
313  
314 =head1 COPYRIGHT AND LICENSE
315  
316 See COPYING file for details.
317  
318 This library is free software; you can redistribute it and/or modify
319 it under the terms of the GNU General Public License as published by
320 the Free Software Foundation; either version 2, or (at your option)
321 any later version.
322  
323 This program is distributed in the hope that it will be useful,
324 but WITHOUT ANY WARRANTY; without even the implied warranty of
325 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
326 GNU General Public License for more details.
327  
328 You should have received a copy of the GNU General Public License
329 along with this program. If not, see L<http://www.gnu.org/licenses/>.
330  
331 =cut
« blib/lib/Lemonldap/NG/Portal/Plugins/MailPasswordReset.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/DecryptValue.pm »