| 1 | package Auth::GoogleAuthenticator; | ||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 3 | ▶ | use Authen::OATH; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 3 | ▶ | use Convert::Base32; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 3 | ▶ | use URI::Escape; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 3 | ▶ | use vars qw($VERSION); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 3 | $VERSION= '0.05'; | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | Auth::GoogleAuthenticator - Authenticate through Google Authenticator | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | =cut | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 1 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||
| 17 | 1 | my ($class, %args) = @_; | |||||||||||||||||||
| 18 | 1 | ▶ | if( $args{ secret_base32 }) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 19 | 1 | $args{ secret } = decode_base32( delete $args{ secret_base32 }); | |||||||||||||||||||
| 20 | }; | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 1 | ▶ | $args{ auth } ||= Authen::OATH->new(); | ||||||||||||||||||
|
Condition33%1/3
$args{"auth"} ||= "Authen::OATH"->new
MC/DC0%0/2
$args{"auth"} ||= "Authen::OATH"->new
$args{"auth"}
"Authen::OATH"->new
Truth table33%1/3
A ||= B
A= $args{"auth"}B= "Authen::OATH"->new
| |||||||||||||||||||||
| 23 | 1 | bless \%args => $class; | |||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 2 | ▶ | sub auth { $_[0]->{auth} }; | ||||||||||||||||||
|
Subroutine
auth: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 0 | ▶ | sub registration_qr_code { | ||||||||||||||||||
|
Subroutine
registration_qr_code: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 29 | 0 | my ($self, $label, $type) = @_; | |||||||||||||||||||
| 30 | # if we have an OTP, dislay the QRCode to the user | ||||||||||||||||||||
| 31 | 0 | require Imager::QRCode; | |||||||||||||||||||
| 32 | 0 | my $qrcode = Imager::QRCode->new( | |||||||||||||||||||
| 33 | size => 4, | ||||||||||||||||||||
| 34 | margin => 4, | ||||||||||||||||||||
| 35 | version => 1, | ||||||||||||||||||||
| 36 | level => 'M', | ||||||||||||||||||||
| 37 | casesensitive => 1, | ||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||
| 39 | 0 | my $img = $qrcode->plot($self->registration_url($label, $type)); | |||||||||||||||||||
| 40 | 0 | $img->write( data => \my $res, type => 'png' ); | |||||||||||||||||||
| 41 | 0 | $res | |||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 0 | ▶ | sub registration_key { | ||||||||||||||||||
|
Subroutine
registration_key: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 45 | 0 | return encode_base32( $_[0]->{secret} ); | |||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 2 | ▶ | sub totp { | ||||||||||||||||||
|
Subroutine
totp: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 49 | 2 | my ($self, $ts) = @_; | |||||||||||||||||||
| 50 | 2 | $self->auth->totp( $self->{secret}, $ts ) | |||||||||||||||||||
| 51 | }; | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 0 | ▶ | sub registration_url { | ||||||||||||||||||
|
Subroutine
registration_url: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 54 | 0 | my ($self, $label, $type) = @_; | |||||||||||||||||||
| 55 | 0 | ▶ | $type ||= 'totp'; | ||||||||||||||||||
|
Condition0%0/2
$type ||= "totp"
MC/DC0%0/1
$type ||= "totp"
$type
Truth table0%0/2
A
A= $type
| |||||||||||||||||||||
| 56 | 0 | $label= uri_escape($label); | |||||||||||||||||||
| 57 | 0 | return "otpauth://$type/$label?secret=" . $self->registration_key | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 0 | ▶ | sub verify { | ||||||||||||||||||
|
Subroutine
verify: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 61 | 0 | my ($self, $code, $ts) = @_; | |||||||||||||||||||
| 62 | 0 | ▶ | return ($code and | ||||||||||||||||||
|
Condition0%0/3
$code && $self->totp($ts) == $code
MC/DC0%0/2
$code && $self->totp($ts) == $code
$code
$self->totp($ts) == $code
Truth table0%0/3
A && B
A= $codeB= $self->totp($ts) == $code
| |||||||||||||||||||||
| 63 | $self->totp( $ts ) == $code); | ||||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 3 | 1; | |||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | =head1 WORKFLOW | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | =over 4 | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | =item * | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | Install Google Authenticator | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | =item * | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | Visit the "Install Two Factor Authentication" page | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | =item * | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | Display the secret key there | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | ->registration_qr_code | ||||||||||||||||||||
| 85 | ->registration_key | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | Display the "Panic" OTPs there so that the user can print them out | ||||||||||||||||||||
| 88 | on paper and store them in a secure location: | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | my @recovery_passwords = generate_recovery_strings( 3 ); | ||||||||||||||||||||
| 91 | for my $pass ( @recovery_passwords ) { | ||||||||||||||||||||
| 92 | print $pass, "\n"; | ||||||||||||||||||||
| 93 | }; | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | =item * | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | Photograph the QR code | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | or | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | Manually enter the key into the Authenticator | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | =item * | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | On the Login page enter the password | ||||||||||||||||||||
| 106 | and the OTP code from the Authenticator | ||||||||||||||||||||
| 107 | or on the Recovery page, enter one of the panic keys. | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | =back | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | =head1 PASSWORD STORAGE | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | The password should be stored as a hash. | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | The shared authenticator secret needs to be stored as plaintext. | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | =head1 RECOVERY | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | As phones tend to get lost, the recovery passphrases become | ||||||||||||||||||||
| 120 | important. They also are password equivalent. So, my recommendation | ||||||||||||||||||||
| 121 | is to store the recovery passphrases only as hashes, just | ||||||||||||||||||||
| 122 | like you store passwords. | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | =head1 COMPATIBILITY | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | At least on iDevices, using C<< < >> or C<< > >> made registering | ||||||||||||||||||||
| 127 | the generated accounts through QRcodes fail. The QRcodes work | ||||||||||||||||||||
| 128 | with Android devices. | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | =head1 SEE ALSO | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | TOTP: Time-Based One-Time Password Algorithm | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | L<http://tools.ietf.org/html/rfc6238> | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | =cut | ||||||||||||||||||||