#!/usr/bin/perl
# Convert otpauth URIs into clear text Aegis vault format
# Copyright       : https://www.fsf.org/copyleft/gpl.html
# Author          : Dan Jacobson -- https://www.jidanni.org/
# Created On      : Wed Apr  8 08:59:06 2020
# Last Modified On: Thu Apr  9 21:19:27 2020
# Update Count    : 138
use strict;
use warnings FATAL => q(all);
use JSON;
use URI;
use URI::QueryParam;
use UUID 'uuid';
binmode STDOUT;
my %h = (
    db     => { version => 1 },
    header => {
        params => undef,
        slots  => undef
    },
    version => 1,
);
while (<>) {
    next unless /otpauth:.*/;
    my $u = URI->new($&);
    push @{ $h{db}->{entries} }, {
        icon => undef,
        info => {
            algo      => "SHA1",    #set a default
            period    => 30,        #set a default
            digits    => 6,         #set a default
            # Do not copy other keys like 'algorithm'!
            #%{ $u->query_form_hash },
        },
        name => ( $u->path_segments )[-1],
        issuer => ( $u->path_segments )[-1],
        type => $u->authority,
        uuid => uuid(),
    };
}
for ( @{ $h{db}->{entries} } ) {
    $_->{info}->{digits} += 0;
    $_->{info}->{period} += 0;
    #$_->{issuer} = delete $_->{info}->{issuer};
    ## https://github.com/google/google-authenticator/wiki/Key-Uri-Format
    ## (We see there algo should be algorithm, etc...)
}
my $json = JSON->new;
$json = $json->canonical(1);
print $json->pretty->encode( \%h );
