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/;
20 # constant->import(have_want => eval { require Want });
23 our $VERSION = "0.02";
25 sub set_authenticated {
26 my ( $c, $user ) = @_;
29 $c->request->{user} = $user; # compatibility kludge
31 if ( $c->isa("Catalyst::Plugin::Session")
32 and $c->config->{authentication}{use_session}
33 and $user->supports("session") )
35 $c->save_user_in_session($user);
38 $c->NEXT::set_authenticated($user);
50 if ( $user and !Scalar::Util::blessed($user) ) {
51 # return 1 if have_want() && Want::want("BOOL");
52 return $c->auth_restore_user($user);
58 sub save_user_in_session {
59 my ( $c, $user ) = @_;
61 my $store = $user->store || ref $user;
62 $c->session->{__user_store} = $c->get_auth_store_name($store) || $store;
63 $c->session->{__user} = $user->for_session;
71 if ( $c->isa("Catalyst::Plugin::Session")
72 and $c->config->{authentication}{use_session} )
74 delete @{ $c->session }{qw/__user __user_store/};
81 if ( my $store = $c->default_auth_store ) {
82 return $store->get_user($uid);
85 Catalyst::Exception->throw(
86 "The user id $uid was passed to an authentication "
87 . "plugin, but no default store was specified" );
92 my $c = shift->NEXT::prepare(@_);
94 if ( $c->isa("Catalyst::Plugin::Session")
97 if ( $c->sessionid and my $frozen_user = $c->session->{__user} ) {
98 $c->_user($frozen_user);
105 sub auth_restore_user {
106 my ( $c, $frozen_user, $store_name ) = @_;
109 unless $c->isa("Catalyst::Plugin::Session")
110 and $c->config->{authentication}{use_session}
113 $store_name ||= $c->session->{__user_store};
114 $frozen_user ||= $c->session->{__user};
116 my $store = $c->get_auth_store($store_name);
117 $c->_user( my $user = $store->from_session( $c, $frozen_user ) );
126 my $cfg = $c->config->{authentication} || {};
133 $c->register_auth_stores(
134 default => $cfg->{store},
135 %{ $cfg->{stores} || {} },
142 my ( $self, $name ) = @_;
143 $self->auth_stores->{$name} || ( Class::Inspector->loaded($name) && $name );
146 sub get_auth_store_name {
147 my ( $self, $store ) = @_;
148 $self->auth_store_names->{$store};
151 sub register_auth_stores {
152 my ( $self, %new ) = @_;
154 foreach my $name ( keys %new ) {
155 my $store = $new{$name} or next;
156 $self->auth_stores->{$name} = $store;
157 $self->auth_store_names->{$store} = $name;
163 $self->_auth_stores(@_) || $self->_auth_stores( {} );
166 sub auth_store_names {
169 $self->_auth_store_names || do {
170 tie my %hash, 'Tie::RefHash';
171 $self->_auth_store_names( \%hash );
175 sub default_auth_store {
178 if ( my $new = shift ) {
179 $self->register_auth_stores( default => $new );
182 $self->get_auth_store("default");
193 Catalyst::Plugin::Authentication - Infrastructure plugin for the Catalyst
194 authentication framework.
200 Authentication::Store::Foo
201 Authentication::Credential::Password
206 The authentication plugin is used by the various authentication and
207 authorization plugins in catalyst.
209 It defines the notion of a logged in user, and provides integration with the
210 L<Catalyst::Plugin::Session> plugin,
218 Returns the currently logged user or undef if there is none.
222 Delete the currently logged in user from C<user> and the session.
226 Delegate C<get_user> to the default store.
230 =head1 METHODS FOR STORE MANAGEMENT
234 =item default_auth_store
236 Return the store whose name is 'default'.
238 This is set to C<<$c->config->{authentication}{store}>> if that value exists,
239 or by using a Store plugin:
241 use Catalyst qw/Authentication Authentication::Store::Minimal/;
243 Sets the default store to
244 L<Catalyst::Plugin::Authentication::Store::Minimal::Backend>.
247 =item get_auth_store $name
249 Return the store whose name is $name.
251 =item get_auth_store_name $store
253 Return the name of the store $store.
257 A hash keyed by name, with the stores registered in the app.
259 =item auth_store_names
261 A ref-hash keyed by store, which contains the names of the stores.
263 =item register_auth_stores %stores_by_name
265 Register stores into the application.
269 =head1 INTERNAL METHODS
273 =item set_authenticated $user
275 Marks a user as authenticated. Should be called from a
276 C<Catalyst::Plugin::Authentication::Credential> plugin after successful
279 This involves setting C<user> and the internal data in C<session> if
280 L<Catalyst::Plugin::Session> is loaded.
282 =item auth_restore_user $user
284 Used to restore a user from the session, by C<user> only when it's actually
287 =item save_user_in_session $user
289 Used to save the user in a session.
293 Revives a user from the session object if there is one.
297 Sets the default configuration parameters.
309 Whether or not to store the user's logged in state in the session, if the
310 application is also using the L<Catalyst::Plugin::Authentication> plugin.
316 L<Catalyst::Plugin::Authentication::Credential::Password>,
317 L<Catalyst::Plugin::Authentication::Store::Minimal>,
318 L<Catalyst::Plugin::Authorization::ACL>,
319 L<Catalyst::Plugin::Authorization::Roles>.
323 Yuval Kogman, C<nothingmuch@woobling.org>
325 =head1 COPYRIGHT & LICNESE
327 Copyright (c) 2005 the aforementioned authors. All rights
328 reserved. This program is free software; you can redistribute
329 it and/or modify it under the same terms as Perl itself.