I use LWP::UserAgent to automate windows login process. I set the credentials using $ua->credentials("host:port","realm","id","password" ); but it fetches only the current page requested. It fails for subsequent requests and pops up the windows form again to supply the credentials. I have created a cookie jar too but this doesnt help as the authentication is not sticking. Any help on this is highly appreciated.
The code is as follows.
Expand|Select|Wrap|Line Numbers
- # Module: authenticate.pl
- # @author Kiran Kamath
- #flush the buffer
- $|=1;
- use CGI;
- use CGI::Request;
- use LWP::UserAgent;
- use HTTP::Request;
- use LWP::ConnCache;
- use HTTP::Cookies;
- use LWP::DebugFile;
- # Configurations required for the script
- require "authvar.ph";
- use POSIX qw(strftime);
- # Set up Alert log
- open(ALERTS,"$AUTH_LOG") or warn "Cannot open alert log file:$!";
- $AlertDate=`date $AUTH_DATE_FORMAT`;
- chomp($AlertDate);
- print ALERTS "$AlertDate authenticate-module entry\n\n" if($AUTH_DEBUG);
- #Get username and password from POST message
- $req = GetRequest($pkg);
- $uid=$req->param('uid');
- $passwd=$req->param('passwd');
- my $timeout=10;
- $uid=some id ;
- $passwd=some passwd ;
- if(defined($uid) && defined($passwd))
- {
- chomp($uid); chomp($passwd);
- print ALERTS "$AlertDate [ UID=$uid , PASSWD=$passwd ]\n";
- my($res,$request);
- # LWP::UserAgent is a class implementing a web user agent used to dispatch web requests.
- $ua = LWP::UserAgent->new();
- $ua->agent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; .NET CLR 2.0.50727)');
- #Set the timeout so that requests is aborted if no activity on the connection to the server is observed for timeout seconds.
- $ua->timeout($timeout);
- # Set the credentials for a realm
- $ua->credentials("$AUTH_DOMAIN:$AUTH_PORT","$AUTH_NAME","$uid","$passwd");
- # LWP::ConCache is used for the browser to support HTTP Keep-Alive
- # This is required so that after the request is performed connection is not lost and is persistent.
- $cache=$ua->conn_cache(LWP::ConnCache->new());
- # Cache all the connections
- $ua->conn_cache->total_capacity(undef);
- $cookie_jar=HTTP::Cookies->new();
- $cookie_jar->set_cookie("1","nortelid","axptnrtl","/","naxptsts.intec.telcordia.com","4443",1,1,3600,0);
- # Give the browser an in-memory empty cookie jar
- $ua->cookie_jar($cookie_jar);
- $ua->default_header('Accept-Language'=>'en-us');
- $ua->default_header('Accept-Encoding'=>'gzip, deflate');
- #$ua->default_header('Accept'=>'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*');
- $ua->default_header('Accept'=>'*/*');
- $request = new HTTP::Request('GET', $AUTH_URI);
- $cookie_jar->add_cookie_header($request);
- $res=$ua->request($request);
- $cookie_jar->extract_cookies($res);
- my $status = $res->code;
- print ALERTS "$AlertDate Return Status for $AUTH_URI was $status\n";
- if($res->is_success){
- $status_message=$res->as_string;
- #print ALERTS "$AlertDate [DEBUG STATUS MESSAGE=$status_message]\n\n";
- print $res->content;
- print ALERTS "$AlertDate [Success Redirecting]\n\n";
- }
- else
- {
- print ALERTS "$AlertDate [Authenticate Header= " . $res->header('WWW-Authenticate') . "]\n";
- print ALERTS "$AlertDate [ERROR STATUS: " .$res->status_line. "]\n\n";
- }
- }
- print ALERTS "$AlertDate authenticate-module exit\n\n" if($AUTH_DEBUG);
- close(ALERTS);
Thanks
Kiran