blib/lib/Lemonldap/NG/Common/TOTP.pm

filter: Statementstmt 89.1% 74 / 83 Branchbran 75.0% 15 / 20 Conditioncond 42.8% 6 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 83.3% 15 / 18 Podpod 0.0% 0 / 7 totaltotal 73.3% 110 / 150 CC 18 SCAR 17.1
CC
18
Cov
81%
verify_totp
12.9
__ANON__
6.0
_code
3.1
CRAP 5.5
« blib/lib/Lemonldap/NG/Common/Conf/Backends/RDBI.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Conf/Wrapper.pm »
1 package Lemonldap::NG::Common::TOTP;
2  
3 # This module is inspired by Auth::GoogleAuth written by Gryphon Shafer
4 # <gryphon@cpan.org>
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Convert::Base32 qw(decode_base32 encode_base32);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Crypt::URandom;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Lemonldap::NG::Common::Crypto;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 my $hash_func = {
14 "sha1" => \&Digest::SHA::hmac_sha1_hex,
15 "sha256" => \&Digest::SHA::hmac_sha256_hex,
16 "sha512" => \&Digest::SHA::hmac_sha512_hex,
17 };
18  
19 1 our $VERSION = '2.23.0';
20  
21 has key => (
22 is => 'ro',
23 lazy => 1,
24 default => sub {
25 0 ▶ my ($self) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
26 0 ▶ return $self->conf->{totp2fKey} || $self->conf->{key};
Condition0%0/3
$self->conf->{'totp2fKey'} || $self->conf->{'key'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->conf->{'totp2fKey'} || $self->conf->{'key'}
$self->conf->{'totp2fKey'} $self->conf->{'key'}
Truth table0%0/3
A || B
A= $self->conf->{'totp2fKey'}B= $self->conf->{'key'}
ABresult
1X1
011
000
27 }
28 1 );
29  
30 has encryptSecret => (
31 is => 'ro',
32 lazy => 1,
33 default => sub {
34 0 ▶ my ($self) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 0 return $self->conf->{totp2fEncryptSecret};
36 }
37 1 );
38  
39 has crypto => (
40 is => 'ro',
41 lazy => 1,
42 default => sub {
43 4 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
44 4 Lemonldap::NG::Common::Crypto->new( $self->key );
45 }
46 1 );
47  
48 1 ▶use constant PREFIX => "{llngcrypt}";
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 sub is_encrypted {
51 12 ▶ my ( $self, $secret ) = @_;
Subroutine is_encrypted: called CC=1 SCAR=0.0
Pod undocumented
52 12 return ( substr( $secret, 0, length(PREFIX) ) eq PREFIX );
53 }
54  
55 sub get_ciphertext {
56 4 ▶ my ( $self, $secret ) = @_;
Subroutine get_ciphertext: called CC=1 SCAR=0.0
Pod undocumented
57 4 return substr( $secret, length(PREFIX) );
58 }
59  
60 # This returns the TOTP secret from its stored form
61 sub get_cleartext_secret {
62 12 ▶ my ( $self, $secret ) = @_;
Subroutine get_cleartext_secret: called CC=2 SCAR=6.9
Pod undocumented
63 12 my $cleartext_secret = $secret;
64 12 ▶ if ( $self->is_encrypted($secret) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_encrypted($secret)) 4 8 12
65 4 $cleartext_secret =
66 $self->crypto->decrypt( $self->get_ciphertext($secret) );
67 }
68 12 return $cleartext_secret;
69 }
70  
71 # This returns the cleartext or encrypted code for storage
72 sub get_storable_secret {
73 2 ▶ my ( $self, $secret ) = @_;
Subroutine get_storable_secret: called CC=2 SCAR=6.9
Pod undocumented
74 2 my $storable_secret = $secret;
75 2 ▶ if ( $self->encryptSecret ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->encryptSecret) 1 1 2
76 1 $storable_secret = PREFIX . $self->crypto->encrypt($secret);
77 }
78 2 return $storable_secret;
79 }
80  
81 # DEPRECATED: legacy API
82 sub verifyCode {
83 12 ▶ my ( $self, $interval, $range, $digits, $stored_secret, $code ) = @_;
Subroutine verifyCode: called CC=3 SCAR=11.0
Pod undocumented
84 12 my $result = $self->verify_totp(
85 interval => $interval,
86 range_tolerance => $range,
87 digits => $digits,
88 stored_secret => $stored_secret,
89 code => $code,
90 );
91 12 ▶ if ( $result->{result} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{'result'} == 1) { } 7 5 12
92 7 ▶ return ( wantarray() ? ( 1, $result->{offset} ) : 1 );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 7 7
93 }
94 else {
95 5 $self->logger->error( $result->{error} );
96 5 return $result->{result};
97 }
98 }
99  
100 # Verify that TOTP $code matches with $secret
101 sub verify_totp {
102 12 ▶ my ( $self, %args ) = @_;
Subroutine verify_totp: called CC=11 SCAR=25.6
Pod undocumented
103  
104 12 ▶ my $interval = $args{interval} || 30;
Condition50%1/2
$args{'interval'} || 30
l!l
120
MC/DC0%0/1
$args{'interval'} || 30
$args{'interval'}
Truth table50%1/2
A
A= $args{'interval'}
Aresult
11
00
105 12 ▶ my $digits = $args{digits} || 6;
Condition50%1/2
$args{'digits'} || 6
l!l
120
MC/DC0%0/1
$args{'digits'} || 6
$args{'digits'}
Truth table50%1/2
A
A= $args{'digits'}
Aresult
11
00
106 12 ▶ my $range = $args{range_tolerance} // 0;
Condition50%1/2
$args{"range_tolerance"} // 0
l!l
120
MC/DC0%0/1
$args{"range_tolerance"} // 0
$args{"range_tolerance"}
Truth table50%1/2
A
A= $args{"range_tolerance"}
Aresult
11
00
107 12 my $stored_secret = $args{stored_secret};
108 12 my $code = $args{code};
109 12 ▶ my $hash = $args{hash} || "sha1";
Condition50%1/2
$args{'hash'} || 'sha1'
l!l
012
MC/DC0%0/1
$args{'hash'} || 'sha1'
$args{'hash'}
Truth table50%1/2
A
A= $args{'hash'}
Aresult
11
00
110  
111 12 ▶ if ( !$stored_secret ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stored_secret) 0 12 12
112 return {
113 0 result => -1,
114 error => "No secret provided",
115 };
116 }
117  
118 12 ▶ if ( !$code ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code) 0 12 12
119 return {
120 0 result => -1,
121 error => "No code provided",
122 };
123 }
124  
125 12 my $secret = $self->get_cleartext_secret($stored_secret);
126 12 ▶ if ( !$secret ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($secret) 1 11 12
127 return {
128 1 result => -1,
129 error => "Unable to decrypt TOTP secret",
130 };
131 }
132  
133 11 my $s = eval { decode_base32($secret) };
134 11 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 11 11
135 return {
136 0 result => -1,
137 error => "Bad characters in TOTP secret",
138 };
139 }
140 11 for ( -$range .. $range ) {
141 24 ▶ if ( $code eq $self->_code( $s, $_, $interval, $digits, $hash ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq $self->_code($s, $_, $interval, $digits, $hash)) 7 17 24
142 return {
143 7 result => 1,
144 offset => $_,
145 };
146 }
147 }
148 return {
149 4 result => 0,
150 error => "Code did not match",
151 };
152 }
153  
154 # Internal subroutine that calculates TOTP code using $secret and $interval
155 sub _code {
156 42 ▶ my ( $self, $secret, $offset, $interval, $digits, $hash, $time ) = @_;
Subroutine _code: called CC=3 SCAR=11.2
157  
158 42 ▶ $time ||= time();
Condition66%2/3
$time ||= &CORE::GLOBAL::time()
l!l&&r!l&&!r
18240
MC/DC0%0/2
$time ||= &CORE::GLOBAL::time()
$time &CORE::GLOBAL::time()
Truth table66%2/3
A ||= B
A= $timeB= &CORE::GLOBAL::time()
ABresult
1X1
011
000
159  
160 42 my $hash_func = $hash_func->{$hash};
161 42 ▶ die "Unknown hash $hash" unless $hash_func;
Branch: 1 branch
BranchTrueFalseTotal
unless $hash_func 0 42 42
162  
163 42 my $hmac = $hash_func->(
164 pack( 'H*', sprintf( '%016x', int( $time / $interval ) - $offset ) ),
165 $secret,
166 );
167  
168 42 return sprintf(
169 '%0' . $digits . 'd',
170 (
171 hex( substr( $hmac, hex( substr( $hmac, -1 ) ) * 2, 8 ) ) &
172 0x7fffffff
173 ) % 10**$digits
174 );
175 }
176  
177 # Simply generate new base32 secret
178 sub newSecret {
179 0 ▶ my ($self) = @_;
Subroutine newSecret: not called CC=1 SCAR=6.9
Pod undocumented
180 0 return encode_base32( Crypt::URandom::urandom(20) );
181 }
182  
183 1 1;
« blib/lib/Lemonldap/NG/Common/Conf/Backends/RDBI.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Conf/Wrapper.pm »