Airlock
view release on metacpan or search on metacpan
lib/Airlock/Factor/TOTP.pm view on Meta::CPAN
# The time step this proof is valid for, or nothing. Every step of the window
# is computed whether or not an earlier one matched.
sub _step {
my ( $self, $subject, $proof ) = @_;
return unless defined $proof;
$proof =~ s/\s//g;
return unless $proof =~ /\A[0-9]+\z/ && length $proof == $self->digits;
my $secret = $self->_secret->($subject);
return unless defined $secret && length $secret;
my $current = int( $self->now->() / $self->period );
my $hit;
for my $step ( $current - $self->window .. $current + $self->window ) {
next unless $self->code_class->equals( $self->code_at( $secret, $step ), $proof );
$hit = $step;
}
return unless defined $hit;
my $last = $self->_last_step->($subject);
return if defined $last && $hit <= $last;
return $hit;
}
sub verify {
my ( $self, $subject, $proof ) = @_;
return defined $self->_step( $subject, $proof ) ? 1 : 0;
}
sub commit {
my ( $self, $subject, $proof ) = @_;
my $step = $self->_step( $subject, $proof );
return 0 unless defined $step;
return $self->_accept_step->( $subject, $step ) ? 1 : 0;
}
sub generate_secret { urandom(20) }
sub base32 {
my ( $self, $bytes ) = @_;
my @alphabet = ( 'A' .. 'Z', '2' .. '7' );
my $bits = unpack 'B*', $bytes;
$bits .= '0' x ( ( 5 - length($bits) % 5 ) % 5 );
return join '', map { $alphabet[ oct '0b'.$_ ] } $bits =~ /(.{5})/g;
}
sub otpauth_uri {
my ( $self, %arg ) = @_;
for (qw( secret account issuer )) {
croak __PACKAGE__.'->otpauth_uri needs '.$_ unless defined $arg{$_} && length $arg{$_};
}
return 'otpauth://totp/'.$self->_escape( $arg{issuer} ).':'.$self->_escape( $arg{account} )
.'?secret='.$self->base32( $arg{secret} )
.'&issuer='.$self->_escape( $arg{issuer} )
.'&algorithm=SHA1&digits='.$self->digits.'&period='.$self->period;
}
sub _escape {
my ( $self, $text ) = @_;
utf8::encode($text) if utf8::is_utf8($text);
$text =~ s/([^A-Za-z0-9\-._~])/sprintf '%%%02X', ord $1/ge;
return $text;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Airlock::Factor::TOTP - Time-based one-time password (RFC 6238) as an Airlock factor
=head1 VERSION
version 0.001
=head1 SYNOPSIS
my $totp = Airlock::Factor::TOTP->new(
secret => sub { my ( $subject ) = @_; $db->totp_secret( $subject->{id} ) },
last_step => sub { my ( $subject ) = @_; $db->totp_step( $subject->{id} ) },
accept_step => sub { my ( $subject, $step ) = @_; $db->set_totp_step( $subject->{id}, $step ) },
);
# enrolment
my $secret = $totp->generate_secret;
my $uri = $totp->otpauth_uri( secret => $secret, account => 'getty@example.org', issuer => 'Mothership' );
=head1 DESCRIPTION
TOTP with HMAC-SHA1, which is what authenticator apps implement. Airlock stores
nothing itself: the secret and the last accepted time step come from the host
application through three coderefs.
A code is accepted once. C<last_step> and C<accept_step> are what make that
true, which is why both are required.
=head2 secret
Required. Coderef called with the subject; returns the raw secret bytes, or
nothing (or an empty string) when the subject has not enrolled.
=head2 last_step
Required. Coderef called with the subject; returns the last accepted time
step, or nothing when there is none yet.
=head2 accept_step
Required. Coderef called with the subject and the time step that is being
accepted. It stores the step, so the same code cannot be used again, and
returns true. Where two requests can arrive at once, store only if the new
step is greater than the stored one and return false otherwise; the approval
then fails instead of accepting one code twice.
=head2 digits
( run in 1.152 second using v1.01-cache-2.11-cpan-234fa220139 )