summaryrefslogtreecommitdiffstats
path: root/modules/authtwitter/lib/Auth/Source/Twitter.php
blob: 1fba2eb5009525d50e9e12f6615fc2e883548d83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php

require_once(dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/oauth/libextinc/OAuth.php');

/**
 * Authenticate using Twitter.
 *
 * @author Andreas Åkre Solberg, UNINETT AS.
 * @package simpleSAMLphp
 * @version $Id$
 */
class sspmod_authtwitter_Auth_Source_Twitter extends SimpleSAML_Auth_Source {

	/**
	 * The string used to identify our states.
	 */
	const STAGE_INIT = 'twitter:init';

	/**
	 * The key of the AuthId field in the state.
	 */
	const AUTHID = 'twitter:AuthId';

	private $key;
	private $secret;


	/**
	 * Constructor for this authentication source.
	 *
	 * @param array $info  Information about this authentication source.
	 * @param array $config  Configuration.
	 */
	public function __construct($info, $config) {
		assert('is_array($info)');
		assert('is_array($config)');

		/* Call the parent constructor first, as required by the interface. */
		parent::__construct($info, $config);

		if (!array_key_exists('key', $config))
			throw new Exception('Twitter authentication source is not properly configured: missing [key]');
		
		$this->key = $config['key'];
		
		if (!array_key_exists('secret', $config))
			throw new Exception('Twitter authentication source is not properly configured: missing [secret]');

		$this->secret = $config['secret'];

		// require_once(dirname(dirname(dirname(dirname(__FILE__)))) . '/extlibinc/facebook.php');

	}


	/**
	 * Log-in using Facebook platform
	 *
	 * @param array &$state  Information about the current authentication.
	 */
	public function authenticate(&$state) {
		assert('is_array($state)');

		/* We are going to need the authId in order to retrieve this authentication source later. */
		$state[self::AUTHID] = $this->authId;
		
		$stateID = SimpleSAML_Auth_State::saveState($state, self::STAGE_INIT);
		
		// SimpleSAML_Logger::debug('facebook auth state id = ' . $stateID);
		
		$consumer = new sspmod_oauth_Consumer($this->key, $this->secret);

		// Get the request token
		$requestToken = $consumer->getRequestToken('http://twitter.com/oauth/request_token');
		SimpleSAML_Logger::debug("Got a request token from the OAuth service provider [" . 
			$requestToken->key . "] with the secret [" . $requestToken->secret . "]");

		$oauthState = array(
			'requestToken' => serialize($requestToken),
			'stateid' => $stateID,
		);
		$session = SimpleSAML_Session::getInstance();
		$session->setData('oauth', 'oauth', $oauthState);

		// Authorize the request token
		$consumer->getAuthorizeRequest('http://twitter.com/oauth/authenticate', $requestToken);

	}
	
	
	
	public function finalStep(&$state) {
		
		
		
		
		$requestToken = unserialize($state['requestToken']);
		
		#echo '<pre>'; print_r($requestToken); exit;
		
		$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
		$accessToken = $consumer->getAccessToken('http://twitter.com/oauth/access_token', $requestToken);
		SimpleSAML_Logger::debug("Got an access token from the OAuth service provider [" . 
			$accessToken->key . "] with the secret [" . $accessToken->secret . "]");
			

		
		$userdata = $consumer->getUserInfo('http://twitter.com/account/verify_credentials.json', $accessToken);
		
		$attributes = array();
		foreach($userdata AS $key => $value) {
			if (is_string($value))
				$attributes['twitter.' . $key] = array((string)$value);
			
		}
		
		if (array_key_exists('screen_name', $userdata) ) {
			$attributes['twitter_at_screen_name'] = array('@' . $userdata['screen_name']);
			$attributes['twitter_screen_n_realm'] = array($userdata['screen_name'] . '@twitter.com');
		}
		if (array_key_exists('id_str', $userdata) )
			$attributes['twitter_targetedID'] = array('http://twitter.com!' . $userdata['id_str']);
			
		
		$state['Attributes'] = $attributes;
	}

}

?>