Apache2-Authen-Passphrase

 view release on metacpan or  search on metacpan

aap-passwd  view on Meta::CPAN

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use IO::Prompter [qw/-e* -stdio/];
 
die "Usage: aap-passwd [rootdir] username\n" if @ARGV == 0 || @ARGV > 2;
($Apache2::Authen::Passphrase::rootdir) = (shift =~ /(.*)/s) if @ARGV == 2;
 
my $username = shift;
die "Invalid username\n" unless $username =~ USER_REGEX;
($username) = ($username =~ /(.*)/s);
 
my $passwd = prompt 'Enter new Apache2::Authen::Passphrase password: ';
my $confirm = prompt 'Retype new Apache2::Authen::Passphrase password: ';
 
die "Sorry, passwords do not match\n" unless $passwd eq $confirm;
pwset $username, $passwd;
say 'Password updated successfully'; ## no critic (RequireCheckedSyscalls)
 
__END__
 
=head1 NAME
 
aap-passwd - passwd utility for Apache2::Authen::Passphrase
 
=head1 SYNOPSIS
 
  user@hostname:~$ aap-passwd /path/to/rootdir marius
  Enter new Apache2::Authen::Passphrase password: ***
  Retype new Apache2::Authen::Passphrase password: ***
  Password updated successfully
 
=head1 DESCRIPTION
 
aap-passwd updates the password of an Apache2::Authen::Passphrase user.
 
It is used like this:
 
    aap-passwd /path/to/rootdir username
 
where the C<rootdir> is the first argument and the username whose password is to be changed is the second argument, or like this:
 
    aap-passwd username
 
where the C<rootdir> is taken from the environment and the username is the only argument.
 
=head1 ENVIRONMENT
 
=over
 
=item AAP_ROOTDIR

lib/Apache2/Authen/Passphrase.pm  view on Meta::CPAN

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use strict;
use subs qw/OK HTTP_UNAUTHORIZED/;
 
our $VERSION = 0.002002;
 
use constant USER_REGEX => qr/^\w{2,20}$/pas;
use constant PASSPHRASE_VERSION => 1;
use constant INVALID_USER => "invalid-user\n";
use constant BAD_PASSWORD => "bad-password\n";
 
use if $ENV{MOD_PERL}, 'Apache2::RequestRec';
use if $ENV{MOD_PERL}, 'Apache2::RequestUtil';
use if $ENV{MOD_PERL}, 'Apache2::Access';
use if $ENV{MOD_PERL}, 'Apache2::Const' => qw/OK HTTP_UNAUTHORIZED/;
use YAML::Any qw/LoadFile DumpFile/;
 
our @EXPORT_OK = qw/pwset pwcheck pwhash USER_REGEX PASSPHRASE_VERSION INVALID_USER BAD_PASSWORD/;

lib/Apache2/Authen/Passphrase.pm  view on Meta::CPAN

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
__END__
 
=head1 NAME
 
Apache2::Authen::Passphrase - basic authentication with Authen::Passphrase
 
=head1 SYNOPSIS
 
  use Apache2::Authen::Passphrase qw/pwcheck pwset pwhash/;
  $Apache2::Authen::Passphrase::rootdir = "/path/to/user/directory"
  my $hash = pwhash $username, $password;
  pwset $username, "pass123";
  eval { pwcheck $username, "pass123" };
 
  # In Apache2 config
  <Location /secret>
    PerlAuthenHandler Apache2::Authen::Passphrase
    PerlSetVar AuthenPassphraseRootdir /path/to/user/directory
    AuthName MyAuth
    Require valid-user
  </Location>
 
=head1 DESCRIPTION
 
Apache2::Authen::Passphrase is a perl module which provides easy-to-use Apache2 authentication. It exports some utility functions and it contains a PerlAuthenHandler.
 
The password hashes are stored in YAML files in an directory (called the C<rootdir>), one file per user.
 
Set the C<rootdir> like this:
 
  $Apache2::Authen::Passphrase::rootdir = '/path/to/rootdir';
 
or by setting the C<AAP_ROOTDIR> enviroment variable to the desired value.
 
=head1 FUNCTIONS
 
=over
 
=item B<pwhash>()
 
Takes the password as a single argument and returns the password hash.
 
=item B<pwset>(I<$username>, I<$password>)
 
Sets the password of $username to $password.
 
=item B<pwcheck>(I<$username>, I<$password>)
 
Checks the given username and password, throwing an exception if the username is invalid or the password is incorrect.
 
=item B<handler>
 
The PerlAuthenHandler for use in apache2. It uses Basic Access Authentication.
 
=item B<USER_REGEX>
 
A regex that matches valid usernames. Usernames must be at least 2 characters, at most 20 characters, and they may only contain word characters (C<[A-Za-z0-9_]>).
 
=item B<INVALID_USER>
 
Exception thrown if the username does not match C<USER_REGEX>.
 
=item B<BAD_PASSWORD>
 
Exception thrown if the password is different from the one stored in the user's yml file.
 
=item B<PASSPHRASE_VERSION>
 
The version of the passphrase. It is incremented each time the passphrase hashing scheme is changed. Versions so far:
 
=over
 
=item Version 1 B<(current)>
 
Uses C<Authen::Passphrase::BlowfishCrypt> with a cost factor of 10

t/Apache2-Authen-Passphrase.t  view on Meta::CPAN

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
        eval { pwcheck $user, $pass };
        is $@, '', $testname;
}
 
sub pw_nok {
        my ($user, $pass, $testname) = @_;
        eval { pwcheck $user, $pass };
        isnt $@, '', $testname;
}
 
pwset marius => 'password';
pw_ok marius => 'password', 'Set password and check it';
pw_nok marius => 'anotherpassword', 'Check an incorrect password';
 
pwset marius => 'anotherpassword';
pw_ok marius => 'anotherpassword', 'Change the password and check it';
 
pw_nok 'BadUsername++', 'a', 'Bad username';
pw_nok 'a', 'a', 'Short username';
pw_nok 'asfwe0g3girg4ih45jho45ih45hi45h045jh4oh', 'a', 'Long username';



( run in 0.273 second using v1.01-cache-2.11-cpan-a9ef4e587e4 )