blib/lib/Lemonldap/NG/Portal/2F/Okta.pm

filter: Statementstmt 68.9% 129 / 187 Branchbran 46.1% 24 / 52 Conditioncond 25.0% 2 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 92.8% 13 / 14 Podpod 14.2% 1 / 7 totaltotal 61.9% 169 / 273 CC 31 SCAR 20.8
CC
31
Cov
63%
verify_external
30.0
_issueFactor
7.8
run
7.2
CRAP 8.0
« blib/lib/Lemonldap/NG/Portal/Register/LDAP.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Auth/SSL.pm »
1 package Lemonldap::NG::Portal::2F::Okta;
2  
3 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Lemonldap::NG::Common::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Lemonldap::NG::Portal::Main::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
9 PE_OK
10 PE_BADCREDENTIALS
11 PE_BADOTP
12 PE_ERROR
13 PE_NOTOKEN
14 PE_SENDRESPONSE
15 PE_TOKENEXPIRED
16 1 );
17  
18 1 our $VERSION = '2.23.0';
19  
20 1 extends 'Lemonldap::NG::Portal::Lib::Code2F';
21 1 with 'Lemonldap::NG::Portal::Lib::Okta';
22  
23 1 has prefix => ( is => 'ro', default => 'okta' );
24 1 has conf_type => ( is => 'ro', default => 'okta' );
25  
26 3 ▶sub init {
Subroutine init: called CC=4 SCAR=15.2
Pod undocumented
27 3 my ($self) = @_;
28  
29 # Check mandatory parameters
30 3 ▶ unless ( $self->conf->{okta2fAdminURL} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->conf->{"okta2fAdminURL"}) 0 3 3
31 0 $self->error("Okta 2F: Admin URL is required");
32 0 return 0;
33 }
34 3 ▶ unless ( $self->conf->{okta2fApiKey} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->conf->{"okta2fApiKey"}) 0 3 3
35 0 $self->error("Okta 2F: API key is required");
36 0 return 0;
37 }
38  
39 # Add Okta factor choice endpoint
40 3 ▶ unless ( $self->noRoute ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->noRoute) 3 0 3
41 3 $self->logger->debug( 'Adding ' . $self->prefix . '2fchoice route' );
42 3 $self->addUnauthRoute(
43 $self->prefix . '2fchoice' => '_issueFactor',
44 ['POST']
45 );
46 3 $self->addUnauthRoute(
47 $self->prefix . '2fchoice' => '_redirect',
48 ['GET']
49 );
50 3 $self->logger->debug( 'Adding ' . $self->prefix . '2fwait route' );
51 3 $self->addUnauthRoute(
52 $self->prefix . '2fwait' => '_waitingFactor',
53 ['POST']
54 );
55 3 $self->addUnauthRoute(
56 $self->prefix . '2fwait' => '_redirect',
57 ['GET']
58 );
59 }
60 3 return $self->SUPER::init();
61 }
62  
63 1 ▶sub run {
Subroutine run: called CC=5 SCAR=19.7
Pod documented
64 1 my ( $self, $req, $token ) = @_;
65  
66 1 ▶ my $okta_login_attribute = $self->conf->{'okta2fLoginAttribute'} || '_user';
Condition50%1/2
$self->conf->{'okta2fLoginAttribute'} || '_user'
l!l
10
MC/DC0%0/1
$self->conf->{'okta2fLoginAttribute'} || '_user'
$self->conf->{'okta2fLoginAttribute'}
Truth table50%1/2
A
A= $self->conf->{'okta2fLoginAttribute'}
Aresult
11
00
67 1 my $okta_login = $req->sessionInfo->{$okta_login_attribute};
68 1 my $okta_userid = $self->getOktaUserID($okta_login);
69 1 ▶ unless ($okta_userid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($okta_userid) 0 1 1
70 0 $self->logger->error( "No Okta user found for " . $okta_login );
71 0 return PE_ERROR;
72 }
73 1 $self->ott->updateToken( $token, __oktaUserId => $okta_userid );
74  
75 # Get available factors ( factorid, factortype )
76 1 my $okta_factors = $self->getOktaActiveFactors( $req, $okta_userid );
77  
78 1 ▶ unless ( keys %$okta_factors ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %$okta_factors) 0 1 1
79 0 $self->logger->error("No factor found for $okta_userid");
80 0 return PE_ERROR;
81 }
82  
83 1 $self->logger->debug("Found factors for $okta_userid");
84 1 $self->p->_dump($okta_factors);
85  
86 # Only one factor available, skip choice
87 1 ▶ if ( scalar keys %$okta_factors == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$okta_factors == 1) { } 0 1 1
88 0 my $factor_id = ( keys %$okta_factors )[0];
89 0 $self->logger->debug("Only one factor found ($factor_id), use it");
90  
91 # Redirect on 2Fchoice endpoint
92 0 my $redirect_uri = $self->p->portal . $self->prefix . "2fchoice";
93  
94 0 $self->logger->debug("Redirect user to $redirect_uri with POST");
95 0 $req->postUrl($redirect_uri);
96 0 $req->postFields( { token => $token, sf => $factor_id } );
97  
98 0 $req->steps( ['autoPost'] );
99 0 return PE_OK;
100 }
101  
102 # More than one factor has been found, display choice
103 else {
104 1 $self->logger->debug("Prepare Okta Factor choice");
105 my $res = $self->p->sendHtml(
106 $req,
107 'okta2fchoice',
108 params => {
109 TOKEN => $token,
110 TARGET => $self->p->relativeUrl( $req, 'okta2fchoice' ),
111 MODULES => [
112 1 map { { CODE => $_, LABEL => $okta_factors->{$_} } }
113 sort keys %$okta_factors
114 ],
115 ALERT => 'positive',
116 MSG => 'okta2fSelectFactor',
117 $self->get2fTplParams($req),
118 }
119 );
120 1 $req->response($res);
121 1 return PE_SENDRESPONSE;
122 }
123 }
124  
125 1 ▶sub verify {
Subroutine verify: called CC=6 SCAR=19.1
Pod undocumented
126 1 my ( $self, $req, $session ) = @_;
127  
128 1 ▶ if ( $req->param('code') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->param("code")) 0 1 1
129 0 $self->logger->debug("Okta 2F is a code, verify it with Code2F module");
130 0 return $self->SUPER::verify( $req, $session );
131 }
132  
133 1 $self->logger->debug("Okta 2F is not a code, wait for user interaction");
134 1 my $okta_userid = $session->{__oktaUserId};
135 1 my $okta_poll_url = $session->{__oktaPollUrl};
136  
137 1 my $okta_poll_result;
138 my $poll_response;
139 1 my $poll_content;
140 1 ▶ for ( my $i = 0 ; $i <= 60 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 60) 1 0 1
141  
142 1 $poll_response = $self->pollFactor($okta_poll_url);
143 1 ▶ last unless ($poll_response);
Branch: 1 branch
BranchTrueFalseTotal
unless $poll_response 0 1 1
144  
145 1 $self->logger->debug(
146 "Get poll response for $okta_userid:" . $poll_response );
147  
148 1 $poll_content = $self->decodeJSON($poll_response);
149  
150 1 $okta_poll_result = $poll_content->{factorResult};
151  
152 1 ▶ unless ( $okta_poll_result eq "WAITING" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($okta_poll_result eq "WAITING") 1 0 1
153 1 $self->logger->debug(
154 "Okta push response received for $okta_userid ");
155 1 last;
156 }
157  
158 0 sleep 1;
159 }
160  
161 1 ▶ if ( $okta_poll_result eq "SUCCESS" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($okta_poll_result eq 'SUCCESS') { } 1 0 1
162 1 $self->logger->debug("Okta push verified for $okta_userid");
163 1 return PE_OK;
164 }
165 else {
166 0 return PE_ERROR;
167 }
168 }
169  
170 0 ▶sub verify_external {
Subroutine verify_external: not called CC=5 SCAR=34.0
Pod undocumented
171 0 my ( $self, $req, $session, $code ) = @_;
172  
173 0 my $okta_userid = $session->{__oktaUserId};
174 0 my $selected_factor = $session->{__oktaFactorId};
175 0 ▶ unless ( $okta_userid && $selected_factor ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($okta_userid and $selected_factor) 0 0 0
Condition0%0/3
$okta_userid and $selected_factor
!ll&&!rl&&r
000
MC/DC0%0/2
$okta_userid and $selected_factor
$okta_userid $selected_factor
Truth table0%0/3
A and B
A= $okta_useridB= $selected_factor
ABresult
0X0
100
111
176 0 $self->logger->error("Missing Okta data (userid, factorid) in OTT");
177 0 return PE_ERROR;
178 }
179  
180 0 my $verify_factor_response =
181 $self->verifyFactor( $okta_userid, $selected_factor, $code );
182 0 ▶ return PE_ERROR unless ($verify_factor_response);
Branch: 1 branch
BranchTrueFalseTotal
unless $verify_factor_response 0 0 0
183  
184 0 $self->logger->debug(
185 "Verify 2FA code $code with factor $selected_factor for $okta_userid:"
186 . $verify_factor_response );
187 0 my $okta_verification = $self->decodeJSON($verify_factor_response);
188  
189 0 ▶ unless ( $okta_verification->{factorResult} eq "SUCCESS" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($okta_verification->{"factorResult"} eq "SUCCESS") 0 0 0
190 0 $self->logger->error(
191 "Verifiation failed for code $code for $okta_userid in Okta");
192 0 return PE_BADOTP;
193 }
194  
195 0 $self->logger->debug("2FA code verified in Okta for $okta_userid");
196  
197 0 return PE_OK;
198 }
199  
200 # Convert JSON to HashRef
201 # @return HashRef JSON decoded content
202 4 ▶sub decodeJSON {
Subroutine decodeJSON: called CC=3 SCAR=11.8
Pod undocumented
203 4 my ( $self, $json ) = @_;
204 4 my $json_hash;
205  
206 4 eval { $json_hash = from_json( $json, { allow_nonref => 1 } ); };
207 4 ▶ return undef if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
208 4 ▶ unless ( ref $json_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $json_hash) 0 4 4
209 0 $self->logger->error("Wanted a JSON object, got: $json_hash");
210 0 return undef;
211 }
212  
213 4 return $json_hash;
214 }
215  
216 # Get Okta user ID
217 # @return String Okta user ID
218 1 ▶sub getOktaUserID {
Subroutine getOktaUserID: called CC=4 SCAR=15.0
Pod undocumented
219 1 my ( $self, $okta_login ) = @_;
220 1 my $okta_userid;
221  
222 # Search user
223 1 $self->logger->debug("Search $okta_login in Okta");
224 1 my $search_profile_response = $self->searchProfile($okta_login);
225 1 ▶ return unless $search_profile_response;
Branch: 1 branch
BranchTrueFalseTotal
unless $search_profile_response 0 1 1
226  
227 1 my $okta_user = $self->decodeJSON($search_profile_response);
228 1 ▶ unless ( $okta_user and $okta_user->[0]->{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($okta_user and $okta_user->[0]{"id"}) 0 1 1
Condition33%1/3
$okta_user and $okta_user->[0]{"id"}
!ll&&!rl&&r
001
MC/DC0%0/2
$okta_user and $okta_user->[0]{"id"}
$okta_user $okta_user->[0]{"id"}
Truth table33%1/3
A and B
A= $okta_userB= $okta_user->[0]{"id"}
ABresult
0X0
100
111
229 0 $self->logger->error("User $okta_login not found in Okta");
230 0 return;
231 }
232  
233 1 $okta_userid = $okta_user->[0]->{id};
234  
235 1 $self->logger->debug("Found Okta ID $okta_userid for user $okta_login");
236  
237 1 return $okta_userid;
238 }
239  
240 # Get Okta active factors
241 # @return HashRef Okta factor list
242 1 ▶sub getOktaActiveFactors {
Subroutine getOktaActiveFactors: called CC=5 SCAR=17.1
Pod undocumented
243 1 my ( $self, $req, $okta_userid ) = @_;
244 1 my $okta_factors = {};
245  
246 # Search factors
247 1 my $search_factors_response = $self->searchFactors($okta_userid);
248 1 ▶ return unless $search_factors_response;
Branch: 1 branch
BranchTrueFalseTotal
unless $search_factors_response 0 1 1
249  
250 1 $self->logger->debug(
251 "Get factors for $okta_userid:" . $search_factors_response );
252  
253 1 my $okta_factor = $self->decodeJSON($search_factors_response);
254 1 ▶ unless ($okta_factor) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($okta_factor) 0 1 1
255 0 $self->logger->error("No factors for $okta_userid in Okta");
256 0 return $okta_factors;
257 }
258  
259 # Keep active factors
260 1 foreach (@$okta_factor) {
261  
262 3 my $factor_id = $_->{id};
263 3 my $factor_type = $_->{factorType};
264 3 my $factor_status = $_->{status};
265 3 ▶ if ( $factor_status ne "ACTIVE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($factor_status ne "ACTIVE") 0 3 3
266 0 $self->logger->debug(
267 "Factor $factor_id of type $factor_type for $okta_userid is not active, skipping it"
268 );
269 0 next;
270 }
271  
272 3 $factor_type =~ s/://g;
273 3 $okta_factors->{$factor_id} = $factor_type;
274 }
275  
276 1 return $okta_factors;
277 }
278  
279 2 ▶sub _issueFactor {
Subroutine _issueFactor: called CC=6 SCAR=20.5
280 2 my ( $self, $req ) = @_;
281  
282 2 my $token;
283 2 ▶ unless ( $token = $req->param('token') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($token = $req->param("token")) 1 1 2
284 1 $self->userLogger->error( $self->prefix . ' 2F access without token' );
285 1 eval { $self->setSecurity($req) };
286 1 $req->mustRedirect(1);
287 1 return $self->p->doPE( $req, PE_NOTOKEN );
288 }
289  
290 1 my $session;
291 1 ▶ unless ( $session = $self->ott->getToken( $token, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session = $self->ott->getToken($token, 1)) 0 1 1
292 0 $self->userLogger->info(
293 'Invalid token during ' . $self->prefix . '2f choice' );
294 0 $req->noLoginDisplay(1);
295 0 return $self->p->doPE( $req, PE_TOKENEXPIRED );
296 }
297  
298 1 my $okta_userid = $session->{__oktaUserId};
299 1 my $selected_factor = $req->param('sf');
300 1 $self->ott->updateToken( $token, __oktaFactorId => $selected_factor );
301  
302 1 my $issue_factor_response =
303 $self->issueFactor( $okta_userid, $selected_factor );
304 1 ▶ unless ($issue_factor_response) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($issue_factor_response) 0 1 1
305 0 eval { $self->setSecurity($req) };
306 0 $req->mustRedirect(1);
307 0 return $self->p->doPE( $req, PE_ERROR );
308 }
309  
310 $self->logger->debug(
311 1 "2FA sent to $okta_userid: " . $issue_factor_response );
312  
313 1 my $okta_issue = $self->decodeJSON($issue_factor_response);
314  
315 1 ▶ if ( $okta_issue->{factorResult} eq "CHALLENGE" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($okta_issue->{'factorResult'} eq 'CHALLENGE') { } 0 1 1
elsif ($okta_issue->{'factorResult'} eq 'WAITING') { } 1 0 1
316  
317 0 $self->logger->debug(
318 "2FA is a challenge, redirect $okta_userid to code form");
319  
320 0 my $tmp = $self->sendCodeForm( $req, TOKEN => $token );
321 0 $req->response($tmp);
322  
323 0 return $self->p->doPE( $req, PE_SENDRESPONSE );
324 }
325  
326 elsif ( $okta_issue->{factorResult} eq "WAITING" ) {
327  
328 1 $self->logger->debug(
329 "2FA is not a code, redirect $okta_userid to waiting screen");
330  
331 # Get poll URL
332 1 my $okta_poll_url = $okta_issue->{_links}->{poll}->{href};
333 1 $self->ott->updateToken( $token, __oktaPollUrl => $okta_poll_url );
334  
335 # Redirect on 2Fcheck endpoint
336 1 my $redirect_uri = $self->p->portal . $self->prefix . "2fcheck";
337  
338 1 $self->logger->debug("Redirect user to $redirect_uri with POST");
339 1 $req->postUrl($redirect_uri);
340 1 $req->postFields( { token => $token } );
341  
342 1 $req->data->{sfWait} = 1;
343 1 return $self->p->do( $req, ['autoPost'] );
344 }
345  
346 0 eval { $self->setSecurity($req) };
347 0 $req->mustRedirect(1);
348 0 return $self->p->doPE( $req, PE_ERROR );
349 }
350  
351 1 1;
« blib/lib/Lemonldap/NG/Portal/Register/LDAP.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Auth/SSL.pm »