1 package Catalyst::Plugin::Authentication::Credential::HTTP::Proxy;
2 use base qw/Catalyst::Plugin::Authentication::Credential::Password/;
10 use Catalyst::Plugin::Authentication::Credential::HTTP::User;
13 our $VERSION = "0.02";
15 sub authenticate_http_proxy {
18 my $headers = $c->req->headers;
20 croak "url setting required for authentication"
21 unless $c->config->{authentication}{http_proxy}{url};
22 if ( my ( $user, $password ) = $headers->authorization_basic ) {
24 my $ua=Catalyst::Plugin::Authentication::Credential::HTTP::User->new;
25 $ua->credentials($user,$password);
26 my $resp= $ua->get($c->config->{authentication}{http_proxy}{url});
27 if ( $resp->is_success ) {
28 if ( my $store = $c->config->{authentication}{http_proxy}{store} ) {
29 $user = $store->get_user($user);
30 } elsif ( my $user_obj = $c->get_user($user) ) {
34 $c->log->debug("User '$user' doesn't exist in the default store")
38 $c->set_authenticated($user);
40 } elsif ( $c->debug ) {
41 $c->log->info('Remote authentication failed:'.$resp->message);
44 } elsif ( $c->debug ) {
45 $c->log->info('No credentials provided for basic auth');
50 sub authorization_required {
51 my ( $c, %opts ) = @_;
53 return 1 if $c->authenticate_http_proxy;
55 $c->authorization_required_response( %opts );
57 die $Catalyst::DETACH;
60 sub authorization_required_response {
61 my ( $c, %opts ) = @_;
67 if ( my $realm = $opts{realm} ) {
68 push @opts, sprintf 'realm=%s', String::Escape::qprintable($realm);
71 if ( my $domain = $opts{domain} ) {
72 Catalyst::Excpetion->throw("domain must be an array reference")
73 unless ref($domain) && ref($domain) eq "ARRAY";
76 $c->config->{authentication}{http}{use_uri_for}
77 ? ( map { $c->uri_for($_) } @$domain )
78 : ( map { URI::Escape::uri_escape($_) } @$domain );
80 push @opts, qq{domain="@uris"};
83 $c->res->headers->www_authenticate(join " ", "Basic", @opts);
94 Catalyst::Plugin::Authentication::Credential::HTTP::Proxy - HTTP Proxy authentication
101 Authentication::Store::Moose
102 Authentication::Store::Elk
103 Authentication::Credential::HTTP::Proxy
106 $c->config->{authentication}{http_proxy}= {
107 url =>'http://elkland.no/auth',
108 store => 'Authentication::Store::Moose'
112 my ( $self, $c ) = @_;
114 $c->authorization_required( realm => "foo" ); # named after the status code ;-)
116 # either user gets authenticated or 401 is sent
122 __PACKAGE__->deny_access_unless("/path", sub { $_[0]->authenticate_http });
125 my ( $self, $c ) = @_;
127 $c->authorization_required_response( realm => "foo" );
133 This moduule lets you use HTTP Proxy authentication with
134 L<Catalyst::Plugin::Authentication>.
136 Currently this module only supports the Basic scheme, but upon request Digest
137 will also be added. Patches welcome!
142 This module reads config from $c->config->{authentication}{http_proxy}. The following settings
149 Required. A url protected with basic authentication to authenticate against.
153 To specify what store to use. will use the default store if not set.
161 =item authorization_required
163 Tries to C<authenticate_http_proxy>, and if that fails calls
164 C<authorization_required_response> and detaches the current action call stack.
166 =item authenticate_http_proxy
168 Looks inside C<< $c->request->headers >> and processes the basic (badly named)
169 authorization header. Then authenticates this against the provided url.
171 =item authorization_required_response
173 Sets C<< $c->response >> to the correct status code, and adds the correct
174 header to demand authentication data from the user agent.
180 Marcus Ramberg <mramberg@cpan.org
182 =head1 COPYRIGHT & LICENSE
184 Copyright (c) 2005 the aforementioned authors. All rights
185 reserved. This program is free software; you can redistribute
186 it and/or modify it under the same terms as Perl itself.