Skip to content

Commit fa02745

Browse files
committed
New authentication source: authmyspace
Thanks to Brook Schofield for implementing this. git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@2735 44740490-163a-0410-bde0-09ae8108e29a
1 parent 75ed25c commit fa02745

6 files changed

Lines changed: 254 additions & 0 deletions

File tree

attributemap/myspace2name.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
$attributemap = array(
3+
4+
// See http://developerwiki.myspace.com/index.php?title=People_API for attributes
5+
6+
// Generated MySpace Attributes
7+
'myspace_user' => 'eduPersonPrincipalName', // username OR uid @ myspace.com
8+
'myspace_targetedID' => 'eduPersonTargetedID', // http://myspace.com!uid
9+
'myspace_username' => 'uid', // myspace username (maybe numeric uid)
10+
//'myspace_uid' => 'uid', // numeric myspace user id
11+
12+
// Attributes Returned by MySpace
13+
'myspace.name.givenName' => 'givenName',
14+
'myspace.name.familyName' => 'sn',
15+
'myspace.displayName' => 'displayName',
16+
//'myspace.thumbnailUrl' => 'jpegPhoto', // URL not image data
17+
'myspace.profileUrl' => 'labeledURI',
18+
);

config-templates/authsources.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,28 @@
145145
),
146146
*/
147147

148+
/*
149+
// MySpace OAuth Authentication API.
150+
// Register your application to get an API key here:
151+
// http://developer.myspace.com/
152+
'myspace' => array(
153+
'authmyspace:MySpace',
154+
'key' => 'xxxxxxxxxxxxxxxx',
155+
'secret' => 'xxxxxxxxxxxxxxxx',
156+
),
157+
*/
158+
159+
/*
160+
// Windows Live ID Authentication API.
161+
// Register your application to get an API key here:
162+
// https://manage.dev.live.com
163+
'windowslive' => array(
164+
'authwindowslive:LiveID',
165+
'key' => 'xxxxxxxxxxxxxxxx',
166+
'secret' => 'xxxxxxxxxxxxxxxx',
167+
),
168+
*/
169+
148170
/*
149171
// Example of a LDAP authentication source.
150172
'example-ldap' => array(
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This file indicates that the default state of this module
2+
is disabled. To enable, create a file named enable in the
3+
same directory as this file.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Using the MySpace authentication source with simpleSAMLphp
2+
==========================================================
3+
4+
Remember to configure `authsources.php`, with both your Client ID and Secret key.
5+
6+
To get an API key and a secret, register the application at:
7+
8+
* <http://developer.myspace.com/Modules/Apps/Pages/CreateAppAccount.aspx>
9+
10+
Create a MySpace ID App and set the callback evaluation URL to be:
11+
12+
* `http://sp.example.org/`
13+
14+
Replace `sp.example.org` with your hostname.
15+
16+
## Testing authentication
17+
18+
On the SimpleSAMLphp frontpage, go to the *Authentication* tab, and use the link:
19+
20+
* *Test configured authentication sources*
21+
22+
Then choose the *myspace* authentication source.
23+
24+
Expected behaviour would then be that you are sent to MySpace, and asked to login.
25+
There is no consent screen for attribute release.
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* Handle linkback() response from MySpace.
5+
*/
6+
7+
if (array_key_exists('stateid', $_REQUEST)) {
8+
$stateId = $_REQUEST['stateid'];
9+
} else {
10+
throw new Exception('State Lost - not returned by MySpace Auth');
11+
}
12+
13+
$state = SimpleSAML_Auth_State::loadState($stateId, sspmod_authmyspace_Auth_Source_MySpace::STAGE_INIT);
14+
15+
if (array_key_exists('oauth_problem', $_REQUEST)) {
16+
// oauth_problem of 'user_refused' means user chose not to login with MySpace
17+
if (strcmp($_REQUEST['oauth_problem'],'user_refused') == 0) {
18+
$e = new SimpleSAML_Error_UserAborted('User aborted authentication.');
19+
SimpleSAML_Auth_State::throwException($state, $e);
20+
}
21+
22+
// Error
23+
$e = new SimpleSAML_Error_Error('Authentication failed: ' . $_REQUEST['oauth_problem']);
24+
SimpleSAML_Auth_State::throwException($state, $e);
25+
}
26+
27+
if (array_key_exists('oauth_verifier', $_REQUEST)) {
28+
$state['authmyspace:oauth_verifier'] = $_REQUEST['oauth_verifier'];
29+
} else {
30+
throw new Exception('OAuth verifier not returned.');;
31+
}
32+
33+
/* Find authentication source. */
34+
assert('array_key_exists(sspmod_authmyspace_Auth_Source_MySpace::AUTHID, $state)');
35+
$sourceId = $state[sspmod_authmyspace_Auth_Source_MySpace::AUTHID];
36+
37+
$source = SimpleSAML_Auth_Source::getById($sourceId);
38+
if ($source === NULL) {
39+
throw new Exception('Could not find authentication source with id ' . $sourceId);
40+
}
41+
42+
$source->finalStep($state);
43+
44+
SimpleSAML_Auth_Source::completeAuth($state);
45+

0 commit comments

Comments
 (0)