| 1 | package App::Tel::Passwd; | ||||||||||||
| 2 | |||||||||||||
| 3 | 12 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 12 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 12 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 12 | ▶ | use Module::Load; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 12 | ▶ | use POSIX qw(); # For isatty | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 12 | ▶ | use IO::Stty; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 12 | require Exporter; | |||||||||||
| 10 | 12 | our @ISA = qw(Exporter); | |||||||||||
| 11 | 12 | our @EXPORT = qw(); | |||||||||||
| 12 | 12 | our @EXPORT_OK = qw ( keyring input_password ); | |||||||||||
| 13 | |||||||||||||
| 14 | # unit test override variables | ||||||||||||
| 15 | 12 | our $appname = 'tel script'; | |||||||||||
| 16 | 12 | our $_mock = 0; | |||||||||||
| 17 | |||||||||||||
| 18 | =head1 NAME | ||||||||||||
| 19 | |||||||||||||
| 20 | App::Tel::Passwd - Methods for managing App::Tel::Passwd:: modules | ||||||||||||
| 21 | |||||||||||||
| 22 | =cut | ||||||||||||
| 23 | |||||||||||||
| 24 | 12 | my $plugins = [ 'KeePass', 'PWSafe', 'Pass', 'KeyRing', 'Mock' ]; | |||||||||||
| 25 | |||||||||||||
| 26 | =head2 load_module | ||||||||||||
| 27 | |||||||||||||
| 28 | my $name = App::Tel::Passwd::load_module($password_module, $file, $passwd); | ||||||||||||
| 29 | |||||||||||||
| 30 | Loads the module for the specified password store type. Returns the module's | ||||||||||||
| 31 | namespace. | ||||||||||||
| 32 | |||||||||||||
| 33 | =cut | ||||||||||||
| 34 | |||||||||||||
| 35 | 5 | ▶ | sub load_module { | ||||||||||
|
Subroutine
load_module: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 36 | 5 | my ($type, $file, $passwd) = @_; | |||||||||||
| 37 | 12 | ▶ | no warnings 'uninitialized'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 38 | 5 | my $mod = 'App::Tel::Passwd::'. $type; | |||||||||||
| 39 | 5 | my $load = eval { | |||||||||||
| 40 | 5 | Module::Load::load $mod; | |||||||||||
| 41 | 3 | $mod->new(file => $file, passwd => $passwd); | |||||||||||
| 42 | }; | ||||||||||||
| 43 | 5 | ▶ | croak "Something went wrong with our load of passwd module $type:\n$@" if ($@); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 44 | 1 | return $load; | |||||||||||
| 45 | } | ||||||||||||
| 46 | |||||||||||||
| 47 | =head2 input_password | ||||||||||||
| 48 | |||||||||||||
| 49 | my $password = input_password($prompt); | ||||||||||||
| 50 | |||||||||||||
| 51 | Prompts the user for a password then disables local echo on STDIN, reads a | ||||||||||||
| 52 | line and returns it. | ||||||||||||
| 53 | |||||||||||||
| 54 | =cut | ||||||||||||
| 55 | |||||||||||||
| 56 | 2 | ▶ | sub input_password { | ||||||||||
|
Subroutine
input_password: called CC=5 SCAR=17.6
Pod
documented
| |||||||||||||
| 57 | 2 | my $prompt = shift; | |||||||||||
| 58 | 2 | ▶ | $prompt ||= ''; | ||||||||||
|
Condition50%1/2
$prompt ||= ""
MC/DC0%0/1
$prompt ||= ""
$prompt
Truth table50%1/2
A
A= $prompt
| |||||||||||||
| 59 | 2 | my $old_mode; | |||||||||||
| 60 | # uncoverable branch true | ||||||||||||
| 61 | 2 | ▶ | if (!$_mock) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 0 | ▶ | die 'STDIN not a tty' if (!POSIX::isatty(\*STDIN)); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | 0 | $old_mode=IO::Stty::stty(\*STDIN,'-g'); | |||||||||||
| 64 | 0 | IO::Stty::stty(\*STDIN,'-echo'); | |||||||||||
| 65 | } | ||||||||||||
| 66 | 2 | print "Enter password for $prompt: "; | |||||||||||
| 67 | 2 | my $password=<STDIN>; | |||||||||||
| 68 | |||||||||||||
| 69 | 2 | chomp($password); | |||||||||||
| 70 | # uncoverable branch true | ||||||||||||
| 71 | 2 | ▶ | if (!$_mock) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 72 | 0 | IO::Stty::stty(\*STDIN,$old_mode); | |||||||||||
| 73 | } else { | ||||||||||||
| 74 | 2 | print "\n"; | |||||||||||
| 75 | } | ||||||||||||
| 76 | 2 | return $password; | |||||||||||
| 77 | } | ||||||||||||
| 78 | |||||||||||||
| 79 | =head2 keyring | ||||||||||||
| 80 | |||||||||||||
| 81 | my $password = keyring($user, $domain, $group); | ||||||||||||
| 82 | |||||||||||||
| 83 | Reads a password from a keyring using Passwd::Keyring::Auto if it's available. | ||||||||||||
| 84 | If the password isn't found the user is prompted to enter a password, then we | ||||||||||||
| 85 | try to store it in the keyring. | ||||||||||||
| 86 | |||||||||||||
| 87 | =cut | ||||||||||||
| 88 | |||||||||||||
| 89 | 3 | ▶ | sub keyring { | ||||||||||
|
Subroutine
keyring: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 90 | 3 | my ($user, $domain, $group) = @_; | |||||||||||
| 91 | |||||||||||||
| 92 | 3 | my $p = eval { | |||||||||||
| 93 | 3 | load Passwd::Keyring::Auto, 'get_keyring'; | |||||||||||
| 94 | 3 | my $keyring = get_keyring(app=>$appname, group=>$group); | |||||||||||
| 95 | 3 | my $pass = $keyring->get_password($user, $domain); | |||||||||||
| 96 | 3 | ▶ | if (!$pass) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 97 | 2 | $pass = input_password($domain); | |||||||||||
| 98 | 2 | $keyring->set_password($user, $pass, $domain); | |||||||||||
| 99 | } | ||||||||||||
| 100 | 3 | return $pass; | |||||||||||
| 101 | }; | ||||||||||||
| 102 | |||||||||||||
| 103 | 3 | ▶ | warn $@ if ($@); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | 3 | return $p; | |||||||||||
| 105 | } | ||||||||||||
| 106 | |||||||||||||
| 107 | =head2 load_from_profile | ||||||||||||
| 108 | |||||||||||||
| 109 | my $pass = load_from_profile($profile); | ||||||||||||
| 110 | |||||||||||||
| 111 | Given an App::Tel profile, see if it contains entries for Passwd modules. If | ||||||||||||
| 112 | it does attempt to load them and return the password associated. | ||||||||||||
| 113 | |||||||||||||
| 114 | I'm not too happy with the flexibility of this, but I think it will get the | ||||||||||||
| 115 | job done for right now. | ||||||||||||
| 116 | |||||||||||||
| 117 | =cut | ||||||||||||
| 118 | |||||||||||||
| 119 | 1 | ▶ | sub load_from_profile { | ||||||||||
|
Subroutine
load_from_profile: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||
| 120 | 1 | my $profile = shift; | |||||||||||
| 121 | |||||||||||||
| 122 | 1 | foreach my $plugin (@$plugins) { | |||||||||||
| 123 | 5 | my $type = lc($plugin); | |||||||||||
| 124 | 5 | ▶ | if (defined($profile->{$type .'_file'})) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 125 | 1 | my $file = $type . '_file'; | |||||||||||
| 126 | 1 | my $entry = $type . '_entry'; | |||||||||||
| 127 | 1 | my $safe_password = $profile->{$type . '_passwd'}; | |||||||||||
| 128 | |||||||||||||
| 129 | 1 | ▶ | if ($safe_password eq 'KEYRING') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 130 | 1 | $safe_password = keyring($type,$type,$type); | |||||||||||
| 131 | } | ||||||||||||
| 132 | |||||||||||||
| 133 | 1 | my $mod = load_module $plugin, $profile->{$file}, $safe_password; | |||||||||||
| 134 | 1 | my $e = $mod->passwd($profile->{$entry}); | |||||||||||
| 135 | 1 | ▶ | return $e if $e; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 136 | } | ||||||||||||
| 137 | } | ||||||||||||
| 138 | } | ||||||||||||
| 139 | |||||||||||||