| File: | blib/lib/Database/BI/Command/password.pm |
| Coverage: | 80.9% |
| line | stmt | bran | cond | sub | time | code |
|---|---|---|---|---|---|---|
| 1 | package Database::BI::Command::password; | |||||
| 2 | ||||||
| 3 | 5 5 5 | 7018 4 39 | use strict; | |||
| 4 | 5 5 5 | 5 3 128 | use warnings; | |||
| 5 | ||||||
| 6 | our $VERSION = '0.025.0'; | |||||
| 7 | ||||||
| 8 | 5 5 5 | 6 4 9 | use Mojo::Base 'Mojolicious::Command'; | |||
| 9 | ||||||
| 10 | 5 5 5 | 507 4 85 | use Carp qw(croak); | |||
| 11 | 5 5 5 | 320 5 86 | use Database::BI::Password qw(hash_password); | |||
| 12 | 5 5 5 | 5 4 21 | use Database::BI::I18N (); | |||
| 13 | 5 5 5 | 6 2 1056 | use Readonly; | |||
| 14 | ||||||
| 15 | # What the command stops with. Each ends in a newline, so croak prints it as | |||||
| 16 | # it is, without the line it was raised at: they are for a person at a shell. | |||||
| 17 | Readonly our %MESSAGES => ( | |||||
| 18 | error_no_password => "password: no password given\n", | |||||
| 19 | error_mismatch => "password: the two did not match\n", | |||||
| 20 | ); | |||||
| 21 | ||||||
| 22 | # _text($key) -> the template for $key in the language the locale names. | |||||
| 23 | 5 | 9 | sub _text { return Database::BI::I18N::text(__PACKAGE__, \%MESSAGES, $_[0]) } | |||
| 24 | ||||||
| 25 | has description => 'Make the password_hash line that turns on the login'; | |||||
| 26 | has usage => sub { shift->extract_usage }; | |||||
| 27 | ||||||
| 28 | # _interactive() -> bool: whether a person is typing (STDIN is a terminal), | |||||
| 29 | # who is asked twice. A sub of its own so a test can be that person. | |||||
| 30 | 4 | 15 | sub _interactive { return -t STDIN ? 1 : 0 } | |||
| 31 | ||||||
| 32 | # Reads one line from STDIN. At a terminal the echo is turned off, so the | |||||
| 33 | # password is not left on the screen or in a recording of it. | |||||
| 34 | sub _read_secret { | |||||
| 35 | 3 | 4 | my ($prompt) = @_; | |||
| 36 | 3 | 5 | my $tty = _interactive(); | |||
| 37 | 3 | 4 | my ($termios, $lflag); | |||
| 38 | 3 | 5 | if ($tty) { | |||
| 39 | 0 | 0 | print STDERR $prompt; | |||
| 40 | 0 | 0 | require POSIX; | |||
| 41 | 0 | 0 | $termios = POSIX::Termios->new; | |||
| 42 | 0 | 0 | if ($termios->getattr(0)) { | |||
| 43 | 0 | 0 | $lflag = $termios->getlflag; | |||
| 44 | 0 | 0 | $termios->setlflag($lflag & ~(POSIX::ECHO() | POSIX::ECHONL())); | |||
| 45 | 0 | 0 | $termios->setattr(0, POSIX::TCSANOW()); | |||
| 46 | } | |||||
| 47 | } | |||||
| 48 | 3 | 17 | my $line = <STDIN>; | |||
| 49 | 3 | 6 | if (defined $lflag) { | |||
| 50 | 0 | 0 | $termios->setlflag($lflag); | |||
| 51 | 0 | 0 | $termios->setattr(0, POSIX::TCSANOW()); | |||
| 52 | 0 | 0 | print STDERR "\n"; | |||
| 53 | } | |||||
| 54 | 3 | 5 | return unless defined $line; | |||
| 55 | 1 | 3 | $line =~ s/\r?\n\z//; | |||
| 56 | 1 | 1 | return $line; | |||
| 57 | } | |||||
| 58 | ||||||
| 59 | sub run { | |||||
| 60 | 10 | 17986 | my ($self, @args) = @_; | |||
| 61 | # A person at a shell: messages in the language their locale names. | |||||
| 62 | 10 | 33 | local $Database::BI::I18N::LANGUAGE = $Database::BI::I18N::LANGUAGE // Database::BI::I18N::from_locale(); | |||
| 63 | 10 | 68 | croak $self->usage if @args; | |||
| 64 | ||||||
| 65 | 8 | 14 | my $password = _read_secret('Password: '); | |||
| 66 | 8 | 65 | croak _text('error_no_password') unless defined $password && length $password; | |||
| 67 | 5 | 6 | if (_interactive()) { | |||
| 68 | 3 | 4 | my $again = _read_secret('Again: '); | |||
| 69 | 3 | 7 | croak _text('error_mismatch') unless defined $again && $again eq $password; | |||
| 70 | } | |||||
| 71 | ||||||
| 72 | 3 | 10 | my $hash = hash_password($password); | |||
| 73 | 3 | 40 | print "Put this line in database_bi.conf, then restart the server:\n" if -t STDOUT; | |||
| 74 | 3 | 12 | print " password_hash => '$hash',\n"; | |||
| 75 | 3 | 179 | return; | |||
| 76 | } | |||||
| 77 | ||||||
| 78 | 1; | |||||
| 79 | ||||||
| 80 | =encoding utf8 | |||||
| 81 | ||||||
| 82 - 130 | =head1 NAME Database::BI::Command::password - Make the password_hash for the login =head1 VERSION 0.025.0 =head1 SYNOPSIS Usage: script/database-bi password script/database-bi password # asks twice, without echo printf '%s\n' "$PW" | script/database-bi password # from a script =head1 DESCRIPTION Asks for a password and prints the C<password_hash> line for F<database_bi.conf>. With that line in place, every page asks for the password (see L<Database::BI/Logging in>). Only the hash is printed; the password is not stored anywhere. =head1 METHODS =head2 run =head3 API SPECIFICATION =head4 INPUT @args: none =head4 OUTPUT Prints C<< password_hash => '...', >> on STDOUT. Croaks with the usage when given arguments, and with a message from C<%Database::BI::Command::password::MESSAGES> when no password is given or when the two typed at a terminal differ. =head1 AUTHOR Nigel Horne C<< <njh@nigelhorne.com> >> =head1 LICENCE AND COPYRIGHT Copyright 2026 Nigel Horne. Usage is subject to the GPL2 licence terms. =cut | |||||