Андрей (andy) wrote in changelog,
Андрей
andy
changelog

[ljcom] r9323: LJSUP-6137 (FaceBook authorization. Serv...

Committer: ailyin
LJSUP-6137 (FaceBook authorization. Server side): make the code more maintainable, thanks to Igor's suggestions
U   trunk/cgi-bin/LJ/API/BitLy.pm
U   trunk/cgi-bin/LJ/API/Twitter.pm
U   trunk/cgi-bin/LJ/Hooks/Userinfo.pm
U   trunk/cgi-bin/LJ/Identity/Facebook.pm
U   trunk/cgi-bin/LJ/Worker/Repost/CommentToFacebook.pm
U   trunk/cgi-bin/LJ/Worker/Repost/EntryToFacebook.pm
U   trunk/htdocs/identity/callback-facebook.bml
U   trunk/htdocs/manage/settings/facebook.bml
U   trunk/htdocs/manage/settings/twitter.bml
Modified: trunk/cgi-bin/LJ/API/BitLy.pm
===================================================================
--- trunk/cgi-bin/LJ/API/BitLy.pm	2010-08-09 07:17:24 UTC (rev 9322)
+++ trunk/cgi-bin/LJ/API/BitLy.pm	2010-08-10 07:42:50 UTC (rev 9323)
@@ -6,7 +6,7 @@
 sub shorten {
     my ($class, $url) = @_;
 
-    my $ua = LWP::UserAgent->new;
+    my $ua = LJ::get_useragent( 'role' => 'bit_ly_shorten' );
     my $address = 'http://api.j.mp/v3/shorten?' .
                   'login=' . $LJ::BIT_LY_LOGIN . '&' .
                   'apiKey=' . $LJ::BIT_LY_API_KEY . '&' .

Modified: trunk/cgi-bin/LJ/API/Twitter.pm
===================================================================
--- trunk/cgi-bin/LJ/API/Twitter.pm	2010-08-09 07:17:24 UTC (rev 9322)
+++ trunk/cgi-bin/LJ/API/Twitter.pm	2010-08-10 07:42:50 UTC (rev 9323)
@@ -7,7 +7,6 @@
 use Net::OAuth::AccessTokenRequest;
 use Net::OAuth::ProtectedResourceRequest;
 use HTTP::Request::Common;
-use LWP::UserAgent;
 
 =head1 NAME
 
@@ -100,10 +99,14 @@
 
     $request->sign;
 
-    my $ua = LWP::UserAgent->new;
+    my $ua = LJ::get_useragent( 'role' => 'twitter_auth' );
     my $res = $ua->post($request->to_url);
 
     unless ($res->is_success) {
+        require Data::Dumper;
+        warn 'twitter connectivity error: ' .
+             Data::Dumper::Dumper($res);
+
         die "twitter connectivity error";
     }
 
@@ -134,10 +137,14 @@
 
     $request->sign;
 
-    my $ua = LWP::UserAgent->new;
+    my $ua = LJ::get_useragent( 'role' => 'twitter_auth' );
     my $res = $ua->post($request->to_url);
 
     unless ($res->is_success) {
+        require Data::Dumper;
+        warn 'twitter connectivity error: ' .
+             Data::Dumper::Dumper($res);
+
         die "twitter connectivity error";
     }
 
@@ -190,7 +197,7 @@
 
     $request->sign;
 
-    my $ua = LWP::UserAgent->new;
+    my $ua = LJ::get_useragent( 'role' => 'twitter_auth' );
     my $res;
     if ($http_method eq 'GET') {
         $res = $ua->get($request->to_url);
@@ -199,6 +206,10 @@
     }
 
     unless ($res->is_success) {
+        require Data::Dumper;
+        warn 'twitter connectivity error: ' .
+             Data::Dumper::Dumper($res);
+
         die "twitter connectivity error";
     }
 
@@ -232,7 +243,11 @@
             SELECT secret FROM twitter_request_tokens WHERE public=?
         }, undef, $public);
 
-        return unless $secret;
+        unless ($secret) {
+            warn 'couldn\'t get a Twitter secret by their public '.
+                 'request token: ' . $public;
+            return;
+        }
 
         LJ::MemCache::set("twitter_secret:$public" => $secret);
     }

Modified: trunk/cgi-bin/LJ/Hooks/Userinfo.pm
===================================================================
--- trunk/cgi-bin/LJ/Hooks/Userinfo.pm	2010-08-09 07:17:24 UTC (rev 9322)
+++ trunk/cgi-bin/LJ/Hooks/Userinfo.pm	2010-08-10 07:42:50 UTC (rev 9323)
@@ -479,6 +479,7 @@
     return;
 });
 
+# called from /manage/profile/index.bml
 LJ::register_hook('extra_im_services', sub {
     my ($u) = @_;
 
@@ -524,6 +525,7 @@
     return $ret;
 });
 
