From cd19b1b40c2315cc84f34a77f415f525c366fb4e Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Sun, 8 Mar 2015 16:51:51 +0200 Subject: [PATCH] Make "Invalid username" error message customizable --- lib/Plack/Middleware/Auth/Complex.pm | 8 +++++++- t/Plack-Middleware-Auth-Complex.t | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/Plack/Middleware/Auth/Complex.pm b/lib/Plack/Middleware/Auth/Complex.pm index ac70da6..035a622 100644 --- a/lib/Plack/Middleware/Auth/Complex.pm +++ b/lib/Plack/Middleware/Auth/Complex.pm @@ -32,6 +32,7 @@ sub default_opts {( cache_max_age => 5 * 60, token_max_age => 60 * 60, username_regex => qr/^\w{2,20}$/as, + invalid_username => 'Invalid username', register_url => '/action/register', passwd_url => '/action/passwd', request_reset_url => '/action/request-reset', @@ -176,7 +177,7 @@ sub call_register { return $self->bad_request("Missing parameter $_") unless $parms{$_}; } - return $self->bad_request('Username must match ' . $self->{username_regex}) unless $parms{username} =~ $self->{username_regex}; + return $self->bad_request($self->{invalid_username}) 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}; @@ -391,6 +392,11 @@ Password reset token validity, in seconds. Defaults to 1 hour. Regular expression that matches valid usernames. Defaults to C. +=item invalid_username + +Error message returned when the username does not match +username_regex. Defaults to C<'Invalid username'> + =item register_url URL for registering. Defaults to C<'/action/register'>. diff --git a/t/Plack-Middleware-Auth-Complex.t b/t/Plack-Middleware-Auth-Complex.t index eae34b7..72de572 100644 --- a/t/Plack-Middleware-Auth-Complex.t +++ b/t/Plack-Middleware-Auth-Complex.t @@ -53,7 +53,7 @@ test_psgi $app, sub { is_http $cb->(GET '/', Authorization => 'Hello'), 200, 'Anon', 'GET / with invalid Authorization'; is_http $cb->(GET '/', Authorization => $auth), 200, 'Anon', 'GET / with bad user/pass'; is_http $cb->(POST '/register'), 400, 'Missing parameter username', 'POST /register with no parameters'; - is_http $cb->(POST '/register', [@register_args, username => '???'] ), 400, 'Username must match (?^as:^\w{2,20}$)', 'POST /register with bad username'; + is_http $cb->(POST '/register', [@register_args, username => '???'] ), 400, 'Invalid username', 'POST /register with bad username'; is_http $cb->(POST '/register', [@register_args, password => '???'] ), 400, 'The two passwords do not match', 'POST /register with different passwords'; is_http $cb->(POST '/register', \@register_args), 200, 'Registered successfully', 'POST /register with correct parameters', is_http $cb->(POST '/register', \@register_args), 400, 'Username already in use', 'POST /register with existing user', -- 2.39.2