Debian Codegraph @

lemonldap-ng-common/lib/Lemonldap/NG/Common/TOTP.pm

raw
1 package Lemonldap::NG::Common::TOTP;
2
3 # This module is inspired by Auth::GoogleAuth written by Gryphon Shafer
4 # <[email protected]>
5
6 use strict;
7 use Mouse;
8 use Convert::Base32 qw(decode_base32 encode_base32);
9 use Crypt::URandom;
10 use Digest::SHA;
11 use Lemonldap::NG::Common::Crypto;
12
13 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 our $VERSION = '2.23.0';
20
21 has key => (
22 is => 'ro',
23 lazy => 1,
24 default => sub {
25 my ($self) = @_;
26 return $self->conf->{totp2fKey} || $self->conf->{key};
27 }
28 );
29
30 has encryptSecret => (
31 is => 'ro',
32 lazy => 1,
33 default => sub {
34 my ($self) = @_;
35 return $self->conf->{totp2fEncryptSecret};
36 }
37 );
38
39 has crypto => (
40 is => 'ro',
41 lazy => 1,
42 default => sub {
43 my ($self) = @_;
44 Lemonldap::NG::Common::Crypto->new( $self->key );
45 }
46 );
47
48 use constant PREFIX => "{llngcrypt}";
49
50 sub is_encrypted {
51 my ( $self, $secret ) = @_;
52 return ( substr( $secret, 0, length(PREFIX) ) eq PREFIX );
53 }
54
55 sub get_ciphertext {
56 my ( $self, $secret ) = @_;
57 return substr( $secret, length(PREFIX) );
58 }
59
60 # This returns the TOTP secret from its stored form
61 sub get_cleartext_secret {
62 my ( $self, $secret ) = @_;
63 my $cleartext_secret = $secret;
64 if ( $self->is_encrypted($secret) ) {
65 $cleartext_secret =
66 $self->crypto->decrypt( $self->get_ciphertext($secret) );
67 }
68 return $cleartext_secret;
69 }
70
71 # This returns the cleartext or encrypted code for storage
72 sub get_storable_secret {
73 my ( $self, $secret ) = @_;
74 my $storable_secret = $secret;
75 if ( $self->encryptSecret ) {
76 $storable_secret = PREFIX . $self->crypto->encrypt($secret);
77 }
78 return $storable_secret;
79 }
80
81 # DEPRECATED: legacy API
82 sub verifyCode {
83 my ( $self, $interval, $range, $digits, $stored_secret, $code ) = @_;
84 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 if ( $result->{result} == 1 ) {
92 return ( wantarray() ? ( 1, $result->{offset} ) : 1 );
93 }
94 else {
95 $self->logger->error( $result->{error} );
96 return $result->{result};
97 }
98 }
99
100 # Verify that TOTP $code matches with $secret
101 sub verify_totp {
102 my ( $self, %args ) = @_;
103
104 my $interval = $args{interval} || 30;
105 my $digits = $args{digits} || 6;
106 my $range = $args{range_tolerance} // 0;
107 my $stored_secret = $args{stored_secret};
108 my $code = $args{code};
109 my $hash = $args{hash} || "sha1";
110
111 if ( !$stored_secret ) {
112 return {
113 result => -1,
114 error => "No secret provided",
115 };
116 }
117
118 if ( !$code ) {
119 return {
120 result => -1,
121 error => "No code provided",
122 };
123 }
124
125 my $secret = $self->get_cleartext_secret($stored_secret);
126 if ( !$secret ) {
127 return {
128 result => -1,
129 error => "Unable to decrypt TOTP secret",
130 };
131 }
132
133 my $s = eval { decode_base32($secret) };
134 if ($@) {
135 return {
136 result => -1,
137 error => "Bad characters in TOTP secret",
138 };
139 }
140 for ( -$range .. $range ) {
141 if ( $code eq $self->_code( $s, $_, $interval, $digits, $hash ) ) {
142 return {
143 result => 1,
144 offset => $_,
145 };
146 }
147 }
148 return {
149 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 my ( $self, $secret, $offset, $interval, $digits, $hash, $time ) = @_;
157
158 $time ||= time();
159
160 my $hash_func = $hash_func->{$hash};
161 die "Unknown hash $hash" unless $hash_func;
162
163 my $hmac = $hash_func->(
164 pack( 'H*', sprintf( '%016x', int( $time / $interval ) - $offset ) ),
165 $secret,
166 );
167
168 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 my ($self) = @_;
180 return encode_base32( Crypt::URandom::urandom(20) );
181 }
182
183 1;