From: Jos Boumans Date: Thu, 5 Jun 2008 16:30:54 +0000 (+0000) Subject: fixed a nasty bug in compat mode with store::minimal. From the comments left in ... X-Git-Tag: v0.10009_01~20 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=692dcea459fcc9947180b0e4b951740260a60eb8;p=catagits%2FCatalyst-Plugin-Authentication.git fixed a nasty bug in compat mode with store::minimal. From the comments left in ->setup(): ### If a user does 'use Catalyst qw/Authentication::Store::Minimal/' ### he will be proxied on to this setup routine (and only then -- ### non plugins should NOT have their setup routine invoked!) ### Beware what we pass to the 'new' routine; it wants ### a config has with a top level key 'users'. New style ### configs do not have this, and split by realms. If we ### blindly pass this to new, we will 1) overwrite what we ### already passed and 2) make ->userhash undefined, which ### leads to: ### Can't use an undefined value as a HASH reference at ### lib/Catalyst/Authentication/Store/Minimal.pm line 38. ### ### So only do this compatibility call if: ### 1) we have a {users} config directive ### ### Ideally we could also check for: ### 2) we don't already have a ->userhash ### however, that's an attribute of an object we can't access =/ --- diff --git a/lib/Catalyst/Authentication/Store/Minimal.pm b/lib/Catalyst/Authentication/Store/Minimal.pm index 155a921..6fa764f 100644 --- a/lib/Catalyst/Authentication/Store/Minimal.pm +++ b/lib/Catalyst/Authentication/Store/Minimal.pm @@ -77,12 +77,32 @@ sub get_user { sub setup { my $c = shift; - $c->default_auth_store( - __PACKAGE__->new( - $c->config->{'Plugin::Authentication'}, $c - ) - ); + ### If a user does 'use Catalyst qw/Authentication::Store::Minimal/' + ### he will be proxied on to this setup routine (and only then -- + ### non plugins should NOT have their setup routine invoked!) + ### Beware what we pass to the 'new' routine; it wants + ### a config has with a top level key 'users'. New style + ### configs do not have this, and split by realms. If we + ### blindly pass this to new, we will 1) overwrite what we + ### already passed and 2) make ->userhash undefined, which + ### leads to: + ### Can't use an undefined value as a HASH reference at + ### lib/Catalyst/Authentication/Store/Minimal.pm line 38. + ### + ### So only do this compatibility call if: + ### 1) we have a {users} config directive + ### + ### Ideally we could also check for: + ### 2) we don't already have a ->userhash + ### however, that's an attribute of an object we can't + ### access =/ --kane + + my $cfg = $c->config->{'Plugin::Authentication'}->{users} + ? $c->config->{'Plugin::Authentication'} + : undef; + $c->default_auth_store( __PACKAGE__->new( $cfg, $c ) ) if $cfg; + $c->NEXT::setup(@_); } diff --git a/lib/Catalyst/Plugin/Authentication.pm b/lib/Catalyst/Plugin/Authentication.pm index 8b29d0b..ba2afad 100644 --- a/lib/Catalyst/Plugin/Authentication.pm +++ b/lib/Catalyst/Plugin/Authentication.pm @@ -14,7 +14,7 @@ use Class::Inspector; use Catalyst::Authentication::Realm; -our $VERSION = "0.10007"; +our $VERSION = "0.10007_01"; sub set_authenticated { my ( $c, $user, $realmname ) = @_; diff --git a/t/lib/AuthRealmTestApp.pm b/t/lib/AuthRealmTestApp.pm index 36cb291..bc70351 100644 --- a/t/lib/AuthRealmTestApp.pm +++ b/t/lib/AuthRealmTestApp.pm @@ -2,7 +2,10 @@ package AuthRealmTestApp; use warnings; use strict; -use Catalyst qw/Authentication/; +use Catalyst qw/ + Authentication + Authentication::Store::Minimal +/; use Test::More; use Test::Exception; diff --git a/t/lib/AuthRealmTestAppCompat.pm b/t/lib/AuthRealmTestAppCompat.pm new file mode 100644 index 0000000..4a8b50c --- /dev/null +++ b/t/lib/AuthRealmTestAppCompat.pm @@ -0,0 +1,61 @@ +package AuthRealmTestAppCompat; +use warnings; +use strict; + +### using A::Store::minimal with new style realms +### makes the app blow up, since c::p::a::s::minimal +### isa c:a::s::minimal, and it's compat setup() gets +### run, with an unexpected config has (realms on top, +### not users). This tests makes sure the app no longer +### blows up when this happens. +use Catalyst qw/ + Authentication + Authentication::Store::Minimal +/; + +use Test::More; +use Test::Exception; + +our $members = { + bob => { + password => "s00p3r" + }, +}; + +sub moose : Local { + my ( $self, $c ) = @_; + + while ( my ($user, $info) = each %$members ) { + + my $ok = eval { + $c->authenticate( + { username => $user, password => $info->{password} }, + 'members' + ), + }; + + ok( !$@, "Test did not die: $@" ); + ok( $ok, "user $user authentication" ); + } + + $c->res->body( "ok" ); +} + +__PACKAGE__->config->{'Plugin::Authentication'} = { + default_realm => 'members', + realms => { + members => { + credential => { + class => 'Password', + password_field => 'password', + password_type => 'clear' + }, + store => { + class => 'Minimal', + users => $members, + } + }, + } +}; + +__PACKAGE__->setup; diff --git a/t/live_app_realms_compat.t b/t/live_app_realms_compat.t new file mode 100644 index 0000000..5deccf1 --- /dev/null +++ b/t/live_app_realms_compat.t @@ -0,0 +1,13 @@ +use strict; +use warnings; + +use Test::More; + +BEGIN { + plan "no_plan"; +} + +use lib 't/lib'; +use Catalyst::Test qw/AuthRealmTestAppCompat/; + +ok(get("/moose"), "get ok");