-
Notifications
You must be signed in to change notification settings - Fork 22
Merge bmilesp fork #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
a9c1d64
934a720
c3476d1
6623c5a
9a1cec5
9812229
f520a4f
836c973
7ce02ec
69a6511
c531811
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,12 +11,15 @@ public function after($event = array()) { | |
public $tokens = array( | ||
'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'key' => 'primary'), | ||
'user_id' => array('type' => 'integer', 'null' => false, 'default' => null, 'key' => 'index'), | ||
'organization_id' => array('type' => 'integer', 'null' => false, 'default' => null, 'key' => 'index'), | ||
'api' => array('type' => 'string', 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'), | ||
'access_token' => array('type' => 'text', 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'), | ||
'modified' => array('type' => 'datetime', 'null' => true, 'default' => null), | ||
'refresh_token' => array('type' => 'text', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'), | ||
'token_secret' => array('type' => 'text', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'), | ||
'expires_in' => array('type' => 'string', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'), | ||
'api_domain' => array('type' => 'string', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'), | ||
'sandbox_domain' => array('type' => 'string', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, except with a little more reason to include. Presumably there is code somewhere that makes this work? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. api_domain is necessary to be able to store multiple domains per api (aka if you have multiple Shopify stores). For the sandbox_domain, this is not necessary for Copula at this point- i thought it would be more of a global attribute but i've only used this for a single api thus far. |
||
'indexes' => array( | ||
'PRIMARY' => array('column' => 'id', 'unique' => 1), | ||
'user_id' => array('column' => array('user_id', 'api'), 'unique' => 1) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,16 @@ protected function _checkAuthFailure(Controller $controller) { | |
function getOauthUri($apiName, $path, $extra = array()) { | ||
if (Configure::check('Copula.' . $apiName . '.Auth')) { | ||
$config = Configure::read('Copula.' . $apiName . '.Auth'); | ||
|
||
if(!empty($config['callback']) && is_array($config['callback'])){ | ||
$config['callback'] = Router::url($config['callback']); | ||
} | ||
|
||
//if a domain is passed into the options, override the default | ||
if(!empty($extra['api_domain'])){ | ||
$config['host'] = $extra['api_domain']; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The callback here is okay. I don't feel like this is the appropriate place to change the host, however. I think it would make more sense to do that sort of switching in e.g. a beforeRequest callback. Possibly the default behavior for that should also include a check to see if the model has a method named beforeRequest() -- which was likely the thinking behind this otherwise pointless bit of code. The idea behind extras was for it to be passed directly to Router::querystring(), which can take additional arguments. I don't entirely trust my memory, but I suspect that using |
||
|
||
if ($config['authMethod'] == 'OAuth') { | ||
return $config['scheme'] . '://' . $config['host'] . '/' . $config[$path]; | ||
} elseif ($config['authMethod'] == 'OAuthV2') { | ||
|
@@ -158,7 +168,7 @@ function getOauthRequestToken($apiName, $requestOptions = array()) { | |
* @param string $apiName | ||
* @throws CakeException | ||
*/ | ||
function callback($apiName) { | ||
function callback($apiName, $api_domain = null) { | ||
$method = $this->getOauthMethod($apiName); | ||
if ($method == 'OAuthV2') { | ||
$code = $this->controller->request->query('code'); | ||
|
@@ -167,9 +177,9 @@ function callback($apiName) { | |
} | ||
$accessToken = $this->getAccessTokenV2($apiName, $code); | ||
if (!empty($accessToken)) { | ||
return $this->_afterRequest($accessToken, $apiName, $method); | ||
return $this->_afterRequest($accessToken, $apiName, $method, $api_domain); | ||
} else { | ||
throw new CakeException(__('Could not get OAuthV2 Access Token from %s', $apiName)); | ||
throw new CakeException(__('Could not get OAuthV2 Access Token from %s or Token already exists', $apiName)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems rather ambiguous. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment about api_domain in schema.php |
||
} elseif ($method == 'OAuth') { | ||
$verifier = $this->controller->request->query('oauth_verifier'); | ||
|
@@ -190,8 +200,8 @@ function callback($apiName) { | |
} | ||
} | ||
|
||
protected function _afterRequest(array $accessToken, $apiName, $version) { | ||
if ($this->store($accessToken, $apiName, $version)) { | ||
protected function _afterRequest(array $accessToken, $apiName, $version, $api_domain = null) { | ||
if ($this->store($accessToken, $apiName, $version, $api_domain)) { | ||
if ($this->Session->check('Oauth.redirect')) { | ||
$redirect = $this->Session->read('Oauth.redirect'); | ||
$this->Session->delete('Oauth.redirect'); | ||
|
@@ -200,7 +210,7 @@ protected function _afterRequest(array $accessToken, $apiName, $version) { | |
return $accessToken; | ||
} | ||
} else { | ||
throw new CakeException(__('Could not store access token for API %s', $apiName)); | ||
throw new CakeException(__('Could not store access token for API %s Possibly already in db', $apiName)); | ||
} | ||
} | ||
|
||
|
@@ -210,11 +220,11 @@ protected function _afterRequest(array $accessToken, $apiName, $version) { | |
* @param string|array $accessToken | ||
* @param string $tokenSecret | ||
*/ | ||
public function store(array $accessToken, $apiName, $version) { | ||
public function store(array $accessToken, $apiName, $version, $api_domain = null) { | ||
$storageMethod = (empty($this->controller->Apis[$apiName]['store'])) ? 'Db' : ucfirst($this->controller->Apis[$apiName]['store']); | ||
$Store = ClassRegistry::init('Copula.TokenStore' . $storageMethod); | ||
if ($Store instanceof TokenStoreInterface) { | ||
return $Store->saveToken($accessToken, $apiName, AuthComponent::user('id'), $version); | ||
return $Store->saveToken($accessToken, $apiName, AuthComponent::user('id'), $version, $api_domain); | ||
} else { | ||
throw new CakeException(__('Storage Method: %s not supported.', $storageMethod)); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,7 +89,7 @@ For the purposes of this document, the hosts configuration file is assumed to be | |
```php | ||
|
||
<?php | ||
$config['Copula']['cloudprint']['Auth'] = array( | ||
$config['Copula']['cloudprint']['path']['Auth'] = array( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One of the two hard problems in computer science. At the risk of entering into a bike shed debate, is there a rationale for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i had to add ['path'] in my paths.php file to get Copula working out of the box. It took a while to realize that the docs were wrong, so i had updated them. |
||
'authMethod' => 'OAuthV2', | ||
'scheme' => 'https', | ||
'authorize' => 'o/oauth2/auth', | ||
|
@@ -99,7 +99,7 @@ For the purposes of this document, the hosts configuration file is assumed to be | |
'callback' => 'https://example.com/oauth2callback/' | ||
); | ||
|
||
$config['Copula']['cloudprint']['Api'] = array( | ||
$config['Copula']['cloudprint']['path']['Api'] = array( | ||
'host' => 'www.google.com/cloudprint', | ||
'authMethod' => 'OAuthV2' | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this seems rather implementation-specific and does not have any other code which refers to it, I would recommend against including this line in the project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the idea behind this was to have multiple users be able to share apis, aka an organization's apis. Probably more implementation specific, like you've said.