1 package LWP::Authen::Digest;
4 use base 'LWP::Authen::Basic';
9 my($class, $user, $pass, $request, $ua, $h) = @_;
11 my $auth_param = $h->{auth_param};
13 my $nc = sprintf "%08X", ++$ua->{authen_md5_nonce_count}{$auth_param->{nonce}};
14 my $cnonce = sprintf "%8x", time;
16 my $uri = $request->uri->path_query;
17 $uri = "/" unless length $uri;
19 my $md5 = Digest::MD5->new;
22 $md5->add(join(":", $user, $auth_param->{realm}, $pass));
23 push(@digest, $md5->hexdigest);
26 push(@digest, $auth_param->{nonce});
28 if ($auth_param->{qop}) {
29 push(@digest, $nc, $cnonce, ($auth_param->{qop} =~ m|^auth[,;]auth-int$|) ? 'auth' : $auth_param->{qop});
32 $md5->add(join(":", $request->method, $uri));
33 push(@digest, $md5->hexdigest);
36 $md5->add(join(":", @digest));
37 my($digest) = $md5->hexdigest;
40 my %resp = map { $_ => $auth_param->{$_} } qw(realm nonce opaque);
41 @resp{qw(username uri response algorithm)} = ($user, $uri, $digest, "MD5");
43 if (($auth_param->{qop} || "") =~ m|^auth([,;]auth-int)?$|) {
44 @resp{qw(qop cnonce nc)} = ("auth", $cnonce, $nc);
47 my(@order) = qw(username realm qop algorithm uri nonce nc cnonce response);
48 if($request->method =~ /^(?:POST|PUT)$/) {
49 $md5->add($request->content);
50 my $content = $md5->hexdigest;
52 $md5->add(join(":", @digest[0..1], $content));
54 $resp{"message-digest"} = $md5->hexdigest;
55 push(@order, "message-digest");
57 push(@order, "opaque");
60 next unless defined $resp{$_};
61 push(@pairs, "$_=" . qq("$resp{$_}"));
64 my $auth_value = "Digest " . join(", ", @pairs);