streams/include/api_auth.php

171 lines
5.5 KiB
PHP
Raw Normal View History

2019-05-15 05:16:49 +00:00
<?php
use OAuth2\Request;
2022-02-16 04:08:28 +00:00
use Code\Identity\OAuth2Storage;
use Code\Identity\OAuth2Server;
use Code\Lib\Libzot;
use Code\Lib\System;
use Code\Web\HTTPSig;
use Code\Lib\Channel;
use Code\Extend\Hook;
2019-06-13 03:57:28 +00:00
require_once('include/auth.php');
require_once('include/security.php');
/**
2022-03-15 09:02:44 +00:00
* API Login via basic-auth, OpenWebAuth, or OAuth2
2022-12-30 09:13:33 +00:00
* This function returns true or exits with a 401 and WWW-Authenticate header.
* @noinspection PhpInconsistentReturnPointsInspection
*/
2021-12-03 03:01:39 +00:00
function api_login()
{
$record = null;
if (isset($_SERVER['HTTP_SIGNATURE']) &&
!isset($_SERVER['REDIRECT_REMOTE_USER']) &&
!isset($_SERVER['HTTP_AUTHORIZATION'])) {
$_SERVER['HTTP_AUTHORIZATION'] = 'Signature ' . $_SERVER['HTTP_SIGNATURE'];
}
2021-12-03 03:01:39 +00:00
if (array_key_exists('REDIRECT_REMOTE_USER', $_SERVER) && (! array_key_exists('HTTP_AUTHORIZATION', $_SERVER))) {
$_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['REDIRECT_REMOTE_USER'];
}
// login with oauth
try {
// OAuth 2.0
$storage = new OAuth2Storage(DBA::$dba->db);
$server = new OAuth2Server($storage);
2022-12-29 08:07:40 +00:00
2021-12-03 03:01:39 +00:00
$request = Request::createFromGlobals();
if ($server->verifyResourceRequest($request)) {
$token = $server->getAccessTokenData($request);
$uid = $token['user_id'];
$r = q(
"SELECT * FROM channel WHERE channel_id = %d LIMIT 1",
intval($uid)
);
if ($r) {
$record = $r[0];
} else {
header('HTTP/1.0 401 Unauthorized');
echo('This api requires login');
killme();
}
$_SESSION['uid'] = $record['channel_id'];
$_SESSION['addr'] = $_SERVER['REMOTE_ADDR'];
$x = q(
"select * from account where account_id = %d LIMIT 1",
intval($record['channel_account_id'])
);
if ($x) {
2022-03-15 08:42:34 +00:00
authenticate_success($x[0], false, true, false, true, true);
2021-12-03 03:01:39 +00:00
$_SESSION['allow_api'] = true;
Hook::call('logged_in', App::$user);
2022-12-30 09:13:33 +00:00
return true;
2021-12-03 03:01:39 +00:00
}
}
2022-02-14 19:55:01 +00:00
2021-12-03 03:01:39 +00:00
} catch (Exception $e) {
2022-12-30 09:13:33 +00:00
// Just log the exception. Most of the time it will be because
// a different identity mechanism is being used and no oauth2 parameters were found.
2021-12-03 03:01:39 +00:00
logger($e->getMessage());
}
if (array_key_exists('HTTP_AUTHORIZATION', $_SERVER)) {
/* Basic authentication */
2022-10-21 09:37:20 +00:00
if (str_starts_with(trim($_SERVER['HTTP_AUTHORIZATION']), 'Basic')) {
2021-12-03 03:01:39 +00:00
// ignore base64 decoding errors caused by tricksters
$userpass = @base64_decode(substr(trim($_SERVER['HTTP_AUTHORIZATION']), 6)) ;
if (strlen($userpass)) {
list($name, $password) = explode(':', $userpass);
$_SERVER['PHP_AUTH_USER'] = $name;
$_SERVER['PHP_AUTH_PW'] = $password;
}
}
/* OpenWebAuth */
2022-10-21 09:37:20 +00:00
if (str_starts_with(trim($_SERVER['HTTP_AUTHORIZATION']), 'Signature')) {
2021-12-03 03:01:39 +00:00
$record = null;
$sigblock = HTTPSig::parse_sigheader($_SERVER['HTTP_AUTHORIZATION']);
if ($sigblock) {
$keyId = str_replace('acct:', '', $sigblock['keyId']);
if ($keyId) {
2022-06-17 02:46:54 +00:00
$r = hubloc_id_addr_query($keyId);
2021-12-03 03:01:39 +00:00
if (! $r) {
HTTPSig::get_zotfinger_key($keyId);
2022-06-17 02:46:54 +00:00
$r = hubloc_id_addr_query($keyId);
2021-12-03 03:01:39 +00:00
}
if ($r) {
$r = Libzot::zot_record_preferred($r);
2022-01-25 01:26:12 +00:00
$c = Channel::from_hash($r['hubloc_hash']);
2021-12-03 03:01:39 +00:00
if ($c) {
$a = q(
"select * from account where account_id = %d limit 1",
intval($c['channel_account_id'])
);
if ($a) {
$record = [ 'channel' => $c, 'account' => $a[0] ];
$channel_login = $c['channel_id'];
}
}
}
if ($record) {
$verified = HTTPSig::verify(EMPTY_STR, $record['channel']['channel_pubkey']);
if (! ($verified && $verified['header_signed'] && $verified['header_valid'])) {
$record = null;
}
}
}
}
}
}
// process normal login request
if (isset($_SERVER['PHP_AUTH_USER']) && (! $record)) {
$channel_login = 0;
$record = account_verify_password($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
if ($record && $record['channel']) {
$channel_login = $record['channel']['channel_id'];
}
}
if (isset($record['account'])) {
authenticate_success($record['account']);
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();
}
2022-12-30 09:13:33 +00:00
}
2015-12-09 04:47:55 +00:00
2021-12-03 03:01:39 +00:00
function retry_basic_auth($method = 'Basic')
{
2022-03-15 09:02:44 +00:00
header('WWW-Authenticate: ' . $method . ' realm="' . System::get_project_name() . '"');
2021-12-03 03:01:39 +00:00
header('HTTP/1.0 401 Unauthorized');
2022-03-15 09:02:44 +00:00
echo( t('This API method requires authentication.'));
2021-12-03 03:01:39 +00:00
killme();
2021-03-09 05:52:10 +00:00
}