a776633ef365f4e8e7c247525900fb333f8cd526
[catagits/Catalyst-Authentication-Credential-HTTP-Proxy.git] / lib / Catalyst / Plugin / Authentication / Credential / TypeKey.pm
1 package Catalyst::Plugin::Authentication::Credential::TypeKey;
2
3 use strict;
4 use warnings;
5
6 use Authen::TypeKey;
7 use Carp ();
8 use File::Spec;
9 use Catalyst::Utils ();
10 use NEXT;
11 use UNIVERSAL::require;
12
13 our $VERSION = '0.1';
14
15 sub setup {
16     my $c = shift;
17
18     my $config = $c->config->{authentication}{typekey} ||= {};
19
20     $config->{typekey_object} ||= do {
21         $config->{user_class} ||=
22           "Catalyst::Plugin::Authentication::User::Hash";
23
24         $config->{key_cache} ||=
25           File::Spec->catfile( Catalyst::Utils::class2tempdir( $c, 1 ),
26             'regkeys.txt' );
27
28         my $typekey = Authen::TypeKey->new;
29
30         for (qw/expires key_cache key_url token version/) {
31             $typekey->$_( $config->{$_} || next );
32         }
33
34         $typekey;
35     };
36
37     $c->NEXT::setup(@_);
38 }
39
40 sub authenticate_typekey {
41     my ( $c, %parameters ) = @_;
42
43     my $config = $c->config->{authentication}{typekey};
44
45     my $typekey = delete( $parameters{typekey_object} )
46       || $config->{typekey_object};
47
48     my @fields = qw/email name nick ts sig/;
49
50     foreach my $field (@fields) {
51         $parameters{$field} ||= $c->req->param($field) || return;
52     }
53
54     if ( $typekey->verify( \%parameters ) ) {
55         $c->log->debug("Successfully authenticated user '$parameters{name}'.")
56           if $c->debug;
57
58         my $user;
59
60         if ( my $store = $config->{auth_store} ) {
61             $store = $c->get_auth_store($store) unless ref $store;
62             $user = $store->get_user( \%parameters );
63         }
64         else {
65             my $user_class = $config->{user_class};
66             $user_class->require or die $@;
67             $user = $user_class->new( \%parameters );
68         }
69
70         $c->set_authenticated($user);
71
72         return 1;
73     }
74     else {
75         $c->log->debug(
76             sprintf "Failed to authenticate user '%s'. Reason: '%s'",
77             $parameters{name}, $typekey->errstr )
78           if $c->debug;
79
80         return;
81     }
82 }
83
84 1;
85
86 __END__
87
88 =head1 NAME
89
90 Catalyst::Plugin::Authentication::Credential::TypeKey - TypeKey Authentication
91 for Catalyst.
92
93 =head1 SYNOPSIS
94
95     use Catalyst qw[Authentication::Credential::TypeKey];
96
97     MyApp->config->{authenticate}->{typekey} = {
98         token => 'xxxxxxxxxxxxxxxxxxxx'
99     };
100
101     sub foo : Local {
102                 my ( $self, $c ) = @_;
103
104                 if ( $c->authenticate_typekey( name => $name, email => $email, ... ) ) {
105                         # successful autentication
106
107                         $c->user; # blah
108                 }
109         }
110
111
112         sub auto : Private {
113                 my ( $self, $c ) = @_;
114
115                 $c->authenticate_typekey; # uses $c->req->params
116
117                 return 1;
118         }
119
120 =head1 DESCRIPTION
121
122 This module integrates L<Authen::TypeKey> with
123 L<Catalyst::Plugin::Authentication>.
124
125 =head1 METHODS
126
127 =item authenticate_typekey %parameters
128
129 =item authenticate_typekey
130
131 =item EXTENDED METHODS
132
133 =item setup
134
135 Fills the config with defaults.
136
137 =head1 CONFIGURATION
138
139 C<<$c->config->{autentication}{typekey}>> is a hash with these fields (all can
140 be left out):
141
142 =over 4
143
144 =item typekey_object
145
146 If this field does not exist an L<Authen::TypeKey> object will be created based
147 on the other param and put here.
148
149 =item expires
150
151 =item key_url
152
153 =item token
154
155 =item version
156
157 See L<Authen::TypeKey> for all of these. If they aren't specified
158 L<Authen::TypeKey>'s defaults will be used.
159
160 =item key_cache
161
162 Also see L<Authen::TypeKey>.
163
164 Defaults to C<regkeys.txt> under L<Catalyst::Utils/class2tempdir>.
165
166 =item auth_store
167
168 A store (or store name) to retrieve the user from.
169
170 When a user is successfully authenticated it will call this:
171
172         $store->get_user( $parameters );
173
174 Where C<$parameters> is a the hash reference passed to L<Authen::TypeKey/verify>.
175
176 =item user_class
177
178 If C<auth_store> is not set it will use this class to instantiate an object,
179 calling C<new> on the class with the same C<$parameters> hash ref.
180
181 =back
182
183 =head1 SEE ALSO
184
185 L<Authen::TypeKey>, L<Catalyst>, L<Catalyst::Plugin::Authentication>.
186
187 =head1 AUTHOR
188
189 Christian Hansen
190
191 Yuval Kogman, C<nothingmuch@woobling.org>
192
193 =head1 LICENSE
194
195 This library is free software . You can redistribute it and/or modify it under
196 the same terms as perl itself.
197
198 =cut