/usr/share/perl5/Auth/GoogleAuth.pm is in libauth-googleauth-perl 1.02-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | package Auth::GoogleAuth;
# ABSTRACT: Google Authenticator TBOT Abstraction
use 5.008;
use strict;
use warnings;
use base 'Class::Accessor';
use Digest::HMAC_SHA1 'hmac_sha1_hex';
use Math::Random::MT 'rand';
use URI::Escape 'uri_escape';
use Convert::Base32 qw( encode_base32 decode_base32 );
use Carp 'croak';
our $VERSION = '1.02'; # VERSION
my @accessors = qw( secret secret32 issuer key_id otpauth );
__PACKAGE__->mk_accessors(@accessors);
sub generate_secret32 {
my ($self) = @_;
my @chars = ( 'a' .. 'z', 2 .. 7 );
return $self->secret32( join( '', @chars[ map { rand( scalar(@chars) ) } 1 .. 16 ] ) );
}
sub clear {
my ($self) = @_;
$self->$_(undef) for (@accessors);
return;
}
sub qr_code {
my ( $self, $secret32, $key_id, $issuer, $return_otpauth ) = @_;
$self->_secret_check($secret32);
$self->key_id($key_id) if ($key_id);
$self->issuer($issuer) if ($issuer);
$self->key_id('Undefined') unless ( $self->key_id );
$self->issuer('Undefined') unless ( $self->issuer );
$self->otpauth(
'otpauth://totp/' .
uri_escape( $self->issuer ) . ':' . uri_escape( $self->key_id ) .
'?secret=' . $self->secret32 . '&issuer=' . uri_escape( $self->issuer )
);
return ($return_otpauth)
? $self->otpauth
: 'https://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=' . uri_escape( $self->otpauth );
}
sub code {
my ( $self, $secret32, $timestamp, $interval ) = @_;
$self->_secret_check($secret32);
$timestamp ||= time;
$interval ||= 30;
my $hmac = hmac_sha1_hex(
pack( 'H*', sprintf( '%016x', int( $timestamp / $interval ) ) ),
_decode_base32( $self->secret32 ),
);
return sprintf(
'%06d',
( hex( substr( $hmac, hex( substr( $hmac, -1 ) ) * 2, 8 ) ) & 0x7fffffff ) % 1000000
);
}
sub verify {
my ( $self, $code, $range, $secret32, $timestamp, $interval ) = @_;
$self->_secret_check($secret32);
$code ||= '';
$range ||= 0;
$timestamp ||= time;
$interval ||= 30;
croak('Range value not zero or a positive number') unless ( $range =~ /^\d+$/ and $range >= 0 );
for ( 0 .. $range ) {
return 1 if (
not $_ and $code eq $self->code( $secret32, $timestamp, $interval )
or
$code eq $self->code( $secret32, $timestamp + $interval * $_, $interval )
or
$code eq $self->code( $secret32, $timestamp - $interval * $_, $interval )
);
}
return 0;
}
sub _secret_check {
my ( $self, $secret32 ) = @_;
if ($secret32) {
$self->secret32($secret32);
$self->secret( _decode_base32($secret32) );
}
if ( not $self->secret32 ) {
if ( not $self->secret ) {
$self->secret( _decode_base32( $self->generate_secret32 ) );
}
else {
$self->secret32( encode_base32( $self->secret ) );
}
}
return;
}
sub _decode_base32 {
my ($data) = @_;
my $rv;
eval{ $rv = decode_base32($data) };
croak("Error decoding what should be base32 data: $data")
if ( $@ =~ /Data contains non-base32 characters/ );
return $rv;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Auth::GoogleAuth - Google Authenticator TBOT Abstraction
=head1 VERSION
version 1.02
=for markdown [](https://travis-ci.org/gryphonshafer/Auth-GoogleAuth)
[](https://coveralls.io/r/gryphonshafer/Auth-GoogleAuth)
=head1 SYNOPSIS
use Auth::GoogleAuth;
my $auth = Auth::GoogleAuth->new;
$auth = Auth::GoogleAuth->new({
secret => 'some secret string thing',
issuer => 'Gryphon Shafer',
key_id => 'gryphon@cpan.org',
});
$auth->secret(); # get/set
$auth->secret32(); # get/set
$auth->issuer(); # get/set
$auth->key_id(); # get/set
my $secret32 = $auth->generate_secret32;
$auth->clear;
my $url_0 = $auth->qr_code;
my $url_1 = $auth->qr_code(
'bv5o3disbutz4tl3', # secret32
'gryphon@cpan.org', # key_id
'Gryphon Shafer', # issuer
);
my $url_2 = $auth->qr_code(
'bv5o3disbutz4tl3', 'gryphon@cpan.org', 'Gryphon Shafer', 1,
);
my $otpauth = $auth->otpauth;
my $code_0 = $auth->code;
my $code_1 = $auth->code( 'utz4tl3bv5o3disb', 1438643789, 30 );
my $verification_0 = $auth->verify('879364');
my $verification_1 = $auth->verify(
'879364', # code
1, # range
'utz4tl3bv5o3disb', # secret32
1438643820, # timestamp (defaults to now)
30, # interval (default 30)
);
=head1 DESCRIPTION
This module provides a simplified interface to supporting typical two-factor
authentication (i.e. "2FA") with
L<Google Authenticator|https://en.wikipedia.org/wiki/Google_Authenticator>
using the
L<TOTP Algorithm|https://en.wikipedia.org/wiki/Time-based_One-time_Password_Algorithm>
as defined by L<RFC 6238|http://tools.ietf.org/html/rfc6238>.
Although Google Authenticator supports both TOTP and HOTP, at the moment,
this module only supports TOTP.
=head1 METHODS
The following are the supported methods of this module:
=head2 new
This is a simple instantiator to which you can pass optional default values.
my $auth = Auth::GoogleAuth->new;
$auth = Auth::GoogleAuth->new({
secret => 'some secret string thing',
issuer => 'Gryphon Shafer',
key_id => 'gryphon@cpan.org',
});
The object returned will support the following attribute get/set methods:
=head3 secret
This can be any string. It'll be used as the internal secret key to create
the QR codes and authentication codes.
=head3 secret32
This is a base-32 encoded copy of the secret string. If this is left undefined
and you run one of the methods that require it (like C<qr_code> or C<code>),
the method called will try to create the "secret32" by looking for a value in
"secret". If none exists, a random "secret32" will be generated.
=head3 issuer
This is the label name of the "issuer" of the authentication.
See the
L<key URI format wiki page|https://github.com/google/google-authenticator/wiki/Key-Uri-Format>
for more information.
=head3 key_id
This is the label name of the "key ID" of the authentication.
See the
L<key URI format wiki page|https://github.com/google/google-authenticator/wiki/Key-Uri-Format>
for more information.
=head3 otpauth
This method returns the otpauth key URI generated when you call
C<qr_code>.
=head2 generate_secret32
This method will generate a reasonable random "secret32" value, store it in the
get/set method, and return it.
my $secret32 = $auth->generate_secret32;
=head2 clear
Given that the "secret" and "secret32" values may persist in this object, which
could be a bad idea in some contexts, this C<clear> method lets your clear out
all attribute values.
$auth->clear;
=head2 qr_code
This method will return a Google Chart API URL that will return a QR code based
on the data either in the object or provided to this method.
my $url_0 = $auth->qr_code;
my $url_1 = $auth->qr_code(
'bv5o3disbutz4tl3', # secret32
'gryphon@cpan.org', # key_id
'Gryphon Shafer', # issuer
);
You can optionally add a final true value, and if you do, the method will
return the generated otpauth key URI rather than the Google Chart API URL.
my $url_2 = $auth->qr_code(
'bv5o3disbutz4tl3', 'gryphon@cpan.org', 'Gryphon Shafer', 1,
);
=head2 code
This method returns an authentication code, as if you were using
L<Google Authenticator|https://en.wikipedia.org/wiki/Google_Authenticator>
with the "secret32" value.
my $code_0 = $auth->code;
You can optionally pass override values similar to C<qr_code>:
my $code_1 = $auth->code(
'utz4tl3bv5o3disb', # secret32
1438643789, # timestamp (defaults to now)
30, # interval (default 30)
);
=head2 verify
This method is used for verification of codes entered by a user. Pass in the
code (required) and optionally a range value and any override values.
my $verification_0 = $auth->verify('879364');
The range value is useful because the algorithm checks codes that are time-
based. If clocks are not exactly in sync, it's possible that a "nearly valid"
code would be entered and should be accepted as valid but will be seen as
invalid. By passing in an integer as a range value, you can stipulate how
"fuzzy" the time should be. The default range is 0. A value of 1 will mean that
a code based on a time 1 iteration plus or minus should verify.
my $verification_1 = $auth->verify(
'879364', # code
1, # range
'utz4tl3bv5o3disb', # secret32
1438643820, # timestamp (defaults to now)
30, # interval (default 30)
);
=head1 TYPICAL USE-CASE
Typically, you're probably going to want to either randomly generate a secret or
secret32 (C<generate_secret32>) for a user and store it, or use a specific value
or hash of some value as the secret. In either case, once you have a secret and
its stored, generate a QR code (C<qr_code>) for the user. You can alternatively
provide the "secret32" to the user for them to manually enter it. That's it
for setup.
To authenticate, present the user with a way to provide you a code (which will
be a series of 6-digits). Verify that code (C<verify>) with either no range
or some small range like 1.
=head1 DEPENDENCIES
L<Digest::HMAC_SHA1>, L<Math::Random::MT>, L<URI::Escape>, L<Convert::Base32>,
L<Class::Accessor>, L<Carp>.
=head1 SEE ALSO
You can look for additional information about this module at:
=over 4
=item *
L<GitHub|https://github.com/gryphonshafer/Auth-GoogleAuth>
=item *
L<CPAN|http://search.cpan.org/dist/Auth-GoogleAuth>
=item *
L<MetaCPAN|https://metacpan.org/pod/Auth::GoogleAuth>
=item *
L<AnnoCPAN|http://annocpan.org/dist/Auth-GoogleAuth>
=item *
L<Travis CI|https://travis-ci.org/gryphonshafer/Auth-GoogleAuth>
=item *
L<Coveralls|https://coveralls.io/r/gryphonshafer/Auth-GoogleAuth>
=item *
L<CPANTS|http://cpants.cpanauthors.org/dist/Auth-GoogleAuth>
=item *
L<CPAN Testers|http://www.cpantesters.org/distro/G/Auth-GoogleAuth.html>
=back
You can look for additional information about things related to this module at:
=over 4
=item *
L<TOTP Algorithm|https://en.wikipedia.org/wiki/Time-based_One-time_Password_Algorithm>
=item *
L<RFC 6238|http://tools.ietf.org/html/rfc6238>
=item *
L<Google Authenticator|https://en.wikipedia.org/wiki/Google_Authenticator>
=item *
L<Google Authenticator GitHub|https://github.com/google/google-authenticator>
=back
=head1 AUTHOR
Gryphon Shafer <gryphon@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2018 by Gryphon Shafer.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|