2FA TOTP on Linux

Two factor authentication with timed one-time passwords is good. But most implementations are for smartphones or tablets.

So I wrote my own, in Perl:

#!/usr/bin/perl

use strict;
use warnings;

use Auth::GoogleAuth;

my $home = $ENV{'HOME'};

my %accounts;
open(my $fh, "-|","gpg -d $home/.2fa.gpg") or die "Can't decrypt file: $!";
while (<$fh>) {
        my ($name,$width,$key) = split;
        $accounts{$name} = $key;
}
close $fh;

my $auth = Auth::GoogleAuth->new;

if (scalar @ARGV == 0) {
        foreach my $name (sort keys %accounts) {
                print_key($name);
        }
} else {
        while (my $arg = shift @ARGV) {
                print_key($arg);
        }
}

sub print_key {
        my $account = shift;
        if (exists $accounts{$account}) {
                my $code = $auth->code($accounts{$account});
                print "$code  $account\n";
        }
}

Needs a ~/.2fa.gpg file, where the decrypted file looks like:

service1 6 DEFERFGRKHGHRGDFASDTREYRYRYERY
serv2 6 RTRYR6RTRE8DGDG9