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);
33 $c->NEXT::set_authenticated( $user );
45 if ( $user and !Scalar::Util::blessed($user) ) {
46 return $c->auth_restore_user($user);
52 sub save_user_in_session {
53 my ( $c, $user ) = @_;
55 my $store = $user->store || ref $user;
56 $c->session->{__user_store} = $c->get_auth_store_name($store) || $store;
57 $c->session->{__user} = $user->for_session;
65 if ( $c->isa("Catalyst::Plugin::Session")
66 and $c->config->{authentication}{use_session} )
68 delete @{ $c->session }{qw/__user __user_store/};
75 if ( my $store = $c->default_auth_store ) {
76 return $store->get_user($uid);
79 Catalyst::Exception->throw(
80 "The user id $uid was passed to an authentication "
81 . "plugin, but no default store was specified" );
86 my $c = shift->NEXT::prepare(@_);
88 if ( $c->isa("Catalyst::Plugin::Session")
91 if ( $c->sessionid and my $frozen_user = $c->session->{__user} ) {
92 $c->_user($frozen_user);
99 sub auth_restore_user {
100 my ( $c, $frozen_user, $store_name ) = @_;
102 return unless $c->isa("Catalyst::PLugin::Session") and $c->config->{authentication}{use_session} and $c->sessionid;
104 $store_name ||= $c->session->{__user_store};
105 $frozen_user ||= $c->session->{__user};
107 my $store = $c->get_auth_store($store_name);
108 $c->_user( my $user = $store->from_session( $c, $frozen_user ) );
117 my $cfg = $c->config->{authentication} || {};
124 $c->register_auth_stores(
125 default => $cfg->{store},
126 %{ $cfg->{stores} || {} },
133 my ( $self, $name ) = @_;
134 $self->auth_stores->{$name} || ( Class::Inspector->loaded($name) && $name );
137 sub get_auth_store_name {
138 my ( $self, $store ) = @_;
139 $self->auth_store_names->{$store};
142 sub register_auth_stores {
143 my ( $self, %new ) = @_;
145 foreach my $name ( keys %new ) {
146 my $store = $new{$name} or next;
147 $self->auth_stores->{$name} = $store;
148 $self->auth_store_names->{$store} = $name;
154 $self->_auth_stores(@_) || $self->_auth_stores( {} );
157 sub auth_store_names {
160 $self->_auth_store_names || do {
161 tie my %hash, 'Tie::RefHash';
162 $self->_auth_store_names( \%hash );
166 sub default_auth_store {
169 if ( my $new = shift ) {
170 $self->register_auth_stores( default => $new );
173 $self->get_auth_store("default");
184 Catalyst::Plugin::Authentication - Infrastructure plugin for the Catalyst
185 authentication framework.
191 Authentication::Store::Foo
192 Authentication::Credential::Password
197 The authentication plugin is used by the various authentication and
198 authorization plugins in catalyst.
200 It defines the notion of a logged in user, and provides integration with the
201 L<Catalyst::Plugin::Session> plugin,
209 Returns the currently logged user or undef if there is none.
213 Delete the currently logged in user from C<user> and the session.
217 Delegate C<get_user> to the default store.
219 =item default_auth_store
221 Returns C<< $c->config->{authentication}{store} >>.
225 =head1 INTERNAL METHODS
229 =item set_authenticated $user
231 Marks a user as authenticated. Should be called from a
232 C<Catalyst::Plugin::Authentication::Credential> plugin after successful
235 This involves setting C<user> and the internal data in C<session> if
236 L<Catalyst::Plugin::Session> is loaded.
238 =item auth_restore_user $user
240 Used to restore a user from the session, by C<user> only when it's actually
243 =item save_user_in_session $user
245 Used to save the user in a session.
249 Revives a user from the session object if there is one.
253 Sets the default configuration parameters.
265 Whether or not to store the user's logged in state in the session, if the
266 application is also using the L<Catalyst::Plugin::Authentication> plugin.