Initial draft of auth modules
[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/;
6
7 BEGIN { __PACKAGE__->mk_accessors(qw/user/) }
8
9 use strict;
10 use warnings;
11
12 sub set_authenticated {
13     my ( $c, $user ) = @_;
14
15     $c->user($user);
16
17     if (    $c->isa("Catalyst::Plugin::Session")
18         and $c->config->{authentication}{use_session} )
19     {
20         $c->session->{__user} = $user->for_session if $user->supperts("session");
21         $c->session->{__user_class} = ref $user;
22     }
23 }
24
25 sub logout {
26     my $c = shift;
27
28     $c->user(undef);
29     delete @{ $c->session }{qw/__user __user_class/};
30 }
31
32 sub prepare {
33     my $c = shift->NEXT::prepare(@_);
34
35     if (    $c->isa("Catalyst::Plugin::Session")
36         and $c->config->{authentication}{use_session}
37         and !$c->user )
38     {
39         if ( $c->sessionid and my $user = $c->session->{__user} ) {
40             $c->user( $c->session->{__user_class}->from_session( $c, $user ) );
41         }
42     }
43
44     return $c;
45 }
46
47 sub setup {
48     my $c = shift;
49
50     my $cfg = $c->config->{authentication};
51
52     %$cfg = (
53         use_session => 1,
54         %$cfg,
55     );
56 }
57
58 __PACKAGE__;
59
60 __END__
61
62 =pod
63
64 =head1 NAME
65
66 Catalyst::Plugin::Authentication - 
67
68 =head1 SYNOPSIS
69
70         use Catalyst qw/
71                 Authentication
72                 Authentication::Store::Foo
73                 Authentication::Credential::Password
74         /;
75
76 =head1 DESCRIPTION
77
78 The authentication plugin is used by the various authentication and
79 authorization plugins in catalyst.
80
81 It defines the notion of a logged in user, and provides integration with the 
82
83 =head1 METHODS
84
85 =over 4 
86
87 =item logout
88
89 Delete the currently logged in user from C<user> and the session.
90
91 =item user
92
93 Returns the currently logged user or undef if there is none.
94
95 =back
96
97 =head1 INTERNAL METHODS
98
99 =over 4
100
101 =item set_authenticated $user
102
103 Marks a user as authenticated. Should be called from a
104 C<Catalyst::Plugin::Authentication::Credential> plugin after successful
105 authentication.
106
107 This involves setting C<user> and the internal data in C<session> if
108 L<Catalyst::Plugin::Session> is loaded.
109
110 =item prepare
111
112 Revives a user from the session object if there is one.
113
114 =item setup
115
116 Sets the default configuration parameters.
117
118 =item 
119
120 =back
121
122 =head1 CONFIGURATION
123
124 =over 4
125
126 =item use_session
127
128 Whether or not to store the user's logged in state in the session, if the
129 application is also using the L<Catalyst::Plugin::Authentication> plugin.
130
131 =back
132
133 =cut
134
135