#!/usr/bin/perl
use strict;
use warnings;
# Imports
use LWPx::ParanoidAgent;
use Net::OpenID::Consumer;
use CGI;

#------------------------------------------------------------------------------
# Init
my $cgi = new CGI;

my ($check_url);

my $openIdMode = $cgi->param('openid.mode');

	my $csr = Net::OpenID::Consumer->new(
		ua => LWPx::ParanoidAgent->new(timeout => 5),
		args  => $cgi,
		required_root => "http://www.imx.net/",
		consumer_secret => 'xYZabC0123',
		debug => 1
	);
	my $verifiedId = undef;

		# Get id server info from canonicalized URL
		my $claimed_identity = $csr->claimed_identity("https://www.google.com/accounts/o8/id");

		if ($claimed_identity) {
		  $check_url = $claimed_identity->check_url(
		    return_to  => "http://www.imx.net/test_openid_response.pl",
		    trust_root => "http://www.imx.net/",
		  );
				my $sregNs = "openid.ns.sreg=http://openid.net/extensions/sreg/1.1";
				my $sregParams = "openid.sreg.optional=nickname,fullname,dob,country";
				print $cgi->redirect("$check_url&$sregNs&$sregParams&openid.ax.mode=fetch_request");
		}
if ($openIdMode) {
		# Returning from id server
		if ($csr->user_cancel()) {
			# User cancelled
			print 'errOidCancel';
		}
		elsif (my $setupUrl = $csr->user_setup_url(post_grant => 'return')) {
			# This shouldn't happen since we're not using immediate mode
			# Print header
			print $cgi->header, $cgi->start_html();
			print $setupUrl;
		}
		elsif ($verifiedId = $csr->verified_identity()) {
		}
}

 
# Print header
		print $cgi->header, $cgi->start_html();
    print $check_url, "\n";

my $a= qq~<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<title>OpenId transaction in progress</title>
</head><body onload='document.forms[0].submit();'>
<form accept-charset="UTF-8" enctype="application/x-www-form-urlencoded" id="openid_message" action="https://www.google.com/accounts/o8/ud" method="post">
<input type="hidden" name="openid.ns" value="http://specs.openid.net/auth/2.0" />
<input type="hidden" name="openid.ns.sreg" value="http://openid.net/extensions/sreg/1.1" />
<input type="hidden" name="openid.ns.pape" value="http://specs.openid.net/extensions/pape/1.0" />
<input type="hidden" name="openid.sreg.required" value="email" />
<input type="hidden" name="openid.sreg.optional" value="nickname,fullname,postcode,country,timezone" />
<input type="hidden" name="openid.pape.preferred_auth_policies" value="http://schemas.openid.net/pape/policies/2007/06/multi-factor-physical http://schemas.openid.net/pape/policies/2007/06/multi-factor http://schemas.openid.net/pape/policies/2007/06/phishing-resistant" />
<input type="hidden" name="openid.realm" value="http://www.imx.net/" />
<input type="hidden" name="openid.mode" value="checkid_setup" />
<input type="hidden" name="openid.return_to" value="http://www.imx.net/test_openid_response.pl" />
<input type="hidden" name="openid.identity" value="http://specs.openid.net/auth/2.0/identifier_select" />
<input type="hidden" name="openid.claimed_id" value="http://specs.openid.net/auth/2.0/identifier_select" />
<input type="hidden" name="openid.assoc_handle" value="AOQobUdZdfYE3IL3k1nBSw3ja7BYcWbTiSRaHvy-Pe_s68kl5sqBs0cb" />
<input type="submit" value="Continue" />
<input type="hidden" name="openid.ns.ext1" value="http://openid.net/srv/ax/1.0" />
<input type="hidden" name="openid.ext1.mode" value="fetch_request" />
<input type="hidden" name="openid.ext1.type.email" value="http://schema.openid.net/contact/email" />
<input type="hidden" name="openid.ext1.required" value="email" /></form>~;

exit;