File Coverage

lib/Term/RouterCLI/Auth.pm
Criterion Covered Total %
statement 45 89 50.5
branch 5 18 27.7
condition 7 24 29.1
subroutine 11 14 78.5
pod 0 5 0.0
total 68 150 45.3


line stmt bran cond sub pod time code
1             #####################################################################
2             # This program is not guaranteed to work at all, and by using this #
3             # program you release the author of any and all liability. #
4             # #
5             # You may use this code as long as you are in compliance with the #
6             # license (see the LICENSE file) and this notice, disclaimer and #
7             # comment box remain intact and unchanged. #
8             # #
9             # Package: Term::RouterCLI #
10             # Class: Auth #
11             # Description: Methods for building a Router (Stanford) style CLI #
12             # #
13             # Written by: Bret Jordan (jordan at open1x littledot org) #
14             # Created: 2011-04-27 #
15             #####################################################################
16             #
17             #
18             #
19             #
20             package Term::RouterCLI::Auth;
21              
22 5     5   2451 use 5.8.8;
  5         17  
  5         241  
23 5     5   27 use strict;
  5         8  
  5         204  
24 5     5   33 use warnings;
  5         17  
  5         160  
25 5     5   451 use Term::RouterCLI::Debugger;
  5         10  
  5         173  
26 5     5   29 use Log::Log4perl;
  5         13  
  5         33  
27 5     5   5981 use Term::ReadKey;
  5         40134  
  5         570  
28 5     5   8922 use Digest::SHA qw(hmac_sha512_hex);
  5         45124  
  5         10583  
