Google authentication using perl cgi-application - perl

I have built a CGI::Application currently running on local host and have used 2 authentication methods -
1. descried in http://www.perlmonks.org/?node_id=622071 by storing user password in database and
2. using LDAP credentials.
I was looking for a simple way to do google authentication but haven't found an easy way yet. Can someone point me in the right direction.
I looked at
1. Authen-GoogleAccount and
2. net-Google-FederatedLogin
but not enough documentation for either of these. Where do i start? Please let me know even if you have some pointer to doing this outside of cgi::application

This is the closest solution I could find. I am not a security expert, but I don't think websites serious about it would use this method. It uses WWW::Mechanize to authenticate using google email/password and then pull secure content out.
http://gregjessup.com/login-to-google-using-perl/
if $mech->get($url); returns error, authentication failed.

Here's the code I used for the Android Market for Developers (market.android.com/publish):
use WWW::Mechanize;
use HTTP::Cookies;
my $url = 'https://www.google.com/accounts/ServiceLogin';
my $username = 'username#gmail.com';
my $password = "PASSWORD";
my $mech = WWW::Mechanize->new();
$mech->cookie_jar(HTTP::Cookies->new());
$mech->get($url);
$mech->form_number(1);
$mech->field(Email => $username);
$mech->field(Passwd => $password);
$mech->click();
# Go to the next link, now that we are logged in.
$url = 'https://market.android.com/publish/Home';
$mech->get($url);
print $mech->content();
It's a small edit/cleanup of the link Prateek posted: http://gregjessup.com/login-to-google-using-perl.
I think it should be able to be used for most of Google's services that require you to be logged in.

Related

Perl: HTTPS website login

I am using the following code:
my $login = "https://steamcommunity.com/login/home/?goto=market%2F";
my $username = "USR";
my $password = "PASS";
my $mech = WWW::Mechanize->new();
$mech->cookie_jar(HTTP::Cookies->new());
$mech->get($login);
$mech->form_name('loginForm');
$mech->field(login => $username);
$mech->field(passwd => $password);
$mech->click();
To log into a website, unfortunately it is not working, any trouble shooting help would be really appreciated. You can visit the website with the login url.
EDIT, the modules im using.
use JSON::XS;
use WWW::Mechanize;
use HTTP::Cookies;
use LWP::Simple;
use strict;
use warnings;
Steam's Subscriber Agreement states:
You may not use Cheats, automation software (bots), mods, hacks, or any other unauthorized third-party software, to modify or automate any Subscription Marketplace process.
They also provide an API.
These reduce the incentive to help you manipulate their web site processes. You are better off figuring out how to use the API. If the API doesn't provide you a means to do what you want, it may be best to avoid doing it.

Per https Authentication fails

I have a few problems authenticating to a website via Perl.
I can login to the website without any problems using a browser, if I do, it shows me a little prompt, where I have to supply my credentials.
Easy going!
But now I wanted to connect to the site by using the script and I am always running into a 401 unauthorized error.
The server I am trying to connect to runs IIS7 and the WWW-Authenticate header told me, the realm would be something like 'negotiate, NTLM' (I tried all three possibilities, one of the words or both, nothing happened).
So far I really don't know how to figure out what information is needed, my last try was to get the form, where I have to supply the credentials and then use the submit_form function, but to get the forms, I have to establish the connection.
I am really confused because I tried most of the things I found on google, but I don't know what is going on, especially from the technical view, which could give me a hint.
Well, I would really appreciate it, if someone could help me and I thank you in advance for doing it.
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use WWW::Mechanize;
use Authen::NTLM;
my $url = "some.url";
my $usr = 'bernd';
my $pwd = 'bernd';
my $mech = WWW::Mechanize->new(ssl_opts => {verify_hostname => 0});
$mech -> credentials($url.":443","NTLM",$usr,$pwd);
$mech->get("https://".$url); #added the https, because $url doesn't have it
print $mech->forms;
PS:
The bypassing of ssl verification was due to a certificate problem I had at the beginning.
I speculate that in your use of the 4 parameter form of the credentials method, the netloc and user are wrong.
When the URL is https://www.example.com/foo/bar?quux;baz=42#fnord, the netloc is www.example.com:443.
The user is a string of the NTLM domain, a single backslash, and the NTLM username, e.g. internal\bernd. Mind that if you hardcode that value as a string literal as you did in the example, you need to escape the backslash with a backslash.
Check for these possible errors.
Alright, the problem is not a script or Perl problem as it seems, some of the developers of the site did a really shitty job with the permissions, so you have to be authorized to view everything else, in order to view the single site and the service account I were told to use, does not have this permissions.
Very sad, but thanks for the help so far!

