Skip to content
Snippets Groups Projects
Commit 5a5df09d authored by Andreas Åkre Solberg's avatar Andreas Åkre Solberg
Browse files

Some fixes for the Twitter Authentication module using OAuth, and added documentation as well

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@1933 44740490-163a-0410-bde0-09ae8108e29a
parent cc7aae18
No related branches found
No related tags found
No related merge requests found
Using the Twitter authentication source with simpleSAMLphp
==========================================================
Remember to configure `authsources.php`, with both Consumer key and secret.
To get an API key and a secret, register the application at:
* <http://twitter.com/oauth_clients>
Set the callback URL to be:
* `http://sp.example.org/simplesaml/module.php/authtwitter/linkback.php`
Replace `sp.example.org` with your hostname.
## Testing authentication
On the SimpleSAMLphp frontpage, go to the *Authentication* tab, and use the link:
* *Test configured authentication sources*
Then choose the *twitter* authentication source.
Expected behaviour would then be that you are sent to twitter, and asked to login:
![](http://clippings.erlang.no/ZZ2EE26BF6.jpg)
The first time a user uses your application to login, he/she is asked for consent:
![](http://clippings.erlang.no/ZZ6B18B5D9.jpg)
You will then be authenticated in SimpleSAMLphp and see an attribute set similar to this:
![](http://clippings.erlang.no/ZZ74A6835E.jpg)
<?php <?php
require_once(dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/oauth/libextinc/OAuth.php');
/** /**
* Authenticate using Twitter. * Authenticate using Twitter.
* *
...@@ -74,7 +76,7 @@ class sspmod_authtwitter_Auth_Source_Twitter extends SimpleSAML_Auth_Source { ...@@ -74,7 +76,7 @@ class sspmod_authtwitter_Auth_Source_Twitter extends SimpleSAML_Auth_Source {
$requestToken->key . "] with the secret [" . $requestToken->secret . "]"); $requestToken->key . "] with the secret [" . $requestToken->secret . "]");
$oauthState = array( $oauthState = array(
'requestToken' => $requestToken, 'requestToken' => serialize($requestToken),
'stateid' => $stateID, 'stateid' => $stateID,
); );
$session = SimpleSAML_Session::getInstance(); $session = SimpleSAML_Session::getInstance();
...@@ -89,15 +91,25 @@ class sspmod_authtwitter_Auth_Source_Twitter extends SimpleSAML_Auth_Source { ...@@ -89,15 +91,25 @@ class sspmod_authtwitter_Auth_Source_Twitter extends SimpleSAML_Auth_Source {
public function finalStep(&$state) { public function finalStep(&$state) {
$requestToken = $state['requestToken'];
$requestToken = unserialize($state['requestToken']);
#echo '<pre>'; print_r($requestToken); exit;
$consumer = new sspmod_oauth_Consumer($this->key, $this->secret); $consumer = new sspmod_oauth_Consumer($this->key, $this->secret);
SimpleSAML_Logger::debug("oauth: Using this request token [" .
$requestToken->key . "] with the secret [" . $requestToken->secret . "]");
// Replace the request token with an access token // Replace the request token with an access token
$accessToken = $consumer->getAccessToken('http://twitter.com/oauth/access_token', $requestToken); $accessToken = $consumer->getAccessToken('http://twitter.com/oauth/access_token', $requestToken);
SimpleSAML_Logger::debug("Got an access token from the OAuth service provider [" . SimpleSAML_Logger::debug("Got an access token from the OAuth service provider [" .
$accessToken->key . "] with the secret [" . $accessToken->secret . "]"); $accessToken->key . "] with the secret [" . $accessToken->secret . "]");
$userdata = $consumer->getUserInfo('http://twitter.com/account/verify_credentials.json', $accessToken); $userdata = $consumer->getUserInfo('http://twitter.com/account/verify_credentials.json', $accessToken);
$attributes = array(); $attributes = array();
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
/** /**
* Handle linkback() response from Twitter. * Handle linkback() response from Twitter.
*/ */
sspmod_oauth_Consumer::dummy(); # sspmod_oauth_Consumer::dummy();
// $config = SimpleSAML_Configuration::getInstance(); // $config = SimpleSAML_Configuration::getInstance();
$session = SimpleSAML_Session::getInstance(); $session = SimpleSAML_Session::getInstance();
...@@ -13,7 +13,6 @@ $oauthState = $session->getData('oauth', 'oauth'); ...@@ -13,7 +13,6 @@ $oauthState = $session->getData('oauth', 'oauth');
if (empty($oauthState)) throw new Exception('Could not load oauthstate'); if (empty($oauthState)) throw new Exception('Could not load oauthstate');
if (empty($oauthState['stateid'])) throw new Exception('Could not load oauthstate:stateid'); if (empty($oauthState['stateid'])) throw new Exception('Could not load oauthstate:stateid');
$stateId = $oauthState['stateid']; $stateId = $oauthState['stateid'];
// echo 'stateid is ' . $stateId; // echo 'stateid is ' . $stateId;
...@@ -21,6 +20,8 @@ $stateId = $oauthState['stateid']; ...@@ -21,6 +20,8 @@ $stateId = $oauthState['stateid'];
$state = SimpleSAML_Auth_State::loadState($stateId, sspmod_authtwitter_Auth_Source_Twitter::STAGE_INIT); $state = SimpleSAML_Auth_State::loadState($stateId, sspmod_authtwitter_Auth_Source_Twitter::STAGE_INIT);
$state['requestToken'] = $oauthState['requestToken']; $state['requestToken'] = $oauthState['requestToken'];
/* Find authentication source. */ /* Find authentication source. */
assert('array_key_exists(sspmod_authtwitter_Auth_Source_Twitter::AUTHID, $state)'); assert('array_key_exists(sspmod_authtwitter_Auth_Source_Twitter::AUTHID, $state)');
$sourceId = $state[sspmod_authtwitter_Auth_Source_Twitter::AUTHID]; $sourceId = $state[sspmod_authtwitter_Auth_Source_Twitter::AUTHID];
...@@ -30,10 +31,14 @@ if ($source === NULL) { ...@@ -30,10 +31,14 @@ if ($source === NULL) {
throw new Exception('Could not find authentication source with id ' . $sourceId); throw new Exception('Could not find authentication source with id ' . $sourceId);
} }
$config = SimpleSAML_Configuration::getInstance(); $config = SimpleSAML_Configuration::getInstance();
$source->finalStep($state); $source->finalStep($state);
SimpleSAML_Auth_Source::completeAuth($state); SimpleSAML_Auth_Source::completeAuth($state);
...@@ -60,13 +60,13 @@ class sspmod_oauth_Consumer { ...@@ -60,13 +60,13 @@ class sspmod_oauth_Consumer {
$acc_req = OAuthRequest::from_consumer_and_token($this->consumer, $requestToken, "GET", $url, NULL); $acc_req = OAuthRequest::from_consumer_and_token($this->consumer, $requestToken, "GET", $url, NULL);
$acc_req->sign_request($this->signer, $this->consumer, $requestToken); $acc_req->sign_request($this->signer, $this->consumer, $requestToken);
$response_acc = file_get_contents($acc_req->to_url()); $response_acc = file_get_contents($acc_req->to_url());
if ($response_acc === FALSE) { if ($response_acc === FALSE) {
throw new Exception('Error contacting request_token endpoint on the OAuth Provider'); throw new Exception('Error contacting request_token endpoint on the OAuth Provider');
} }
SimpleSAML_Logger::info(' ==== RESPONSE: '. $response_acc); SimpleSAML_Logger::debug('oauth: Reponse to get access token: '. $response_acc);
parse_str($response_acc, $accessResponseParsed); parse_str($response_acc, $accessResponseParsed);
......
...@@ -326,6 +326,9 @@ class OAuthRequest {/*{{{*/ ...@@ -326,6 +326,9 @@ class OAuthRequest {/*{{{*/
public function get_normalized_http_url() {/*{{{*/ public function get_normalized_http_url() {/*{{{*/
$parts = parse_url($this->http_url); $parts = parse_url($this->http_url);
if (!isset($parts['port'])) $parts['port'] = '80';
if (!isset($parts['path'])) $parts['part'] = '';
$port = @$parts['port']; $port = @$parts['port'];
$scheme = $parts['scheme']; $scheme = $parts['scheme'];
$host = $parts['host']; $host = $parts['host'];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment