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