How do I access the StackExchange API authenticated methods from a perl script?

I'm using WWW::Mechanize. For the methods that do not require authentication, I get those as I would any other url, and then use the perl JSON module to parse out whatever data I want:
my $response = $mech->get('http://api.stackexchange.com/1.1/questions?fromdate=' . $lasthour)
my $q = from_json($response->content())
I've used Mechanize to log into websites in the past, but the Oauth stuff is confusing, and what documentation is provided for using the API suggests that it is intended for web applications (that require registration with StackExchange?).
In particular, I am interested in the notifications method though I would expect the correct code to allow access to any of the auth-required methods.
Have you looked at Net::StackExchange2?
#for methods that require auth.
my $se = Net::StackExchange2->new(
{
site=>"stackoverflow",
access_token => '<THE ACCESS TOKEN>' ,
key => '<YOUR APP KEY>'
}
);
It uses LWP::UserAgent. Even if you don't want to use the Net::StackExchange2 module directly, you have a good chance of finding some good example code to borrow from.

WWW::Mechanize won't work after submit

At my work I build a lot of wordpress sites and I also do a lot of cutting and pasting. In order to streamline this process I'm trying to make a crawler that can fill out and submit form information to wordpress. However, I can't get the crawler to operate correctly in the wordpress admin panel once I'm past the login.
I know it works to submit the login form because I've gotten the page back before. But this script doesn't seem to return the "settings" page, which is what I want. I've been trying to use this site as a guide: www.higherpass.com/Perl/Tutorials/Using-Www-mechanize/3/ for how to use mechanize but I could use some additional pointers for this. Here is my Perl script, I've tried a few variations but I just need to be pointed in the right direction.
Thanks!
use WWW::Mechanize;
my $m = WWW::Mechanize->new();
$url2 = 'http://www.moversbatonrougela.com/wp-admin/options-general.php';
$url = 'http://www.moversbatonrougela.com/wp-admin';
$m->get($url);
$m->form_name('loginform');
$m->set_fields('username' => 'user', 'password' => 'password');
$m->submit();
$response = $m->get($url2);
print $response->decoded_content();
Put the below lines of code just before $m->submit(); . Since WWW::Mechanize is a subclass of LWP::UserAgent you can use any of LWP's methods.
$m->add_handler("request_send", sub { shift->dump; return });
$m->add_handler("response_done", sub { shift->dump; return });
The above would enable logging in your code. Look out for the Request/Response return codes i.e. 200 (OK) or 302 (Redirect) etc. The URL request i.e. the $m->get() is probably getting redirected or the machine's ip is Blocked by the server. If its a redirect, then you can probably use $m->redirect_ok(); to follow the redirect URL, or in case you don't want to follow the redirect URL use $m->requests_redirectable (this is an LWP method). The logs should show something like below-
HTTP/1.1 200 OK
OR
HTTP/1.1 302 Found
If none of the above works, use an alternative of $m->submit(); like below and give it a try-
my $inputobject=$mech->current_form()->find_input( undef, 'submit' );
$m->click_button(input => $inputobject);

Perl WWW::Mechanize::Firefox POST() Implementation

Can I get some help on how to submit a POST with the necessary variables using WWW::Mechanize::Firefox? I've installed all the perl modules, and the firefox plugin and tested such that I can connect to a give host and get responses... my questions is how to submit a POST request. On the documentation Corion says he may never implement. This seems odd, I'm hoping I can use the inherited nature from Mechanize, but can't find any examples. A simple example would help me tremendously.
my $mech = WWW::Mechanize::Firefox->new();
$mech->allow( javascript =>1); # enable javascript
# http
$mech->get("http://www.example.com");
my $c = $mech->content;
Is there a mech->post() option I am simply missing?
many thanks in advance.
R
Normally you would just set the fields and submit the form like this:
$mech->get('http://www.website.com');
$mech->submit_form(
with_fields => {
user => 'me',
pass => 'secret',
}
);
get a page, fill out a form, submit the form
if you're going to be skipping the above steps by using post, you don't need mechanize firefox