use Tie::RefHash;
use Class::Inspector;
+use Catalyst::Plugin::Authentication::Realm;
# this optimization breaks under Template::Toolkit
# use user_exists instead
if (!$realmname) {
$realmname = 'default';
}
+ my $realm = $c->get_auth_realm($realmname);
+
+ if (!$realm) {
+ Catalyst::Exception->throw(
+ "set_authenticated called with nonexistant realm: '$realmname'.");
+ }
if ( $c->isa("Catalyst::Plugin::Session")
and $c->config->{authentication}{use_session}
and $user->supports("session") )
{
- $c->save_user_in_session($user, $realmname);
+ $realm->save_user_in_session($c, $user);
}
- $user->auth_realm($realmname);
+ $user->auth_realm($realm->name);
$c->NEXT::set_authenticated($user, $realmname);
}
}
}
-sub save_user_in_session {
+sub __old_save_user_in_session {
my ( $c, $user, $realmname ) = @_;
$c->session->{__user_realm} = $realmname;
$realmname ||= 'default';
my $realm = $c->get_auth_realm($realmname);
- if ( $realm->{'store'} ) {
- return $realm->{'store'}->find_user($userinfo, $c);
- } else {
- $c->log->debug('find_user: unable to locate a store matching the requested realm');
+
+ if (!$realm) {
+ Catalyst::Exception->throw(
+ "find_user called with nonexistant realm: '$realmname'.");
}
+ return $realm->find_user($userinfo, $c);
}
return unless $realmname; # FIXME die unless? This is an internal inconsistency
my $realm = $c->get_auth_realm($realmname);
- $c->_user( my $user = $realm->{'store'}->from_session( $c, $frozen_user ) );
+ $c->_user( my $user = $realm->from_session( $c, $frozen_user ) );
# this sets the realm the user originated in.
$user->auth_realm($realmname);
foreach my $realm (keys %{$cfg->{'realms'}}) {
$app->setup_auth_realm($realm, $cfg->{'realms'}{$realm});
}
- # if we have a 'default-realm' in the config hash and we don't already
+ # if we have a 'default_realm' in the config hash and we don't already
# have a realm called 'default', we point default at the realm specified
if (exists($cfg->{'default_realm'}) && !$app->get_auth_realm('default')) {
$app->_set_default_auth_realm($cfg->{'default_realm'});
sub setup_auth_realm {
my ($app, $realmname, $config) = @_;
- $app->log->debug("Setting up auth realm $realmname") if $app->debug;
- if (!exists($config->{'store'}{'class'})) {
- Carp::croak "Couldn't setup the authentication realm named '$realmname', no class defined";
- }
-
- # use the
- my $storeclass = $config->{'store'}{'class'};
-
- ## follow catalyst class naming - a + prefix means a fully qualified class, otherwise it's
- ## taken to mean C::P::A::Store::(specifiedclass)
- if ($storeclass !~ /^\+(.*)$/ ) {
- $storeclass = "Catalyst::Plugin::Authentication::Store::${storeclass}";
- } else {
- $storeclass = $1;
- }
-
-
- # a little niceness - since most systems seem to use the password credential class,
- # if no credential class is specified we use password.
- $config->{credential}{class} ||= '+Catalyst::Plugin::Authentication::Credential::Password';
-
- my $credentialclass = $config->{'credential'}{'class'};
-
- ## follow catalyst class naming - a + prefix means a fully qualified class, otherwise it's
- ## taken to mean C::P::A::Credential::(specifiedclass)
- if ($credentialclass !~ /^\+(.*)$/ ) {
- $credentialclass = "Catalyst::Plugin::Authentication::Credential::${credentialclass}";
- } else {
- $credentialclass = $1;
+ my $realmclass = 'Catalyst::Plugin::Authentication::Realm';
+ if (defined($config->{'class'})) {
+ $realmclass = $config->{'class'};
+ Catalyst::Utils::ensure_class_loaded( $realmclass );
}
-
- # if we made it here - we have what we need to load the classes;
- Catalyst::Utils::ensure_class_loaded( $credentialclass );
- Catalyst::Utils::ensure_class_loaded( $storeclass );
-
- # BACKWARDS COMPATIBILITY - if the store class does not define find_user, we define it in terms
- # of get_user and add it to the class. this is because the auth routines use find_user,
- # and rely on it being present. (this avoids per-call checks)
- if (!$storeclass->can('find_user')) {
- no strict 'refs';
- *{"${storeclass}::find_user"} = sub {
- my ($self, $info) = @_;
- my @rest = @{$info->{rest}} if exists($info->{rest});
- $self->get_user($info->{id}, @rest);
- };
- }
-
- ## a little cruft to stay compatible with some poorly written stores / credentials
- ## we'll remove this soon.
- if ($storeclass->can('new')) {
- $app->auth_realms->{$realmname}{'store'} = $storeclass->new($config->{'store'}, $app);
+ my $realm = $realmclass->new($realmname, $config, $app);
+ if ($realm) {
+ $app->auth_realms->{$realmname} = $realm;
} else {
- $app->log->error("THIS IS DEPRECATED: $storeclass has no new() method - Attempting to use uninstantiated");
- $app->auth_realms->{$realmname}{'store'} = $storeclass;
- }
- if ($credentialclass->can('new')) {
- $app->auth_realms->{$realmname}{'credential'} = $credentialclass->new($config->{'credential'}, $app);
- } else {
- $app->log->error("THIS IS DEPRECATED: $credentialclass has no new() method - Attempting to use uninstantiated");
- $app->auth_realms->{$realmname}{'credential'} = $credentialclass;
+ $app->log->debug("realm initialization for '$realmname' failed.");
}
+ return $realm;
}
sub auth_realms {
## note to self - make authenticate throw an exception if realm is invalid.
- if ($realm && exists($realm->{'credential'})) {
- my $user = $realm->{'credential'}->authenticate($app, $realm->{store}, $userinfo);
- if (ref($user)) {
- $app->set_authenticated($user, $realmname);
- return $user;
- }
+ if ($realm) {
+ return $realm->authenticate($app, $userinfo);
} else {
- $app->log->debug("The realm requested, '$realmname' does not exist," .
- " or there is no credential associated with it.")
+ Catalyst::Exception->throw(
+ "authenticate called with nonexistant realm: '$realmname'.");
+
}
return undef;
}
sub default_auth_store {
my $self = shift;
+ my $realm = $self->get_auth_realm('default');
+ if (!$realm) {
+ $realm = $self->setup_auth_realm('default', { class => "Catalyst::Plugin::Authentication::Realm::Compatibility" });
+ }
if ( my $new = shift ) {
- $self->auth_realms->{'default'}{'store'} = $new;
+ $realm->store($new);
my $storeclass;
if (ref($new)) {
}
}
- return $self->get_auth_realm('default')->{'store'};
+ return $self->get_auth_realm('default')->store;
}
## BACKWARDS COMPATIBILITY
sub auth_store_names {
my $self = shift;
- my %hash = ( $self->get_auth_realm('default')->{'store'} => 'default' );
+ my %hash = ( $self->get_auth_realm('default')->store => 'default' );
}
sub get_auth_store {
sub auth_stores {
my $self = shift;
- my %hash = ( 'default' => $self->get_auth_realm('default')->{'store'});
+ my %hash = ( 'default' => $self->get_auth_realm('default')->store);
}
__PACKAGE__;
Configuration of the Catalyst::Plugin::Authentication framework is done in
terms of realms. In simplest terms, a realm is a pairing of a Credential
-verifier and a User storage (Store) backend.
+verifier and a User storage (Store) backend. As of version 0.10003, realms are
+now objects that you can create and customize.
An application can have any number of Realms, each of which operates
independant of the others. Each realm has a name, which is used to identify it
The only rule here is that there must be at least one. A realm consists of a
name, which is used to reference the realm, a credential and a store.
+You can also specify as realm class to instantiate instead of the default
+L<Catalyst::Plugin::Authentication::Realm> class.
+
Each realm config contains two hashes, one called 'credential' and one called
'store', each of which provide configuration details to the respective modules.
The contents of these hashes is specific to the module being used, with the
classname 'B<storename>' is expanded to:
Catalyst::Plugin::Authentication::Store::B<storename>.
-
=back
-
=head1 METHODS
-=over 4
-
-=item authenticate( $userinfo, $realm )
+=head2 authenticate( $userinfo, $realm )
Attempts to authenticate the user using the information in the $userinfo hash
reference using the realm $realm. $realm may be omitted, in which case the
default realm is checked.
-=item user
+=head2 user( )
Returns the currently logged in user or undef if there is none.
-=item user_exists
+=head2 user_exists( )
Returns true if a user is logged in right now. The difference between
user_exists and user is that user_exists will return true if a user is logged
need to know if the user is logged in, depending on the storage mechanism this
can be much more efficient.
-=item user_in_realm ( $realm )
+=head2 user_in_realm( $realm )
Works like user_exists, except that it only returns true if a user is both
logged in right now and was retrieved from the realm provided.
-=item logout
+=head2 logout( )
Logs the user out, Deletes the currently logged in user from $c->user and the session.
-=item find_user( $userinfo, $realm )
+=head2 find_user( $userinfo, $realm )
Fetch a particular users details, matching the provided user info, from the realm
specified in $realm.
-=back
-
=head1 INTERNAL METHODS
These methods are for Catalyst::Plugin::Authentication B<INTERNAL USE> only.
to fix / rewrite your code when things change. They are documented here only
for reference.
-=over 4
-
-=item set_authenticated ( $user, $realmname )
+=head2 set_authenticated( $user, $realmname )
Marks a user as authenticated. This is called from within the authenticate
routine when a credential returns a user. $realmname defaults to 'default'
-=item auth_restore_user ( $user, $realmname )
+=head2 auth_restore_user( $user, $realmname )
Used to restore a user from the session. In most cases this is called without
arguments to restore the user via the session. Can be called with arguments
when restoring a user from some other method. Currently not used in this way.
-=item save_user_in_session ( $user, $realmname )
+=head2 save_user_in_session( $user, $realmname )
Used to save the user in a session. Saves $user in session, marked as
originating in $realmname. Both arguments are required.
-=item auth_realms
+=head2 auth_realms( )
Returns a hashref containing realmname -> realm instance pairs. Realm
instances contain an instantiated store and credential object as the 'store'
and 'credential' elements, respectively
-=item get_auth_realm ( $realmname )
+=head2 get_auth_realm( $realmname )
Retrieves the realm instance for the realmname provided.
-=item
-
-=back
-
=head1 SEE ALSO
This list might not be up to date. Below are modules known to work with the updated
API of 0.10 and are therefore compatible with realms.
+=head2 Realms
+
+L<Catalyst::Plugin::Authentication::Realm>
+
=head2 User Storage Backends
L<Catalyst::Plugin::Authentication::Store::Minimal>,
functionality or any of the methods described above. These are for reference
purposes only.
-=over 4
-
-=item login
+=head2 login( )
This method is used to initiate authentication and user retrieval. Technically
this is part of the old Password credential module and it still resides in the
L<Password|Catalyst::Plugin::Authentication::Credential::Password> class. It is
included here for reference only.
-=item default_auth_store
+=head2 default_auth_store( )
Return the store whose name is 'default'.
Sets the default store to
L<Catalyst::Plugin::Authentication::Store::Minimal>.
-=item get_auth_store $name
+=head2 get_auth_store( $name )
Return the store whose name is $name.
-=item get_auth_store_name $store
+=head2 get_auth_store_name( $store )
Return the name of the store $store.
-=item auth_stores
+=head2 auth_stores( )
A hash keyed by name, with the stores registered in the app.
-=item register_auth_stores %stores_by_name
+=head2 register_auth_stores( %stores_by_name )
Register stores into the application.
-=back
-
-
-
=head1 AUTHORS
Yuval Kogman, C<nothingmuch@woobling.org>
David Kamholz
-
=head1 COPYRIGHT & LICENSE
Copyright (c) 2005 the aforementioned authors. All rights