doc updates
[catagits/Catalyst-Plugin-Authentication.git] / lib / Catalyst / Plugin / Authentication / Realm.pm
1 package Catalyst::Plugin::Authentication::Realm;
2
3 use strict;
4 use warnings;
5
6 use base qw/Class::Accessor::Fast/;
7
8 BEGIN {
9     __PACKAGE__->mk_accessors(qw/store credential name config/);
10 };
11
12 sub new {
13     my ($class, $realmname, $config, $app) = @_;
14
15     my $self = { config => $config };
16     bless $self, $class;
17     
18     $self->name($realmname);
19     
20     $app->log->debug("Setting up auth realm $realmname") if $app->debug;
21
22     # use the Null store as a default
23     if( ! exists $config->{store}{class} ) {
24         $config->{store}{class} = '+Catalyst::Plugin::Authentication::Store::Null';
25         $app->log->debug( qq(No Store specified for realm "$realmname", using the Null store.) );
26     } 
27     my $storeclass = $config->{'store'}{'class'};
28     
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}";
33     } else {
34         $storeclass = $1;
35     }
36
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::Plugin::Authentication::Credential::Password';
40
41     my $credentialclass = $config->{'credential'}{'class'};
42     
43     ## follow catalyst class naming - a + prefix means a fully qualified class, otherwise it's
44     ## taken to mean C::P::A::Credential::(specifiedclass)
45     if ($credentialclass !~ /^\+(.*)$/ ) {
46         $credentialclass = "Catalyst::Plugin::Authentication::Credential::${credentialclass}";
47     } else {
48         $credentialclass = $1;
49     }
50     
51     # if we made it here - we have what we need to load the classes;
52     Catalyst::Utils::ensure_class_loaded( $credentialclass );
53     Catalyst::Utils::ensure_class_loaded( $storeclass );
54     
55     # BACKWARDS COMPATIBILITY - if the store class does not define find_user, we define it in terms 
56     # of get_user and add it to the class.  this is because the auth routines use find_user, 
57     # and rely on it being present. (this avoids per-call checks)
58     if (!$storeclass->can('find_user')) {
59         no strict 'refs';
60         *{"${storeclass}::find_user"} = sub {
61                                                 my ($self, $info) = @_;
62                                                 my @rest = @{$info->{rest}} if exists($info->{rest});
63                                                 $self->get_user($info->{id}, @rest);
64                                             };
65     }
66     
67     ## a little cruft to stay compatible with some poorly written stores / credentials
68     ## we'll remove this soon.
69     if ($storeclass->can('new')) {
70         $self->store($storeclass->new($config->{'store'}, $app, $self));
71     } else {
72         $app->log->error("THIS IS DEPRECATED: $storeclass has no new() method - Attempting to use uninstantiated");
73         $self->store($storeclass);
74     }
75     if ($credentialclass->can('new')) {
76         $self->credential($credentialclass->new($config->{'credential'}, $app, $self));
77     } else {
78         $app->log->error("THIS IS DEPRECATED: $credentialclass has no new() method - Attempting to use uninstantiated");
79         $self->credential($credentialclass);
80     }
81     
82     return $self;
83 }
84
85 sub find_user {
86     my ( $self, $authinfo, $c ) = @_;
87
88     my $res = $self->store->find_user($authinfo, $c);
89     
90     if (!$res) {
91       if ($self->config->{'auto_create_user'} && $self->store->can('auto_create_user') ) {
92           $res = $self->store->auto_create_user($authinfo, $c);
93       }
94     } elsif ($self->config->{'auto_update_user'} && $self->store->can('auto_update_user')) {
95         $res = $self->store->auto_update_user($authinfo, $c, $res);
96     } 
97     
98     return $res;
99 }
100
101 sub authenticate {
102      my ($self, $c, $authinfo) = @_;
103
104      my $user = $self->credential->authenticate($c, $self, $authinfo);
105      if (ref($user)) {
106          $c->set_authenticated($user, $self->name);
107          return $user;
108      } else {
109          return undef;
110      }
111 }
112
113 sub save_user_in_session {
114     my ( $self, $c, $user ) = @_;
115
116     $c->session->{__user_realm} = $self->name;
117     
118     # we want to ask the store for a user prepared for the session.
119     # but older modules split this functionality between the user and the
120     # store.  We try the store first.  If not, we use the old method.
121     if ($self->store->can('for_session')) {
122         $c->session->{__user} = $self->store->for_session($c, $user);
123     } else {
124         $c->session->{__user} = $user->for_session;
125     }
126 }
127
128 sub from_session {
129     my ($self, $c, $frozen_user) = @_;
130     
131     return $self->store->from_session($c, $frozen_user);
132 }
133
134
135 __PACKAGE__;
136
137 __END__
138
139 =pod
140
141 =head1 NAME
142
143 Catalyst::Plugin::Authentication::Realm - Base class for realm objects.
144
145 =head1 DESCRIPTION
146
147 =head1 CONFIGURATION
148
149 =over 4
150
151 =item class
152
153 =item auto_create_user
154
155 =item auto_update_user
156
157 =back
158
159 =head1 METHODS
160
161 =head2 new( )
162
163 =head2 find_user( )
164
165 =head2 authenticate( )
166
167 =head2 save_user_in_session( )
168
169 =head2 from_session( )
170
171 =back
172
173 =cut
174