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