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

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