2012-07-30 14:25:41 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2012, Tom Needham <tom@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2013-02-15 02:29:51 +04:00
|
|
|
require_once '../lib/base.php';
|
2012-07-30 14:25:41 +04:00
|
|
|
// Logic
|
|
|
|
$operation = isset($_GET['operation']) ? $_GET['operation'] : '';
|
2012-09-04 15:10:42 +04:00
|
|
|
$server = OC_OAuth_server::init();
|
|
|
|
|
2012-07-30 14:25:41 +04:00
|
|
|
switch($operation){
|
|
|
|
|
|
|
|
case 'register':
|
2012-09-04 15:10:42 +04:00
|
|
|
|
|
|
|
// Here external apps can register with an ownCloud
|
2013-02-09 19:46:55 +04:00
|
|
|
if(empty($_GET['name']) || empty($_GET['url'])) {
|
2012-09-04 15:10:42 +04:00
|
|
|
// Invalid request
|
|
|
|
echo 401;
|
|
|
|
} else {
|
|
|
|
$callbacksuccess = empty($_GET['callback_success']) ? null : $_GET['callback_success'];
|
|
|
|
$callbackfail = empty($_GET['callback_fail']) ? null : $_GET['callback_fail'];
|
|
|
|
$consumer = OC_OAuth_Server::register_consumer($_GET['name'], $_GET['url'], $callbacksuccess, $callbackfail);
|
|
|
|
|
2013-02-15 02:29:51 +04:00
|
|
|
echo 'Registered consumer successfully! </br></br>Key: ' . $consumer->key
|
|
|
|
. '</br>Secret: ' . $consumer->secret;
|
2012-09-04 15:10:42 +04:00
|
|
|
}
|
2013-02-15 02:29:51 +04:00
|
|
|
break;
|
2012-07-30 14:25:41 +04:00
|
|
|
|
|
|
|
case 'request_token':
|
2012-09-04 15:10:42 +04:00
|
|
|
|
2012-08-03 04:02:31 +04:00
|
|
|
try {
|
|
|
|
$request = OAuthRequest::from_request();
|
2012-09-04 15:10:42 +04:00
|
|
|
$token = $server->get_request_token($request);
|
2012-08-03 04:02:31 +04:00
|
|
|
echo $token;
|
|
|
|
} catch (OAuthException $exception) {
|
2012-08-30 18:01:27 +04:00
|
|
|
OC_Log::write('OC_OAuth_Server', $exception->getMessage(), OC_LOG::ERROR);
|
2012-08-03 04:02:31 +04:00
|
|
|
echo $exception->getMessage();
|
|
|
|
}
|
2012-09-04 15:10:42 +04:00
|
|
|
|
2013-02-15 02:29:51 +04:00
|
|
|
break;
|
2012-07-30 14:25:41 +04:00
|
|
|
case 'authorise';
|
2012-09-04 15:10:42 +04:00
|
|
|
|
2012-08-03 15:36:01 +04:00
|
|
|
OC_API::checkLoggedIn();
|
2012-07-30 14:25:41 +04:00
|
|
|
// Example
|
|
|
|
$consumer = array(
|
|
|
|
'name' => 'Firefox Bookmark Sync',
|
2012-07-30 20:41:07 +04:00
|
|
|
'scopes' => array('ookmarks'),
|
2012-07-30 14:25:41 +04:00
|
|
|
);
|
|
|
|
|
2012-07-30 20:41:07 +04:00
|
|
|
// Check that the scopes are real and installed
|
|
|
|
$apps = OC_App::getEnabledApps();
|
|
|
|
$notfound = array();
|
|
|
|
foreach($consumer['scopes'] as $requiredapp){
|
2012-08-03 15:36:01 +04:00
|
|
|
// App scopes are in this format: app_$appname
|
|
|
|
$requiredapp = end(explode('_', $requiredapp));
|
2013-02-09 19:46:55 +04:00
|
|
|
if(!in_array($requiredapp, $apps)) {
|
2012-07-30 20:41:07 +04:00
|
|
|
$notfound[] = $requiredapp;
|
|
|
|
}
|
|
|
|
}
|
2013-02-09 19:46:55 +04:00
|
|
|
if(!empty($notfound)) {
|
2012-07-30 20:41:07 +04:00
|
|
|
// We need more apps :( Show error
|
2013-02-09 19:46:55 +04:00
|
|
|
if(count($notfound)==1) {
|
2013-02-15 02:29:51 +04:00
|
|
|
$message = 'requires that you have an extra app installed on your ownCloud.'
|
|
|
|
.' Please contact your ownCloud administrator and ask them to install the app below.';
|
2012-07-30 20:41:07 +04:00
|
|
|
} else {
|
2013-02-15 02:29:51 +04:00
|
|
|
$message = 'requires that you have some extra apps installed on your ownCloud.'
|
|
|
|
.' Please contract your ownCloud administrator and ask them to install the apps below.';
|
2012-07-30 20:41:07 +04:00
|
|
|
}
|
|
|
|
$t = new OC_Template('settings', 'oauth-required-apps', 'guest');
|
|
|
|
OC_Util::addStyle('settings', 'oauth');
|
|
|
|
$t->assign('requiredapps', $notfound);
|
|
|
|
$t->assign('consumer', $consumer);
|
|
|
|
$t->assign('message', $message);
|
|
|
|
$t->printPage();
|
|
|
|
} else {
|
|
|
|
$t = new OC_Template('settings', 'oauth', 'guest');
|
|
|
|
OC_Util::addStyle('settings', 'oauth');
|
|
|
|
$t->assign('consumer', $consumer);
|
|
|
|
$t->printPage();
|
|
|
|
}
|
2013-02-15 02:29:51 +04:00
|
|
|
break;
|
2012-07-30 14:25:41 +04:00
|
|
|
|
|
|
|
case 'access_token';
|
2012-08-03 04:02:31 +04:00
|
|
|
try {
|
|
|
|
$request = OAuthRequest::from_request();
|
|
|
|
$token = $server->fetch_access_token($request);
|
|
|
|
echo $token;
|
|
|
|
} catch (OAuthException $exception) {
|
2012-08-30 18:01:27 +04:00
|
|
|
OC_Log::write('OC_OAuth_Server', $exception->getMessage(), OC_LOG::ERROR);
|
2012-08-03 04:02:31 +04:00
|
|
|
echo $exception->getMessage();
|
|
|
|
}
|
2012-09-04 15:10:42 +04:00
|
|
|
|
2013-02-15 02:29:51 +04:00
|
|
|
break;
|
2012-07-30 14:25:41 +04:00
|
|
|
default:
|
2012-08-03 19:20:01 +04:00
|
|
|
// Something went wrong, we need an operation!
|
|
|
|
OC_Response::setStatus(400);
|
2013-02-15 02:29:51 +04:00
|
|
|
break;
|
2012-07-30 14:25:41 +04:00
|
|
|
|
|
|
|
}
|