File Coverage

blib/lib/Catalyst/Authentication/Credential/HTTP/Proxy.pm
Criterion Covered Total %
statement 18 43 41.8
branch 0 16 0.0
condition 0 2 0.0
subroutine 6 8 75.0
pod n/a
total 24 69 34.7


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