2015-11-15 19:51:39 +01:00
|
|
|
<?php /** @file */
|
|
|
|
|
|
|
|
/**
|
2016-01-12 02:47:38 +01:00
|
|
|
* API Login via basic-auth or OAuth
|
2015-11-15 19:51:39 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
function api_login(&$a){
|
2016-01-12 02:47:38 +01:00
|
|
|
|
|
|
|
$record = null;
|
|
|
|
|
|
|
|
require_once('include/oauth.php');
|
|
|
|
|
2015-11-15 19:51:39 +01:00
|
|
|
// login with oauth
|
|
|
|
try {
|
2016-01-12 02:47:38 +01:00
|
|
|
$oauth = new ZotOAuth1();
|
|
|
|
$req = OAuth1Request::from_request();
|
2015-11-15 19:51:39 +01:00
|
|
|
|
|
|
|
list($consumer,$token) = $oauth->verify_request($req);
|
|
|
|
|
|
|
|
if (!is_null($token)){
|
|
|
|
$oauth->loginUser($token->uid);
|
|
|
|
|
2016-04-17 16:29:18 +02:00
|
|
|
App::set_oauth_key($consumer->key);
|
2015-11-15 19:51:39 +01:00
|
|
|
|
2016-04-17 16:29:18 +02:00
|
|
|
call_hooks('logged_in', App::$user);
|
2015-11-15 19:51:39 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-01-12 02:47:38 +01:00
|
|
|
killme();
|
2015-11-15 19:51:39 +01:00
|
|
|
}
|
|
|
|
catch(Exception $e) {
|
2016-01-12 02:47:38 +01:00
|
|
|
logger($e->getMessage());
|
2015-11-15 19:51:39 +01:00
|
|
|
}
|
|
|
|
|
2016-01-12 02:47:38 +01:00
|
|
|
// workarounds for HTTP-auth in CGI mode
|
|
|
|
|
2015-11-15 19:51:39 +01:00
|
|
|
if(x($_SERVER,'REDIRECT_REMOTE_USER')) {
|
|
|
|
$userpass = base64_decode(substr($_SERVER["REDIRECT_REMOTE_USER"],6)) ;
|
|
|
|
if(strlen($userpass)) {
|
|
|
|
list($name, $password) = explode(':', $userpass);
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = $name;
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = $password;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(x($_SERVER,'HTTP_AUTHORIZATION')) {
|
|
|
|
$userpass = base64_decode(substr($_SERVER["HTTP_AUTHORIZATION"],6)) ;
|
|
|
|
if(strlen($userpass)) {
|
|
|
|
list($name, $password) = explode(':', $userpass);
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = $name;
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = $password;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-12 02:47:38 +01:00
|
|
|
require_once('include/auth.php');
|
|
|
|
require_once('include/security.php');
|
2015-11-15 19:51:39 +01:00
|
|
|
|
|
|
|
// process normal login request
|
2016-01-12 02:47:38 +01:00
|
|
|
|
|
|
|
if(isset($_SERVER['PHP_AUTH_USER'])) {
|
|
|
|
$channel_login = 0;
|
|
|
|
$record = account_verify_password($_SERVER['PHP_AUTH_USER'],$_SERVER['PHP_AUTH_PW']);
|
2017-02-05 16:11:01 +01:00
|
|
|
if($record && $record['channel']) {
|
|
|
|
$channel_login = $record['channel']['channel_id'];
|
2015-11-15 19:51:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-05 16:11:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
if($record['account']) {
|
|
|
|
authenticate_success($record['account']);
|
2015-11-15 19:51:39 +01:00
|
|
|
|
2016-01-12 02:47:38 +01:00
|
|
|
if($channel_login)
|
|
|
|
change_channel($channel_login);
|
|
|
|
|
|
|
|
$_SESSION['allow_api'] = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = '*****';
|
|
|
|
logger('API_login failure: ' . print_r($_SERVER,true), LOGGER_DEBUG);
|
|
|
|
log_failed_login('API login failure');
|
|
|
|
retry_basic_auth();
|
|
|
|
}
|
2015-11-15 19:51:39 +01:00
|
|
|
|
|
|
|
}
|
2016-01-12 02:47:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
function retry_basic_auth() {
|
|
|
|
header('WWW-Authenticate: Basic realm="Hubzilla"');
|
|
|
|
header('HTTP/1.0 401 Unauthorized');
|
|
|
|
echo('This api requires login');
|
|
|
|
killme();
|
|
|
|
}
|