|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once(dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/oauth/libextinc/OAuth.php'); |
| 4 | + |
| 5 | +/** |
| 6 | + * Authenticate using MySpace. |
| 7 | + * |
| 8 | + * @author Brook Schofield, TERENA. |
| 9 | + * @package simpleSAMLphp |
| 10 | + * @version $Id$ |
| 11 | + */ |
| 12 | +class sspmod_authmyspace_Auth_Source_MySpace extends SimpleSAML_Auth_Source { |
| 13 | + |
| 14 | + /** |
| 15 | + * The string used to identify our states. |
| 16 | + */ |
| 17 | + const STAGE_INIT = 'authmyspace:init'; |
| 18 | + |
| 19 | + /** |
| 20 | + * The key of the AuthId field in the state. |
| 21 | + */ |
| 22 | + const AUTHID = 'authmyspace:AuthId'; |
| 23 | + |
| 24 | + private $key; |
| 25 | + private $secret; |
| 26 | + |
| 27 | + |
| 28 | + /** |
| 29 | + * Constructor for this authentication source. |
| 30 | + * |
| 31 | + * @param array $info Information about this authentication source. |
| 32 | + * @param array $config Configuration. |
| 33 | + */ |
| 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 | + if (!array_key_exists('key', $config)) |
| 42 | + throw new Exception('MySpace authentication source is not properly configured: missing [key]'); |
| 43 | + |
| 44 | + $this->key = $config['key']; |
| 45 | + |
| 46 | + if (!array_key_exists('secret', $config)) |
| 47 | + throw new Exception('MySpace authentication source is not properly configured: missing [secret]'); |
| 48 | + |
| 49 | + $this->secret = $config['secret']; |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + /** |
| 54 | + * Log-in using MySpace platform |
| 55 | + * |
| 56 | + * @param array &$state Information about the current authentication. |
| 57 | + */ |
| 58 | + public function authenticate(&$state) { |
| 59 | + assert('is_array($state)'); |
| 60 | + |
| 61 | + /* We are going to need the authId in order to retrieve this authentication source later. */ |
| 62 | + $state[self::AUTHID] = $this->authId; |
| 63 | + |
| 64 | + $consumer = new sspmod_oauth_Consumer($this->key, $this->secret); |
| 65 | + |
| 66 | + // Get the request token |
| 67 | + $requestToken = $consumer->getRequestToken('http://api.myspace.com/request_token'); |
| 68 | + SimpleSAML_Logger::debug("Got a request token from the OAuth service provider [" . |
| 69 | + $requestToken->key . "] with the secret [" . $requestToken->secret . "]"); |
| 70 | + |
| 71 | + $state['authmyspace:requestToken'] = $requestToken; |
| 72 | + |
| 73 | + $stateID = SimpleSAML_Auth_State::saveState($state, self::STAGE_INIT); |
| 74 | + SimpleSAML_Logger::debug('authmyspace auth state id = ' . $stateID); |
| 75 | + |
| 76 | + // Authorize the request token |
| 77 | + $consumer->getAuthorizeRequest('http://api.myspace.com/authorize', $requestToken, TRUE, SimpleSAML_Module::getModuleUrl('authmyspace') . '/linkback.php?stateid=' . $stateID); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + public function finalStep(&$state) { |
| 84 | + |
| 85 | + $requestToken = $state['authmyspace:requestToken']; |
| 86 | + |
| 87 | + $consumer = new sspmod_oauth_Consumer($this->key, $this->secret); |
| 88 | + |
| 89 | + SimpleSAML_Logger::debug("oauth: Using this request token [" . |
| 90 | + $requestToken->key . "] with the secret [" . $requestToken->secret . "]"); |
| 91 | + |
| 92 | + // Replace the request token with an access token |
| 93 | + $accessToken = $consumer->getAccessToken('http://api.myspace.com/access_token', $requestToken); |
| 94 | + SimpleSAML_Logger::debug("Got an access token from the OAuth service provider [" . |
| 95 | + $accessToken->key . "] with the secret [" . $accessToken->secret . "]"); |
| 96 | + |
| 97 | + // API depricated on 20th September 2010 |
| 98 | + //$userdata = $consumer->getUserInfo('http://api.myspace.com/v1/user.json', $accessToken); |
| 99 | + |
| 100 | + // People API - http://developerwiki.myspace.com/index.php?title=People_API |
| 101 | + $userdata = $consumer->getUserInfo('http://api.myspace.com/1.0/people/@me/@self?fields=@all', $accessToken); |
| 102 | + |
| 103 | + $attributes = array(); |
| 104 | + |
| 105 | + if (is_array($userdata['person'])) { |
| 106 | + foreach($userdata['person'] AS $key => $value) { |
| 107 | + if (is_string($value) || is_int($value)) |
| 108 | + $attributes['myspace.' . $key] = array((string)$value); |
| 109 | + |
| 110 | + if (is_array($value)) { |
| 111 | + foreach($value AS $key2 => $value2) { |
| 112 | + if (is_string($value2) || is_int($value2)) |
| 113 | + $attributes['myspace.' . $key . '.' . $key2] = array((string)$value2); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if (array_key_exists('id', $userdata['person']) ) { |
| 119 | + |
| 120 | + // person-id in the format of myspace.com.person.1234567890 |
| 121 | + if (preg_match('/(\d+)$/',$userdata['person']['id'],$matches)) { |
| 122 | + $attributes['myspace_targetedID'] = array('http://myspace.com!' . $matches[1]); |
| 123 | + $attributes['myspace_uid'] = array($matches[1]); |
| 124 | + $attributes['myspace_user'] = array($matches[1] . '@myspace.com'); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // profileUrl in the format http://www.myspace.com/username |
| 129 | + if (array_key_exists('profileUrl', $userdata['person']) ) { |
| 130 | + if (preg_match('@/([^/]+)$@',$userdata['person']['profileUrl'],$matches)) { |
| 131 | + $attributes['myspace_username'] = array($matches[1]); |
| 132 | + $attributes['myspace_user'] = array($matches[1] . '@myspace.com'); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + SimpleSAML_Logger::debug('MySpace Returned Attributes: '. implode(", ",array_keys($attributes))); |
| 138 | + |
| 139 | + $state['Attributes'] = $attributes; |
| 140 | + } |
| 141 | +} |
0 commit comments