[Jifty-commit] r4260 - in jifty/trunk: . lib/Jifty/Plugin/OAuth t/TestApp-Plugin-OAuth/lib/TestApp/Plugin/OAuth

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Fri Oct 19 00:30:22 EDT 2007


Author: sartak
Date: Fri Oct 19 00:30:18 2007
New Revision: 4260

Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/Plugin/OAuth/Dispatcher.pm
   jifty/trunk/lib/Jifty/Plugin/OAuth/View.pm
   jifty/trunk/t/TestApp-Plugin-OAuth/lib/TestApp/Plugin/OAuth/Test.pm
   jifty/trunk/t/TestApp-Plugin-OAuth/t/02-request-token.t

Log:
 r43853 at onn:  sartak | 2007-10-19 00:30:11 -0400
 Now we send responses to token requests (and test them)
 What's left:
     Authorizing request tokens is coded, but it's slightly flawed somehow
     Getting access tokens is coded, but not tested
     No code yet for the actual accessing of resources


Modified: jifty/trunk/lib/Jifty/Plugin/OAuth/Dispatcher.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/OAuth/Dispatcher.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin/OAuth/Dispatcher.pm	Fri Oct 19 00:30:18 2007
@@ -9,9 +9,9 @@
 use Net::OAuth::ProtectedResourceRequest;
 use Crypt::OpenSSL::RSA;
 
-before POST '/oauth/request_token' => \&request_token;
+on     POST '/oauth/request_token' => \&request_token;
 before GET  '/oauth/authorize'     => \&authorize;
-before POST '/oauth/access_token'  => \&access_token;
+on     POST '/oauth/access_token'  => \&access_token;
 
 # helper function to abort with a debug message
 sub abortmsg {
@@ -60,7 +60,11 @@
     abortmsg(401, "Unable to create a Request Token: " . $@ || $msg)
         if $@ || !$ok;
 
-    # XXX: actually send the token
+    set oauth_response => {
+        oauth_token        => $token->token,
+        oauth_token_secret => $token->secret
+    };
+    show 'oauth/response';
     abortmsg(200, 'Correctly issued a Request Token');
 }
 
@@ -131,7 +135,11 @@
     abortmsg(401, "Unable to create an Access Token: " . $@ || $msg)
         if $@ || !defined($token) || !$ok;
 
-    # XXX: actually send the token
+    set oauth_response => {
+        oauth_token        => $token->token,
+        oauth_token_secret => $token->secret
+    };
+    show 'oauth/response';
     abortmsg(200, 'Correctly issued an Access Token');
 }
 

Modified: jifty/trunk/lib/Jifty/Plugin/OAuth/View.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/OAuth/View.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin/OAuth/View.pm	Fri Oct 19 00:30:18 2007
@@ -4,6 +4,17 @@
 
 use Jifty::View::Declare -base;
 
+template 'oauth/response' => sub {
+    my $params = get 'oauth_response';
+    if (ref($params) eq 'HASH') {
+        outs_raw join '&',
+                 map { sprintf '%s=%s',
+                       map { Jifty->web->escape_uri($_) }
+                       $_, $params->{$_}
+                 } keys %$params;
+    }
+};
+
 template 'oauth' => page {
     p {
         b { a { attr { href => "http://oauth.net/" } "OAuth" } };

Modified: jifty/trunk/t/TestApp-Plugin-OAuth/lib/TestApp/Plugin/OAuth/Test.pm
==============================================================================
--- jifty/trunk/t/TestApp-Plugin-OAuth/lib/TestApp/Plugin/OAuth/Test.pm	(original)
+++ jifty/trunk/t/TestApp-Plugin-OAuth/lib/TestApp/Plugin/OAuth/Test.pm	Fri Oct 19 00:30:18 2007
@@ -8,7 +8,7 @@
 use Crypt::OpenSSL::RSA;
 use Digest::HMAC_SHA1 'hmac_sha1';
 
-our @EXPORT = qw($timestamp $url $mech $pubkey $seckey response_is sign);
+our @EXPORT = qw($timestamp $url $mech $pubkey $seckey response_is sign get_latest_token);
 
 sub setup {
     my $class = shift;
@@ -65,6 +65,14 @@
 
     local $Test::Builder::Level = $Test::Builder::Level + 1;
     main::is($r->code, $code, $testname);
+
+    my $token = get_latest_token();
+    if ($code == 200) {
+        main::ok($token, "Successfully loaded a token object with token ".$token->token.".");
+    }
+    else {
+        main::ok(!$token, "Did not get a token");
+    }
 }
 
 sub sign {
@@ -131,5 +139,43 @@
     return $contents;
 }
 
+sub get_latest_token {
+    my $content = $mech->content;
+
+    $content =~ s/\boauth_token=(\w+)//
+        or return;
+    my $token = $1;
+
+    $content =~ s/\boauth_token_secret=(\w+)//
+        or return;
+    my $secret = $1;
+
+    local $Test::Builder::Level = $Test::Builder::Level + 1;
+    main::is($content, '&', "the output was exactly oauth_token=...&oauth_secret=...");
+
+    my $package = 'Jifty::Plugin::OAuth::Model::';
+
+    if ($mech->uri =~ /request_token/) {
+        $package .= 'RequestToken';
+    }
+    elsif ($mech->uri =~ /request_token/) {
+        $package .= 'AccessToken';
+    }
+    else {
+        Jifty->log->error("Called get_latest_token, but I cannot grok the URI " . $mech->uri);
+        return;
+    }
+
+    my $token_obj = $package->new(current_user => Jifty::CurrentUser->superuser);
+    $token_obj->load_by_cols(token => $token);
+
+    if (!$token_obj->id) {
+        Jifty->log->error("Could not find a $package with token $token");
+        return;
+    }
+
+    return $token_obj;
+}
+
 1;
 

Modified: jifty/trunk/t/TestApp-Plugin-OAuth/t/02-request-token.t
==============================================================================
--- jifty/trunk/t/TestApp-Plugin-OAuth/t/02-request-token.t	(original)
+++ jifty/trunk/t/TestApp-Plugin-OAuth/t/02-request-token.t	Fri Oct 19 00:30:18 2007
@@ -5,7 +5,7 @@
 use lib 't/lib';
 use Jifty::SubTest;
 
-use TestApp::Plugin::OAuth::Test tests => 24;
+use TestApp::Plugin::OAuth::Test tests => 50;
 use Jifty::Test::WWW::Mechanize;
 
 my $server  = Jifty::Test->make_server;


More information about the Jifty-commit mailing list