| 1 | package App::OATH; | ||
| 2 | our $VERSION = '1.20260324'; # VERSION | ||
| 3 | |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | ▶ | use Convert::Base32; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Digest::HMAC_SHA1 qw(hmac_sha1); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use English qw{ -no_match_vars }; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use Fcntl ':flock'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use File::HomeDir qw{ my_home }; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use JSON; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 1 | ▶ | use POSIX; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 1 | ▶ | use URL::Encode qw{ url_encode }; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 0 | ▶ | use Text::QRCode; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 16 | use Term::ANSIColor; | ||
| 17 | |||
| 18 | use App::OATH::Crypt; | ||
| 19 | |||
| 20 | if ( $OSNAME eq 'MSWin32' ) { # uncoverable statement | ||
| 21 | require Term::ReadPassword::Win32; # uncoverable statement | ||
| 22 | } # uncoverable statement | ||
| 23 | else { # uncoverable statement | ||
| 24 | require Term::ReadPassword; # uncoverable statement | ||
| 25 | } # uncoverable statement | ||
| 26 | |||
| 27 | sub new { | ||
| 28 | my ( $class ) = @_; | ||
| 29 | my $self = { | ||
| 30 | 'filename' => my_home() . '/.oath.json', | ||
| 31 | }; | ||
| 32 | bless $self, $class; | ||
| 33 | return $self; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub usage { | ||
| 37 | my ( $self ) = @_; | ||
| 38 | print "usage: $0 --add string --file filename --help --init --list --newpass --raw --rawqr --search string \n\n"; | ||
| 39 | print "options:\n\n"; | ||
| 40 | print "--add string\n"; | ||
| 41 | print " add a new password to the database, the format can be one of the following\n"; | ||
| 42 | print " text: identifier:secret\n"; | ||
| 43 | print " url: otpauth://totp/alice\@google.com?secret=JBSWY3DPEHPK3PXP\n\n"; | ||
| 44 | print "--file filename\n"; | ||
| 45 | print " filename for database, default ~/.oath.json\n\n"; | ||
| 46 | print "--help\n"; | ||
| 47 | print " show this help\n\n"; | ||
| 48 | print "--init\n"; | ||
| 49 | print " initialise the database, file must not exist\n\n"; | ||
| 50 | print "--list\n"; | ||
| 51 | print " list keys in database\n\n"; | ||
| 52 | print "--newpass\n"; | ||
| 53 | print " resave database with a new password\n\n"; | ||
| 54 | print "--raw\n"; | ||
| 55 | print " show the raw oath code (useful for migration to another device)\n\n"; | ||
| 56 | print "--rawqr\n"; | ||
| 57 | print " show the raw oath code as an importable qr code (useful for migration to another device)\n\n"; | ||
| 58 | print "--search string\n"; | ||
| 59 | print " search database for keys matching string\n\n"; | ||
| 60 | exit 0; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub set_raw { | ||
| 64 | my ( $self ) = @_; | ||
| 65 | $self->{'raw'} = 1; | ||
| 66 | return; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub set_rawqr { | ||
| 70 | my ( $self ) = @_; | ||
| 71 | $self->{'rawqr'} = 1; | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub set_search { | ||
| 76 | my ( $self, $search ) = @_; | ||
| 77 | $self->{'search'} = $search; | ||
| 78 | return; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub get_search { | ||
| 82 | my ( $self ) = @_; | ||
| 83 | return $self->{'search'}; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub init { | ||
| 87 | my ( $self ) = @_; | ||
| 88 | my $filename = $self->get_filename(); | ||
| 89 | if ( -e $filename ) { | ||
| 90 | print "Error: file already exists\n"; | ||
| 91 | exit 1; | ||
| 92 | } | ||
| 93 | $self->{ 'data_plaintext' } = {}; | ||
| 94 | $self->encrypt_data(); | ||
| 95 | $self->save_data(); | ||
| 96 | return; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub add_entry { | ||
| 100 | my ( $self, $entry ) = @_; | ||
| 101 | my $search = $self->get_search(); | ||
| 102 | my $data = $self->get_plaintext(); | ||
| 103 | |||
| 104 | if ( $entry =~ /^otpauth:\/\/totp\// ) { | ||
| 105 | # Better parsing required | ||
| 106 | my ( $key, $rest ) = $entry =~ /^otpauth:\/\/totp\/(.*)\?(.*)$/; | ||
| 107 | my ( $value ) = $rest =~ /secret=([^&]*)/; | ||
| 108 | if ( exists( $data->{$key} ) ) { | ||
| 109 | print "Error: Key already exists\n"; | ||
| 110 | exit 1; | ||
| 111 | } | ||
| 112 | else { | ||
| 113 | print "Adding OTP for $key\n"; | ||
| 114 | $self->{'data_plaintext'}->{$key} = $value; | ||
| 115 | } | ||
| 116 | |||
| 117 | } | ||
| 118 | elsif ( $entry =~ /^[^:]+:[^:]+$/ ) { | ||
| 119 | my ( $key, $value ) = $entry =~ /^([^:]+):([^:]+)$/; | ||
| 120 | if ( exists( $data->{$key} ) ) { | ||
| 121 | print "Error: Key already exists\n"; | ||
| 122 | exit 1; | ||
| 123 | } | ||
| 124 | else { | ||
| 125 | print "Adding OTP for $key\n"; | ||
| 126 | $self->{'data_plaintext'}->{$key} = $value; | ||
| 127 | } | ||
| 128 | |||
| 129 | } | ||
| 130 | else { | ||
| 131 | print "Error: Unknown format\n"; | ||
| 132 | exit 1; | ||
| 133 | } | ||
| 134 | |||
| 135 | $self->encrypt_data(); | ||
| 136 | $self->save_data(); | ||
| 137 | |||
| 138 | return; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub list_keys { | ||
| 142 | my ( $self ) = @_; | ||
| 143 | my $search = $self->get_search(); | ||
| 144 | my $data = $self->get_encrypted(); | ||
| 145 | |||
| 146 | my $counter = int( time() / 30 ); | ||
| 147 | |||
| 148 | foreach my $account ( sort keys %$data ) { | ||
| 149 | if ( $search ) { | ||
| 150 | next if ( index( lc $account, lc $search ) == -1 ); | ||
| 151 | } | ||
| 152 | print "$account\n"; | ||
| 153 | } | ||
| 154 | |||
| 155 | print "\n"; | ||
| 156 | return; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub get_counter { | ||
| 160 | my ( $self ) = @_; | ||
| 161 | my $counter = int( time() / 30 ); | ||
| 162 | return $counter; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub display_codes { | ||
| 166 | my ( $self ) = @_; | ||
| 167 | my $search = $self->get_search(); | ||
| 168 | my $data = $self->get_plaintext(); | ||
| 169 | my $counter = $self->get_counter(); | ||
| 170 | |||
| 171 | my $max_len = 0; | ||
| 172 | |||
| 173 | foreach my $account ( sort keys %$data ) { | ||
| 174 | if ( $search ) { | ||
| 175 | next if ( index( lc $account, lc $search ) == -1 ); | ||
| 176 | } | ||
| 177 | $max_len = length( $account ) if length $account > $max_len; | ||
| 178 | } | ||
| 179 | |||
| 180 | print "\n"; | ||
| 181 | foreach my $account ( sort keys %$data ) { | ||
| 182 | if ( $search ) { | ||
| 183 | next if ( index( lc $account, lc $search ) == -1 ); | ||
| 184 | } | ||
| 185 | my $secret = uc $data->{ $account }; | ||
| 186 | if ( $self->{'raw'} ) { | ||
| 187 | printf( '%*3$s : %s' . "\n", $account, $secret, $max_len ); | ||
| 188 | } | ||
| 189 | elsif ( $self->{'rawqr'} ) { | ||
| 190 | my $account_enc = url_encode( $account ); | ||
| 191 | my $url = 'otpauth://totp/' . $account_enc . '?secret=' . $secret; | ||
| 192 | my $qrcode = $self->make_qr( $url ); | ||
| 193 | printf( "%s\n%s\n%s", $account, $qrcode, $url ); | ||
| 194 | } | ||
| 195 | else { | ||
| 196 | printf( '%*3$s : %s' . "\n", $account, $self->oath_auth( $secret, $counter ), $max_len ); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | print "\n"; | ||
| 200 | return; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub make_qr { | ||
| 204 | my ( $self, $string ) = @_; | ||
| 205 | my $qr = Text::QRCode->new(); | ||
| 206 | my $code = $qr->plot( $string ); | ||
| 207 | my $qrcode = q{}; | ||
| 208 | my $width = scalar @{$code->[0]}; | ||
| 209 | |||
| 210 | my $pad = q{}; | ||
| 211 | $pad .= color('white on_white'); | ||
| 212 | for( my $i=0 ;$i<$width+4;$i++ ) { | ||
| 213 | $pad .= color('white on_white') . ' '; | ||
| 214 | } | ||
| 215 | $pad .= color('reset'); | ||
| 216 | $pad .= "\n"; | ||
| 217 | |||
| 218 | $qrcode .= $pad .$pad; | ||
| 219 | |||
| 220 | foreach my $row ( @$code ) { | ||
| 221 | $qrcode .= color('white on_white') . ' '; | ||
| 222 | foreach my $col ( @$row ) { | ||
| 223 | if ( $col eq ' ' ) { | ||
| 224 | $qrcode .= color('white on_white'); | ||
| 225 | } | ||
| 226 | else { | ||
| 227 | $qrcode .= color('black on_black'); | ||
| 228 | } | ||
| 229 | $qrcode .= $col . $col; | ||
| 230 | $qrcode .= color('reset'); | ||
| 231 | } | ||
| 232 | $qrcode .= color('white on_white') . ' '; | ||
| 233 | $qrcode .= color('reset'); | ||
| 234 | $qrcode .= "\n"; | ||
| 235 | } | ||
| 236 | |||
| 237 | $qrcode .= $pad .$pad; | ||
| 238 | |||
| 239 | return $qrcode; | ||
| 240 | } | ||
| 241 | |||
| 242 | sub oath_auth { | ||
| 243 | my ( $self, $key, $tm ) = @_; | ||
| 244 | |||
| 245 | my @chal; | ||
| 246 | for (my $i=7;$i;$i--) { | ||
| 247 | $chal[$i] = $tm & 0xFF; | ||
| 248 | $tm >>= 8; | ||
| 249 | } | ||
| 250 | |||
| 251 | my $challenge; | ||
| 252 | { | ||
| 253 | no warnings; | ||
| 254 | $challenge = pack('C*',@chal); | ||
| 255 | } | ||
| 256 | |||
| 257 | my $secret = decode_base32($key); | ||
| 258 | |||
| 259 | my $hashtxt = hmac_sha1($challenge,$secret); | ||
| 260 | my @hash = unpack("C*",$hashtxt); | ||
| 261 | my $offset = $hash[$#hash]& 0xf ; | ||
| 262 | |||
| 263 | my $truncatedHash=0; | ||
| 264 | for (my $i=0;$i<4;$i++) { | ||
| 265 | $truncatedHash <<=8; | ||
| 266 | $truncatedHash |= $hash[$offset+$i]; | ||
| 267 | } | ||
| 268 | $truncatedHash &=0x7fffffff; | ||
| 269 | $truncatedHash %= 1000000; | ||
| 270 | $truncatedHash = substr( '0'x6 . $truncatedHash, -6 ); | ||
| 271 | |||
| 272 | return $truncatedHash; | ||
| 273 | } | ||
| 274 | |||
| 275 | sub set_filename { | ||
| 276 | my ( $self, $filename ) = @_; | ||
| 277 | $self->drop_lock() if $self->{'filename'} ne $filename; | ||
| 278 | $self->{'filename'} = $filename; | ||
| 279 | return; | ||
| 280 | } | ||
| 281 | |||
| 282 | sub get_filename { | ||
| 283 | my ( $self ) = @_; | ||
| 284 | return $self->{'filename'}; | ||
| 285 | } | ||
| 286 | |||
| 287 | sub get_lockfilename { | ||
| 288 | my ( $self ) = @_; | ||
| 289 | my $filename = $self->get_filename(); | ||
| 290 | my $lockfilename = $filename . '.lock'; | ||
| 291 | return $lockfilename; | ||
| 292 | } | ||
| 293 | |||
| 294 | sub drop_lock { | ||
| 295 | my ( $self ) = @_; | ||
| 296 | delete $self->{'lockhandle'}; | ||
| 297 | return; | ||
| 298 | } | ||
| 299 | |||
| 300 | sub get_lock { | ||
| 301 | my ( $self ) = @_; | ||
| 302 | |||
| 303 | my $lockh; | ||
| 304 | my $lockfilename = $self->get_lockfilename(); | ||
| 305 | if ( ! -e $lockfilename ) { | ||
| 306 | open $lockh, '>', $lockfilename; | ||
| 307 | close $lockh; | ||
| 308 | chmod( 0600, $lockfilename ); | ||
| 309 | } | ||
| 310 | open $lockh, '<', $lockfilename; | ||
| 311 | if ( !flock( $lockh, LOCK_EX | LOCK_NB ) ) { | ||
| 312 | return 0; | ||
| 313 | } | ||
| 314 | $self->{'lockhandle'} = $lockh; | ||
| 315 | return 1; | ||
| 316 | } | ||
| 317 | |||
| 318 | sub load_data { | ||
| 319 | my ( $self ) = @_; | ||
| 320 | my $json = JSON->new(); | ||
| 321 | my $filename = $self->get_filename(); | ||
| 322 | open( my $file, '<', $filename ) || die "cannot open file $!"; | ||
| 323 | my @content = <$file>; | ||
| 324 | close $file; | ||
| 325 | my $data = $json->decode( join( "\n", @content ) ); | ||
| 326 | $self->{'data_encrypted'} = $data; | ||
| 327 | return; | ||
| 328 | } | ||
| 329 | |||
| 330 | sub save_data { | ||
| 331 | my ( $self ) = @_; | ||
| 332 | my $data = $self->get_encrypted(); | ||
| 333 | my $json = JSON->new->canonical->pretty; | ||
| 334 | my $content = $json->encode( $data ); | ||
| 335 | my $filename = $self->get_filename(); | ||
| 336 | open( my $file, '>', $filename ) || die "cannot open file $!"; | ||
| 337 | print $file $content; | ||
| 338 | close $file; | ||
| 339 | chmod( 0600, $filename ); | ||
| 340 | return; | ||
| 341 | } | ||
| 342 | |||
| 343 | sub encrypt_data { | ||
| 344 | my ( $self ) = @_; | ||
| 345 | my $newpass = $self->{newpass}; | ||
| 346 | my $data = $self->get_plaintext(); | ||
| 347 | $self->drop_password() if $newpass; | ||
| 348 | my $crypt = App::OATH::Crypt->new( $self->get_password() ); | ||
| 349 | my $edata = {}; | ||
| 350 | foreach my $k ( keys %$data ) { | ||
| 351 | $edata->{$k} = $crypt->encrypt( $data->{$k} ); | ||
| 352 | } | ||
| 353 | $self->{'data_encrypted'} = $edata; | ||
| 354 | return; | ||
| 355 | } | ||
| 356 | |||
| 357 | sub decrypt_data { | ||
| 358 | my ( $self ) = @_; | ||
| 359 | my $data = $self->get_encrypted(); | ||
| 360 | my $crypt = App::OATH::Crypt->new( $self->get_password() ); | ||
| 361 | my $ddata = {}; | ||
| 362 | foreach my $k ( keys %$data ) { | ||
| 363 | my $d = $crypt->decrypt( $data->{$k} ); | ||
| 364 | if ( ! $d ) { | ||
| 365 | print "Invalid password\n"; | ||
| 366 | exit 1; | ||
| 367 | } | ||
| 368 | $ddata->{$k} = $d; | ||
| 369 | } | ||
| 370 | $self->{'data_plaintext'} = $ddata; | ||
| 371 | return; | ||
| 372 | } | ||
| 373 | |||
| 374 | sub get_plaintext { | ||
| 375 | my ( $self ) = @_; | ||
| 376 | $self->decrypt_data() if ! exists $self->{'data_plaintext'}; | ||
| 377 | return $self->{'data_plaintext'}; | ||
| 378 | } | ||
| 379 | |||
| 380 | sub get_encrypted { | ||
| 381 | my ( $self ) = @_; | ||
| 382 | $self->load_data() if ! exists $self->{'data_encrypted'}; | ||
| 383 | return $self->{'data_encrypted'}; | ||
| 384 | } | ||
| 385 | |||
| 386 | sub set_newpass { | ||
| 387 | my ( $self ) = @_; | ||
| 388 | $self->{'newpass'} = 1; | ||
| 389 | return; | ||
| 390 | } | ||
| 391 | |||
| 392 | sub drop_password { | ||
| 393 | my ( $self ) = @_; | ||
| 394 | delete $self->{'password'}; | ||
| 395 | return; | ||
| 396 | } | ||
| 397 | |||
| 398 | sub _read_password_stdin { | ||
| 399 | # uncoverable subroutine | ||
| 400 | # Cannot be covered as we do not yet | ||
| 401 | # have a reliable method of faking this | ||
| 402 | # input. | ||
| 403 | # This is fine as we are simply acting as | ||
| 404 | # a wrapper around Term::ReadPassword | ||
| 405 | # NB, Term::ReadPassword is not Win32 safe | ||
| 406 | my ( $self ) = @_; # uncoverable statement | ||
| 407 | my $password; # uncoverable statement | ||
| 408 | if ( $OSNAME eq 'MSWin32' ) { # uncoverable statement | ||
| 409 | $password = Term::ReadPassword::Win32::read_password('Password:'); # uncoverable statement | ||
| 410 | } # uncoverable statement | ||
| 411 | else { # uncoverable statement | ||
| 412 | $password = Term::ReadPassword::read_password('Password:'); # uncoverable statement | ||
| 413 | } # uncoverable statement | ||
| 414 | return $password; # uncoverable statement | ||
| 415 | } | ||
| 416 | |||
| 417 | sub get_password { | ||
| 418 | my ( $self ) = @_; | ||
| 419 | return $self->{'password'} if $self->{'password'}; | ||
| 420 | my $password = $self->_read_password_stdin(); | ||
| 421 | $self->{'password'} = $password; | ||
| 422 | delete $self->{'newpass'}; | ||
| 423 | return $password; | ||
| 424 | } | ||
| 425 | |||
| 426 | 1; | ||
| 427 | |||
| 428 | # ABSTRACT: Simple OATH authenticator | ||
| 429 | __END__ | ||