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 ) = @_;
25 if ( $c->isa("Catalyst::Plugin::Session")
26 and $c->config->{authentication}{use_session}
27 and $user->supports("session") )
29 $c->save_user_in_session($user);
33 sub save_user_in_session {
34 my ( $c, $user ) = @_;
36 my $store = $user->store || ref $user;
37 $c->session->{__user_store} = $c->get_auth_store_name($store) || $store;
38 $c->session->{__user} = $user->for_session;
46 if ( $c->isa("Catalyst::Plugin::Session")
47 and $c->config->{authentication}{use_session} )
49 delete @{ $c->session }{qw/__user __user_store/};
56 if ( my $store = $c->default_auth_store ) {
57 return $store->get_user($uid);
60 Catalyst::Exception->throw(
61 "The user id $uid was passed to an authentication "
62 . "plugin, but no default store was specified" );
67 my $c = shift->NEXT::prepare(@_);
69 if ( $c->isa("Catalyst::Plugin::Session")
70 and $c->default_auth_store
73 if ( $c->sessionid and my $user_id = $c->session->{__user} ) {
74 my $store = $c->get_auth_store( $c->session->{__user_store} );
75 $c->user( $store->from_session( $c, $user_id ) );
76 $c->request->{user} = $c->user; # compatibility kludge
86 my $cfg = $c->config->{authentication} || {};
93 $c->register_auth_stores(
94 default => $cfg->{store},
95 %{ $cfg->{stores} || {} },
102 my ( $self, $name ) = @_;
103 $self->auth_stores->{$name} || ( Class::Inspector->loaded($name) && $name );
106 sub get_auth_store_name {
107 my ( $self, $store ) = @_;
108 $self->auth_store_names->{$store};
111 sub register_auth_stores {
112 my ( $self, %new ) = @_;
114 foreach my $name ( keys %new ) {
115 my $store = $new{$name} or next;
116 $self->auth_stores->{$name} = $store;
117 $self->auth_store_names->{$store} = $name;
123 $self->_auth_stores(@_) || $self->_auth_stores( {} );
126 sub auth_store_names {
129 unless ( $self->_auth_store_names ) {
130 tie my %hash, 'Tie::RefHash';
131 $self->_auth_store_names( \%hash );
134 $self->_auth_store_names;
137 sub default_auth_store {
140 if ( my $new = shift ) {
141 $self->register_auth_stores( default => $new );
144 $self->get_auth_store("default");
155 Catalyst::Plugin::Authentication -
161 Authentication::Store::Foo
162 Authentication::Credential::Password
167 The authentication plugin is used by the various authentication and
168 authorization plugins in catalyst.
170 It defines the notion of a logged in user, and provides integration with the
178 Delete the currently logged in user from C<user> and the session.
182 Returns the currently logged user or undef if there is none.
186 Delegate C<get_user> to the default store.
188 =item default_auth_store
190 Returns C<< $c->config->{authentication}{store} >>.
194 =head1 INTERNAL METHODS
198 =item set_authenticated $user
200 Marks a user as authenticated. Should be called from a
201 C<Catalyst::Plugin::Authentication::Credential> plugin after successful
204 This involves setting C<user> and the internal data in C<session> if
205 L<Catalyst::Plugin::Session> is loaded.
209 Revives a user from the session object if there is one.
213 Sets the default configuration parameters.
225 Whether or not to store the user's logged in state in the session, if the
226 application is also using the L<Catalyst::Plugin::Authentication> plugin.