blib/lib/Database/BI/Command/password.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 112 CC 11 SCAR 29.8
CC
11
Cov
0%
run
42.0
_read_secret
30.0
_interactive
6.0
CRAP 19.6
1 package Database::BI::Command::password;
2  
3 use strict;
4 use warnings;
5  
6 our $VERSION = '0.023.1';
7  
8 use Mojo::Base 'Mojolicious::Command';
9  
10 use Carp qw(croak);
11 use Database::BI::Password qw(hash_password);
12 use Database::BI::I18N ();
13 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 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 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 my ($prompt) = @_;
36 my $tty = _interactive();
37 my ($termios, $lflag);
38 if ($tty) {
39 print STDERR $prompt;
40 require POSIX;
41 $termios = POSIX::Termios->new;
42 if ($termios->getattr(0)) {
43 $lflag = $termios->getlflag;
44 $termios->setlflag($lflag & ~(POSIX::ECHO() | POSIX::ECHONL()));
45 $termios->setattr(0, POSIX::TCSANOW());
46 }
47 }
48 my $line = <STDIN>;
49 if (defined $lflag) {
50 $termios->setlflag($lflag);
51 $termios->setattr(0, POSIX::TCSANOW());
52 print STDERR "\n";
53 }
54 return unless defined $line;
55 $line =~ s/\r?\n\z//;
56 return $line;
57 }
58  
59 sub run {
60 my ($self, @args) = @_;
61 # A person at a shell: messages in the language their locale names.
62 local $Database::BI::I18N::LANGUAGE = $Database::BI::I18N::LANGUAGE // Database::BI::I18N::from_locale();
63 croak $self->usage if @args;
64  
65 my $password = _read_secret('Password: ');
66 croak _text('error_no_password') unless defined $password && length $password;
67 if (_interactive()) {
68 my $again = _read_secret('Again: ');
69 croak _text('error_mismatch') unless defined $again && $again eq $password;
70 }
71  
72 my $hash = hash_password($password);
73 print "Put this line in database_bi.conf, then restart the server:\n" if -t STDOUT;
74 print " password_hash => '$hash',\n";
75 return;
76 }
77  
78 1;
79  
80 =encoding utf8
81  
82 =head1 NAME
83  
84 Database::BI::Command::password - Make the password_hash for the login
85  
86 =head1 VERSION
87  
88 0.023.1
89  
90 =head1 SYNOPSIS
91  
92 Usage: script/database-bi password
93  
94 script/database-bi password # asks twice, without echo
95 printf '%s\n' "$PW" | script/database-bi password # from a script
96  
97 =head1 DESCRIPTION
98  
99 Asks for a password and prints the C<password_hash> line for
100 F<database_bi.conf>. With that line in place, every page asks for the
101 password (see L<Database::BI/Logging in>). Only the hash is printed; the
102 password is not stored anywhere.
103  
104 =head1 METHODS
105  
106 =head2 run
107  
108 =head3 API SPECIFICATION
109  
110 =head4 INPUT
111  
112 @args: none
113  
114 =head4 OUTPUT
115  
116 Prints C<< password_hash => '...', >> on STDOUT. Croaks with the usage when
117 given arguments, and with a message from C<%Database::BI::Command::password::MESSAGES>
118 when no password is given or when the two typed at a terminal differ.
119  
120 =head1 AUTHOR
121  
122 Nigel Horne C<< <njh@nigelhorne.com> >>
123  
124 =head1 LICENCE AND COPYRIGHT
125  
126 Copyright 2026 Nigel Horne.
127  
128 Usage is subject to the GPL2 licence terms.
129  
130 =cut