Skip to content

Commit 4e6773f

Browse files
committed
initial checkin
1 parent c35d51a commit 4e6773f

14 files changed

+1664
-0
lines changed

Controller/.DS_Store

6 KB
Binary file not shown.

Controller/Component/.DS_Store

6 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
abstract class AbstractAuthMechanism {
4+
5+
public $supportsAPI = false;
6+
7+
abstract public function login($providerName, $callbackURL);
8+
9+
public function loginCallback($providerName, $token, $callbackURL) {
10+
throw new BadMethodCallException('This AuthMechanism does not have a login callback');
11+
}
12+
13+
public function apiRequest($provider, $url, $token = null, $parameters = array(), $method = 'GET', $headers = array()) {
14+
throw new BadMethodCallException('This AuthMechanism has not implemented API Requests');
15+
}
16+
17+
protected function _doGet($url) {
18+
$socket = new HttpSocket();
19+
$result = $socket->get($url);
20+
21+
return $result;
22+
}
23+
24+
protected function _doPost($url, $data) {
25+
$socket = new HttpSocket();
26+
$result = $socket->post($url, $data);
27+
28+
return $result;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
// imported code from https://code.google.com/p/oauth/source/browse/#svn%2Fcode%2Fphp
4+
App::import("Vendor", "ExtAuth.OAuth/OAuth");
5+
6+
require_once('OAuthAuthMechanism.php');
7+
8+
class OAuth1AuthMechanism extends OAuthAuthMechanism {
9+
10+
public function login($provider, $callbackURL) {
11+
$requestToken = $this->getRequestToken($provider, $callbackURL);
12+
13+
if ($requestToken['success']) {
14+
15+
$query = str_replace(
16+
array('{REQUEST_TOKEN_KEY}', '{REQUEST_TOKEN_SECRET}'),
17+
array($requestToken['data']->key, $requestToken['data']->secret),
18+
$provider->authenticateParameters
19+
);
20+
21+
return array(
22+
'success' => true,
23+
'requestToken' => $requestToken['data'],
24+
'redirectURL' => $provider->authenticateURL . '?' . http_build_query($query)
25+
);
26+
} else {
27+
return $requestToken;
28+
}
29+
}
30+
31+
public function getRequestToken($provider, $callbackURL) {
32+
return $this->_getToken(
33+
$provider,
34+
'request',
35+
array(
36+
'oauth_callback' => $callbackURL
37+
)
38+
);
39+
}
40+
41+
public function getAccessToken($provider, $requestToken, $callbackURL = null) {
42+
$parsed_params = OAuthUtil::parse_parameters($_SERVER['QUERY_STRING']);
43+
44+
return $this->_getToken(
45+
$provider,
46+
'access',
47+
array(
48+
'oauth_verifier' => $parsed_params['oauth_verifier']
49+
),
50+
$requestToken
51+
);
52+
}
53+
54+
protected function _getToken($provider, $tokenType, $parameters, $token = null) {
55+
56+
$response = $this->apiRequest(
57+
$provider,
58+
$provider->{$tokenType . 'TokenURL'},
59+
$token,
60+
$provider->{'get' . ucfirst($tokenType) . 'TokenParameters'}($parameters),
61+
$provider->{$tokenType . 'TokenRequestMethod'}
62+
);
63+
64+
if ($response['success']) {
65+
66+
$result = array();
67+
parse_str($response['data'], $result);
68+
69+
if (isset($result['oauth_token']) && isset($result['oauth_token_secret'])) {
70+
return array(
71+
'success' => true,
72+
'data' => new OAuthToken($result['oauth_token'], $result['oauth_token_secret'])
73+
);
74+
} else {
75+
return array(
76+
'success' => false,
77+
'message' => 'Could not parse the response to the token request',
78+
'data' => $result
79+
);
80+
}
81+
} else {
82+
$response['message'] = $tokenType . ' Token ' . $response['message'];
83+
return $response;
84+
}
85+
}
86+
87+
public function apiRequest($provider, $url, $token, $parameters = array(), $method = 'GET', $headers = array()) {
88+
//TODO: headers
89+
90+
$consumer = new OAuthConsumer($provider->key, $provider->secret);
91+
92+
$request = OAuthRequest::from_consumer_and_token(
93+
$consumer,
94+
$token,
95+
$method,
96+
$url,
97+
$parameters
98+
);
99+
100+
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, $token);
101+
102+
switch ($method) {
103+
case 'POST':
104+
$response = $this->_doPost($url, $request->to_postdata());
105+
break;
106+
107+
case 'GET':
108+
default:
109+
$response = $this->_doGet($request->to_url());
110+
}
111+
112+
if ($response->code === "200") {
113+
return array(
114+
'success' => true,
115+
'data' => $response->body
116+
);
117+
} else {
118+
return array(
119+
'success' => false,
120+
'message' => 'API Request HTTP Error: status ' . $response->code,
121+
'data' => $response
122+
);
123+
}
124+
}
125+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
require_once('AbstractAuthMechanism.php');
4+
5+
abstract class OAuthAuthMechanism extends AbstractAuthMechanism {
6+
7+
public function loginCallback($provider, $requestToken, $callbackURL) {
8+
$accessToken = $this->getAccessToken($provider, $requestToken, $callbackURL);
9+
10+
if ($accessToken['success']) {
11+
$response = array(
12+
'success' => true,
13+
'accessToken' => $accessToken['data']
14+
);
15+
16+
if ($provider->profileURL) {
17+
$profile = $this->getNormalizedProfile($provider, $accessToken['data']);
18+
if ($profile['success']) {
19+
$response['profile'] = $profile['data'];
20+
return $response;
21+
} else {
22+
$profile['accessToken'] = $accessToken['data'];
23+
return $profile;
24+
}
25+
} else {
26+
return $response;
27+
}
28+
} else {
29+
return $accessToken;
30+
}
31+
}
32+
33+
public function getNormalizedProfile($provider, $accessToken) {
34+
if ($provider->profileURL) {
35+
$response = $this->apiRequest(
36+
$provider,
37+
$provider->profileURL,
38+
$accessToken,
39+
$provider->profileParameters,
40+
$provider->profileRequestMethod,
41+
$provider->profileRequestHeaders
42+
);
43+
44+
if ($response['success']) {
45+
return $provider->normalizeProfile($response['data']);
46+
} else {
47+
return $response;
48+
}
49+
50+
} else {
51+
return array(
52+
'success' => false,
53+
'message' => 'Profiles are unavailable from this provider'
54+
);
55+
}
56+
}
57+
58+
abstract public function getAccessToken($provider, $requestToken);
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
require_once('OAuthAuthMechanism.php');
4+
5+
class OAuth2AuthMechanism extends OAuthAuthMechanism {
6+
7+
public function login($provider, $callbackURL) {
8+
9+
$query = $provider->AuthDialogParameters;
10+
11+
// resolve substitutions
12+
array_walk($query, function(&$value, $key, $substitutions) {
13+
$value = str_replace(
14+
array_keys($substitutions),
15+
array_values($substitutions),
16+
$value
17+
);
18+
}, array(
19+
'{STATE}' => null,
20+
'{CALLBACK_URL}' => $callbackURL
21+
));
22+
23+
return array(
24+
'success' => true,
25+
'requestToken' => null,
26+
'redirectURL' => $provider->AuthDialogURL . '?' . http_build_query($query)
27+
);
28+
}
29+
30+
public function getAccessToken($provider, $requestToken = null, $callbackURL = null) {
31+
32+
$query = $provider->getAccessTokenParameters();
33+
34+
// resolve substitutions
35+
array_walk($query, function(&$value, $key, $substitutions) {
36+
$value = str_replace(
37+
array_keys($substitutions),
38+
array_values($substitutions),
39+
$value
40+
);
41+
}, array(
42+
'{CALLBACK_URL}' => $callbackURL
43+
));
44+
45+
$response = $this->apiRequest(
46+
null, // provider not required for OAuth2 apiRequest
47+
$provider->accessTokenURL,
48+
null,
49+
$query,
50+
$provider->accessTokenRequestMethod
51+
);
52+
53+
if ($response['success']) {
54+
$frags = json_decode($response['data'], true);
55+
if (!$frags) {
56+
parse_str($response['data'], $frags);
57+
}
58+
59+
if (isset($frags['access_token'])) {
60+
return array(
61+
'success' => true,
62+
'data' => $frags['access_token']
63+
);
64+
} else {
65+
return array(
66+
'success' => false,
67+
'message' => 'Unable to parse Access Token response',
68+
'data' => $response['data']
69+
);
70+
}
71+
} else {
72+
return $response;
73+
}
74+
}
75+
76+
public function apiRequest($provider = null, $url, $accessToken = null, $parameters = array(), $method = 'GET', $headers = array()) {
77+
//TODO: headers
78+
79+
// merge accessToken into parameters, if present
80+
if ($accessToken) {
81+
$parameters['access_token'] = $accessToken;
82+
}
83+
84+
switch ($method) {
85+
case 'POST':
86+
$response = $this->_doPost($url, $parameters);
87+
break;
88+
89+
case 'GET';
90+
default:
91+
$response = $this->_doGet($url . '?' . http_build_query($parameters));
92+
}
93+
94+
if ($response->code === "200") {
95+
return array(
96+
'success' => true,
97+
'data' => $response->body
98+
);
99+
} else {
100+
return array(
101+
'success' => false,
102+
'message' => 'API Request HTTP Error: status ' . $response->code,
103+
'data' => $response
104+
);
105+
}
106+
}
107+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
abstract class AbstractAuthProvider {
4+
5+
public function __construct($mechanism) {
6+
$this->mechanism = $mechanism;
7+
8+
$this->requestTokenRequestMethod = 'GET';
9+
$this->accessTokenRequestMethod = 'POST';
10+
$this->profileRequestMethod = 'GET';
11+
12+
$this->requestTokenParameters = array();
13+
$this->accessTokenParameters = array();
14+
$this->profileParameters = array();
15+
16+
$this->requestTokenRequestHeaders = array();
17+
$this->authenticateParameters = array(
18+
'oauth_token' => '{REQUEST_TOKEN_KEY}'
19+
);
20+
$this->accessTokenRequestHeaders = array();
21+
$this->profileRequestHeaders = array();
22+
23+
}
24+
25+
public function getRequestTokenParameters($params = null) {
26+
if ($params === null) {
27+
$parameters = $this->requestTokenParameters;
28+
} else {
29+
$parameters = array_merge($this->requestTokenParameters, $params);
30+
}
31+
return $parameters;
32+
}
33+
34+
public function getAuthDialogParameters($params = null) {
35+
if ($params === null) {
36+
$parameters = $this->accessTokenParameters;
37+
} else {
38+
$parameters = array_merge($this->accessTokenParameters, $params);
39+
}
40+
return $parameters;
41+
}
42+
43+
public function getAccessTokenParameters($params = null) {
44+
if ($params === null) {
45+
$parameters = $this->accessTokenParameters;
46+
} else {
47+
$parameters = array_merge($this->accessTokenParameters, $params);
48+
}
49+
return $parameters;
50+
}
51+
52+
abstract public function normalizeProfile($raw_profile);
53+
}

0 commit comments

Comments
 (0)