blib/lib/Lemonldap/NG/Portal/Plugins/OIDC/PasswordGrant.pm

filter: Statementstmt 66.6% 58 / 87 Branchbran 40.0% 16 / 40 Conditioncond 31.5% 6 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 52.4% 86 / 164 CC 28 SCAR 45.9
CC
28
Cov
55%
_run
124.1
handlePasswordGrant
3.3
BEGIN
1.0
CRAP 98.2
« blib/lib/Lemonldap/NG/Portal/Lib/CrowdSec.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Auth/OpenIDConnect.pm »
1 package Lemonldap::NG::Portal::Plugins::OIDC::PasswordGrant;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Lemonldap::NG::Portal::Main::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
6 PE_OK
7 PE_SENDRESPONSE
8 PE_FIRSTACCESS
9 1 );
10  
11 1 our $VERSION = '2.23.1';
12  
13 1 extends 'Lemonldap::NG::Portal::Lib::OIDCPlugin';
14  
15 1 ▶use constant hook => { oidcGotTokenRequest => 'handlePasswordGrant' };
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 ▶sub handlePasswordGrant {
Subroutine handlePasswordGrant: called CC=3 SCAR=12.0
Pod undocumented
18 3 my ( $self, $req, $rp, $grant_type ) = @_;
19 3 ▶ return PE_OK unless $grant_type eq 'password';
Branch: 1 branch
BranchTrueFalseTotal
unless $grant_type eq "password" 2 1 3
20  
21 1 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->oidc->rpOptions->{$rp}{"oidcRPMetaDataOptionsAllowPasswordGrant"}) 0 1 1
22 $self->oidc->rpOptions->{$rp}->{oidcRPMetaDataOptionsAllowPasswordGrant}
23 )
24 {
25 0 $self->logger->warn(
26 "Access to grant_type=password, is not allowed for RP $rp");
27 0 $req->response(
28 $self->oidc->sendOIDCError( $req, 'unauthorized_client', 400 ) );
29 0 return PE_SENDRESPONSE;
30 }
31  
32 1 $req->response( $self->_run( $req, $rp ) );
33 1 return PE_SENDRESPONSE;
34 }
35  
36 1 ▶sub _run {
Subroutine _run: called CC=26 SCAR=48.2
37 1 my ( $self, $req, $rp ) = @_;
38 1 my $oidc = $self->oidc;
39  
40 1 my $client_id = $oidc->rpOptions->{$rp}->{oidcRPMetaDataOptionsClientID};
41 1 ▶ my $req_scope = $req->param('scope') || '';
Condition50%1/2
$req->param('scope') || ''
l!l
10
MC/DC0%0/1
$req->param('scope') || ''
$req->param('scope')
Truth table50%1/2
A
A= $req->param('scope')
Aresult
11
00
42 1 my $username = $req->param('username');
43 1 my $password = $req->param('password');
44  
45 1 ▶ unless ( $username and $password ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($username and $password) 0 1 1
Condition33%1/3
$username and $password
!ll&&!rl&&r
001
MC/DC0%0/2
$username and $password
$username $password
Truth table33%1/3
A and B
A= $usernameB= $password
ABresult
0X0
100
111
46 0 $self->logger->error("Missing username or password");
47 0 return $oidc->sendOIDCError( $req, 'invalid_request', 400 );
48 }
49  
50 ####
51 # Authenticate user by running through the regular login process
52 # minus the buildCookie step
53 1 $req->parameters->{user} = ($username);
54 1 $req->parameters->{password} = $password;
55 1 $req->data->{skipToken} = 1;
56  
57 # This makes Auth::Choice use authChoiceAuthBasic if defined
58 1 $req->data->{_pwdCheck} = 1;
59  
60 $req->steps( [
61 1 @{ $self->p->beforeAuth },
62 $self->p->authProcess,
63 1 @{ $self->p->betweenAuthAndData },
64 $self->p->sessionData,
65 1 @{ $self->p->afterData },
66 'storeHistory',
67 1 @{ $self->p->endAuth },
68 ]
69 );
70 1 my $result = $self->p->process($req);
71  
72 1 ▶ if ( ( $result == PE_FIRSTACCESS )
Branch: 1 branch
BranchTrueFalseTotal
if ($result == 9 and $self->conf->{"authentication"} eq "Choice") 0 1 1
Condition33%1/3
$result == 9 and $self->conf->{"authentication"} eq "Choice"
!ll&&!rl&&r
100
MC/DC0%0/2
$result == 9 and $self->conf->{"authentication"} eq "Choice"
$result == 9 $self->conf->{"authentication"} eq "Choice"
Truth table33%1/3
A and B
A= $result == 9B= $self->conf->{"authentication"} eq "Choice"
ABresult
0X0
100
111
73 and ( $self->conf->{authentication} eq "Choice" ) )
74 {
75 0 $self->logger->warn(
76 "Choice module did not know which module to choose. "
77 . "You should define authChoiceAuthBasic or specify desired module in the URL"
78 );
79 }
80  
81 1 ▶ $self->logger->debug( "Credentials check returned "
Branch: 1 branch
BranchTrueFalseTotal
if $result 0 1 1
82 . $self->p->_formatProcessResult($result) )
83 if $result;
84  
85 ## Make sure we returned successfuly from the process AND we were able to create a session
86 1 ▶ return $oidc->sendOIDCError( $req, 'invalid_grant', 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $result == 0 and $req->id and $req->user 0 1 1
Condition33%2/6
$result == 0 and $req->id and $req->user
!ll&&!rl&&r
001
$result == 0 and $req->id
!ll&&!rl&&r
001
MC/DC0%0/3
$result == 0 and $req->id and $req->user
$result == 0 $req->id $req->user
Truth table25%1/4
A and B and C
A= $result == 0B= $req->idC= $req->user
ABCresult
0XX0
10X0
1100
1111
87 unless ( $result == PE_OK and $req->id and $req->user );
88  
89 ## Make sure the current user is allowed to use this RP
90 1 ▶ if ( my $rule = $oidc->rpRules->{$rp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rule = $oidc->rpRules->{$rp}) 0 1 1
91 my $ruleVariables =
92 0 ▶ { %{ $req->sessionInfo || {} }, _oidc_grant_type => "password", };
Condition0%0/2
$req->sessionInfo || {}
l!l
00
MC/DC0%0/1
$req->sessionInfo || {}
$req->sessionInfo
Truth table0%0/2
A
A= $req->sessionInfo
Aresult
11
00
93 0 ▶ unless ( $rule->( $req, $ruleVariables ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$rule($req, $ruleVariables)) 0 0 0
94 $self->userLogger->warn( 'User '
95 . $req->sessionInfo->{ $self->conf->{whatToTrace} }
96 0 . " is not authorized to access $rp" );
97 0 $self->p->deleteSession($req);
98 0 return $oidc->sendOIDCError( $req, 'invalid_grant', 400 );
99 }
100 }
101  
102 1 my $user = $req->sessionInfo->{ $self->conf->{whatToTrace} };
103  
104 # Resolve scopes
105 1 my $scope = $oidc->getScope( $req, $rp, $req_scope );
106 1 ▶ unless ($scope) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($scope) 0 1 1
107 0 $self->userLogger->warn( "User $user was not granted"
108 . " any requested scopes ($req_scope) for $rp" );
109 0 return $oidc->sendOIDCError( $req, 'invalid_scope', 400 );
110 }
111  
112 1 my $user_id = $oidc->getUserIDForRP( $req, $rp, $req->sessionInfo );
113  
114 1 ▶ $self->logger->debug(
Branch: 1 branch
BranchTrueFalseTotal
$user_id ? : 1 0 1
115 $user_id
116 ? "Found corresponding user: $user_id"
117 : 'Corresponding user not found'
118 );
119  
120 # Generate access_token
121 1 my $access_token = $oidc->newAccessToken(
122 $req, $rp, $scope,
123 $req->sessionInfo,
124 {
125 grant_type => "password",
126 user_session_id => $req->id,
127 }
128 );
129  
130 1 ▶ unless ($access_token) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($access_token) 0 1 1
131 0 $self->userLogger->error("Unable to create Access Token");
132 0 return $oidc->sendOIDCError( $req, 'server_error', 500 );
133 }
134  
135 1 $self->logger->debug("Generated access token: $access_token");
136  
137 # Generate refresh_token
138 1 my $refresh_token = undef;
139  
140 1 ▶ if ( $oidc->rpOptions->{$rp}->{oidcRPMetaDataOptionsRefreshToken} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oidc->rpOptions->{$rp}{"oidcRPMetaDataOptionsRefreshToken"}) 0 1 1
141 my $refreshTokenSession = $oidc->_generateRefreshToken(
142 $req, $rp,
143 {
144 scope => $scope,
145 client_id => $client_id,
146 user_session_id => $req->id,
147 grant_type => "password",
148 0 $self->conf->{whatToTrace} => $user,
149 _oidc_logout_sub =>
150 $oidc->getUserIDForRP( $req, $rp, $req->userData ),
151 },
152 0,
153 );
154  
155 0 ▶ unless ($refreshTokenSession) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($refreshTokenSession) 0 0 0
156 0 $self->userLogger->error(
157 "Unable to create OIDC session for refresh_token");
158 0 return $oidc->sendOIDCError( $req,
159 'Could not create refresh token session', 500 );
160 }
161  
162 0 $refresh_token = $refreshTokenSession->id;
163  
164 0 $self->logger->debug("Generated refresh token: $refresh_token");
165 }
166  
167 # Generate ID token
168 1 my $id_token = undef;
169 1 ▶ if ( $oidc->_hasScope( "openid", $scope ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oidc->_hasScope("openid", $scope)) 0 1 1
170  
171 # Compute hash to store in at_hash
172 my $alg =
173 0 $oidc->rpOptions->{$rp}->{oidcRPMetaDataOptionsIDTokenSignAlg};
174 0 my ($hash_level) = ( $alg =~ /(?:\w{2})(\d{3})/ );
175 0 ▶ my $at_hash = $oidc->createHash( $access_token, $hash_level )
Branch: 1 branch
BranchTrueFalseTotal
if $hash_level 0 0 0
176 if $hash_level;
177  
178 0 ▶ $id_token =
Branch: 1 branch
BranchTrueFalseTotal
$at_hash ? : 0 0 0
179 $oidc->_generateIDToken( $req, $rp, $scope, $req->sessionInfo, 0,
180 { ( $at_hash ? ( at_hash => $at_hash ) : () ), } );
181  
182 0 ▶ unless ($id_token) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id_token) 0 0 0
183 0 $self->logger->error(
184 "Failed to generate ID Token for service: $client_id");
185 0 return $oidc->sendOIDCError( $req, 'server_error', 500 );
186 }
187 }
188  
189 # Send token response
190 my $expires_in =
191 $oidc->rpOptions->{$rp}->{oidcRPMetaDataOptionsAccessTokenExpiration}
192 1 ▶ || $self->conf->{oidcServiceAccessTokenExpiration};
Condition33%1/3
$oidc->rpOptions->{$rp}{'oidcRPMetaDataOptionsAccessTokenExpiration'} || $self->conf->{'oidcServiceAccessTokenExpiration'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$oidc->rpOptions->{$rp}{'oidcRPMetaDataOptionsAccessTokenExpiration'} || $self->conf->{'oidcServiceAccessTokenExpiration'}
$oidc->rpOptions->{$rp}{'oidcRPMetaDataOptionsAccessTokenExpiration'} $self->conf->{'oidcServiceAccessTokenExpiration'}
Truth table33%1/3
A || B
A= $oidc->rpOptions->{$rp}{'oidcRPMetaDataOptionsAccessTokenExpiration'}B= $self->conf->{'oidcServiceAccessTokenExpiration'}
ABresult
1X1
011
000
193  
194 1 ▶ my $token_response = {
Branch: 3 branches
BranchTrueFalseTotal
$scope ne $req_scope ? : 0 1 1
$refresh_token ? : 0 1 1
$id_token ? : 0 1 1
195 access_token => "$access_token",
196 token_type => 'Bearer',
197 expires_in => $expires_in + 0,
198 ( ( $scope ne $req_scope ) ? ( scope => "$scope" ) : () ),
199 ( $refresh_token ? ( refresh_token => "$refresh_token" ) : () ),
200 ( $id_token ? ( id_token => "$id_token" ) : () ),
201 };
202  
203 1 $self->logger->debug("Send token response");
204  
205 1 return $self->p->sendJSONresponse( $req, $token_response );
206 }
207  
208 package Lemonldap::NG::Portal::Plugins::OIDCPasswordGrant;
209  
210 1 our @ISA = ('Lemonldap::NG::Portal::Plugins::OIDC::PasswordGrant');
211  
212 1 1;
« blib/lib/Lemonldap/NG/Portal/Lib/CrowdSec.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Auth/OpenIDConnect.pm »