Why don't my LWP::UserAgent credentials work?

Ram picture Ram · Nov 25, 2009 · Viewed 24.4k times · Source

I'm trying to access a protected file. Server is using digest authentication - which I can see from the printed out response. Here is the sample code:

use LWP;
use strict;

my $url = 'http://somesite.com/aa/bb/cc.html';
my $username = 'scott';
my $password = 'tiger';

my $browser = LWP::UserAgent->new('Mozilla');
$browser->credentials("http://somesite.com:80","realm-name",$username=>$password);
my $response=$browser->get($url);

print $response->content;

Name of the realm I got it from the popup window I get when I try to access that resource from the browser. Same username and password are working extremely fine in the browser and I'm able to see the content but when I run the above script it always says 401 Authorization required.

How does LWP work?

Do I need to ask LWP to send MD5 hash (digest) of the username and password or is it like internally it checks which authentication to use and sends the corresponding (basic/digest) way of sending credentials. My questions are

  1. How can I set LWP so that it sends digest of username and password?
  2. What if the server is using windows NTLM authentication protocol? How should I go about in such a situation?

any quick help is highly appreciated !

Answer

Greg Bacon picture Greg Bacon · Nov 25, 2009

Consider the following excerpt from the LWP::UserAgent module's documentation:

$ua->credentials( $netloc, $realm )
$ua->credentials( $netloc, $realm, $uname, $pass )

Get/set the user name and password to be used for a realm.

The $netloc is a string of the form "<host>:<port>". The username and password will only be passed to this server. Example:

$ua->credentials("www.example.com:80", "Some Realm", "foo", "secret");

Change

$browser->credentials("http://somesite.com:80","realm-name",$username=>$password);

to

$browser->credentials("somesite.com:80","realm-name",$username=>$password);