ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Twitter.php
Go to the documentation of this file.
1 <?php
2 
3 require_once(dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/oauth/libextinc/OAuth.php');
4 
12 
16  const STAGE_INIT = 'twitter:init';
17 
21  const AUTHID = 'twitter:AuthId';
22 
23  private $key;
24  private $secret;
25  private $force_login;
26 
27 
34  public function __construct($info, $config) {
35  assert('is_array($info)');
36  assert('is_array($config)');
37 
38  // Call the parent constructor first, as required by the interface
39  parent::__construct($info, $config);
40 
41  $configObject = SimpleSAML_Configuration::loadFromArray($config, 'authsources[' . var_export($this->authId, TRUE) . ']');
42 
43  $this->key = $configObject->getString('key');
44  $this->secret = $configObject->getString('secret');
45  $this->force_login = $configObject->getBoolean('force_login', FALSE);
46  $this->include_email = $configObject->getBoolean('include_email', FALSE);
47  }
48 
49 
55  public function authenticate(&$state) {
56  assert('is_array($state)');
57 
58  // We are going to need the authId in order to retrieve this authentication source later
59  $state[self::AUTHID] = $this->authId;
60 
61  $stateID = SimpleSAML_Auth_State::saveState($state, self::STAGE_INIT);
62 
63  $consumer = new sspmod_oauth_Consumer($this->key, $this->secret);
64  // Get the request token
65  $linkback = SimpleSAML\Module::getModuleURL('authtwitter/linkback.php', array('AuthState' => $stateID));
66  $requestToken = $consumer->getRequestToken('https://api.twitter.com/oauth/request_token', array('oauth_callback' => $linkback));
67  SimpleSAML\Logger::debug("Got a request token from the OAuth service provider [" .
68  $requestToken->key . "] with the secret [" . $requestToken->secret . "]");
69 
70  $state['authtwitter:authdata:requestToken'] = $requestToken;
71  SimpleSAML_Auth_State::saveState($state, self::STAGE_INIT);
72 
73  // Authorize the request token
74  $url = 'https://api.twitter.com/oauth/authenticate';
75  if ($this->force_login) {
76  $url = \SimpleSAML\Utils\HTTP::addURLParameters($url, array('force_login' => 'true'));
77  }
78  $consumer->getAuthorizeRequest($url, $requestToken);
79  }
80 
81 
82  public function finalStep(&$state) {
83  $requestToken = $state['authtwitter:authdata:requestToken'];
84  $parameters = array();
85 
86  if (!isset($_REQUEST['oauth_token'])) {
87  throw new SimpleSAML_Error_BadRequest("Missing oauth_token parameter.");
88  }
89  if ($requestToken->key !== (string)$_REQUEST['oauth_token']) {
90  throw new SimpleSAML_Error_BadRequest("Invalid oauth_token parameter.");
91  }
92 
93  if (!isset($_REQUEST['oauth_verifier'])) {
94  throw new SimpleSAML_Error_BadRequest("Missing oauth_verifier parameter.");
95  }
96  $parameters['oauth_verifier'] = (string)$_REQUEST['oauth_verifier'];
97 
98  $consumer = new sspmod_oauth_Consumer($this->key, $this->secret);
99 
100  SimpleSAML\Logger::debug("oauth: Using this request token [" .
101  $requestToken->key . "] with the secret [" . $requestToken->secret . "]");
102 
103  // Replace the request token with an access token
104  $accessToken = $consumer->getAccessToken('https://api.twitter.com/oauth/access_token', $requestToken, $parameters);
105  SimpleSAML\Logger::debug("Got an access token from the OAuth service provider [" .
106  $accessToken->key . "] with the secret [" . $accessToken->secret . "]");
107 
108  $verify_credentials_url = 'https://api.twitter.com/1.1/account/verify_credentials.json';
109  if ($this->include_email) {
110  $verify_credentials_url = $verify_credentials_url . '?include_email=true';
111  }
112  $userdata = $consumer->getUserInfo($verify_credentials_url, $accessToken);
113 
114  if (!isset($userdata['id_str']) || !isset($userdata['screen_name'])) {
115  throw new SimpleSAML_Error_AuthSource($this->authId, 'Authentication error: id_str and screen_name not set.');
116  }
117 
118  $attributes = array();
119  foreach($userdata AS $key => $value) {
120  if (is_string($value))
121  $attributes['twitter.' . $key] = array((string)$value);
122  }
123 
124  $attributes['twitter_at_screen_name'] = array('@' . $userdata['screen_name']);
125  $attributes['twitter_screen_n_realm'] = array($userdata['screen_name'] . '@twitter.com');
126  $attributes['twitter_targetedID'] = array('http://twitter.com!' . $userdata['id_str']);
127 
128  $state['Attributes'] = $attributes;
129  }
130 
131 }
Add rich text string
__construct($info, $config)
Constructor for this authentication source.
Definition: Twitter.php:34
static debug($string)
Definition: Logger.php:213
$userdata
Definition: demo.php:48
$attributes
authenticate(&$state)
Log-in using Twitter platform.
Definition: Twitter.php:55
$requestToken
Definition: demo.php:33
const STAGE_INIT
The string used to identify our states.
Definition: Twitter.php:16
static getModuleURL($resource, array $parameters=array())
Get absolute URL to a specified module resource.
Definition: Module.php:303
const AUTHID
The key of the AuthId field in the state.
Definition: Twitter.php:21
if(!array_key_exists('stateid', $_REQUEST)) $state
Handle linkback() response from LinkedIn.
Definition: linkback.php:10
$consumer
Definition: demo.php:30
Create styles array
The data for the language used.
$accessToken
Definition: demo.php:45
$url
$info
Definition: index.php:5
static loadFromArray($config, $location='[ARRAY]', $instance=null)
Loads a configuration from the given array.
static saveState(&$state, $stage, $rawId=false)
Save the state.
Definition: State.php:194