File Coverage

blib/lib/App/iTan/Command/Get.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             # ================================================================
2             package App::iTan::Command::Get;
3             # ================================================================
4 1     1   1189 use utf8;
  1         2  
  1         4  
5 1     1   379 use Moose;
  0            
  0            
6             use 5.0100;
7              
8             our $VERSION = $App::iTan::VERSION;
9              
10             use MooseX::App::Command;
11             with qw(App::iTan::Utils);
12              
13             option 'next' => (
14             is => 'ro',
15             isa => 'Bool',
16             documentation => q[Get the next available iTAN],
17             );
18              
19             option 'index' => (
20             is => 'ro',
21             isa => 'Int',
22             documentation => q[iTAN index number that should be fetched],
23             );
24              
25             option 'lowerinvalid' => (
26             is => 'ro',
27             isa => 'Bool',
28             documentation => q[Mark all iTANs with a lower index as invalid (only in combination with --index)],
29             );
30              
31             option 'memo' => (
32             is => 'ro',
33             isa => 'Str',
34             documentation => q[Optional memo on iTAN usage],
35             );
36              
37             sub execute {
38             my ( $self, $opts, $args ) = @_;
39            
40             my $index;
41             if ($self->next) {
42             ($index) = $self->dbh->selectrow_array("SELECT tindex
43             FROM itan
44             WHERE used IS NULL
45             AND valid = 1
46             ORDER BY tindex
47             LIMIT 1");
48            
49             unless (defined $index) {
50             say 'No more iTANs left';
51             return;
52             }
53             } else {
54             $index = $self->index;
55             }
56            
57             unless (defined $index) {
58             say 'Option --index or --next must be set';
59             } else {
60             my $tan_data = $self->get($index);
61             my $itan = $self->decrypt_string($tan_data->{itan});
62             say 'iTAN '.$index.' marked as used';
63             say 'iTAN '.$itan;
64            
65             eval {
66             if ($^O eq 'darwin') {
67             Class::Load::load_class('Mac::Pasteboard');
68             my $pb = Mac::Pasteboard->new();
69             $pb->clear;
70             $pb->copy($itan);
71             } else {
72             Class::Load::load_class('Clipboard');
73             Clipboard->copy($itan);
74             }
75             say 'iTan has been coppied to the clipboard';
76             };
77            
78             $self->mark($index,$self->memo);
79             if ($self->lowerinvalid) {
80             $self->dbh->do('UPDATE itan SET valid = 0 WHERE tindex < '.$index)
81             or die "ERROR: Cannot execute: " . $self->dbh->errstr();
82             }
83             }
84            
85             return;
86             }
87              
88             __PACKAGE__->meta->make_immutable;
89             1;
90              
91             =pod
92              
93             =encoding utf8
94              
95             =head1 NAME
96              
97             App::iTan::Command::Get - Fetches selected iTAN
98              
99             =head1 SYNOPSIS
100              
101             itan get [--next] OR [--index INDEX [--lowerinactive]] [--memo MEMO]
102              
103             =head1 DESCRIPTION
104              
105             Fetches an iTan an marks it as used. If possible the iTAN is also copied
106             to the clipboard.
107              
108             You will be prompted a password to decrypt the selected iTan.
109              
110             =head1 OPTIONS
111              
112             =head2 next
113              
114             Get the next available iTAN
115              
116             =head2 index
117              
118             iTAN index number that should be fetched
119              
120             =head2 lowerinvalid
121              
122             Mark all iTANs with a lower index as invalid (only in combination with
123             --index)
124              
125             =head2 memo
126              
127             Optional memo on iTAN usage
128              
129             =cut