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.01";
16 sub authenticate_http_proxy {
19 my $headers = $c->req->headers;
21 croak "url setting required for authentication"
22 unless $c->config->{authentication}{http_proxy}{url};
23 if ( my ( $user, $password ) = $headers->authorization_basic ) {
25 my $ua=Catalyst::Plugin::Authentication::Credential::HTTP::User->new;
26 $ua->credentials($user,$password);
27 my $resp= $ua->get($c->config->{authentication}{http_proxy}{url});
28 if ( $resp->is_success ) {
29 if ( my $store = $c->config->{authentication}{http_proxy}{store} ) {
30 $user = $store->get_user($user);
31 } elsif ( my $user_obj = $c->get_user($user) ) {
35 $c->log->debug("User '$user' doesn't exist in the default store")
39 $c->set_authenticated($user);
41 } elsif ( $c->debug ) {
42 $c->log->info('Remote authentication failed:'.$resp->message);
45 } elsif ( $c->debug ) {
46 $c->log->info('No credentials provided for basic auth');
51 sub authorization_required {
52 my ( $c, %opts ) = @_;
54 return 1 if $c->authenticate_http_proxy;
56 $c->authorization_required_response( %opts );
58 die $Catalyst::DETACH;
61 sub authorization_required_response {
62 my ( $c, %opts ) = @_;
68 if ( my $realm = $opts{realm} ) {
69 push @opts, sprintf 'realm=%s', String::Escape::qprintable($realm);
72 if ( my $domain = $opts{domain} ) {
73 Catalyst::Excpetion->throw("domain must be an array reference")
74 unless ref($domain) && ref($domain) eq "ARRAY";
77 $c->config->{authentication}{http}{use_uri_for}
78 ? ( map { $c->uri_for($_) } @$domain )
79 : ( map { URI::Escape::uri_escape($_) } @$domain );
81 push @opts, qq{domain="@uris"};
84 $c->res->headers->www_authenticate(join " ", "Basic", @opts);
95 Catalyst::Plugin::Authentication::Credential::HTTP - HTTP Basic authentication
102 Authentication::Store::Moose
103 Authentication::Store::Elk
104 Authentication::Credential::HTTP::Proxy
107 $c->config->{authentication}{http_proxy}= {
108 url =>'http://elkland.no/auth',
109 store => 'Authentication::Store::Moose'
113 my ( $self, $c ) = @_;
115 $c->authorization_required( realm => "foo" ); # named after the status code ;-)
117 # either user gets authenticated or 401 is sent
123 __PACKAGE__->deny_access_unless("/path", sub { $_[0]->authenticate_http });
126 my ( $self, $c ) = @_;
128 $c->authorization_required_response( realm => "foo" );
134 This moduule lets you use HTTP Proxy authentication with
135 L<Catalyst::Plugin::Authentication>.
137 Currently this module only supports the Basic scheme, but upon request Digest
138 will also be added. Patches welcome!
143 This module reads config from $c->config->{authentication}{http_proxy}. The following settings
150 Required. A url protected with basic authentication to authenticate against.
154 To specify what store to use. will use the default store if not set.
162 =item authorization_required
164 Tries to C<authenticate_http_proxy>, and if that fails calls
165 C<authorization_required_response> and detaches the current action call stack.
167 =item authenticate_http_proxy
169 Looks inside C<< $c->request->headers >> and processes the basic (badly named)
170 authorization header. Then authenticates this against the provided url.
172 =item authorization_required_response
174 Sets C<< $c->response >> to the correct status code, and adds the correct
175 header to demand authentication data from the user agent.
181 Marcus Ramberg <mramberg@cpan.org
183 =head1 COPYRIGHT & LICENSE
185 Copyright (c) 2005 the aforementioned authors. All rights
186 reserved. This program is free software; you can redistribute
187 it and/or modify it under the same terms as Perl itself.