tweak progressive realm documentation and add check to error on
[catagits/Catalyst-Plugin-Authentication.git] / lib / Catalyst / Authentication / Realm.pm
CommitLineData
e0499ed6 1package Catalyst::Authentication::Realm;
810966b5 2
3use strict;
4use warnings;
2c7d23af 5
810966b5 6use base qw/Class::Accessor::Fast/;
7
8BEGIN {
9 __PACKAGE__->mk_accessors(qw/store credential name config/);
10};
11
9a37ffba 12## Add use_session config item to realm.
13
810966b5 14sub new {
15 my ($class, $realmname, $config, $app) = @_;
16
17 my $self = { config => $config };
18 bless $self, $class;
19
20 $self->name($realmname);
21
9a37ffba 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'};
25 } else {
26 $self->config->{'use_session'} = 1;
27 }
28 }
9c08483f 29
810966b5 30 $app->log->debug("Setting up auth realm $realmname") if $app->debug;
8513d51b 31
32 # use the Null store as a default
33 if( ! exists $config->{store}{class} ) {
0f673ca7 34 $config->{store}{class} = '+Catalyst::Authentication::Store::Null';
8513d51b 35 $app->log->debug( qq(No Store specified for realm "$realmname", using the Null store.) );
810966b5 36 }
810966b5 37 my $storeclass = $config->{'store'}{'class'};
38
39 ## follow catalyst class naming - a + prefix means a fully qualified class, otherwise it's
40 ## taken to mean C::P::A::Store::(specifiedclass)
41 if ($storeclass !~ /^\+(.*)$/ ) {
0f673ca7 42 $storeclass = "Catalyst::Authentication::Store::${storeclass}";
810966b5 43 } else {
44 $storeclass = $1;
45 }
810966b5 46
47 # a little niceness - since most systems seem to use the password credential class,
48 # if no credential class is specified we use password.
0f673ca7 49 $config->{credential}{class} ||= '+Catalyst::Authentication::Credential::Password';
810966b5 50
51 my $credentialclass = $config->{'credential'}{'class'};
52
53 ## follow catalyst class naming - a + prefix means a fully qualified class, otherwise it's
0f673ca7 54 ## taken to mean C::A::Credential::(specifiedclass)
810966b5 55 if ($credentialclass !~ /^\+(.*)$/ ) {
0f673ca7 56 $credentialclass = "Catalyst::Authentication::Credential::${credentialclass}";
810966b5 57 } else {
58 $credentialclass = $1;
59 }
60
0f673ca7 61 # if we made it here - we have what we need to load the classes
62
63 ### BACKWARDS COMPATIBILITY - DEPRECATION WARNING:
64 ### we must eval the ensure_class_loaded - because we might need to try the old-style
65 ### ::Plugin:: module naming if the standard method fails.
66
71486cb0 67 ## Note to self - catch second exception and bitch in detail?
68
0f673ca7 69 eval {
70 Catalyst::Utils::ensure_class_loaded( $credentialclass );
71 };
72
73 if ($@) {
607d5cc0 74 # If the file is missing, then try the old-style fallback,
75 # but re-throw anything else for the user to deal with.
76 die unless $@ =~ /^Can't locate/;
0f673ca7 77 $app->log->warn( qq(Credential class "$credentialclass" not found, trying deprecated ::Plugin:: style naming. ) );
71486cb0 78 my $origcredentialclass = $credentialclass;
0f673ca7 79 $credentialclass =~ s/Catalyst::Authentication/Catalyst::Plugin::Authentication/;
71486cb0 80
81 eval { Catalyst::Utils::ensure_class_loaded( $credentialclass ); };
82 if ($@) {
607d5cc0 83 # Likewise this croak is useful if the second exception is also "not found",
84 # but would be confusing if it's anything else.
85 die unless $@ =~ /^Can't locate/;
71486cb0 86 Carp::croak "Unable to load credential class, " . $origcredentialclass . " OR " . $credentialclass .
87 " in realm " . $self->name;
88 }
0f673ca7 89 }
90
91 eval {
92 Catalyst::Utils::ensure_class_loaded( $storeclass );
93 };
94
95 if ($@) {
607d5cc0 96 # If the file is missing, then try the old-style fallback,
97 # but re-throw anything else for the user to deal with.
98 die unless $@ =~ /^Can't locate/;
0f673ca7 99 $app->log->warn( qq(Store class "$storeclass" not found, trying deprecated ::Plugin:: style naming. ) );
71486cb0 100 my $origstoreclass = $storeclass;
0f673ca7 101 $storeclass =~ s/Catalyst::Authentication/Catalyst::Plugin::Authentication/;
71486cb0 102 eval { Catalyst::Utils::ensure_class_loaded( $storeclass ); };
103 if ($@) {
607d5cc0 104 # Likewise this croak is useful if the second exception is also "not found",
105 # but would be confusing if it's anything else.
106 die unless $@ =~ /^Can't locate/;
71486cb0 107 Carp::croak "Unable to load store class, " . $origstoreclass . " OR " . $storeclass .
108 " in realm " . $self->name;
109 }
0f673ca7 110 }
810966b5 111
112 # BACKWARDS COMPATIBILITY - if the store class does not define find_user, we define it in terms
113 # of get_user and add it to the class. this is because the auth routines use find_user,
114 # and rely on it being present. (this avoids per-call checks)
115 if (!$storeclass->can('find_user')) {
116 no strict 'refs';
117 *{"${storeclass}::find_user"} = sub {
118 my ($self, $info) = @_;
119 my @rest = @{$info->{rest}} if exists($info->{rest});
120 $self->get_user($info->{id}, @rest);
121 };
122 }
123
124 ## a little cruft to stay compatible with some poorly written stores / credentials
125 ## we'll remove this soon.
126 if ($storeclass->can('new')) {
127 $self->store($storeclass->new($config->{'store'}, $app, $self));
128 } else {
129 $app->log->error("THIS IS DEPRECATED: $storeclass has no new() method - Attempting to use uninstantiated");
130 $self->store($storeclass);
131 }
132 if ($credentialclass->can('new')) {
133 $self->credential($credentialclass->new($config->{'credential'}, $app, $self));
134 } else {
135 $app->log->error("THIS IS DEPRECATED: $credentialclass has no new() method - Attempting to use uninstantiated");
136 $self->credential($credentialclass);
137 }
138
139 return $self;
140}
141
142sub find_user {
143 my ( $self, $authinfo, $c ) = @_;
144
145 my $res = $self->store->find_user($authinfo, $c);
146
4437366d 147 if (!$res) {
9b840849 148 if ($self->config->{'auto_create_user'} && $self->store->can('auto_create_user') ) {
149 $res = $self->store->auto_create_user($authinfo, $c);
4437366d 150 }
9b840849 151 } elsif ($self->config->{'auto_update_user'} && $self->store->can('auto_update_user')) {
152 $res = $self->store->auto_update_user($authinfo, $c, $res);
4437366d 153 }
810966b5 154
155 return $res;
156}
157
158sub authenticate {
159 my ($self, $c, $authinfo) = @_;
160
161 my $user = $self->credential->authenticate($c, $self, $authinfo);
162 if (ref($user)) {
163 $c->set_authenticated($user, $self->name);
164 return $user;
165 } else {
166 return undef;
167 }
168}
169
71486cb0 170sub user_is_restorable {
171 my ($self, $c) = @_;
172
173 return unless
174 $c->isa("Catalyst::Plugin::Session")
9a37ffba 175 and $self->config->{'use_session'}
71486cb0 176 and $c->session_is_valid;
810966b5 177
71486cb0 178 return $c->session->{__user};
179}
180
181sub restore_user {
182 my ($self, $c, $frozen_user) = @_;
810966b5 183
71486cb0 184 $frozen_user ||= $self->user_is_restorable($c);
185 return unless defined($frozen_user);
186
9c08483f 187 my $user = $self->from_session( $c, $frozen_user );
71486cb0 188
9c08483f 189 if ($user) {
190 $c->_user( $user );
71486cb0 191
9c08483f 192 # this sets the realm the user originated in.
193 $user->auth_realm($self->name);
194 } else {
195 Catalyst::Exception->throw("Store claimed to have a restorable user, but restoration failed. Did you change the user's id_field?");
196 }
197
71486cb0 198 return $user;
199}
200
201sub persist_user {
202 my ($self, $c, $user) = @_;
203
204 if (
205 $c->isa("Catalyst::Plugin::Session")
9a37ffba 206 and $self->config->{'use_session'}
71486cb0 207 and $user->supports("session")
208 ) {
209 $c->session->{__user_realm} = $self->name;
210
211 # we want to ask the store for a user prepared for the session.
212 # but older modules split this functionality between the user and the
213 # store. We try the store first. If not, we use the old method.
214 if ($self->store->can('for_session')) {
215 $c->session->{__user} = $self->store->for_session($c, $user);
216 } else {
217 $c->session->{__user} = $user->for_session;
218 }
810966b5 219 }
71486cb0 220 return $user;
221}
222
223sub remove_persisted_user {
224 my ($self, $c) = @_;
225
226 if (
227 $c->isa("Catalyst::Plugin::Session")
9a37ffba 228 and $self->config->{'use_session'}
71486cb0 229 and $c->session_is_valid
230 ) {
231 delete @{ $c->session }{qw/__user __user_realm/};
232 }
233}
234
235## backwards compatibility - I don't think many people wrote realms since they
236## have only existed for a short time - but just in case.
237sub save_user_in_session {
238 my ( $self, $c, $user ) = @_;
239
240 return $self->persist_user($c, $user);
810966b5 241}
242
243sub from_session {
244 my ($self, $c, $frozen_user) = @_;
245
246 return $self->store->from_session($c, $frozen_user);
247}
248
249
250__PACKAGE__;
251
b72f8c2e 252__END__
2c7d23af 253
254=pod
255
256=head1 NAME
257
e0499ed6 258Catalyst::Authentication::Realm - Base class for realm objects.
2c7d23af 259
260=head1 DESCRIPTION
261
d2ca09b8 262=head1 CONFIGURATION
2c7d23af 263
264=over 4
265
d2ca09b8 266=item class
267
71486cb0 268By default this class is used by
269L<Catalyst::Plugin::Authentication|Catalyst::Plugin::Authentication> for all
270realms. The class parameter allows you to choose a different class to use for
271this realm. Creating a new Realm class can allow for authentication methods
272that fall outside the normal credential/store methodology.
deebc899 273
d2ca09b8 274=item auto_create_user
2c7d23af 275
deebc899 276Set this to true if you wish this realm to auto-create user accounts when the
277user doesn't exist (most useful for remote authentication schemes).
278
d2ca09b8 279=item auto_update_user
2c7d23af 280
deebc899 281Set this to true if you wish this realm to auto-update user accounts after
282authentication (most useful for remote authentication schemes).
283
928722c0 284=item use_session
285
286Sets session usage for this particular realm - overriding the global use_sesion setting.
287
288
d2ca09b8 289=back
290
291=head1 METHODS
2c7d23af 292
71486cb0 293=head2 new( $realmname, $config, $app )
2c7d23af 294
deebc899 295Instantiantes this realm, plus the specified store and credential classes.
296
297=head2 store( )
298
71486cb0 299Returns an instance of the store object for this realm.
deebc899 300
301=head2 credential( )
302
71486cb0 303Returns an instance of the credential object for this realm.
deebc899 304
71486cb0 305=head2 find_user( $authinfo, $c )
d2ca09b8 306
71486cb0 307Retrieves the user given the authentication information provided. This
308is most often called from the credential. The default realm class simply
309delegates this call the store object. If enabled, auto-creation and
310auto-updating of users is also handled here.
deebc899 311
71486cb0 312=head2 authenticate( $c, $authinfo)
d2ca09b8 313
71486cb0 314Performs the authentication process for the current realm. The default
315realm class simply delegates this to the credential and sets
316the authenticated user on success. Returns the authenticated user object;
deebc899 317
928722c0 318=head1 USER PERSISTENCE
d2ca09b8 319
a98ce95b 320The Realm class allows complete control over the persistance of users
321between requests. By default the realm attempts to use the Catalyst
322session system to accomplish this. By overriding the methods below
323in a custom Realm class, however, you can handle user persistance in
324any way you see fit.
deebc899 325
bc119a79 326=head2 persist_user($c, $user)
327
a98ce95b 328persist_user is the entry point for saving user information between requests
329in most cases this will utilize the session. By default this uses the
330catalyst session system to store the user by calling for_session on the
331active store. The user object must be a subclass of
332Catalyst::Authentication::User. If you have updated the user object, you
333must call persist_user again to ensure that the persisted user object reflects
334your updates.
bc119a79 335
336=head2 remove_persisted_user($c)
337
a98ce95b 338Removes any persisted user data. By default, removes the user from the session.
bc119a79 339
a98ce95b 340=head2 user_is_restorable( $c )
bc119a79 341
a98ce95b 342Returns whether there is a persisted user that may be restored. Returns
343a token used to restore the user. With the default session persistance
344it returns the raw frozen user information.
bc119a79 345
a98ce95b 346=head2 restore_user($c, [$frozen_user])
347
348Restores the user from the given frozen_user parameter, or if not provided,
349using the response from $self->user_is_restorable(); Uses $self->from_session()
350to decode the frozen user.
bc119a79 351
bc119a79 352
71486cb0 353=head2 from_session($c, $frozenuser )
2c7d23af 354
a98ce95b 355Decodes the frozenuser information provided and returns an instantiated
356user object. By default, this call is delegated to $store->from_session().
deebc899 357
a98ce95b 358=head2 save_user_in_session($c, $user)
2c7d23af 359
a98ce95b 360DEPRECATED. Use persist_user instead. (this simply calls persist_user)
361
362=cut