|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SimpleSAML\Module\authtwitter\Auth\Source; |
| 4 | + |
| 5 | +use Webmozart\Assert\Assert; |
| 6 | + |
| 7 | +$default = dirname(dirname(dirname(dirname(dirname(__FILE__))))).'/oauth/libextinc/OAuth.php'; |
| 8 | +$travis = dirname(dirname(dirname(dirname(__FILE__)))).'/vendor/simplesamlphp/simplesamlphp/modules/oauth/libextinc/OAuth.php'; |
| 9 | + |
| 10 | +if (file_exists($default)) { |
| 11 | + require_once($default); |
| 12 | +} else if (file_exists($travis)) { |
| 13 | + require_once($travis); |
| 14 | +} else { |
| 15 | + // Probably codecov, but we can't raise an exception here or Travis will fail |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Authenticate using Twitter. |
| 20 | + * |
| 21 | + * @author Andreas Åkre Solberg, UNINETT AS. |
| 22 | + * @package SimpleSAMLphp |
| 23 | + */ |
| 24 | + |
| 25 | +class Twitter extends \SimpleSAML\Auth\Source |
| 26 | +{ |
| 27 | + /** |
| 28 | + * The string used to identify our states. |
| 29 | + */ |
| 30 | + const STAGE_INIT = 'twitter:init'; |
| 31 | + |
| 32 | + /** |
| 33 | + * The key of the AuthId field in the state. |
| 34 | + */ |
| 35 | + const AUTHID = 'twitter:AuthId'; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var string |
| 39 | + */ |
| 40 | + private $key; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var string |
| 44 | + */ |
| 45 | + private $secret; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var bool |
| 49 | + */ |
| 50 | + private $force_login; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var bool |
| 54 | + */ |
| 55 | + private $include_email; |
| 56 | + |
| 57 | + /** |
| 58 | + * Constructor for this authentication source. |
| 59 | + * |
| 60 | + * @param array $info Information about this authentication source. |
| 61 | + * @param array $config Configuration. |
| 62 | + */ |
| 63 | + public function __construct($info, $config) |
| 64 | + { |
| 65 | + Assert::isArray($info); |
| 66 | + Assert::isArray($config); |
| 67 | + |
| 68 | + // Call the parent constructor first, as required by the interface |
| 69 | + parent::__construct($info, $config); |
| 70 | + |
| 71 | + $configObject = \SimpleSAML\Configuration::loadFromArray( |
| 72 | + $config, |
| 73 | + 'authsources['.var_export($this->authId, true).']' |
| 74 | + ); |
| 75 | + |
| 76 | + $this->key = $configObject->getString('key'); |
| 77 | + $this->secret = $configObject->getString('secret'); |
| 78 | + $this->force_login = $configObject->getBoolean('force_login', false); |
| 79 | + $this->include_email = $configObject->getBoolean('include_email', false); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Log-in using Twitter platform |
| 84 | + * |
| 85 | + * @param array &$state Information about the current authentication. |
| 86 | + * @return void |
| 87 | + */ |
| 88 | + public function authenticate(&$state) |
| 89 | + { |
| 90 | + Assert::isArray($state); |
| 91 | + |
| 92 | + // We are going to need the authId in order to retrieve this authentication source later |
| 93 | + $state[self::AUTHID] = $this->authId; |
| 94 | + |
| 95 | + $stateID = \SimpleSAML\Auth\State::saveState($state, self::STAGE_INIT); |
| 96 | + |
| 97 | + $consumer = new \SimpleSAML\Module\oauth\Consumer($this->key, $this->secret); |
| 98 | + // Get the request token |
| 99 | + $linkback = \SimpleSAML\Module::getModuleURL('authtwitter/linkback.php', ['AuthState' => $stateID]); |
| 100 | + $requestToken = $consumer->getRequestToken( |
| 101 | + 'https://api.twitter.com/oauth/request_token', |
| 102 | + ['oauth_callback' => $linkback] |
| 103 | + ); |
| 104 | + \SimpleSAML\Logger::debug("Got a request token from the OAuth service provider [". |
| 105 | + $requestToken->key."] with the secret [".$requestToken->secret."]"); |
| 106 | + |
| 107 | + $state['authtwitter:authdata:requestToken'] = $requestToken; |
| 108 | + \SimpleSAML\Auth\State::saveState($state, self::STAGE_INIT); |
| 109 | + |
| 110 | + // Authorize the request token |
| 111 | + $url = 'https://api.twitter.com/oauth/authenticate'; |
| 112 | + if ($this->force_login) { |
| 113 | + $url = \SimpleSAML\Utils\HTTP::addURLParameters($url, ['force_login' => 'true']); |
| 114 | + } |
| 115 | + $consumer->getAuthorizeRequest($url, $requestToken); |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + /** |
| 120 | + * @param array &$state |
| 121 | + * @return void |
| 122 | + */ |
| 123 | + public function finalStep(&$state) |
| 124 | + { |
| 125 | + $requestToken = $state['authtwitter:authdata:requestToken']; |
| 126 | + $parameters = []; |
| 127 | + |
| 128 | + if (!isset($_REQUEST['oauth_token'])) { |
| 129 | + throw new \SimpleSAML\Error\BadRequest("Missing oauth_token parameter."); |
| 130 | + } |
| 131 | + if ($requestToken->key !== (string) $_REQUEST['oauth_token']) { |
| 132 | + throw new \SimpleSAML\Error\BadRequest("Invalid oauth_token parameter."); |
| 133 | + } |
| 134 | + |
| 135 | + if (!isset($_REQUEST['oauth_verifier'])) { |
| 136 | + throw new \SimpleSAML\Error\BadRequest("Missing oauth_verifier parameter."); |
| 137 | + } |
| 138 | + $parameters['oauth_verifier'] = (string) $_REQUEST['oauth_verifier']; |
| 139 | + |
| 140 | + $consumer = new \SimpleSAML\Module\oauth\Consumer($this->key, $this->secret); |
| 141 | + |
| 142 | + \SimpleSAML\Logger::debug("oauth: Using this request token [". |
| 143 | + $requestToken->key."] with the secret [".$requestToken->secret."]"); |
| 144 | + |
| 145 | + // Replace the request token with an access token |
| 146 | + $accessToken = $consumer->getAccessToken( |
| 147 | + 'https://api.twitter.com/oauth/access_token', |
| 148 | + $requestToken, |
| 149 | + $parameters |
| 150 | + ); |
| 151 | + \SimpleSAML\Logger::debug("Got an access token from the OAuth service provider [". |
| 152 | + $accessToken->key."] with the secret [".$accessToken->secret."]"); |
| 153 | + |
| 154 | + $verify_credentials_url = 'https://api.twitter.com/1.1/account/verify_credentials.json'; |
| 155 | + if ($this->include_email) { |
| 156 | + $verify_credentials_url = $verify_credentials_url.'?include_email=true'; |
| 157 | + } |
| 158 | + $userdata = $consumer->getUserInfo($verify_credentials_url, $accessToken); |
| 159 | + |
| 160 | + if (!isset($userdata['id_str']) || !isset($userdata['screen_name'])) { |
| 161 | + throw new \SimpleSAML\Error\AuthSource( |
| 162 | + $this->authId, |
| 163 | + 'Authentication error: id_str and screen_name not set.' |
| 164 | + ); |
| 165 | + } |
| 166 | + |
| 167 | + $attributes = []; |
| 168 | + foreach ($userdata as $key => $value) { |
| 169 | + if (is_string($value)) { |
| 170 | + $attributes['twitter.'.$key] = [(string) $value]; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + $attributes['twitter_at_screen_name'] = ['@'.$userdata['screen_name']]; |
| 175 | + $attributes['twitter_screen_n_realm'] = [$userdata['screen_name'].'@twitter.com']; |
| 176 | + $attributes['twitter_targetedID'] = ['http://twitter.com!'.$userdata['id_str']]; |
| 177 | + |
| 178 | + $state['Attributes'] = $attributes; |
| 179 | + } |
| 180 | +} |
0 commit comments