From 34d674622a15c61ce26bc9c71184c70a8ccdaccd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andreas=20=C3=85kre=20Solberg?= <andreas.solberg@uninett.no>
Date: Fri, 8 May 2009 14:41:34 +0000
Subject: [PATCH] Improvements to OAuth proof of concept code...

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@1496 44740490-163a-0410-bde0-09ae8108e29a
---
 modules/oauth/bin/demo.php     |  8 +++++++-
 modules/oauth/lib/Consumer.php | 29 +++++++++++++++++++----------
 2 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/modules/oauth/bin/demo.php b/modules/oauth/bin/demo.php
index e73fe124e..ee455bdc0 100755
--- a/modules/oauth/bin/demo.php
+++ b/modules/oauth/bin/demo.php
@@ -22,7 +22,13 @@ $requestToken = $consumer->getRequestToken($baseurl . '/module.php/oauth/request
 echo "Got a request token from the OAuth service provider [" . $requestToken->key . "] with the secret [" . $requestToken->secret . "]\n";
 
 // Authorize the request token
-$consumer->getAuthorizeRequest($baseurl . '/module.php/oauth/authorize.php', $requestToken);
+$url = $consumer->getAuthorizeRequest($baseurl . '/module.php/oauth/authorize.php', $requestToken, FALSE);
+
+echo('Go to this URL to authenticate/authorize the request: ' . $url . "\n");
+system('open ' . $url);
+
+echo('Waiting 15 seconds for you to complete the authorization...' . "\n");
+sleep(15);
 
 // Replace the request token with an access token
 $accessToken = $consumer->getAccessToken( $baseurl . '/module.php/oauth/accessToken.php', $requestToken);
diff --git a/modules/oauth/lib/Consumer.php b/modules/oauth/lib/Consumer.php
index 4a7407442..6c80ad8d7 100644
--- a/modules/oauth/lib/Consumer.php
+++ b/modules/oauth/lib/Consumer.php
@@ -19,13 +19,17 @@ class sspmod_oauth_Consumer {
 		$this->signer = new OAuthSignatureMethod_HMAC_SHA1();
 	}
 	
+	// Used only to load the libextinc library early.
+	public static function dummy() {}
+	
 	public function getRequestToken($url) {
 		$req_req = OAuthRequest::from_consumer_and_token($this->consumer, NULL, "GET", $url, NULL);
 		$req_req->sign_request($this->signer, $this->consumer, NULL);
 
-		echo "Requesting a request token\n";
-		// echo 'go to url: ' . $req_req->to_url() . "\n"; exit;
 		$response_req = file_get_contents($req_req->to_url());
+		if ($response_req === FALSE) {
+			throw new Exception('Error contacting request_token endpoint on the OAuth Provider');
+		}
 
 		parse_str($response_req, $responseParsed);
 		
@@ -38,15 +42,16 @@ class sspmod_oauth_Consumer {
 		return new OAuthToken($requestToken, $requestTokenSecret);
 	}
 	
-	public function getAuthorizeRequest($url, $requestToken) {
+	public function getAuthorizeRequest($url, $requestToken, $redirect = TRUE, $callback = NULL) {
 		$authorizeURL = $url . '?oauth_token=' . $requestToken->key;
-
-		echo "Please go to this URL to authorize access: " . $authorizeURL . "\n";
-		system("open " . $authorizeURL);
-
-		echo "Waiting 15 seconds for you to authenticate. Usually you should let the user enter return or click a continue button.\n";
-
-		sleep(15);
+		if ($callback) {
+			$authorizeURL .= '&oauth_callback=' . urlencode($callback);
+		}
+		if ($redirect) {
+			SimpleSAML_Utilities::redirect($authorizeURL);
+			exit;
+		}	
+		return $authorizeURL;
 	}
 	
 	public function getAccessToken($url, $requestToken) {
@@ -55,6 +60,10 @@ class sspmod_oauth_Consumer {
 		$acc_req->sign_request($this->signer, $this->consumer, $requestToken);
 
 		$response_acc = file_get_contents($acc_req->to_url());
+		if ($response_acc === FALSE) {
+			throw new Exception('Error contacting request_token endpoint on the OAuth Provider');
+		}
+
 		
 		parse_str($response_acc, $accessResponseParsed);
 		
-- 
GitLab