Added "source" detection

This commit is contained in:
Michael 2021-06-08 17:48:41 +00:00
parent 0f0a301280
commit 8bf5dd187b

View File

@ -77,10 +77,27 @@ class BasicAuth
return self::$current_token; return self::$current_token;
} }
$source = $_REQUEST['source'] ?? '';
// Support for known clients that doesn't send a source name
if (empty($source) && !empty($_SERVER['HTTP_USER_AGENT'])) {
if(strpos($_SERVER['HTTP_USER_AGENT'], "Twidere") !== false) {
$source = 'Twidere';
}
Logger::info('Unrecognized user-agent', ['http_user_agent' => $_SERVER['HTTP_USER_AGENT']]);
} else {
Logger::info('Empty user-agent');
}
if (empty($source)) {
$source = 'api';
}
self::$current_token = [ self::$current_token = [
'uid' => self::$current_user_id, 'uid' => self::$current_user_id,
'id' => 0, 'id' => 0,
'name' => api_source(), 'name' => $source,
'website' => '', 'website' => '',
'created_at' => DBA::NULL_DATETIME, 'created_at' => DBA::NULL_DATETIME,
'read' => true, 'read' => true,
@ -114,7 +131,7 @@ class BasicAuth
} }
} }
$user = $_SERVER['PHP_AUTH_USER'] ?? ''; $user = $_SERVER['PHP_AUTH_USER'] ?? '';
$password = $_SERVER['PHP_AUTH_PW'] ?? ''; $password = $_SERVER['PHP_AUTH_PW'] ?? '';
// allow "user@server" login (but ignore 'server' part) // allow "user@server" login (but ignore 'server' part)
@ -155,7 +172,7 @@ class BasicAuth
if (!$do_login) { if (!$do_login) {
return 0; return 0;
} }
Logger::debug('failed', ['module' => 'api', 'action' => 'login', 'parameters' => $_SERVER]); Logger::debug('Access denied', ['parameters' => $_SERVER]);
header('WWW-Authenticate: Basic realm="Friendica"'); header('WWW-Authenticate: Basic realm="Friendica"');
throw new UnauthorizedException("This API requires login"); throw new UnauthorizedException("This API requires login");
} }