1 package Catalyst::Plugin::Authentication::Realm;
6 use base qw/Class::Accessor::Fast/;
9 __PACKAGE__->mk_accessors(qw/store credential name config/);
13 my ($class, $realmname, $config, $app) = @_;
15 my $self = { config => $config };
18 $self->name($realmname);
20 $app->log->debug("Setting up auth realm $realmname") if $app->debug;
22 # use the Null store as a default
23 if( ! exists $config->{store}{class} ) {
24 $config->{store}{class} = '+Catalyst::Plugin::Authentication::Store::Null';
25 $app->log->debug( qq(No Store specified for realm "$realmname", using the Null store.) );
27 my $storeclass = $config->{'store'}{'class'};
29 ## follow catalyst class naming - a + prefix means a fully qualified class, otherwise it's
30 ## taken to mean C::P::A::Store::(specifiedclass)
31 if ($storeclass !~ /^\+(.*)$/ ) {
32 $storeclass = "Catalyst::Plugin::Authentication::Store::${storeclass}";
37 # a little niceness - since most systems seem to use the password credential class,
38 # if no credential class is specified we use password.
39 $config->{credential}{class} ||= '+Catalyst::Plugin::Authentication::Credential::Password';
41 my $credentialclass = $config->{'credential'}{'class'};
43 ## follow catalyst class naming - a + prefix means a fully qualified class, otherwise it's
44 ## taken to mean C::P::A::Credential::(specifiedclass)
45 if ($credentialclass !~ /^\+(.*)$/ ) {
46 $credentialclass = "Catalyst::Plugin::Authentication::Credential::${credentialclass}";
48 $credentialclass = $1;
51 # if we made it here - we have what we need to load the classes;
52 Catalyst::Utils::ensure_class_loaded( $credentialclass );
53 Catalyst::Utils::ensure_class_loaded( $storeclass );
55 # BACKWARDS COMPATIBILITY - if the store class does not define find_user, we define it in terms
56 # of get_user and add it to the class. this is because the auth routines use find_user,
57 # and rely on it being present. (this avoids per-call checks)
58 if (!$storeclass->can('find_user')) {
60 *{"${storeclass}::find_user"} = sub {
61 my ($self, $info) = @_;
62 my @rest = @{$info->{rest}} if exists($info->{rest});
63 $self->get_user($info->{id}, @rest);
67 ## a little cruft to stay compatible with some poorly written stores / credentials
68 ## we'll remove this soon.
69 if ($storeclass->can('new')) {
70 $self->store($storeclass->new($config->{'store'}, $app, $self));
72 $app->log->error("THIS IS DEPRECATED: $storeclass has no new() method - Attempting to use uninstantiated");
73 $self->store($storeclass);
75 if ($credentialclass->can('new')) {
76 $self->credential($credentialclass->new($config->{'credential'}, $app, $self));
78 $app->log->error("THIS IS DEPRECATED: $credentialclass has no new() method - Attempting to use uninstantiated");
79 $self->credential($credentialclass);
86 my ( $self, $authinfo, $c ) = @_;
88 my $res = $self->store->find_user($authinfo, $c);
91 if ($self->config->{'auto_create_user'} && $self->store->can('auto_create_user') ) {
92 $res = $self->store->auto_create_user($authinfo, $c);
94 } elsif ($self->config->{'auto_update_user'} && $self->store->can('auto_update_user')) {
95 $res = $self->store->auto_update_user($authinfo, $c, $res);
102 my ($self, $c, $authinfo) = @_;
104 my $user = $self->credential->authenticate($c, $self, $authinfo);
106 $c->set_authenticated($user, $self->name);
113 sub save_user_in_session {
114 my ( $self, $c, $user ) = @_;
116 $c->session->{__user_realm} = $self->name;
118 # we want to ask the store for a user prepared for the session.
119 # but older modules split this functionality between the user and the
120 # store. We try the store first. If not, we use the old method.
121 if ($self->store->can('for_session')) {
122 $c->session->{__user} = $self->store->for_session($c, $user);
124 $c->session->{__user} = $user->for_session;
129 my ($self, $c, $frozen_user) = @_;
131 return $self->store->from_session($c, $frozen_user);
143 Catalyst::Plugin::Authentication::Realm - Base class for realm objects.
153 By default this class is the default realm class. You can specify a custom
154 realm class with this config parameter.
156 =item auto_create_user
158 Set this to true if you wish this realm to auto-create user accounts when the
159 user doesn't exist (most useful for remote authentication schemes).
161 =item auto_update_user
163 Set this to true if you wish this realm to auto-update user accounts after
164 authentication (most useful for remote authentication schemes).
172 Instantiantes this realm, plus the specified store and credential classes.
176 Holds an instance of the store object for this realm.
180 Holds an instance of the credential object for this realm.
184 Delegates to the store object. Will also re-delegate auto_create_user and
185 auto_update_user at this time, if necessary.
187 =head2 authenticate( )
189 Delegates to the credential objects and sets the authenticated user on success.
191 =head2 save_user_in_session( )
193 Delegates to the store object.
195 =head2 from_session( )
197 Delegates to the store object.