Add some uncoverable comments
[plack-middleware-auth-complex.git] / lib / Plack / Middleware / Auth / Complex.pm
index 55fc5ee1861579b4af2e2a6de06fc8ee5f50be2c..fe545482597db1e9ed2c1f1e8737b61842c69550 100644 (file)
@@ -29,7 +29,7 @@ sub default_opts {(
        realm             => 'restricted area',
        cache_fail        => 0,
        cache_max_age     => 5 * 60,
-       token_max_age     => 60 * 60 * 24,
+       token_max_age     => 60 * 60,
        username_regex    => qr/^\w{2,20}$/a,
        register_url      => '/action/register',
        passwd_url        => '/action/passwd',
@@ -49,12 +49,18 @@ sub new {
 sub init {
        my ($self) = @_;
        $self->{dbh} = DBI->connect(@{$self->{dbi_connect}})              or die $DBI::errstr;
-       $self->{post_connect_cb}->($self) if $self->{post_connect_cb};
+       $self->{post_connect_cb}->($self) if $self->{post_connect_cb}; # uncoverable branch false
        $self->{insert_sth} = $self->{dbh}->prepare($self->{insert_user}) or die $self->{dbh}->errstr;
        $self->{select_sth} = $self->{dbh}->prepare($self->{select_user}) or die $self->{dbh}->errstr;
        $self->{update_sth} = $self->{dbh}->prepare($self->{update_pass}) or die $self->{dbh}->errstr;
 }
 
+sub create_user {
+       my ($self, $parms) = @_;
+       my %parms = $parms->flatten;
+       $self->{insert_sth}->execute($parms{username}, $self->hash_passphrase($parms{password}), $parms{email})
+}
+
 sub get_user {
        my ($self, $user) = @_;
        $self->{select_sth}->execute($user) or die $self->{sth}->errstr;
@@ -68,7 +74,7 @@ sub check_passphrase {
                $self->{cache} = \%cache;
        }
        my $cachekey = sha256 "$username:$passphrase";
-       return $self->{cache}{$cachekey} if exists $self->{cache}{$cachekey};
+       return $self->{cache}{$cachekey} if exists $self->{cache}{$cachekey}; # uncoverable branch true
        my $user = $self->get_user($username);
        return 0 unless $user;
        my $ret = Authen::Passphrase->from_rfc2307($user->{passphrase})->match($passphrase);
@@ -92,7 +98,7 @@ sub set_passphrase {
 
 sub make_reset_hmac {
        my ($self, $username, @data) = @_;
-       $self->{hmackey} //= random_bytes 512;
+       $self->{hmackey} //= random_bytes 512; # uncoverable condition false
        my $user = $self->get_user($username);
        my $message = join ' ', $username, $user->{passphrase}, @data;
        hmac_sha1_base64 $message, $self->{hmackey};
@@ -101,7 +107,7 @@ sub make_reset_hmac {
 sub mail_body {
        my ($self, $username, $token) = @_;
        my $hours = $self->{token_max_age} / 60 / 60;
-       $hours .= $hours == 1 ? ' hour' : ' hours';
+       $hours .= $hours == 1 ? ' hour' : ' hours'; # uncoverable branch false
        <<EOF;
 Someone has requested a password reset for your account.
 
@@ -125,7 +131,7 @@ sub send_reset_email {
                header => [
                        From    => $self->{mail_from},
                        To      => $user->{email},
-                       Subject => $user->{mail_subject},
+                       Subject => $self->{mail_subject},
                ],
                body => $self->mail_body($username, $token),
        ));
@@ -172,7 +178,8 @@ sub call_register {
        return $self->bad_request('Username must match ' . $self->{username_regex}) unless $parms{username} =~ /$self->{username_regex}/;
        return $self->bad_request('Username already in use') if $self->get_user($parms{username});
        return $self->bad_request('The two passwords do not match') unless $parms{password} eq $parms{confirm_password};
-       $self->{insert_sth}->execute($parms{username}, $self->hash_passphrase($parms{password}), $parms{email});
+
+       $self->create_user($req->parameters);
        return $self->reply('Registered successfully')
 }
 
@@ -270,7 +277,7 @@ allows user registration, password changing and password reset.
 
 AuthComplex sets REMOTE_USER if the request includes correct basic
 authentication and intercepts POST requests to some configurable URLs.
-It also sets C<$env->{authcomplex}> to itself before passing the
+It also sets C<< $env->{authcomplex} >> to itself before passing the
 request.
 
 Some options can be controlled by passing a hashref to the
@@ -372,7 +379,7 @@ to 5 minutes.
 
 =item token_max_age
 
-Password reset token validity, in seconds. Defaults to 24 hours.
+Password reset token validity, in seconds. Defaults to 1 hour.
 
 =item username_regex
 
@@ -417,6 +424,11 @@ Called at the end of the constructor. The default implementation
 connects to the database, calls C<post_connect_cb> and prepares the
 SQL statements.
 
+=item B<create_user>(I<$parms>)
+
+Inserts a new user into the database. I<$parms> is a
+L<Hash::MultiValue> object containing the request parameters.
+
 =item B<get_user>(I<$username>)
 
 Returns a hashref with (at least) the following keys: passphrase (the
This page took 0.011241 seconds and 4 git commands to generate.