1 package Catalyst::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::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::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::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::A::Credential::(specifiedclass)
45 if ($credentialclass !~ /^\+(.*)$/ ) {
46 $credentialclass = "Catalyst::Authentication::Credential::${credentialclass}";
48 $credentialclass = $1;
51 # if we made it here - we have what we need to load the classes
53 ### BACKWARDS COMPATIBILITY - DEPRECATION WARNING:
54 ### we must eval the ensure_class_loaded - because we might need to try the old-style
55 ### ::Plugin:: module naming if the standard method fails.
58 Catalyst::Utils::ensure_class_loaded( $credentialclass );
62 $app->log->warn( qq(Credential class "$credentialclass" not found, trying deprecated ::Plugin:: style naming. ) );
63 $credentialclass =~ s/Catalyst::Authentication/Catalyst::Plugin::Authentication/;
64 Catalyst::Utils::ensure_class_loaded( $credentialclass );
68 Catalyst::Utils::ensure_class_loaded( $storeclass );
72 $app->log->warn( qq(Store class "$storeclass" not found, trying deprecated ::Plugin:: style naming. ) );
73 $storeclass =~ s/Catalyst::Authentication/Catalyst::Plugin::Authentication/;
74 Catalyst::Utils::ensure_class_loaded( $storeclass );
77 # BACKWARDS COMPATIBILITY - if the store class does not define find_user, we define it in terms
78 # of get_user and add it to the class. this is because the auth routines use find_user,
79 # and rely on it being present. (this avoids per-call checks)
80 if (!$storeclass->can('find_user')) {
82 *{"${storeclass}::find_user"} = sub {
83 my ($self, $info) = @_;
84 my @rest = @{$info->{rest}} if exists($info->{rest});
85 $self->get_user($info->{id}, @rest);
89 ## a little cruft to stay compatible with some poorly written stores / credentials
90 ## we'll remove this soon.
91 if ($storeclass->can('new')) {
92 $self->store($storeclass->new($config->{'store'}, $app, $self));
94 $app->log->error("THIS IS DEPRECATED: $storeclass has no new() method - Attempting to use uninstantiated");
95 $self->store($storeclass);
97 if ($credentialclass->can('new')) {
98 $self->credential($credentialclass->new($config->{'credential'}, $app, $self));
100 $app->log->error("THIS IS DEPRECATED: $credentialclass has no new() method - Attempting to use uninstantiated");
101 $self->credential($credentialclass);
108 my ( $self, $authinfo, $c ) = @_;
110 my $res = $self->store->find_user($authinfo, $c);
113 if ($self->config->{'auto_create_user'} && $self->store->can('auto_create_user') ) {
114 $res = $self->store->auto_create_user($authinfo, $c);
116 } elsif ($self->config->{'auto_update_user'} && $self->store->can('auto_update_user')) {
117 $res = $self->store->auto_update_user($authinfo, $c, $res);
124 my ($self, $c, $authinfo) = @_;
126 my $user = $self->credential->authenticate($c, $self, $authinfo);
128 $c->set_authenticated($user, $self->name);
135 sub save_user_in_session {
136 my ( $self, $c, $user ) = @_;
138 $c->session->{__user_realm} = $self->name;
140 # we want to ask the store for a user prepared for the session.
141 # but older modules split this functionality between the user and the
142 # store. We try the store first. If not, we use the old method.
143 if ($self->store->can('for_session')) {
144 $c->session->{__user} = $self->store->for_session($c, $user);
146 $c->session->{__user} = $user->for_session;
151 my ($self, $c, $frozen_user) = @_;
153 return $self->store->from_session($c, $frozen_user);
165 Catalyst::Authentication::Realm - Base class for realm objects.
175 By default this class is the default realm class. You can specify a custom
176 realm class with this config parameter.
178 =item auto_create_user
180 Set this to true if you wish this realm to auto-create user accounts when the
181 user doesn't exist (most useful for remote authentication schemes).
183 =item auto_update_user
185 Set this to true if you wish this realm to auto-update user accounts after
186 authentication (most useful for remote authentication schemes).
194 Instantiantes this realm, plus the specified store and credential classes.
198 Holds an instance of the store object for this realm.
202 Holds an instance of the credential object for this realm.
206 Delegates to the store object. Will also re-delegate auto_create_user and
207 auto_update_user at this time, if necessary.
209 =head2 authenticate( )
211 Delegates to the credential objects and sets the authenticated user on success.
213 =head2 save_user_in_session( )
215 Delegates to the store object.
217 =head2 from_session( )
219 Delegates to the store object.