blib/lib/Lemonldap/NG/Portal/2F/Register/Generic.pm

filter: Statementstmt 37.3% 28 / 75 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 60.0% 6 / 10 Podpod 0.0% 0 / 3 totaltotal 28.8% 34 / 118 CC 14 SCAR 33.8
CC
14
Cov
27%
verify
56.0
sendcode
42.0
__ANON__
6.0
CRAP 29.3
« blib/lib/Lemonldap/NG/Portal/Lib/Radius.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/LocationDetect.pm »
1 # A generic 2FA module which registers a simple value into the psession
2 # This is meant to be used with sfExtra
3 package Lemonldap::NG::Portal::2F::Register::Generic;
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use JSON qw(from_json to_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Lemonldap::NG::Common::Crypto;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Lemonldap::NG::Portal::Main::Constants 'PE_OK';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '2.21.0';
12  
13 1 extends 'Lemonldap::NG::Portal::2F::Register::Base';
14 1 with 'Lemonldap::NG::Portal::Lib::2fDevices';
15  
16 # INITIALIZATION
17  
18 has ott => (
19 is => 'ro',
20 lazy => 1,
21 default => sub {
22 my $ott =
23 0 ▶ $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken');
Subroutine __ANON__: not called CC=2 SCAR=17.9
24 my $timeout = $_[0]->{conf}->{sfRegisterTimeout}
25 0 ▶ // $_[0]->{conf}->{formTimeout};
Condition0%0/3
$_[0]{"conf"}{"sfRegisterTimeout"} // $_[0]{"conf"}{"formTimeout"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"conf"}{"sfRegisterTimeout"} // $_[0]{"conf"}{"formTimeout"}
$_[0]{"conf"}{"sfRegisterTimeout"} $_[0]{"conf"}{"formTimeout"}
Truth table0%0/3
A // B
A= $_[0]{"conf"}{"sfRegisterTimeout"}B= $_[0]{"conf"}{"formTimeout"}
ABresult
1X1
011
000
26 0 $ott->timeout($timeout);
27 0 return $ott;
28 }
29 1 );
30  
31 # The verification module that we'll use
32 1 has verificationModule => (
33 is => 'rw',
34 isa => 'Lemonldap::NG::Portal::Lib::Code2F',
35 weak_ref => 1,
36 );
37  
38 # Overriden by sfExtra
39 1 has logo => ( is => 'rw', default => 'generic.png' );
40 1 has prefix => ( is => 'rw', default => 'generic' );
41 1 has template => ( is => 'ro', default => 'generic2fregister' );
42 1 has welcome => ( is => 'ro', default => 'generic2fwelcome' );
43  
44 1 use constant supportedActions => {
45 sendcode => "sendcode",
46 verify => "verify",
47 delete => "delete",
48 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 sub sendcode {
51 0 ▶ my ( $self, $req ) = @_;
Subroutine sendcode: not called CC=6 SCAR=37.4
Pod undocumented
52 0 my $user = $req->userData->{ $self->conf->{whatToTrace} };
53 0 my $generic = $req->param('generic');
54  
55 0 ▶ unless ($generic) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($generic) 0 0 0
56 0 return $self->failResponse( $req, 'PE79', 200 );
57 }
58  
59 0 ▶ return $self->failResponse( $req, 'csrfError', 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->checkCsrf($req) 0 0 0
60 unless $self->checkCsrf($req);
61  
62 # Validate format
63 0 ▶ unless ( $self->validateFormat($generic) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->validateFormat($generic)) 0 0 0
64 my $error_label = $self->conf->{generic2fFormatErrorLabel}
65 0 ▶ || 'generic2fFormatError';
Condition0%0/2
$self->conf->{'generic2fFormatErrorLabel'} || 'generic2fFormatError'
l!l
00
MC/DC0%0/1
$self->conf->{'generic2fFormatErrorLabel'} || 'generic2fFormatError'
$self->conf->{'generic2fFormatErrorLabel'}
Truth table0%0/2
A
A= $self->conf->{'generic2fFormatErrorLabel'}
Aresult
11
00
66 0 return $self->failResponse( $req, $error_label, 200 );
67 }
68  
69 # Generate and send code
70  
71 # Save current session info into a token
72 0 my $sessionInfo = { %{ $req->userData } };
73  
74 # Inject candidate value
75 0 $sessionInfo->{destination} = $generic;
76 0 my $token = $self->ott->createToken($sessionInfo);
77 0 my $result =
78 $self->verificationModule->challenge( $req, $sessionInfo, $token );
79 0 ▶ return $self->failResponse( $req, 'serverError' ) unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
80  
81 # Send response
82 0 $self->userLogger->notice(
83 $self->prefix . "2f: send verification code to $generic for $user" );
84 0 return $self->successResponse( $req, { result => 1, token => $token } );
85 }
86  
87 sub verify {
88  
89 0 ▶ my ( $self, $req ) = @_;
Subroutine verify: not called CC=7 SCAR=40.3
Pod undocumented
90  
91 0 ▶ return $self->failResponse( $req, 'csrfError', 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->checkCsrf($req) 0 0 0
92 unless $self->checkCsrf($req);
93  
94 0 my $user = $req->userData->{ $self->conf->{whatToTrace} };
95 0 my $generic = $req->param('generic');
96 0 my $tokenid = $req->param("token");
97 0 my $genericcode = $req->param('genericcode');
98 0 my $genericname =
99 $self->checkNameSfa( $req, $self->prefix, $req->param('genericname') );
100 0 ▶ return $self->failResponse( $req, 'badName', 200 ) unless $genericname;
Branch: 1 branch
BranchTrueFalseTotal
unless $genericname 0 0 0
101  
102 # Verify code
103 0 my $token = $self->ott->getToken( $tokenid, 1 );
104 0 my $res = $self->verificationModule->verify_supplied_code( $req, $token,
105 $genericcode );
106 0 ▶ return $self->failResponse( $req, "PE$res", 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $res == 0 0 0 0
107 unless ( $res == PE_OK );
108  
109 # Now generic is verified, let's store it in persistent data
110 # Reading existing 2FDevices
111 0 my @generic2f =
112 $self->find2fDevicesByType( $req, $req->userData, $self->prefix );
113  
114 # Delete previous generic if any
115 0 ▶ if (@generic2f) {
Branch: 1 branch
BranchTrueFalseTotal
if (@generic2f) 0 0 0
116 0 ▶ if ( $self->del2fDevices( $req, $req->userData, \@generic2f ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->del2fDevices($req, $req->userData, \@generic2f)) { } 0 0 0
117 0 $self->logger->debug( $self->prefix . "2f: old device(s) deleted" );
118 }
119 else {
120 0 $self->logger->error(
121 $self->prefix . "2f: unable to delete old device(s)" );
122 0 return $self->failResponse( $req, 'serverError' );
123 }
124 }
125  
126 # Add a new one
127 0 $res = $self->registerDevice(
128 $req,
129 $req->userData,
130 {
131 _generic => $generic,
132 name => $genericname,
133 type => $self->prefix,
134 epoch => time()
135 }
136 );
137 0 ▶ if ( $res == PE_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res == 0) { } 0 0 0
138 0 return $self->successResponse( $req, { result => 1 } );
139 }
140 else {
141 0 $self->logger->error( $self->prefix . "2f: unable to add device" );
142 0 return $self->failResponse( $req, "PE$res" );
143 }
144 }
145  
146 sub validateFormat {
147 0 ▶ my ( $self, $generic ) = @_;
Subroutine validateFormat: not called CC=2 SCAR=17.9
Pod undocumented
148 0 my $format_regex = $self->{conf}->{generic2fFormatRegex};
149 0 ▶ return 1 if ( !$format_regex );
Branch: 1 branch
BranchTrueFalseTotal
unless $format_regex 0 0 0
150 0 return scalar $generic =~ $format_regex;
151 }
152  
153 1 1;
154  
« blib/lib/Lemonldap/NG/Portal/Lib/Radius.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/LocationDetect.pm »