+# called from /manage/profile/index.bml
 LJ::register_hook('save_extra_profile_fields', sub {
     my ($u) = @_;
 

Modified: trunk/cgi-bin/LJ/Identity/Facebook.pm
===================================================================
--- trunk/cgi-bin/LJ/Identity/Facebook.pm	2010-08-09 07:17:24 UTC (rev 9322)
+++ trunk/cgi-bin/LJ/Identity/Facebook.pm	2010-08-10 07:42:50 UTC (rev 9323)
@@ -1,3 +1,10 @@
+=head1 SEE ALSO
+
+Facebook API documentation, including OAuth 2 and Graph API:
+http://developers.facebook.com/docs/
+
+=cut
+
 package LJ::Identity::Facebook;
 use strict;
 
@@ -67,7 +74,7 @@
 sub initialize_user {
     my ($self, $u, $extra) = @_;
 
-    my $ua = LWP::UserAgent->new;
+    my $ua = LJ::get_useragent( 'role' => 'facebook_auth' );
 
     my $token = $extra->{'token'};
     die "no access token passed" unless $token;

Modified: trunk/cgi-bin/LJ/Worker/Repost/CommentToFacebook.pm
===================================================================
--- trunk/cgi-bin/LJ/Worker/Repost/CommentToFacebook.pm	2010-08-09 07:17:24 UTC (rev 9322)
+++ trunk/cgi-bin/LJ/Worker/Repost/CommentToFacebook.pm	2010-08-10 07:42:50 UTC (rev 9323)
@@ -1,3 +1,12 @@
+=head1 SEE ALSO
+
+Facebook API documentation, including OAuth 2 and Graph API:
+http://developers.facebook.com/docs/
+
+LJ::Identity::Facebook
+
+=cut
+
 package LJ::Worker::Repost::CommentToFacebook;
 use strict;
 
@@ -62,7 +71,7 @@
     $data->{'name'} = $title;
     $data->{'description'} = $body;
 
-    my $ua      = LWP::UserAgent->new;
+    my $ua      = LJ::get_useragent( 'role' => 'facebook_auth' );
     $ua->post('https://graph.facebook.com/me/feed', $data);
 
     return $job->completed;

Modified: trunk/cgi-bin/LJ/Worker/Repost/EntryToFacebook.pm
===================================================================
--- trunk/cgi-bin/LJ/Worker/Repost/EntryToFacebook.pm	2010-08-09 07:17:24 UTC (rev 9322)
+++ trunk/cgi-bin/LJ/Worker/Repost/EntryToFacebook.pm	2010-08-10 07:42:50 UTC (rev 9323)
@@ -1,3 +1,12 @@
+=head1 SEE ALSO
+
+Facebook API documentation, including OAuth 2 and Graph API:
+http://developers.facebook.com/docs/
+
+LJ::Identity::Facebook
+
+=cut
+
 package LJ::Worker::Repost::EntryToFacebook;
 use strict;
 
@@ -62,7 +71,7 @@
     $data->{'name'} = $title;
     $data->{'description'} = $event;
 
-    my $ua      = LWP::UserAgent->new;
+    my $ua      = LJ::get_useragent( 'role' => 'facebook_auth' );
     $ua->post('https://graph.facebook.com/me/feed', $data);
 
     return $job->completed;

Modified: trunk/htdocs/identity/callback-facebook.bml
===================================================================
--- trunk/htdocs/identity/callback-facebook.bml	2010-08-09 07:17:24 UTC (rev 9322)
+++ trunk/htdocs/identity/callback-facebook.bml	2010-08-10 07:42:50 UTC (rev 9323)
@@ -13,13 +13,23 @@
     my $returl_fail = LJ::Request->get_param('ret_fail') ||
                       $returl ||$LJ::SITEROOT;
 
-    return LJ::Request->redirect($returl_fail)
-        unless LJ::Identity::Facebook->enabled;
+    unless (LJ::Identity::Facebook->enabled) {
+        return 'This feature is disabled.';
+    }
 
     my $fb_code = LJ::Request->get_param('code');
 
-    return LJ::Request->redirect($returl_fail) unless $fb_code;
+    unless ($fb_code) {
+        return LJ::Request->redirect($returl_fail)
+            if LJ::Request->get_param('error_reason') eq 'user_denied';
 
+        require Data::Dumper;
+        warn "facebook connectivity error: " .
+            Data::Dumper::Dumper(\%GET);
+
+        return '<?errorbar Facebook connectivity error errorbar?>';
+    }
+
     my $selfurl = "$LJ::SITEROOT/identity/callback-facebook.bml?" .
                   'ret=' . LJ::Text->eurl($returl) . '&' .
                   'ret_fail=' . LJ::Text->eurl($returl_fail);
@@ -30,10 +40,14 @@
                "client_secret=$LJ::FACEBOOK_CONNECT_SECRET&" .
                "code=" . LJ::Text->eurl($fb_code);
 
-    my $ua = LWP::UserAgent->new;
+    my $ua = LJ::get_useragent( 'role' => 'facebook_auth' );
     my $res = $ua->get($addr);
 
     unless ($res->is_success) {
+        require Data::Dumper;
+        warn "facebook connectivity error: " .
+            Data::Dumper::Dumper($res);
+
         return '<?errorbar Facebook connectivity error errorbar?>';
     }
 

Modified: trunk/htdocs/manage/settings/facebook.bml
===================================================================
--- trunk/htdocs/manage/settings/facebook.bml	2010-08-09 07:17:24 UTC (rev 9322)
+++ trunk/htdocs/manage/settings/facebook.bml	2010-08-10 07:42:50 UTC (rev 9323)
@@ -14,24 +14,42 @@
     my $settings_page = "$LJ::SITEROOT/manage/settings/?cat=privacy";
 
     # the feature is disabled, go away
-    return LJ::Request->redirect($settings_page)
-        unless LJ::is_enabled('repost_facebook')
-            && $LJ::FACEBOOK_CONNECT_API_KEY;
+    unless (     LJ::is_enabled('repost_facebook')
+             && $LJ::FACEBOOK_CONNECT_API_KEY)
+    {
+        return 'This feature is disabled.';
+    }
 
-    # bogus remote or parameters, redirect them back to settings
-    return LJ::Request->redirect($settings_page)
-        unless $remote
-            && $act =~ /^(?:connect|disconnect)$/;
+    # bogus remote or parameters, go away
+    unless ($remote) {
+        return 'You must be logged in to access this page.';
+    }
 
-    return LJ::Request->redirect($settings_page)
-        if    $remote->is_identity
-           && $remote->identity->short_code eq 'facebook';
+    unless ($act =~ /^(?:connect|disconnect)$/) {
+        return 'Bogus form data; please go back and try again.';
+    }
 
+    if (    $remote->is_identity
+         && $remote->identity->short_code eq 'facebook' )
+    {
+        return 'You cannot change your settings here, because your ' .
+               'account is a Facebook identity.';
+    }
+
     if ($act eq 'connect') {
         my $fb_code = LJ::Request->get_param('code');
 
-        return LJ::Request->redirect($settings_page) unless $fb_code;
+        unless ($fb_code) {
+            return LJ::Request->redirect($settings_page)
+                if LJ::Request->get_param('error_reason') eq 'user_denied';
 
+            require Data::Dumper;
+            warn "facebook connectivity error: " .
+                Data::Dumper::Dumper(\%GET);
+
+            return '<?errorbar Facebook connectivity error errorbar?>';
+        }
+
         my $selfurl = "$LJ::SITEROOT/manage/settings/facebook.bml?" .
                       'act=connect';
 
@@ -41,10 +59,14 @@
                    "client_secret=$LJ::FACEBOOK_CONNECT_SECRET&" .
                    "code=" . LJ::Text->eurl($fb_code);
 
-        my $ua = LWP::UserAgent->new;
+        my $ua = LJ::get_useragent( 'role' => 'facebook_auth' );
         my $res = $ua->get($addr);
 
         unless ($res->is_success) {
+            require Data::Dumper;
+            warn "facebook connectivity error: " .
+                Data::Dumper::Dumper($res);
+
             return '<?errorbar Facebook connectivity error errorbar?>';
         }
 

Modified: trunk/htdocs/manage/settings/twitter.bml
===================================================================
--- trunk/htdocs/manage/settings/twitter.bml	2010-08-09 07:17:24 UTC (rev 9322)
+++ trunk/htdocs/manage/settings/twitter.bml	2010-08-10 07:42:50 UTC (rev 9323)
@@ -16,22 +16,35 @@
     my $settings_page = "$LJ::SITEROOT/manage/settings/?cat=privacy";
 
     # the feature is disabled, go away
-    return LJ::Request->redirect($settings_page)
-        unless LJ::is_enabled('repost_twitter')
-            && $LJ::TWITTER_CONSUMER_KEY;
+    unless (      LJ::is_enabled('repost_twitter')
+             && $LJ::TWITTER_CONSUMER_KEY )
+    {
+        return 'This feature is disabled.';
+    }
 
-    # bogus remote or parameters, redirect them back to settings
-    return LJ::Request->redirect($settings_page)
-        unless $remote
-            && $act =~ /^(?:connect|disconnect)$/;
+    # bogus remote or parameters, go away
+    unless ($remote) {
+        return 'You must be logged in to access this page.';
+    }
 
+    unless ($act =~ /^(?:connect|disconnect)$/) {
+        return 'Bogus form data; please go back and try again.';
+    }
+
     if ($act eq 'connect') {
         my $public   = LJ::Request->get_param('oauth_token');
         my $verifier = LJ::Request->get_param('oauth_verifier');
 
-        return LJ::Request->redirect($settings_page)
-            unless $public && $verifier;
+        unless ( $public && $verifier ) {
+            require Data::Dumper;
 
+            warn 'could not get a token or a verifier from Twitter ' .
+                 '(this could be a user cancel) '.
+                 Data::Dumper::Dumper(\%GET);
+
+            return LJ::Request->redirect($settings_page);
+        }
+
         my $request_token = LJ::API::Twitter->fetch_request_token($public);
         my $access_token  = LJ::API::Twitter->request_access_token(
             $request_token, $verifier

Tags: andy, bml, ljcom, pm
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments