3 package Catalyst::Plugin::Authentication;
5 use base qw/Class::Accessor::Fast Class::Data::Inheritable/;
8 __PACKAGE__->mk_accessors(qw/_user/);
9 __PACKAGE__->mk_classdata($_) for qw/_auth_stores _auth_store_names/;
18 our $VERSION = "0.01";
20 sub set_authenticated {
21 my ( $c, $user ) = @_;
24 $c->request->{user} = $user; # compatibility kludge
26 if ( $c->isa("Catalyst::Plugin::Session")
27 and $c->config->{authentication}{use_session}
28 and $user->supports("session") )
30 $c->save_user_in_session($user);
43 if ( $user and !Scalar::Util::blessed($user) ) {
44 return $c->auth_restore_user($user);
50 sub save_user_in_session {
51 my ( $c, $user ) = @_;
53 my $store = $user->store || ref $user;
54 $c->session->{__user_store} = $c->get_auth_store_name($store) || $store;
55 $c->session->{__user} = $user->for_session;
63 if ( $c->isa("Catalyst::Plugin::Session")
64 and $c->config->{authentication}{use_session} )
66 delete @{ $c->session }{qw/__user __user_store/};
73 if ( my $store = $c->default_auth_store ) {
74 return $store->get_user($uid);
77 Catalyst::Exception->throw(
78 "The user id $uid was passed to an authentication "
79 . "plugin, but no default store was specified" );
84 my $c = shift->NEXT::prepare(@_);
86 if ( $c->isa("Catalyst::Plugin::Session")
87 and $c->default_auth_store
90 if ( $c->sessionid and my $frozen_user = $c->session->{__user} ) {
91 $c->_user($frozen_user);
98 sub auth_restore_user {
99 my ( $c, $frozen_user, $store_name ) = @_;
101 $store_name ||= $c->session->{__user_store};
102 $frozen_user ||= $c->session->{__user};
104 my $store = $c->get_auth_store($store_name);
105 $c->_user( my $user = $store->from_session( $c, $frozen_user ) );
114 my $cfg = $c->config->{authentication} || {};
121 $c->register_auth_stores(
122 default => $cfg->{store},
123 %{ $cfg->{stores} || {} },
130 my ( $self, $name ) = @_;
131 $self->auth_stores->{$name} || ( Class::Inspector->loaded($name) && $name );
134 sub get_auth_store_name {
135 my ( $self, $store ) = @_;
136 $self->auth_store_names->{$store};
139 sub register_auth_stores {
140 my ( $self, %new ) = @_;
142 foreach my $name ( keys %new ) {
143 my $store = $new{$name} or next;
144 $self->auth_stores->{$name} = $store;
145 $self->auth_store_names->{$store} = $name;
151 $self->_auth_stores(@_) || $self->_auth_stores( {} );
154 sub auth_store_names {
157 unless ( $self->_auth_store_names ) {
158 tie my %hash, 'Tie::RefHash';
159 $self->_auth_store_names( \%hash );
162 $self->_auth_store_names;
165 sub default_auth_store {
168 if ( my $new = shift ) {
169 $self->register_auth_stores( default => $new );
172 $self->get_auth_store("default");
183 Catalyst::Plugin::Authentication -
189 Authentication::Store::Foo
190 Authentication::Credential::Password
195 The authentication plugin is used by the various authentication and
196 authorization plugins in catalyst.
198 It defines the notion of a logged in user, and provides integration with the
206 Delete the currently logged in user from C<user> and the session.
210 Returns the currently logged user or undef if there is none.
214 Delegate C<get_user> to the default store.
216 =item default_auth_store
218 Returns C<< $c->config->{authentication}{store} >>.
222 =head1 INTERNAL METHODS
226 =item set_authenticated $user
228 Marks a user as authenticated. Should be called from a
229 C<Catalyst::Plugin::Authentication::Credential> plugin after successful
232 This involves setting C<user> and the internal data in C<session> if
233 L<Catalyst::Plugin::Session> is loaded.
235 =item auth_restore_user $user
237 Used to restore a user from the session, by C<user> only when it's actually
240 =item save_user_in_session $user
242 Used to save the user in a session.
246 Revives a user from the session object if there is one.
250 Sets the default configuration parameters.
262 Whether or not to store the user's logged in state in the session, if the
263 application is also using the L<Catalyst::Plugin::Authentication> plugin.