1 package Catalyst::Authentication::Realm;
6 use base qw/Class::Accessor::Fast/;
9 __PACKAGE__->mk_accessors(qw/store credential name config/);
12 ## Add use_session config item to realm.
15 my ($class, $realmname, $config, $app) = @_;
17 my $self = { config => $config };
20 $self->name($realmname);
22 if (!exists($self->config->{'use_session'})) {
23 if (exists($app->config->{'Plugin::Authentication'}{'use_session'})) {
24 $self->config->{'use_session'} = $app->config->{'Plugin::Authentication'}{'use_session'};
26 $self->config->{'use_session'} = 1;
30 $app->log->debug("Setting up auth realm $realmname") if $app->debug;
32 # use the Null store as a default - Don't complain if the realm class is being overridden,
33 # as the new realm may behave differently.
34 if( ! exists($config->{store}{class}) ) {
35 $config->{store}{class} = '+Catalyst::Authentication::Store::Null';
36 if (! exists($config->{class})) {
37 $app->log->debug( qq(No Store specified for realm "$realmname", using the Null store.) );
40 my $storeclass = $config->{'store'}{'class'};
42 ## follow catalyst class naming - a + prefix means a fully qualified class, otherwise it's
43 ## taken to mean C::P::A::Store::(specifiedclass)
44 if ($storeclass !~ /^\+(.*)$/ ) {
45 $storeclass = "Catalyst::Authentication::Store::${storeclass}";
50 # a little niceness - since most systems seem to use the password credential class,
51 # if no credential class is specified we use password.
52 $config->{credential}{class} ||= '+Catalyst::Authentication::Credential::Password';
54 my $credentialclass = $config->{'credential'}{'class'};
56 ## follow catalyst class naming - a + prefix means a fully qualified class, otherwise it's
57 ## taken to mean C::A::Credential::(specifiedclass)
58 if ($credentialclass !~ /^\+(.*)$/ ) {
59 $credentialclass = "Catalyst::Authentication::Credential::${credentialclass}";
61 $credentialclass = $1;
64 # if we made it here - we have what we need to load the classes
66 ### BACKWARDS COMPATIBILITY - DEPRECATION WARNING:
67 ### we must eval the ensure_class_loaded - because we might need to try the old-style
68 ### ::Plugin:: module naming if the standard method fails.
70 ## Note to self - catch second exception and bitch in detail?
73 Catalyst::Utils::ensure_class_loaded( $credentialclass );
77 # If the file is missing, then try the old-style fallback,
78 # but re-throw anything else for the user to deal with.
79 die unless $@ =~ /^Can't locate/;
80 $app->log->warn( qq(Credential class "$credentialclass" not found, trying deprecated ::Plugin:: style naming. ) );
81 my $origcredentialclass = $credentialclass;
82 $credentialclass =~ s/Catalyst::Authentication/Catalyst::Plugin::Authentication/;
84 eval { Catalyst::Utils::ensure_class_loaded( $credentialclass ); };
86 # Likewise this croak is useful if the second exception is also "not found",
87 # but would be confusing if it's anything else.
88 die unless $@ =~ /^Can't locate/;
89 Carp::croak "Unable to load credential class, " . $origcredentialclass . " OR " . $credentialclass .
90 " in realm " . $self->name;
95 Catalyst::Utils::ensure_class_loaded( $storeclass );
99 # If the file is missing, then try the old-style fallback,
100 # but re-throw anything else for the user to deal with.
101 die unless $@ =~ /^Can't locate/;
102 $app->log->warn( qq(Store class "$storeclass" not found, trying deprecated ::Plugin:: style naming. ) );
103 my $origstoreclass = $storeclass;
104 $storeclass =~ s/Catalyst::Authentication/Catalyst::Plugin::Authentication/;
105 eval { Catalyst::Utils::ensure_class_loaded( $storeclass ); };
107 # Likewise this croak is useful if the second exception is also "not found",
108 # but would be confusing if it's anything else.
109 die unless $@ =~ /^Can't locate/;
110 Carp::croak "Unable to load store class, " . $origstoreclass . " OR " . $storeclass .
111 " in realm " . $self->name;
115 # BACKWARDS COMPATIBILITY - if the store class does not define find_user, we define it in terms
116 # of get_user and add it to the class. this is because the auth routines use find_user,
117 # and rely on it being present. (this avoids per-call checks)
118 if (!$storeclass->can('find_user')) {
120 *{"${storeclass}::find_user"} = sub {
121 my ($self, $info) = @_;
122 my @rest = @{$info->{rest}} if exists($info->{rest});
123 $self->get_user($info->{id}, @rest);
127 ## a little cruft to stay compatible with some poorly written stores / credentials
128 ## we'll remove this soon.
129 if ($storeclass->can('new')) {
130 $self->store($storeclass->new($config->{'store'}, $app, $self));
132 $app->log->error("THIS IS DEPRECATED: $storeclass has no new() method - Attempting to use uninstantiated");
133 $self->store($storeclass);
135 if ($credentialclass->can('new')) {
136 $self->credential($credentialclass->new($config->{'credential'}, $app, $self));
138 $app->log->error("THIS IS DEPRECATED: $credentialclass has no new() method - Attempting to use uninstantiated");
139 $self->credential($credentialclass);
146 my ( $self, $authinfo, $c ) = @_;
148 my $res = $self->store->find_user($authinfo, $c);
151 if ($self->config->{'auto_create_user'} && $self->store->can('auto_create_user') ) {
152 $res = $self->store->auto_create_user($authinfo, $c);
154 } elsif ($self->config->{'auto_update_user'} && $self->store->can('auto_update_user')) {
155 $res = $self->store->auto_update_user($authinfo, $c, $res);
162 my ($self, $c, $authinfo) = @_;
164 my $user = $self->credential->authenticate($c, $self, $authinfo);
166 $c->set_authenticated($user, $self->name);
173 sub user_is_restorable {
178 and $self->config->{'use_session'}
179 and $c->session_is_valid;
181 return $c->session->{__user};
185 my ($self, $c, $frozen_user) = @_;
187 $frozen_user ||= $self->user_is_restorable($c);
188 return unless defined($frozen_user);
190 my $user = $self->from_session( $c, $frozen_user );
195 # this sets the realm the user originated in.
196 $user->auth_realm($self->name);
199 $self->failed_user_restore($c) ||
200 $c->error("Store claimed to have a restorable user, but restoration failed. Did you change the user's id_field?");
206 ## this occurs if there is a session but the thing the session refers to
207 ## can not be found. Do what you must do here.
208 ## Return true if you can fix the situation and find a user, false otherwise
209 sub failed_user_restore {
212 $self->remove_persisted_user($c);
217 my ($self, $c, $user) = @_;
221 and $self->config->{'use_session'}
222 and $user->supports("session")
224 $c->session->{__user_realm} = $self->name;
226 # we want to ask the store for a user prepared for the session.
227 # but older modules split this functionality between the user and the
228 # store. We try the store first. If not, we use the old method.
229 if ($self->store->can('for_session')) {
230 $c->session->{__user} = $self->store->for_session($c, $user);
232 $c->session->{__user} = $user->for_session;
238 sub remove_persisted_user {
243 and $self->config->{'use_session'}
244 and $c->session_is_valid
246 delete @{ $c->session }{qw/__user __user_realm/};
250 ## backwards compatibility - I don't think many people wrote realms since they
251 ## have only existed for a short time - but just in case.
252 sub save_user_in_session {
253 my ( $self, $c, $user ) = @_;
255 return $self->persist_user($c, $user);
259 my ($self, $c, $frozen_user) = @_;
261 return $self->store->from_session($c, $frozen_user);
273 Catalyst::Authentication::Realm - Base class for realm objects.
283 By default this class is used by
284 L<Catalyst::Plugin::Authentication|Catalyst::Plugin::Authentication> for all
285 realms. The class parameter allows you to choose a different class to use for
286 this realm. Creating a new Realm class can allow for authentication methods
287 that fall outside the normal credential/store methodology.
289 =item auto_create_user
291 Set this to true if you wish this realm to auto-create user accounts when the
292 user doesn't exist (most useful for remote authentication schemes).
294 =item auto_update_user
296 Set this to true if you wish this realm to auto-update user accounts after
297 authentication (most useful for remote authentication schemes).
301 Sets session usage for this particular realm - overriding the global use_sesion setting.
308 =head2 new( $realmname, $config, $app )
310 Instantiantes this realm, plus the specified store and credential classes.
314 Returns an instance of the store object for this realm.
318 Returns an instance of the credential object for this realm.
320 =head2 find_user( $authinfo, $c )
322 Retrieves the user given the authentication information provided. This
323 is most often called from the credential. The default realm class simply
324 delegates this call the store object. If enabled, auto-creation and
325 auto-updating of users is also handled here.
327 =head2 authenticate( $c, $authinfo)
329 Performs the authentication process for the current realm. The default
330 realm class simply delegates this to the credential and sets
331 the authenticated user on success. Returns the authenticated user object;
333 =head1 USER PERSISTENCE
335 The Realm class allows complete control over the persistance of users
336 between requests. By default the realm attempts to use the Catalyst
337 session system to accomplish this. By overriding the methods below
338 in a custom Realm class, however, you can handle user persistance in
341 =head2 persist_user($c, $user)
343 persist_user is the entry point for saving user information between requests
344 in most cases this will utilize the session. By default this uses the
345 catalyst session system to store the user by calling for_session on the
346 active store. The user object must be a subclass of
347 Catalyst::Authentication::User. If you have updated the user object, you
348 must call persist_user again to ensure that the persisted user object reflects
351 =head2 remove_persisted_user($c)
353 Removes any persisted user data. By default, removes the user from the session.
355 =head2 user_is_restorable( $c )
357 Returns whether there is a persisted user that may be restored. Returns
358 a token used to restore the user. With the default session persistance
359 it returns the raw frozen user information.
361 =head2 restore_user($c, [$frozen_user])
363 Restores the user from the given frozen_user parameter, or if not provided,
364 using the response from $self->user_is_restorable(); Uses $self->from_session()
365 to decode the frozen user.
367 =head2 failed_user_restore($c)
369 If there is a session to restore, but the restore fails for any reason then this method
370 is called. This method supplied just removes the persisted user, but can be overridden
371 if required to have more complex logic (e.g. finding a the user by their 'old' username).
373 =head2 from_session($c, $frozenuser )
375 Decodes the frozenuser information provided and returns an instantiated
376 user object. By default, this call is delegated to $store->from_session().
378 =head2 save_user_in_session($c, $user)
380 DEPRECATED. Use persist_user instead. (this simply calls persist_user)