redisplay
changeset 2 fb6344bb32dc
parent 1 0fde44ae8f1d
child 3 5c973cf5a4d6
equal deleted inserted replaced
1:0fde44ae8f1d 2:fb6344bb32dc
     1 #! /usr/bin/perl
     1 #! /usr/bin/perl
     2 # Heiko Schlittermann
     2 # Heiko Schlittermann
       
     3 # needs qrencode
     3 use 5.014;
     4 use 5.014;
     4 use warnings;
     5 use warnings;
     5 use Sys::Hostname;
     6 use Sys::Hostname;
       
     7 use Pod::Usage;
       
     8 use Getopt::Long;
       
     9 
       
    10 our $VERSION = 0.1;
     6 
    11 
     7 sub url_encode {
    12 sub url_encode {
     8     shift =~ s/(?i:([^a-z]))/sprintf '%%%02x', ord $1/rge;
    13     return shift =~ s/(?i:([^a-z]))/sprintf '%%%02x', ord $1/rge;
     9 }
    14 }
    10 
    15 
    11 sub main {
    16 sub main {
       
    17 
       
    18     GetOptions(
       
    19         'h|help' => sub { pod2usage(-exit => 0, -verbose => 1) },
       
    20         'm|man'  => sub { pod2usage(-exit => 0, -verbose => 2) },
       
    21     ) or pod2usage;
       
    22 
    12     if (@ARGV) { splice @ARGV, 1 }
    23     if (@ARGV) { splice @ARGV, 1 }
    13     else       { @ARGV = ("$ENV{HOME}/.google_authenticator") }
    24     else       { @ARGV = ("$ENV{HOME}/.google_authenticator") }
    14 
    25 
    15     my $google = do { local $/ = undef; <> }
    26     my $google = do { local $/ = undef; <> }
    16       or die "$0: Can't read any input from $ARGV.\n";
    27       or die "$0: Can't read any input from $ARGV.\n";
    17 
    28 
    18     my ($secret) = $google =~ /\A(\S+)/
    29     my ($secret) = $google =~ /\A(\S+)/
    19       or die "$0: Can't read secret from $ARGV.\n";
    30       or die "$0: Can't read secret from $ARGV.\n";
    20 
    31 
    21     my ($method) = map { lc } $google =~ /^"\s(.)\S+_AUTH$/m
    32     my ($method) = map { lc } $google =~ /^"\s+(.)\S+_AUTH$/m
    22       or die "$0: Can't read method (time/hash) from $ARGV.\n";
    33       or die "$0: Can't read method (time/hash) from $ARGV.\n";
    23 
    34 
    24     my $user = $ENV{USER} // $ENV{LOGNAME} // getpwuid $>;
    35     my $user = $ENV{USER} // $ENV{LOGNAME} // getpwuid $>;
    25     my $host = hostname;
    36     my $host = hostname;
    26 
    37 
    27     open(my $o, '>&STDOUT');
    38     open(my $o, '>&STDOUT');
    28     if (-t STDIN) {
    39     if (-t STDIN) {
    29         open STDOUT, '|qrencode -t ansi256'
    40         open STDOUT, '|qrencode -t ansi256'
    30           or die "$0: Can't open qrencode: $!\n";
    41           or die "$0: Can't open helper binary `qrencode': $!\n";
    31     }
    42     }
    32 
    43 
    33     printf "otpauth://%sotp/%s?secret=%s\n", $method,
    44     printf "otpauth://%sotp/%s?secret=%s\n", $method,
    34       url_encode("$user\@$host"), $secret;
    45       url_encode("$user\@$host"), $secret;
    35 
    46 
    36     close(STDOUT);
    47     close(STDOUT);
    37 
    48     return 0;
    38 }
    49 }
    39 
    50 
    40 exit main unless caller;
    51 exit main unless caller;
       
    52 
       
    53 =head1 NAME
       
    54 
       
    55   redisplay - redisplay the google authenticator qr code
       
    56 
       
    57 =head1 SYNOPSIS
       
    58 
       
    59   redisplay [secretfile]
       
    60 
       
    61 =head1 DESCRIPTION
       
    62 
       
    63 B<redisplay> reads the F<.google_authenticator> file, extracts
       
    64 the information an redisplays an ANSI256 QR code.
       
    65 
       
    66 The external B<qrencode> tool needs to be present in your PATH.
       
    67 
       
    68 =head1 AUTHOR
       
    69 
       
    70 Heiko Schlittermann L<hs@schlittermann.de>. The
       
    71 
       
    72 Source is at L<https://ssl.schlittermann.de/hg/google-authenticator-qr>
       
    73 
       
    74 =cut