Current location: Hot Scripts Forums » Programming Languages » Perl » [SOLVED] Perl LWP + Authentication

[SOLVED] Perl LWP + Authentication

Reply
  #1 (permalink)  
Old
New Member
 
Join Date: Jun 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Perl LWP + Authentication

Hey folks,

I've a question related to Perl LWP. I'm trying to fetch a website which redirects me
to a secure login for authentication. If the login is successful, the original website is retrieved. I'm not able to figure out how to authenticate a redirected page and get back to the original page. I did quite a bit of research but couldn't succeed yet.

The authorization_basic call on HTTP::Request or providing the credentials to the LWP user agent works fine only if the login window pops up in the first requested webpage itself and not if the login is done on the redirected page.

Could any of you give me a hand on this one?
I hope I made myself clear. In case you've questions, route it back to me.

Thanks,
Kash
Reply With Quote

Featured Marketplace Scripts (view more)

  #2 (permalink)  
Old
curbview.com's Avatar
Junior Code Guru
 
Join Date: May 2006
Posts: 555
Thanks: 0
Thanked 0 Times in 0 Posts
Please post your code... It's rather hard to define if you either have a hitch in your code or not calling the page correctly. (How do you know if your app is retrieving the login form?)
__________________
Whatever you decide, you should make sure best security methods are used and practiced. Should you really need more help, PM me.
Reply With Quote
  #3 (permalink)  
Old
New Member
 
Join Date: Jun 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

Thanks for writing back. I was able to figure out the issue yesterday. I was trying to use LWP but that dint work. I used WWW::Mechanize. It supports calls to enter form details and do a submit.

Here's what I did.
Code:
 #!/usr/bin/perl

use WWW::Mechanize;
use strict;

  print "Content-type: text/html\n\n";

  my $username = "xxxx";
  my $password = "xxxx";
  my $outfile = "out.txt";

  my $mech = WWW::Mechanize->new( );

  my $url = 'URL I want';

  $mech->get($url);
  $mech->form_name('Submit');
  $mech->field(username => "$username");
  $mech->field(passwd => "$password");
  $mech->click();

  my $response = $mech->content();

  open(OUTFILE, ">$outfile");
  print OUTFILE "$response";
  close(OUTFILE);

I was trying to achieve this using LWP but it doesn't support ways to enter credentials to a form. My application takes me to a secure login form for authentication and if authenticated redirects me to the page I originally requested. The secure login model expects username and password and do a submit.

This is what I did using LWP.

#!/usr/bin/perl

use CGI;
use LWP::UserAgent;
use strict;

  print "Content-type: text/html\n\n";

  # Create a new user agent for HTTP request
  my $ua = LWP::UserAgent->new( );

 my $URL = 'URL I want';

 # Create HTTP request object
  my $req =  HTTP::Request->new( GET => $URL);

 # Authenticate the user
  $req->authorization_basic( 'username', 'passwd' );

  # Send the HTTP request to the user agent
  my $res = $ua->request( $req,"myData.txt" );

  print $res->status_line, "\n";

  # Check if the response is successful
  if ( !$res->is_success )
  {
    die $res->status_line;
  }
This way "myData.txt" showed authentication failed message.

I'm glad it worked. I really appreciate you writing back.

Thanks,
Kash

Last edited by Nico; 06-13-08 at 11:14 AM. Reason: Please use [code] wrappers.
Reply With Quote
  #4 (permalink)  
Old
New Member
 
Join Date: Nov 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
hello Kashyap,

I tried your solution, but i think i am getting stuck at proxy.

secondly after entering credentials my website produces a new page where i have to click an 'ok' button and then it takes me back to where i was.

here is the problem description in short:

1. post your url in browser: https://myrel/files/file.doc
2. you get a logon page (URL still looking same)
3. enter field names: username/password
4. press 'ok' button (name = bsubmit)

after successfull login:
5. you get new page: https://myurl/pagSuccess/
6. you have to click 'ok' ( field name = successbtn ) on this page now to go back to where you wanted to go...
7. then you get a firefox dialog:
where you have to select (radio button) 'Save File'
press ok
8. you are done!!! (saves the file to default location)


here is the code which is failing on proxy:



#!/usr/bin/perl

use strict;
use warnings;
use WWW::Mechanize;

#print "Content-type: text/html\n\n";

my $username = "myid";
my $password = "mypass";
my $outfile = "testout.doc";
$ENV{HTTPS_PROXY} = 'http://myproxy:8080';

my $mech = WWW::Mechanize->new( );

my $url = 'https://myurl/files/file.doc';

$mech->get($url);
$mech->form_name('frmLogin');
$mech->field(username => "$username");
$mech->field(password => "$password");
$mech->click('bsubmit');

$mech->click('successbtn'); # here i am hoping this will click the 'ok' button the next page

my $response = $mech->content();

open(OUTFILE, ">$outfile");
print OUTFILE "$response";
close(OUTFILE);


error i am getting:

Error GETing https://myurl/files/file.doc: proxy connect failed: PROXY ERROR HEADER, could be non-SSL URL:
HTTP/1.0 504 Gateway Time-out
Server: squid/
Date:
Content-Type: text/html
Content-Length: 1117
X-Squid-Error: ERR_CONNECT_FAIL 238
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl script NationVoice Perl 4 05-08-08 02:46 PM
Perl upload progress bar with these spec: scott2500uk Script Requests 4 03-16-07 05:04 PM
Perl for a dummy kragnesb414 Perl 1 01-31-06 05:34 AM
perl for a dummy kragnesb414 New Members & Introductions 0 12-21-05 03:37 PM
Perl FormMail Problem ennanguyen2002 Perl 2 03-09-05 05:47 PM


All times are GMT -5. The time now is 11:18 AM.
vBulletin® Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.