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

filter: Statementstmt 33.3% 24 / 72 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 62.5% 5 / 8 Podpod 0.0% 0 / 2 totaltotal 26.6% 29 / 109 CC 13 SCAR 40.5
CC
13
Cov
25%
verify
110.0
getkey
12.0
__ANON__
6.0
CRAP 57.6
« blib/lib/Lemonldap/NG/Portal/UserDB/LDAP.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/Notifications.pm »
1 # Self TOTP registration
2 package Lemonldap::NG::Portal::2F::Register::TOTP;
3  
4 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 ▶use Lemonldap::NG::Portal::Main::Constants ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 24 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 24 ▶use JSON qw(from_json to_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 24 our $VERSION = '2.23.0';
10  
11 24 extends qw(
12 Lemonldap::NG::Portal::2F::Register::Base
13 Lemonldap::NG::Common::TOTP
14 );
15 24 with 'Lemonldap::NG::Portal::Lib::2fDevices';
16  
17 # INITIALIZATION
18  
19 24 has logo => ( is => 'rw', default => 'totp.png' );
20 24 has prefix => ( is => 'rw', default => 'totp' );
21 24 has template => ( is => 'ro', default => 'totp2fregister' );
22 24 has welcome => ( is => 'ro', default => 'yourNewTotpKey' );
23 has ott => (
24 is => 'rw',
25 lazy => 1,
26 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
27 my $ott =
28 0 $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken');
29 my $timeout = $_[0]->{conf}->{sfRegisterTimeout}
30 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
31 0 $ott->timeout($timeout);
32 0 return $ott;
33 }
34 24 );
35  
36 24 ▶use constant supportedActions => {
Subroutine BEGIN: called CC=1 SCAR=0.0
37 delete => "delete",
38 modify => "modify",
39 verify => "verify",
40 getkey => "getkey",
41 24 };
42  
43 0 ▶sub verify {
Subroutine verify: not called CC=10 SCAR=47.0
Pod undocumented
44 0 my ( $self, $req ) = @_;
45 0 my $user = $req->userData->{ $self->conf->{whatToTrace} };
46  
47 0 ▶ return $self->failResponse( $req, 'csrfError', 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->checkCsrf($req) 0 0 0
48 unless $self->checkCsrf($req);
49  
50 # Get form token
51 0 my $token = $req->param('token');
52 0 ▶ unless ($token) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($token) 0 0 0
53 0 return $self->failResponse( $req, 'noTOTPFound', 400 );
54 }
55  
56 # Verify that token exists in DB (note that "keep" flag is set to
57 # permit more than 1 try during token life
58 0 ▶ unless ( $token = $self->ott->getToken( $token, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($token = $self->ott->getToken($token, 1)) 0 0 0
59 0 return $self->failResponse( $req, PE_TOKENEXPIRED, 400 );
60 }
61  
62 # Now check TOTP code to verify that user has a valid TOTP app
63 0 my $code = $req->param('code');
64 0 ▶ unless ($code) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code) 0 0 0
65 0 return $self->failResponse( $req, 'missingCode', 400 );
66 }
67  
68 0 my $TOTPName =
69 $self->checkNameSfa( $req, $self->type, $req->param('TOTPName') );
70 0 ▶ return $self->failResponse( $req, 'badName', 200 ) unless $TOTPName;
Branch: 1 branch
BranchTrueFalseTotal
unless $TOTPName 0 0 0
71  
72 my $verify_result = $self->verify_totp(
73 interval => $self->conf->{totp2fInterval},
74 digits => $self->conf->{totp2fDigits},
75 hash => $self->conf->{totp2fAlgorithm},
76 range_tolerance => $self->conf->{totp2fRange},
77 stored_secret => $token->{_totp2fSecret},
78 0 code => $code,
79 );
80  
81 0 my $r = $verify_result->{result};
82  
83 0 ▶ if ( $r == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r == -1) 0 0 0
84 $self->logger->error(
85 0 "TOTP registration failed: " . $verify_result->{error} );
86 0 return $self->failResponse( $req, 'serverError' );
87 }
88  
89 # Invalid try is returned with a 200 code. Javascript will read error
90 # and propose to retry
91 0 ▶ if ( $r == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r == 0) 0 0 0
92 0 return $self->failResponse( $req, 'badCode', 200 );
93 }
94  
95 0 my $range = $verify_result->{offset};
96  
97 0 $self->userLogger->info("Codes match at range $range");
98 0 $self->logger->debug( $self->prefix . '2f: code verified' );
99  
100 0 my $storable_secret = $self->get_storable_secret( $token->{_totp2fSecret} );
101 0 ▶ unless ($storable_secret) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($storable_secret) 0 0 0
102 0 $self->logger->error( $self->prefix . '2f: unable to encrypt secret' );
103 0 return $self->failResponse( $req, "serverError" );
104 }
105  
106 # Store TOTP secret
107 0 my $res = $self->registerDevice(
108 $req,
109 $req->userData,
110 {
111 _secret => $storable_secret,
112 type => $self->type,
113 name => $TOTPName,
114 epoch => time()
115 }
116 );
117 0 ▶ if ( $res == PE_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res == 0) { } 0 0 0
118 0 return $self->successResponse( $req, { result => 1 } );
119 }
120 else {
121 0 $self->logger->error( $self->prefix . "2f: unable to add device" );
122 0 return $self->failResponse( $req, $res );
123 }
124 }
125  
126 0 ▶sub getkey {
Subroutine getkey: not called CC=3 SCAR=24.8
Pod undocumented
127 0 my ( $self, $req ) = @_;
128 0 my $user = $req->userData->{ $self->conf->{whatToTrace} };
129 0 my ( $nk, $secret, $issuer ) = ( 0, '' );
130  
131 0 ▶ return $self->failResponse( $req, 'csrfError', 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->checkCsrf($req) 0 0 0
132 unless $self->checkCsrf($req);
133  
134 0 $secret = $self->newSecret;
135 0 $self->logger->debug( $self->prefix . "2f: generate new secret ($secret)" );
136 0 $nk = 1;
137  
138 # Secret is stored in a token: we choose to not accept secret returned
139 # by Ajax request to avoid some attacks
140 0 my $token = $self->ott->createToken( {
141 _totp2fSecret => $secret,
142 }
143 );
144 0 ▶ unless ( $issuer = $self->conf->{totp2fIssuer} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($issuer = $self->conf->{"totp2fIssuer"}) 0 0 0
145 0 $issuer = $req->portal;
146 0 $issuer =~ s#^https?://([^/:]+).*$#$1#;
147 }
148  
149 # QR-code will be generated by a javascript, here we just send data
150 return $self->successResponse(
151 $req,
152 {
153 secret => $secret,
154 token => $token,
155 portal => $issuer,
156 user => $user,
157 newkey => $nk,
158 digits => $self->conf->{totp2fDigits},
159 interval => $self->conf->{totp2fInterval},
160 algorithm => $self->conf->{totp2fAlgorithm}
161 }
162 0 );
163 }
164  
165 24 1;
« blib/lib/Lemonldap/NG/Portal/UserDB/LDAP.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/Notifications.pm »