In the interest of giving back to the community, I am posting the
scripts that I wrote that successfully use the JanRain libraries for openid logins.<br><br>I am writing a Mason web application, so this is in an init block on the page that handles the post from the login form.<br><br>Because I am running Apache 2, I had to do some funky stuff with my redirect page, so I posted that at the bottom, too.
<br>
<br>Amiri<br><br>__________<br><br><br>&lt;%args&gt;<br>$openid_url =&gt; undef<br>&lt;/%args&gt;<br><br>&lt;%init&gt;<br>use strict;<br>use warnings;<br><br>use DBI;<br>use CGI;<br>use CGI::Session;<br>use Secret;<br>use Secret qw($name);
<br>use Secret qw($pass);<br>use URI;<br>use URI::Query;<br>use URI::Escape;<br>use URI::QueryParam;<br>use Net::OpenID::JanRain::Consumer;<br>use Net::OpenID::JanRain::Stores::MySQLStore;<br><br>my $dbh;<br>my $cgi;<br>my $session;
<br>my $cookie;<br>my $store;<br>my $consumer;<br>my $request;<br>my $redirect;<br>my $q;<br>my $query;<br>my %query;<br>my $complete;<br><br><br>unless ( $ARGS{&#39;openid.mode&#39;} ) {<br>&nbsp;&nbsp;&nbsp; $dbh = DBI-&gt;connect( &quot;dbi:mysql:openid&quot;, $name, $pass ) || die $DBI::errstr;
<br>&nbsp;&nbsp;&nbsp; $cgi = CGI-&gt;new;<br>&nbsp;&nbsp;&nbsp; $session = CGI::Session-&gt;new( &quot;driver:MySQL&quot;, undef, { Handle =&gt; $dbh } );<br>&nbsp;&nbsp;&nbsp; $cookie = $cgi-&gt;cookie(CGISESSID =&gt; $session-&gt;id );<br>#&nbsp;&nbsp;&nbsp; print $cgi-&gt;header(-cookie=&gt;$cookie);
<br>&nbsp;&nbsp;&nbsp; $store = Net::OpenID::JanRain::Stores::MySQLStore-&gt;new($dbh);<br>&nbsp;&nbsp;&nbsp; $consumer = Net::OpenID::JanRain::Consumer-&gt;new( $session, $store );<br><br><br>&nbsp;&nbsp;&nbsp; if ($openid_url) {<br>&nbsp;&nbsp;&nbsp; $request = $consumer-&gt;begin($openid_url);
<br><br>&nbsp;&nbsp;&nbsp; if ( $request-&gt;status eq &#39;failure&#39; ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my $redirect5 = &quot;/users/login_failed_all.html&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $m-&gt;comp( &quot;/generic/action/openidredir.ml&quot;, url =&gt; $redirect5 );
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; elsif ( $request-&gt;status eq &#39;success&#39; || &#39;in_progress&#39; ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my $trust_root = &quot;http://***.***.***.***/users&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my $return_to&nbsp; = &quot;http://***.***.***.***/users/openid.html&quot;;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $request-&gt;addExtensionArg( &quot;sreg&quot;, &quot;optional&quot;,&quot;nickname,email,fullname,dob,gender,postcode,country,language,timezone&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $redirect = $request-&gt;redirectURL( $trust_root, $return_to );
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $m-&gt;comp( &quot;/generic/action/openidredir.ml&quot;, url =&gt; $redirect, cookie =&gt; $cookie );<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; }<br>}<br><br>if ( $ARGS{&#39;openid.mode&#39;} ) {<br>#&nbsp; &nbsp;&nbsp;&nbsp; $q = URI::Escape::uri_unescape($ENV{QUERY_STRING});
<br>#&nbsp; &nbsp;&nbsp;&nbsp; $query&nbsp; = URI::Query-&gt;new($q);<br>#&nbsp;&nbsp;&nbsp; %query&nbsp; = $query-&gt;hash;<br><br>&nbsp;&nbsp;&nbsp; $dbh = DBI-&gt;connect( &quot;dbi:mysql:openid&quot;, $name, $pass ) || die $DBI::errstr;<br>&nbsp;&nbsp;&nbsp; $cgi = CGI-&gt;new;<br>&nbsp;&nbsp;&nbsp; my $sid = $cgi-&gt;cookie(&quot;CGISESSID&quot;);
<br>&nbsp;&nbsp;&nbsp; $session = &nbsp;&nbsp;&nbsp; CGI::Session-&gt;new( &quot;driver:MySQL&quot;, $sid, { Handle =&gt; $dbh } );<br>&nbsp;&nbsp;&nbsp; $store = Net::OpenID::JanRain::Stores::MySQLStore-&gt;new($dbh);<br><br>&nbsp;&nbsp;&nbsp; $consumer = Net::OpenID::JanRain::Consumer-&gt;new( $session, $store );
<br>&nbsp;&nbsp;&nbsp; $complete = $consumer-&gt;complete( \%ARGS );<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; if ( $complete-&gt;status eq &#39;success&#39; ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my $redirect1 = &quot;/users/login_done.html&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $m-&gt;comp( &quot;/generic/action/openidredir.ml&quot;, url =&gt; $redirect1, cookie =&gt; $cookie );
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; elsif ( $complete-&gt;status eq &#39;failure&#39; ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my $redirect2 = &quot;/users/completemessage.html&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $m-&gt;comp( &quot;/users/completemessage.html&quot;, complete =&gt; $complete-&gt;message, cookie =&gt; $cookie );
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; elsif ( $complete-&gt;status eq &#39;cancel&#39; ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my $redirect3 = &quot;/users/login_canceled.html&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $m-&gt;comp( &quot;/generic/action/openidredir.ml&quot;, url =&gt; $redirect3, cookie =&gt; $cookie );
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; else {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my $redirect4 = &quot;/users/login_failed_some.html&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $m-&gt;comp( &quot;/generic/action/openidredir.ml&quot;, url =&gt; $redirect4, cookie =&gt; $cookie );
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>}<br>&lt;/%init&gt;<br>
____________________<br><br>&lt;%args&gt;<br>$url<br>$cookie<br>&lt;/%args&gt;<br><br>&lt;%init&gt;<br>use CGI::Cookie;<br>use Apache2::RequestRec;<br>use APR::Table ();<br>use Apache2::Const -compile =&gt; qw(REDIRECT);
<br>&nbsp; <br>$r-&gt;method(&#39;GET&#39;);<br>$r-&gt;headers_in-&gt;unset(&#39;Content-length&#39;);<br><br>$r-&gt;content_type(&#39;text/html&#39;);<br>$r-&gt;err_headers_out-&gt;add(&#39;Set-Cookie&#39; =&gt; $cookie) ;<br>
$r-&gt;headers_out-&gt;set(&#39;Location&#39; =&gt; $url);<br>$r-&gt;status(Apache2::Const::REDIRECT);<br>#$m-&gt;abort(302);<br>&lt;/%init&gt;<br>