Authen-TOTP
view release on metacpan or search on metacpan
lib/Authen/TOTP.pm view on Meta::CPAN
$self->valid_when();
$self->valid_tolerance();
$self->valid_secret();
return $self;
}
sub new {
my $this = shift;
my $class = ref($this) || $this;
my $self = {};
bless $self, $class;
return $self->initialize(@_);
}
1;
__END__
=head1 NAME
Authen::TOTP - Interface to RFC6238 two factor authentication (2FA)
Version 0.1.1
=head1 SYNOPSIS
use Authen::TOTP;
=head1 DESCRIPTION
C<Authen::TOTP> is a simple interface for creating and verifying RFC6238 OTPs
as used by Google Authenticator, Authy, Duo Mobile etc
It currently passes RFC6238 Test Vectors for SHA1, SHA256, SHA512
=head1 USAGE
my $gen = new Authen::TOTP(
secret => "some_random_stuff",
);
#will generate a TOTP URI, suitable to use in a QR Code
my $uri = $gen->generate_otp(user => 'user\@example.com', issuer => "example.com");
print qq{$uri\n};
#store $gen->secret() or $gen->base32secret() someplace safe!
#use Imager::QRCode to plot the secret for the user
use Imager::QRCode;
my $qrcode = Imager::QRCode->new(
size => 4,
margin => 3,
level => 'L',
casesensitive => 1,
lightcolor => Imager::Color->new(255, 255, 255),
darkcolor => Imager::Color->new(0, 0, 0),
);
my $img = $qrcode->plot($uri);
$img->write(file => "totp.png", type => "png");
#...or you can pass it to google charts and be done with it
#compare user's OTP with computed one
if ($gen->validate_otp(otp => <user_input>, secret => <stored_secret>, tolerance => 1)) {
#2FA success
}
else {
#no match
}
# Get generated OTP and validate it
my $otp=$gen->otp();
print "Generated OTP is $otp\n";
my $matches=$gen->validate_otp(otp => $otp);
print "Self generated OTP is ".($matches?"OK":"NOK")."\n";
=head1 new Authen::TOTP
my $gen = new Authen::TOTP(
digits => [6|8],
period => [30|60],
algorithm => "SHA1", #SHA256 and SHA512 are equally valid
secret => "some_random_stuff",
when => <some_epoch>,
tolerance => 0,
);
=head2 Parameters/Properties (defaults listed)
=over 4
=item digits
C<6>=> How many digits to produce/compare
=item period
C<30>=> OTP is valid for this many seconds
=item algorithm
C<SHA1>=> supported values are SHA1, SHA256 and SHA512, although most clients only support SHA1 AFAIK
=item secret
C<random_20byte_string>=> Secret used as seed for the OTP
=item base32secret
C<base32_encoded_random_12byte_string>=> Alternative way to set secret (base32 encoded)
=item when
C<epoch>=> Time used for comparison of OTPs
=item tolerance
C<1>=> Due to time sync issues, you may want to tune this and compare
this many OTPs before and after
( run in 3.328 seconds using v1.01-cache-2.11-cpan-788537b7465 )