updates to auth draft
[catagits/Catalyst-Plugin-Authentication.git] / lib / Catalyst / Plugin / Authentication.pm
CommitLineData
06675d2e 1#!/usr/bin/perl
2
3package Catalyst::Plugin::Authentication;
4
5use base qw/Class::Accessor::Fast/;
6
7BEGIN { __PACKAGE__->mk_accessors(qw/user/) }
8
9use strict;
10use warnings;
11
7d0922d8 12sub default_auth_store {
13 my $c = shift;
14 $c->config->{authentication}{store};
15}
16
06675d2e 17sub set_authenticated {
18 my ( $c, $user ) = @_;
19
20 $c->user($user);
21
22 if ( $c->isa("Catalyst::Plugin::Session")
23 and $c->config->{authentication}{use_session} )
24 {
7d0922d8 25 $c->session->{__user} = $user->for_session
26 if $user->supperts("session");
06675d2e 27 $c->session->{__user_class} = ref $user;
28 }
29}
30
31sub logout {
32 my $c = shift;
33
34 $c->user(undef);
35 delete @{ $c->session }{qw/__user __user_class/};
36}
37
7d0922d8 38sub get_user {
39 my ( $c, $uid ) = @_;
40
41 if ( my $store = $c->default_auth_store ) {
42 return $store->get_user($uid);
43 }
44 else {
45 Catalyst::Exception->throw(
46 "The user id $uid was passed to an authentication "
47 . "plugin, but no default store was specified" );
48 }
49}
50
06675d2e 51sub prepare {
52 my $c = shift->NEXT::prepare(@_);
53
54 if ( $c->isa("Catalyst::Plugin::Session")
55 and $c->config->{authentication}{use_session}
56 and !$c->user )
57 {
58 if ( $c->sessionid and my $user = $c->session->{__user} ) {
59 $c->user( $c->session->{__user_class}->from_session( $c, $user ) );
60 }
61 }
62
63 return $c;
64}
65
66sub setup {
67 my $c = shift;
68
69 my $cfg = $c->config->{authentication};
70
71 %$cfg = (
72 use_session => 1,
73 %$cfg,
74 );
75}
76
77__PACKAGE__;
78
79__END__
80
81=pod
82
83=head1 NAME
84
85Catalyst::Plugin::Authentication -
86
87=head1 SYNOPSIS
88
89 use Catalyst qw/
90 Authentication
91 Authentication::Store::Foo
92 Authentication::Credential::Password
93 /;
94
95=head1 DESCRIPTION
96
97The authentication plugin is used by the various authentication and
98authorization plugins in catalyst.
99
100It defines the notion of a logged in user, and provides integration with the
101
102=head1 METHODS
103
104=over 4
105
106=item logout
107
108Delete the currently logged in user from C<user> and the session.
109
110=item user
111
112Returns the currently logged user or undef if there is none.
113
7d0922d8 114=item get_user $uid
115
116Delegate C<get_user> to the default store.
117
118=item default_auth_store
119
120Returns C<< $c->config->{authentication}{store} >>.
121
06675d2e 122=back
123
124=head1 INTERNAL METHODS
125
126=over 4
127
128=item set_authenticated $user
129
130Marks a user as authenticated. Should be called from a
131C<Catalyst::Plugin::Authentication::Credential> plugin after successful
132authentication.
133
134This involves setting C<user> and the internal data in C<session> if
135L<Catalyst::Plugin::Session> is loaded.
136
137=item prepare
138
139Revives a user from the session object if there is one.
140
141=item setup
142
143Sets the default configuration parameters.
144
145=item
146
147=back
148
149=head1 CONFIGURATION
150
151=over 4
152
153=item use_session
154
155Whether or not to store the user's logged in state in the session, if the
156application is also using the L<Catalyst::Plugin::Authentication> plugin.
157
158=back
159
160=cut
161
162