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/;
17 our $VERSION = "0.01";
19 sub set_authenticated {
20 my ( $c, $user ) = @_;
24 if ( $c->isa("Catalyst::Plugin::Session")
25 and $c->config->{authentication}{use_session}
26 and $user->supports("session")
29 $c->session->{__user_store} = $c->get_auth_store_name( $user->store );
30 $c->session->{__user} = $user->for_session;
39 if ( $c->isa("Catalyst::Plugin::Session")
40 and $c->config->{authentication}{use_session} )
42 delete @{ $c->session }{qw/__user __user_store/};
49 if ( my $store = $c->default_auth_store ) {
50 return $store->get_user($uid);
53 Catalyst::Exception->throw(
54 "The user id $uid was passed to an authentication "
55 . "plugin, but no default store was specified" );
60 my $c = shift->NEXT::prepare(@_);
62 if ( $c->isa("Catalyst::Plugin::Session")
63 and $c->default_auth_store
66 if ( $c->sessionid and my $user_id = $c->session->{__user} ) {
67 my $store = $c->get_auth_store( $c->session->{__user_store} );
68 $c->user( $store->from_session( $c, $user_id ) );
69 $c->request->{user} = $c->user; # compatibility kludge
80 my $cfg = $c->config->{authentication} || {};
87 $c->register_auth_stores(
88 default => $cfg->{store},
89 %{ $cfg->{stores} || {} },
96 my ( $self, $name ) = @_;
97 $self->auth_stores->{$name};
100 sub get_auth_store_name {
101 my ( $self, $store ) = @_;
102 $self->auth_store_names->{$store};
105 sub register_auth_stores {
106 my ( $self, %new ) = @_;
108 foreach my $name ( keys %new ) {
109 my $store = $new{$name} or next;
110 $self->auth_stores->{$name} = $store;
111 $self->auth_store_names->{$store} = $name;
117 $self->_auth_stores(@_) || $self->_auth_stores({});
120 sub auth_store_names {
123 unless ($self->_auth_store_names) {
124 tie my %hash, 'Tie::RefHash';
125 $self->_auth_store_names( \%hash );
128 $self->_auth_store_names;
131 sub default_auth_store {
134 if ( my $new = shift ) {
135 $self->register_auth_stores( default => $new );
138 $self->get_auth_store("default");
149 Catalyst::Plugin::Authentication -
155 Authentication::Store::Foo
156 Authentication::Credential::Password
161 The authentication plugin is used by the various authentication and
162 authorization plugins in catalyst.
164 It defines the notion of a logged in user, and provides integration with the
172 Delete the currently logged in user from C<user> and the session.
176 Returns the currently logged user or undef if there is none.
180 Delegate C<get_user> to the default store.
182 =item default_auth_store
184 Returns C<< $c->config->{authentication}{store} >>.
188 =head1 INTERNAL METHODS
192 =item set_authenticated $user
194 Marks a user as authenticated. Should be called from a
195 C<Catalyst::Plugin::Authentication::Credential> plugin after successful
198 This involves setting C<user> and the internal data in C<session> if
199 L<Catalyst::Plugin::Session> is loaded.
203 Revives a user from the session object if there is one.
207 Sets the default configuration parameters.
219 Whether or not to store the user's logged in state in the session, if the
220 application is also using the L<Catalyst::Plugin::Authentication> plugin.