redisplay
changeset 0 5e0fc4f25c44
child 1 0fde44ae8f1d
equal deleted inserted replaced
-1:000000000000 0:5e0fc4f25c44
       
     1 #! /usr/bin/perl
       
     2 # Heiko Schlittermann
       
     3 use 5.014;
       
     4 use warnings;
       
     5 use Sys::Hostname;
       
     6 
       
     7 sub url_encode {
       
     8     shift =~ s/(?i:([^a-z]))/sprintf '%%%02x', ord $1/rge;
       
     9 }
       
    10 
       
    11 sub main {
       
    12     if (@ARGV) { splice @ARGV, 1 }
       
    13     else       { @ARGV = ("$ENV{HOME}/.google_authenticator") }
       
    14 
       
    15     my $google = do { local $/ = undef; <> }
       
    16       or die "$0: Can't read any input from $ARGV.\n";
       
    17 
       
    18     my ($secret) = $google =~ /\A(\S+)/
       
    19       or die "$0: Can't read secret from $ARGV.\n";
       
    20 
       
    21     my ($method) = map { lc } $google =~ /^"\s(.)\S+_AUTH$/m
       
    22       or die "$0: Can't read method (time/hash) from $ARGV.\n";
       
    23 
       
    24     my $user = $ENV{USER} // $ENV{LOGNAME} // getpwuid $>;
       
    25     my $host = hostname;
       
    26 
       
    27     open my $qr, '|qrencode -t ansi256'
       
    28       or die "$0: Can't open qrencode: $!\n";
       
    29 
       
    30     $qr->printf(
       
    31         "otpauth://%sotp/%s?secret=%s\n", $method,
       
    32         url_encode("$user\@$host"),       $secret
       
    33     );
       
    34 
       
    35     close $qr;
       
    36 }
       
    37 
       
    38 exit main unless caller;