Tag old version. Update trunk to new version with additional tests
[catagits/Catalyst-Authentication-Credential-HTTP-Proxy.git] / lib / Catalyst / Authentication / Credential / HTTP / Proxy.pm
CommitLineData
b2cd5ce7 1package Catalyst::Authentication::Credential::HTTP::Proxy;
2use base qw/Catalyst::Authentication::Credential::HTTP/;
3
4use strict;
5use warnings;
6
7use String::Escape ();
8use URI::Escape ();
9use Catalyst::Authentication::Credential::HTTP::Proxy::User;
10
11our $VERSION = "0.04";
12
13sub init {
14 my ($self) = @_;
15
16 my $type = $self->_config->{'type'} ||= 'basic';
17
18 if (!$self->_config->{url}) {
19 Catalyst::Exception->throw(__PACKAGE__ . " configuration does not include a 'url' key, cannot proceed");
20 }
21
22 if (!grep /^$type$/, ('basic')) {
23 Catalyst::Exception->throw(__PACKAGE__ . " used with unsupported authentication type: " . $type);
24 }
25}
26
27sub authenticate_basic {
28 my ( $self, $c, $realm, $auth_info ) = @_;
29
30 $c->log->debug('Checking http basic authentication.') if $c->debug;
31
32 my $headers = $c->req->headers;
33
34 if ( my ( $user, $password ) = $headers->authorization_basic ) {
35 my $ua = Catalyst::Authentication::Credential::HTTP::Proxy::User->new;
36 $ua->credentials($user, $password);
37 my $resp = $ua->get($self->_config->{url});
38 if ( $resp->is_success ) {
39 # Config username_field TODO
40 my $user_obj = $realm->find_user( { username => $user }, $c);
41 unless ($user_obj) {
42 $c->log->debug("User '$user' doesn't exist in the default store")
43 if $c->debug;
44 return;
45 }
46 $c->set_authenticated($user_obj);
47 return 1;
48 }
49 else {
50 $c->log->info('Remote authentication failed:'.$resp->message);
51 return 0;
52 }
53 }
54 elsif ( $c->debug ) {
55 $c->log->info('No credentials provided for basic auth');
56 return 0;
57 }
58}
59
60__END__
61
62=pod
63
64=head1 NAME
65
66Catalyst::Authentication::Credential::HTTP::Proxy - HTTP Proxy authentication
67for Catlayst.
68
69=head1 SYNOPSIS
70
71 use Catalyst qw/
72 Authentication
73 /;
74
75 $c->config( authentication => {
76 realms => {
77 example => {
78 credential => {
79 class => 'HTTP::Proxy',
80 type => 'basic', # Only basic supported
81 url => 'http://elkland.no/auth',
82 },
83 },
84 store => {
85 class => 'Minimal',
86 users => {
87 Mufasa => { password => "Circle Of Life", },
88 },
89 },
90 },
91 });
92
93 sub foo : Local {
94 my ( $self, $c ) = @_;
95
96 $c->authenticate();
97
98 # either user gets authenticated or 401 is sent
99
100 do_stuff();
101 }
102
103=head1 DESCRIPTION
104
105This moduule lets you use HTTP Proxy authentication with
106L<Catalyst::Plugin::Authentication>.
107
108Currently this module only supports the Basic scheme, but upon request Digest
109will also be added. Patches welcome!
110
111=head1 CONFIG
112
113All configuration is stored in C<< YourApp->config(authentication => { yourrealm => { credential => { class => 'HTTP::Proxy', %config } } } >>.
114
115This should be a hash, and it can contain the following entries:
116
117=over 4
118
119=item url
120
121Required. A url protected with basic authentication to authenticate against.
122
123=item type
124
125Must be either C<basic> or not present (then it defaults to C<basic>).
126
127This will be used to support digest authentication in future.
128
129=back
130
131=head1 METHODS
132
133=over
134
135=item init
136
137Initializes the configuration.
138
139=item authenticate_basic
140
141Looks inside C<< $c->request->headers >> and processes the basic (badly named)
142authorization header. Then authenticates this against the provided url.
143
144=back
145
146=head1 AUTHORS
147
148Marcus Ramberg <mramberg@cpan.org>
149
150Tomas Doran <bobtfish@bobtfish.net>
151
152=head1 COPYRIGHT & LICENSE
153
154 Copyright (c) 2005-2008 the aforementioned authors. All rights
155 reserved. This program is free software; you can redistribute
156 it and/or modify it under the same terms as Perl itself.
157
158=cut
159