|
| 1 | +#===================================================================== |
| 2 | +# SQL-Ledger ERP |
| 3 | +# Copyright (C) 2025 |
| 4 | +# |
| 5 | +# Author: Tekki |
| 6 | +# Web: https://tekki.ch |
| 7 | +# |
| 8 | +#====================================================================== |
| 9 | +# |
| 10 | +# Time-based one-time passwords |
| 11 | +# |
| 12 | +#====================================================================== |
| 13 | +package SL::TOTP; |
| 14 | + |
| 15 | +use Digest::SHA 'hmac_sha1'; |
| 16 | +use Encode 'decode'; |
| 17 | + |
| 18 | +sub add_secret { |
| 19 | + my ($user, $memberfile, $userspath) = @_; |
| 20 | + |
| 21 | + $user->{totp_secret} = &generate_secret; |
| 22 | + |
| 23 | + if ($memberfile && $userspath) { |
| 24 | + for (qw|name signature|) { |
| 25 | + $user->{$_} = decode 'UTF-8', $user->{$_}; |
| 26 | + } |
| 27 | + $user->{packpw} = 1; |
| 28 | + $user->save_member($memberfile, $userspath); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +sub check_code { |
| 33 | + my ($user, $code, $timestamp) = @_; |
| 34 | + |
| 35 | + $timestamp //= time; |
| 36 | + my $hash = hmac_sha1(pack('Q>', int($timestamp / 30)), decode_base32($user->{totp_secret})); |
| 37 | + my $offset = ord(substr($hash, -1)) & 0x0f; |
| 38 | + my $otp = ((unpack("N", substr($hash, $offset, 4))) & 0x7fffffff) % 10**6; |
| 39 | + |
| 40 | + return sprintf("%06d", $otp) eq $code; |
| 41 | +} |
| 42 | + |
| 43 | +# decode_base32 and encode_base32: from MIME::Base32 by Jens Rehsack |
| 44 | + |
| 45 | +sub decode_base32 { |
| 46 | + my $arg = uc(shift || ''); # mimic MIME::Base64 |
| 47 | + $arg =~ tr|A-Z2-7|\0-\37|; |
| 48 | + $arg = unpack('B*', $arg); |
| 49 | + $arg =~ s/000(.....)/$1/g; |
| 50 | + my $l = length $arg; |
| 51 | + $arg = substr($arg, 0, $l & ~7) if $l & 7; |
| 52 | + $arg = pack('B*', $arg); |
| 53 | + return $arg; |
| 54 | +} |
| 55 | + |
| 56 | +sub encode_base32 { |
| 57 | + my $arg = shift; |
| 58 | + return '' unless defined($arg); # mimic MIME::Base64 |
| 59 | + $arg = unpack('B*', $arg); |
| 60 | + $arg =~ s/(.....)/000$1/g; |
| 61 | + my $l = length($arg); |
| 62 | + if ($l & 7) { |
| 63 | + my $e = substr($arg, $l & ~7); |
| 64 | + $arg = substr($arg, 0, $l & ~7); |
| 65 | + $arg .= "000$e" . '0' x (5 - length $e); |
| 66 | + } |
| 67 | + $arg = pack('B*', $arg); |
| 68 | + $arg =~ tr|\0-\37|A-Z2-7|; |
| 69 | + return $arg; |
| 70 | +} |
| 71 | + |
| 72 | +sub generate_secret { |
| 73 | + my $secret = join '', map { chr int rand 256 } 1 .. 20; |
| 74 | + |
| 75 | + return encode_base32($secret); |
| 76 | +} |
| 77 | + |
| 78 | +sub url { |
| 79 | + my ($user) = @_; |
| 80 | + |
| 81 | + my $account = $user->{login}; |
| 82 | + $account .= "\@$ENV{SERVER_NAME}" if $ENV{SERVER_NAME}; |
| 83 | + |
| 84 | + return |
| 85 | + qq|otpauth://totp/SQL-Ledger:$account?secret=$user->{totp_secret}&issuer=SQL-Ledger&algorithm=SHA1&digits=6&period=30|; |
| 86 | +} |
| 87 | + |
| 88 | +1; |
| 89 | + |
| 90 | +=encoding utf8 |
| 91 | +
|
| 92 | +=head1 NAME |
| 93 | +
|
| 94 | +SL::TOTP - Time-based one-time passwords |
| 95 | +
|
| 96 | +=head1 SYNOPSIS |
| 97 | +
|
| 98 | + use SL::TOTP; |
| 99 | + use SL::User; |
| 100 | +
|
| 101 | + my $user = User->new($memberfile, $form->{login}); |
| 102 | + SL::TOTP->add_secret($user, $memberfile, $userspath); |
| 103 | +
|
| 104 | + my $url = SL::TOTP::url($user); |
| 105 | +
|
| 106 | + SL::TOTP->check_code($user, $code); |
| 107 | +
|
| 108 | +=head1 DESCRIPTION |
| 109 | +
|
| 110 | +L<SL::TOTP> provides functions for time-based one-time passwords. |
| 111 | +
|
| 112 | +=head1 DEPENDENCIES |
| 113 | +
|
| 114 | +L<SL::TOTP> |
| 115 | +
|
| 116 | +=over |
| 117 | +
|
| 118 | +=item * uses |
| 119 | +L<Digest::SHA> |
| 120 | +
|
| 121 | +=back |
| 122 | +
|
| 123 | +=head1 FUNCTIONS |
| 124 | +
|
| 125 | +L<SL::TOTP> implements the following functions: |
| 126 | +
|
| 127 | +=head2 add_secret |
| 128 | +
|
| 129 | + my $user = User->new($memberfile, $form->{login}); |
| 130 | + SL::TOTP::add_secret($user, $memberfile, $userspath); |
| 131 | +
|
| 132 | + SL::TOTP::add_secret($user); # without saving |
| 133 | +
|
| 134 | +=head2 check_code |
| 135 | +
|
| 136 | + my $ok = SL::TOTP::check_code($user, $code); # current time |
| 137 | + my $ok = SL::TOTP::check_code($user, $code, $timestamp); |
| 138 | +
|
| 139 | +=head2 decode_base32 |
| 140 | +
|
| 141 | + my $decoded = SL::TOTP::decode_base32($encoded_string); |
| 142 | +
|
| 143 | +=head2 encode_base32 |
| 144 | +
|
| 145 | + my $encoded_string = SL::TOTP::encode_base32($unencoded_string); |
| 146 | +
|
| 147 | +=head2 generate_secret |
| 148 | +
|
| 149 | + my $secret = SL::TOTP::generate_secret(); |
| 150 | +
|
| 151 | +=head2 url |
| 152 | +
|
| 153 | + my $url = SL::TOTP::url($user); |
| 154 | +
|
| 155 | +=cut |
0 commit comments