1 package Catalyst::Plugin::Authentication::Realm;
5 use base qw/Class::Accessor::Fast/;
8 __PACKAGE__->mk_accessors(qw/store credential name config/);
12 my ($class, $realmname, $config, $app) = @_;
14 my $self = { config => $config };
17 $self->name($realmname);
19 $app->log->debug("Setting up auth realm $realmname") if $app->debug;
20 if (!exists($config->{'store'}{'class'})) {
21 Carp::croak "Couldn't setup the authentication realm named '$realmname', no class defined";
24 $config->{store}{class} ||= '+Catalyst::Plugin::Authentication::Store::Null';
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}";
38 # a little niceness - since most systems seem to use the password credential class,
39 # if no credential class is specified we use password.
40 $config->{credential}{class} ||= '+Catalyst::Plugin::Authentication::Credential::Password';
42 my $credentialclass = $config->{'credential'}{'class'};
44 ## follow catalyst class naming - a + prefix means a fully qualified class, otherwise it's
45 ## taken to mean C::P::A::Credential::(specifiedclass)
46 if ($credentialclass !~ /^\+(.*)$/ ) {
47 $credentialclass = "Catalyst::Plugin::Authentication::Credential::${credentialclass}";
49 $credentialclass = $1;
52 # if we made it here - we have what we need to load the classes;
53 Catalyst::Utils::ensure_class_loaded( $credentialclass );
54 Catalyst::Utils::ensure_class_loaded( $storeclass );
56 # BACKWARDS COMPATIBILITY - if the store class does not define find_user, we define it in terms
57 # of get_user and add it to the class. this is because the auth routines use find_user,
58 # and rely on it being present. (this avoids per-call checks)
59 if (!$storeclass->can('find_user')) {
61 *{"${storeclass}::find_user"} = sub {
62 my ($self, $info) = @_;
63 my @rest = @{$info->{rest}} if exists($info->{rest});
64 $self->get_user($info->{id}, @rest);
68 ## a little cruft to stay compatible with some poorly written stores / credentials
69 ## we'll remove this soon.
70 if ($storeclass->can('new')) {
71 $self->store($storeclass->new($config->{'store'}, $app, $self));
73 $app->log->error("THIS IS DEPRECATED: $storeclass has no new() method - Attempting to use uninstantiated");
74 $self->store($storeclass);
76 if ($credentialclass->can('new')) {
77 $self->credential($credentialclass->new($config->{'credential'}, $app, $self));
79 $app->log->error("THIS IS DEPRECATED: $credentialclass has no new() method - Attempting to use uninstantiated");
80 $self->credential($credentialclass);
87 my ( $self, $authinfo, $c ) = @_;
89 my $res = $self->store->find_user($authinfo, $c);
91 if (!$res && $self->config->{'auto_create'} ) {
92 $res = $self->store->auto_create($authinfo, $c);
99 my ($self, $c, $authinfo) = @_;
101 my $user = $self->credential->authenticate($c, $self, $authinfo);
103 $c->set_authenticated($user, $self->name);
110 sub save_user_in_session {
111 my ( $self, $c, $user ) = @_;
113 $c->session->{__user_realm} = $self->name;
115 # we want to ask the store for a user prepared for the session.
116 # but older modules split this functionality between the user and the
117 # store. We try the store first. If not, we use the old method.
118 if ($self->store->can('for_session')) {
119 $c->session->{__user} = $self->store->for_session($c, $user);
121 $c->session->{__user} = $user->for_session;
126 my ($self, $c, $frozen_user) = @_;
128 return $self->store->from_session($c, $frozen_user);