File Coverage

blib/lib/LWP/Authen/Basic.pm
Criterion Covered Total %
statement 37 37 100.0
branch 12 18 66.6
condition 13 20 65.0
subroutine 5 5 100.0
pod 0 3 0.0
total 67 83 80.7


line stmt bran cond sub pod time code
1             package LWP::Authen::Basic;
2             $LWP::Authen::Basic::VERSION = '6.29';
3 1     1   7 use strict;
  1         1  
  1         553  
4              
5             require MIME::Base64;
6              
7             sub auth_header {
8 3     3 0 9 my($class, $user, $pass) = @_;
9 3         24 return "Basic " . MIME::Base64::encode("$user:$pass", "");
10             }
11              
12             sub authenticate
13             {
14 8     8 0 24 my($class, $ua, $proxy, $auth_param, $response,
15             $request, $arg, $size) = @_;
16              
17 8   50     24 my $realm = $auth_param->{realm} || "";
18 8 50       29 my $url = $proxy ? $request->{proxy} : $request->uri_canonical;
19 8 50       81 return $response unless $url;
20 8         65 my $host_port = $url->host_port;
21 8 50       214 my $auth_header = $proxy ? "Proxy-Authorization" : "Authorization";
22              
23 8 50       24 my @m = $proxy ? (m_proxy => $url) : (m_host_port => $host_port);
24 8         18 push(@m, realm => $realm);
25              
26             my $h = $ua->get_my_handler("request_prepare", @m, sub {
27             $_[0]{callback} = sub {
28 20         45 my($req, $ua, $h) = @_;
29 20         58 my($user, $pass) = $ua->credentials($host_port, $h->{realm});
30 20 100       55 if (defined $user) {
31 11         51 my $auth_value = $class->auth_header($user, $pass, $req, $ua, $h);
32 11         37 $req->header($auth_header => $auth_value);
33             }
34 6     6   39 };
35 8         69 });
36 8         23 $h->{auth_param} = $auth_param;
37              
38 8 100 66     50 if (!$proxy && !$request->header($auth_header) && $ua->credentials($host_port, $realm)) {
      100        
39             # we can make sure this handler applies and retry
40 2         7 add_path($h, $url->path);
41 2         11 return $ua->request($request->clone, $arg, $size, $response);
42             }
43              
44 6         104 my($user, $pass) = $ua->get_basic_credentials($realm, $url, $proxy);
45 6 100 66     1048 unless (defined $user and defined $pass) {
46 2         12 $ua->set_my_handler("request_prepare", undef, @m); # delete handler
47 2         49 return $response;
48             }
49              
50             # check that the password has changed
51 4         17 my ($olduser, $oldpass) = $ua->credentials($host_port, $realm);
52 4 50 66     53 return $response if (defined $olduser and defined $oldpass and
      66        
      33        
53             $user eq $olduser and $pass eq $oldpass);
54              
55 2         9 $ua->credentials($host_port, $realm, $user, $pass);
56 2 50       9 add_path($h, $url->path) unless $proxy;
57 2         12 return $ua->request($request->clone, $arg, $size, $response);
58             }
59              
60             sub add_path {
61 4     4 0 71 my($h, $path) = @_;
62 4         25 $path =~ s,[^/]+\z,,;
63 4         10 push(@{$h->{m_path_prefix}}, $path);
  4         22  
64             }
65              
66             1;