Fix Home Assistant integration auth compatibility.

Accept Authorization Bearer tokens, expose ha_info for discovery without API token, and report api_token_required in haGetInfo.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dadaloop82
2026-06-03 19:49:03 +00:00
parent d33b0ca2fe
commit 0d006625fd
2 changed files with 26 additions and 1 deletions
+19 -1
View File
@@ -61,6 +61,23 @@ if (($_GET['action'] ?? '') === 'app_bootstrap') {
exit; exit;
} }
// ── HA discovery (no token) — lets HACS config flow find the server ───────────
if (($_GET['action'] ?? '') === 'ha_info' && evershelfApiTokenRequired() && !evershelfApiTokenValid()) {
header('Content-Type: application/json; charset=utf-8');
$uniqueId = 'evershelf_' . substr(md5(__DIR__ . php_uname('n')), 0, 12);
echo json_encode([
'name' => 'EverShelf',
'instance' => env('INSTANCE_NAME', php_uname('n')),
'version' => _appVersion(),
'unique_id' => $uniqueId,
'has_token' => true,
'api_token_required' => true,
'api_version' => 1,
'items_count' => null,
], JSON_UNESCAPED_UNICODE);
exit;
}
// ── Google Drive OAuth callback — returns HTML, not JSON ────────────────────── // ── Google Drive OAuth callback — returns HTML, not JSON ──────────────────────
if (($_GET['action'] ?? '') === 'gdrive_oauth_callback') { if (($_GET['action'] ?? '') === 'gdrive_oauth_callback') {
_gdriveHandleOAuthCallback(); _gdriveHandleOAuthCallback();
@@ -1939,7 +1956,8 @@ function haGetInfo(PDO $db): void {
'instance' => env('INSTANCE_NAME', php_uname('n')), 'instance' => env('INSTANCE_NAME', php_uname('n')),
'version' => _appVersion(), 'version' => _appVersion(),
'unique_id' => $uniqueId, 'unique_id' => $uniqueId,
'has_token' => !empty(env('SETTINGS_TOKEN', '')), 'has_token' => evershelfApiTokenRequired(),
'api_token_required' => evershelfApiTokenRequired(),
'api_version' => 1, 'api_version' => 1,
'items_count' => $itemsCount, 'items_count' => $itemsCount,
], JSON_UNESCAPED_UNICODE); ], JSON_UNESCAPED_UNICODE);
+7
View File
@@ -28,6 +28,13 @@ function evershelfGetProvidedApiToken(): string {
if (isset($_GET['api_token'])) { if (isset($_GET['api_token'])) {
return (string)$_GET['api_token']; return (string)$_GET['api_token'];
} }
// Home Assistant ha-evershelf sends Authorization: Bearer (legacy)
$authHeader = $_SERVER['HTTP_AUTHORIZATION']
?? $_SERVER['REDIRECT_HTTP_AUTHORIZATION']
?? '';
if (preg_match('/^Bearer\s+(\S+)/i', $authHeader, $m)) {
return $m[1];
}
return evershelfGetProvidedApiTokenFromHeaders(); return evershelfGetProvidedApiTokenFromHeaders();
} }