]> iEval git - plack-middleware-auth-complex.git/blobdiff - lib/Plack/Middleware/Auth/Complex.pm
Fix reset email subject
[plack-middleware-auth-complex.git] / lib / Plack / Middleware / Auth / Complex.pm
index 0f6759ddb24c1d5eb2e05d97e1b3c06d31a017d5..4efd0ecb48f1588d219e13fff4ea38338d40e41b 100644 (file)
@@ -13,11 +13,12 @@ use Authen::Passphrase;
 use Authen::Passphrase::BlowfishCrypt;
 use Bytes::Random::Secure qw/random_bytes/;
 use DBI;
-use Digest::SHA qw/hmac_sha1_base64/;
+use Digest::SHA qw/hmac_sha1_base64 sha256/;
 use Email::Simple;
 use Email::Sender::Simple qw/sendmail/;
 use MIME::Base64 qw/decode_base64/;
 use Plack::Request;
+use Tie::Hash::Expire;
 
 sub default_opts {(
        dbi_connect       => ['dbi:Pg:', '', ''],
@@ -26,12 +27,14 @@ sub default_opts {(
        insert_user       => 'INSERT INTO users (id, passphrase, email) VALUES (?,?,?)',
        mail_subject      => 'Password reset token',
        realm             => 'restricted area',
+       cache_fail        => 0,
+       cache_max_age     => 5 * 60,
        token_max_age     => 60 * 60 * 24,
        username_regex    => qr/^\w{2,20}$/a,
-       register_url      => '/register',
-       passwd_url        => '/passwd',
-       request_reset_url => '/request-reset',
-       reset_url         => '/reset'
+       register_url      => '/action/register',
+       passwd_url        => '/action/passwd',
+       request_reset_url => '/action/request-reset',
+       reset_url         => '/action/reset'
 )}
 
 sub new {
@@ -52,6 +55,12 @@ sub init {
        $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;
@@ -60,9 +69,17 @@ sub get_user {
 
 sub check_passphrase {
        my ($self, $username, $passphrase) = @_;
+       unless ($self->{cache}) {
+               tie my %cache, 'Tie::Hash::Expire', {expire_seconds => $self->{cache_max_age}};
+               $self->{cache} = \%cache;
+       }
+       my $cachekey = sha256 "$username:$passphrase";
+       return $self->{cache}{$cachekey} if exists $self->{cache}{$cachekey};
        my $user = $self->get_user($username);
        return 0 unless $user;
-       Authen::Passphrase->from_rfc2307($user->{passphrase})->match($passphrase)
+       my $ret = Authen::Passphrase->from_rfc2307($user->{passphrase})->match($passphrase);
+       $self->{cache}{$cachekey} = $ret if $ret || $self->{cache_fail};
+       $ret
 }
 
 sub hash_passphrase {
@@ -114,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),
        ));
@@ -161,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')
 }
 
@@ -275,25 +293,25 @@ error and 500 for server errors. All parameters are mandatory.
 
 =over
 
-=item B<POST> /register?username=user&password=pw&confirm_password=pw&email=user@example.org
+=item B<POST> /action/register?username=user&password=pw&confirm_password=pw&email=user@example.org
 
 This URL creates a new user with the given username, password and
 email. The two passwords must match, the user must match
 C<username_regex> and the user must not already exist.
 
-=item B<POST> /passwd?password=oldpw&new_password=newpw&confirm_new_password=newpw
+=item B<POST> /action/passwd?password=oldpw&new_password=newpw&confirm_new_password=newpw
 
 This URL changes the password of a user. The user must be
 authenticated (otherwise the endpoint will return 401).
 
-=item B<POST> /request-reset?username=user
+=item B<POST> /action/request-reset?username=user
 
 This URL requests a password reset token for the given user. The token
 will be sent to the user's email address.
 
 A reset token in the default implementation is C<< base64(HMAC-SHA1("$username $passphrase $expiration_unix_time")) . ":$expiration_user_time" >>.
 
-=item B<POST> /reset?username=user&new_password=pw&confirm_new_password=pw&token=token
+=item B<POST> /action/reset?username=user&new_password=pw&confirm_new_password=pw&token=token
 
 This URL performs a password reset.
 
@@ -348,6 +366,17 @@ C<'Password reset token'>.
 
 Authentication realm. Defaults to C<'restricted area'>.
 
+=item cache_fail
+
+If true, all authentication results are cached. If false, only
+successful logins are cached. Defaults to false.
+
+=item cache_max_age
+
+Authentication cache timeout, in seconds. Authentication results are
+cached for this number of seconds to avoid expensive hashing. Defaults
+to 5 minutes.
+
 =item token_max_age
 
 Password reset token validity, in seconds. Defaults to 24 hours.
@@ -359,21 +388,21 @@ C<qr/^\w{2,20}$/a>.
 
 =item register_url
 
-URL for registering. Defaults to C<'/register'>.
+URL for registering. Defaults to C<'/action/register'>.
 
 =item passwd_url
 
-URL for changing your password. Defaults to C<'/passwd'>.
+URL for changing your password. Defaults to C<'/action/passwd'>.
 
 =item request_reset_url
 
 URL for requesting a password reset token by email. Defaults to
-C<'/request-reset'>.
+C<'/action/request-reset'>.
 
 =item reset_url
 
 URL for resetting your password with a reset token. Defaults to
-C<'/reset'>.
+C<'/action/reset'>.
 
 =back
 
@@ -395,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.025291 seconds and 4 git commands to generate.