Re: Windows authentication without a password

[email protected] (Lawrence Statton) Wed, 24 Oct 2012 16:45:13 -0500
Newsgroups perl.libwww
Message-ID <[email protected]>
On 10/24/2012 09:21 AM, [email protected] wrote:
> Hi,
>
> Is it possible to authenticate the user on Windows without entering the password?
>
> Our script uses LWP and is talking to a webserver that requires authentication. The code is as follows:
>

Here's an example of a trivial subclass of LWP::UserAgent that uses 
Net::Netrc to store passwords in "not the script"

package Cluon::TestAgent;

use parent 'LWP::UserAgent';
use URI;
use Net::Netrc;

sub get_basic_credentials {
   my $class = shift;
   my $realm = shift; # ignore this for trivial demo
   my $uri = URI->new(shift);
   if (my $tuple = Net::Netrc->lookup($uri->host)) {
     return ($tuple->login, $tuple->password);
   }
   return;
}

#
# instantiate a new Cluon::TestAgent and use it to get a protected
# resource
#
sub test_driver {
   my $class = shift;
   my $ua = $class->new();

   my $response = $ua->get('http://lawrence.cluon.com/');

   die $response->status_line
     unless $response->is_success;

   print "Got it\n\n";

}

1;

You'll need to createa  .netrc file in your $HOME that looks like

machine lawrence.cluon.com login testuser password testpass

And you can test the script with

perl -MCluon::TestAgent -le "Cluon::TestAgent->test_driver"

(This, of course, will only work if TestAgent.pm is under a directory 
named Cluon)

Expanding the TestAgent to use something else like .authinfo or your 
platform's favorite keyring store is an exercise for the reader :)

--Lawrence