29              
30             our $VERSION = '1.00';
31             $VERSION = eval $VERSION;
32              
33              
34             my $oDebugger = new Term::RouterCLI::Debugger();
35              
36              
37             sub new
38             {
39 1     1 0 15 my $pkg = shift;
40 1   33     8 my $class = ref($pkg) || $pkg;
41              
42 1         3 my $self = {};
43 1         2 $self->{'_sName'} = $pkg; # Lets set the object name so we can use it in debugging
44 1         4 bless ($self, $class);
45            
46             # Lets send any passed in arguments to the _init method
47 1         6 $self->_init(@_);
48 1         4 return $self;
49             }
50              
51             sub _init
52             {
53 1     1   2 my $self = shift;
54 1         3 my %hParameters = @_;
55              
56             # Lets overwrite any defaults with values that are passed in
57 1 50       4 if (%hParameters)
58             {
59 0         0 foreach (keys (%hParameters)) { $self->{$_} = $hParameters{$_}; }
  0         0  
60             }
61             }
62              
63             sub DESTROY
64             {
65 1     1   414 my $self = shift;
66 1         113 $self = {};
67             }
68              
69              
70              
71             # ----------------------------------------
72             # Public Methods
73             # ----------------------------------------
74             sub PromptForUsername
75             {
76             # This method will prompt for the username to be entered on the command line
77             # Return:
78             # string_ref (password entered)
79 0     0 0 0 my $self = shift;
80 0         0 my $logger = $oDebugger->GetLogger($self);
81 0         0 my $sUsername = "";
82              
83 0         0 $logger->debug("$self->{'_sName'} - ", '### Entering Method ###');
84            
85 0         0 while(ord(my $key = ReadKey(0)) != 10)
86             {
87             # This will continue until the Enter key is pressed (decimal value of 10)
88             # For all value of ord($key) see http://www.asciitable.com/
89 0 0 0     0 if (ord($key) == 127 || ord($key) == 8)
    0 0        
90             {
91             # DEL/Backspace was pressed
92             # Lets not allow backspace or del if there is not password characters to delete
93 0 0       0 unless ($sUsername eq "")
94             {
95             #1. Remove the last char from the password
96 0         0 chop($sUsername);
97             #2 move the cursor back by one, print a blank character, move the cursor back by one
98 0         0 print "\b \b";
99             }
100             }
101             elsif (ord($key) <= 32 || ord($key) > 127)
102             {
103             # Do nothing with these control characters
104             }
105              
106 0         0 else { $sUsername = $sUsername.$key; }
107             }
108 0         0 $logger->debug("$self->{'_sName'} - ", '### Leaving Method ###');
109 0         0 return \$sUsername;
110             }
111              
112             sub PromptForPassword
113             {
114             # This method will prompt for the password to be entered on the command line
115             # Return:
116             # string_ref (password entered)
117 0     0 0 0 my $self = shift;
118 0         0 my $logger = $oDebugger->GetLogger($self);
119 0         0 my $sPassword = "";
120            
121 0         0 $logger->debug("$self->{'_sName'} - ", '### Entering Method ###');
122            
123             # The following will hide all typeing, the while statement below will print * characters
124 0         0 ReadMode(4);
125 0         0 while(ord(my $key = ReadKey(0)) != 10)
126             {
127             # This will continue until the Enter key is pressed (decimal value of 10)
128             # For all value of ord($key) see http://www.asciitable.com/
129 0 0 0     0 if (ord($key) == 127 || ord($key) == 8)
    0 0        
130             {
131             # DEL/Backspace was pressed
132             # Lets not allow backspace or del if there is not password characters to delete
133 0 0       0 unless ($sPassword eq "")
134             {
135             #1. Remove the last char from the password
136 0         0 chop($sPassword);
137             #2 move the cursor back by one, print a blank character, move the cursor back by one
138 0         0 print "\b \b";
139             }
140             }
141             elsif (ord($key) <= 32 || ord($key) > 127)
142             {
143             # Do nothing with these control characters
144             }
145              
146             else
147             {
148 0         0 $sPassword = $sPassword.$key;
149 0         0 print "*";
150             }
151             }
152             # Reset the terminal
153 0         0 ReadMode(0);
154             # Since the Term::ReadKey method above strips out the carriage return, lets add it back
155 0         0 print "\n";
156            
157 0         0 $logger->debug("$self->{'_sName'} - ", '### Leaving Method ###');
158 0         0 return \$sPassword;
159             }
160              
161             sub EncryptPassword
162             {
163             # This method will encrypt a password with some salt
164             # Required:
165             # int_ref (type)
166             # string_ref (password)
167             # string_ref (salt)
168             # Return:
169             # string_ref (encrypted password)
170 3     3 0 1362 my $self = shift;
171 3         4 my $iCryptIDType = shift;
172 3         25 my $sPassword = shift;
173 3         4 my $sSalt = shift;
174 3         14 my $logger = $oDebugger->GetLogger($self);
175 3         406 my $sCryptPassword = "";
176              
177 3         16 $logger->debug("$self->{'_sName'} - ", '### Entering Method ###');
178              
179             # If Crypt ID Type == 0, then there is no encryption
180 3 100 66     72 if ( $$iCryptIDType == 0 && defined $$sPassword) { $sCryptPassword = $$sPassword; }
  1 100 66     2  
      66        
181 1         29 elsif ( $$iCryptIDType == 6 && defined $$sPassword && defined $$sSalt) { $sCryptPassword = hmac_sha512_hex($$sPassword, $$sSalt); }
182              
183 3         12 $logger->debug("$self->{'_sName'} - ", '### Leaving Method ###');
184 3         23 return \$sCryptPassword;
185             }
186              
187             sub SplitPasswordString
188             {
189             # This method will split a password string of $id$salt$password in to the relevant parts
190             # Required
191             # string_ref (password string)
192             # Return:
193             # int_ref (crypt id)
194             # string_ref (salt)
195             # string_ref (password)
196 0     0 0   my $self = shift;
197 0           my $sPasswordString = shift;
198 0           my $logger = $oDebugger->GetLogger($self);
199 0           my $iID;
200 0           my $sSalt = "";
201 0           my $sPassword = "";
202              
203 0           $logger->debug("$self->{'_sName'} - ", '### Entering Method ###');
204 0           $logger->debug("$self->{'_sName'} - sPasswordString: $$sPasswordString");
205              
206             # Split key from password
207 0           ($iID, $sSalt, $sPassword) = (split /\$/, $$sPasswordString)[1..3];
208              
209              
210 0           $logger->debug("$self->{'_sName'} - iID: $iID");
211 0           $logger->debug("$self->{'_sName'} - sSalt: $sSalt");
212 0           $logger->debug("$self->{'_sName'} - sPassword: $sPassword");
213            
214 0           $logger->debug("$self->{'_sName'} - ", '### Leaving Method ###');
215 0           return (\$iID, \$sSalt, \$sPassword);
216             }
217              
218              
219              
220             return 1;