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

filter: Statementstmt 66.6% 40 / 60 Branchbran 45.0% 9 / 20 Conditioncond 66.6% 6 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 83.3% 10 / 12 Podpod 100% 1 / 1 totaltotal 63.8% 69 / 108 CC 14 SCAR 23.2
CC
14
Cov
62%
_read_secret
30.0
run
9.2
_interactive
6.0
CRAP 10.1
« blib/lib/Database/BI/Calc.pm ↑ Summary blib/lib/Database/BI/Ask.pm »
1 package Database::BI::Command::password;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.024.0';
7  
8 1 ▶use Mojo::Base 'Mojolicious::Command';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Database::BI::Password qw(hash_password);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Database::BI::I18N ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 1 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 3 ▶sub _text { return Database::BI::I18N::text(__PACKAGE__, \%MESSAGES, $_[0]) }
Subroutine _text: called CC=1 SCAR=0.0
24  
25 1 has description => 'Make the password_hash line that turns on the login';
26 1 ▶has usage => sub { shift->extract_usage };
Subroutine __ANON__: called CC=1 SCAR=0.0
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 0 ▶sub _interactive { return -t STDIN ? 1 : 0 }
Subroutine _interactive: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
-t STDIN ? : 0 0 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 0 ▶sub _read_secret {
Subroutine _read_secret: not called CC=5 SCAR=34.0
35 0 my ($prompt) = @_;
36 0 my $tty = _interactive();
37 0 my ($termios, $lflag);
38 0 ▶ if ($tty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tty) 0 0 0
39 0 print STDERR $prompt;
40 0 require POSIX;
41 0 $termios = POSIX::Termios->new;
42 0 ▶ if ($termios->getattr(0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($termios->getattr(0)) 0 0 0
43 0 $lflag = $termios->getlflag;
44 0 $termios->setlflag($lflag & ~(POSIX::ECHO() | POSIX::ECHONL()));
45 0 $termios->setattr(0, POSIX::TCSANOW());
46 }
47 }
48 0 my $line = <STDIN>;
49 0 ▶ if (defined $lflag) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lflag) 0 0 0
50 0 $termios->setlflag($lflag);
51 0 $termios->setattr(0, POSIX::TCSANOW());
52 0 print STDERR "\n";
53 }
54 0 ▶ return unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 0 0
55 0 $line =~ s/\r?\n\z//;
56 0 return $line;
57 }
58  
59 6 ▶sub run {
Subroutine run: called CC=9 SCAR=22.2
Pod documented
60 6 my ($self, @args) = @_;
61 # A person at a shell: messages in the language their locale names.
62 6 ▶ local $Database::BI::I18N::LANGUAGE = $Database::BI::I18N::LANGUAGE // Database::BI::I18N::from_locale();
Condition33%1/3
$Database::BI::I18N::LANGUAGE // Database::BI::I18N::from_locale()
l!l&&r!l&&!r
060
MC/DC0%0/2
$Database::BI::I18N::LANGUAGE // Database::BI::I18N::from_locale()
$Database::BI::I18N::LANGUAGE Database::BI::I18N::from_locale()
Truth table33%1/3
A // B
A= $Database::BI::I18N::LANGUAGEB= Database::BI::I18N::from_locale()
ABresult
1X1
011
000
63 6 ▶ croak $self->usage if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 1 5 6
64  
65 5 my $password = _read_secret('Password: ');
66 5 ▶ croak _text('error_no_password') unless defined $password && length $password;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $password and length $password 1 4 5
Condition66%2/3
defined $password and length $password
!ll&&!rl&&r
014
MC/DC50%1/2
defined $password and length $password
defined $password length $password
Truth table66%2/3
A and B
A= defined $passwordB= length $password
ABresult
0X0
100
111
67 4 ▶ if (_interactive()) {
Branch: 1 branch
BranchTrueFalseTotal
if (_interactive()) 3 1 4
68 3 my $again = _read_secret('Again: ');
69 3 ▶ croak _text('error_mismatch') unless defined $again && $again eq $password;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $again and $again eq $password 2 1 3
Condition100%3/3
defined $again and $again eq $password
!ll&&!rl&&r
111
MC/DC100%2/2
defined $again and $again eq $password
defined $again $again eq $password
Truth table100%3/3
A and B
A= defined $againB= $again eq $password
ABresult
0X0
100
111
70 }
71  
72 2 my $hash = hash_password($password);
73 2 ▶ print "Put this line in database_bi.conf, then restart the server:\n" if -t STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
if -t STDOUT 0 2 2
74 2 print " password_hash => '$hash',\n";
75 2 return;
76 }
77  
78 1 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.024.0
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
« blib/lib/Database/BI/Calc.pm ↑ Summary blib/lib/Database/BI/Ask.pm »