ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Twitter.php
Go to the documentation of this file.
1<?php
2
3require_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
26 private $key;
27
31 private $secret;
32
36 private $force_login;
37
42
49 public function __construct($info, $config)
50 {
51 assert(is_array($info));
52 assert(is_array($config));
53
54 // Call the parent constructor first, as required by the interface
55 parent::__construct($info, $config);
56
57 $configObject = SimpleSAML_Configuration::loadFromArray($config, 'authsources[' . var_export($this->authId, true) . ']');
58
59 $this->key = $configObject->getString('key');
60 $this->secret = $configObject->getString('secret');
61 $this->force_login = $configObject->getBoolean('force_login', false);
62 $this->include_email = $configObject->getBoolean('include_email', false);
63 }
64
65
71 public function authenticate(&$state)
72 {
73 assert(is_array($state));
74
75 // We are going to need the authId in order to retrieve this authentication source later
77
78 $stateID = SimpleSAML_Auth_State::saveState($state, self::STAGE_INIT);
79
80 $consumer = new sspmod_oauth_Consumer($this->key, $this->secret);
81 // Get the request token
82 $linkback = SimpleSAML\Module::getModuleURL('authtwitter/linkback.php', array('AuthState' => $stateID));
83 $requestToken = $consumer->getRequestToken('https://api.twitter.com/oauth/request_token', array('oauth_callback' => $linkback));
84 SimpleSAML\Logger::debug("Got a request token from the OAuth service provider [" .
85 $requestToken->key . "] with the secret [" . $requestToken->secret . "]");
86
87 $state['authtwitter:authdata:requestToken'] = $requestToken;
89
90 // Authorize the request token
91 $url = 'https://api.twitter.com/oauth/authenticate';
92 if ($this->force_login) {
93 $url = \SimpleSAML\Utils\HTTP::addURLParameters($url, array('force_login' => 'true'));
94 }
95 $consumer->getAuthorizeRequest($url, $requestToken);
96 }
97
98
99 public function finalStep(&$state)
100 {
101 $requestToken = $state['authtwitter:authdata:requestToken'];
102 $parameters = array();
103
104 if (!isset($_REQUEST['oauth_token'])) {
105 throw new SimpleSAML_Error_BadRequest("Missing oauth_token parameter.");
106 }
107 if ($requestToken->key !== (string)$_REQUEST['oauth_token']) {
108 throw new SimpleSAML_Error_BadRequest("Invalid oauth_token parameter.");
109 }
110
111 if (!isset($_REQUEST['oauth_verifier'])) {
112 throw new SimpleSAML_Error_BadRequest("Missing oauth_verifier parameter.");
113 }
114 $parameters['oauth_verifier'] = (string)$_REQUEST['oauth_verifier'];
115
116 $consumer = new sspmod_oauth_Consumer($this->key, $this->secret);
117
118 SimpleSAML\Logger::debug("oauth: Using this request token [" .
119 $requestToken->key . "] with the secret [" . $requestToken->secret . "]");
120
121 // Replace the request token with an access token
122 $accessToken = $consumer->getAccessToken('https://api.twitter.com/oauth/access_token', $requestToken, $parameters);
123 SimpleSAML\Logger::debug("Got an access token from the OAuth service provider [" .
124 $accessToken->key . "] with the secret [" . $accessToken->secret . "]");
125
126 $verify_credentials_url = 'https://api.twitter.com/1.1/account/verify_credentials.json';
127 if ($this->include_email) {
128 $verify_credentials_url = $verify_credentials_url . '?include_email=true';
129 }
130 $userdata = $consumer->getUserInfo($verify_credentials_url, $accessToken);
131
132 if (!isset($userdata['id_str']) || !isset($userdata['screen_name'])) {
133 throw new SimpleSAML_Error_AuthSource($this->authId, 'Authentication error: id_str and screen_name not set.');
134 }
135
136 $attributes = array();
137 foreach ($userdata as $key => $value) {
138 if (is_string($value)) {
139 $attributes['twitter.' . $key] = array((string)$value);
140 }
141 }
142
143 $attributes['twitter_at_screen_name'] = array('@' . $userdata['screen_name']);
144 $attributes['twitter_screen_n_realm'] = array($userdata['screen_name'] . '@twitter.com');
145 $attributes['twitter_targetedID'] = array('http://twitter.com!' . $userdata['id_str']);
146
147 $state['Attributes'] = $attributes;
148 }
149}
if(!array_key_exists('stateid', $_REQUEST)) $state
Handle linkback() response from LinkedIn.
Definition: linkback.php:10
An exception for terminatinating execution or to throw for unit testing.
static debug($string)
Definition: Logger.php:211
static getModuleURL($resource, array $parameters=array())
Get absolute URL to a specified module resource.
Definition: Module.php:220
static saveState(&$state, $stage, $rawId=false)
Save the state.
Definition: State.php:194
static loadFromArray($config, $location='[ARRAY]', $instance=null)
Loads a configuration from the given array.
authenticate(&$state)
Log-in using Twitter platform.
Definition: Twitter.php:71
const AUTHID
The key of the AuthId field in the state.
Definition: Twitter.php:21
__construct($info, $config)
Constructor for this authentication source.
Definition: Twitter.php:49
const STAGE_INIT
The string used to identify our states.
Definition: Twitter.php:16
if(array_key_exists('yes', $_REQUEST)) $attributes
Definition: getconsent.php:85
$config
Definition: bootstrap.php:15
$info
Definition: index.php:5
$url