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