Andrew
Andrew

Reputation: 241

An authentication issue with WWW::Scripter

I am using plugin WWW::Scripter (subclass of WWW::Mechanize) to authenticate to my host's login page. They use Ruby with some JavaScript functions on the login page, so I can't just use the LWP::Agent module. Here is the code:

#!/usr/bin/env perl
use strict;
use warnings;
use diagnostics;

use LWP::Debug qw(+);
use LWP::ConnCache;
use WWW::Scripter;

my $url = 'https://control.vp.net/login';
my $username = '[email protected]';
my $password = 'example';

my $w = WWW::Scripter->new(keep_alive => 1) or die "error1: $!\n";
$w->conn_cache(LWP::ConnCache->new);
$w->use_plugin('JavaScript') or die "error2: $!\n";
$w->credentials($url, undef, $username, $password) or die "error3: $!\n";
$w->get($url) or die "error4: $!\n";
print $w->content() or die "error5: $!\n";

I am having the following error:

Uncaught exception from user code:
error3

I have spent number of hours googling and I feel that I really need some of your help now. I will appreciate any help which help to understand why I can't authenticate. My Perl version is 5.10.1 on Ubuntu 11 if it matters.

Thanks.

Update

I have changed one line in the code to:

$w->credentials($username, $password) or die "error3: $!\n";

and getting just white page now. If I enable the diagnostics pragma, a rather general error appears:

Use of uninitialized value in subroutine entry at blib/lib/Net/SSLeay.pm
(autosplit into blib/lib/auto/Net/SSLeay/randomize.al) line 2227 (#1)
(W uninitialized) An undefined value was used as if it were already
defined.  It was interpreted as a "" or a 0, but maybe it was a mistake.
To suppress this warning assign a defined value to your variables.

Upvotes: 1

Views: 1126

Answers (2)

daxim
daxim

Reputation: 39158

credentials is good for standard HTTP authentication, but Web forms are something different. Remove that method call and learn how Web forms function. The JavaScript has no influence on the Web form, Mechanize is enough.

use strictures;
my ($url, $username, $password)
    = qw(https://control.vps.net/login [email protected] example);
my $w = WWW::Mechanize->new;
$w->get($url);  # automatically raises exception on HTTP errors
$w->submit_form(with_fields => {
    'session[email_address]' => $username,
    'session[password]' => $password,
});
die 'login failed'
    if $w->content =~ /your email and password combination were incorrect/;

Upvotes: 4

Michael Slade
Michael Slade

Reputation: 13877

You can't simply expect every function to return a truthy value on success and a falsy value on failure like that.

The entry for credentials, for example, says nothing about what it returns, so you shouldn't attempt to do anything with whatever it returns.

Same goes for get - it returns a response object, which will presumably always be true.

You need to call all of the functions and then check $w->status(). If it returns 401 or 403 then authentication failed.

Upvotes: 2

Related Questions