diff --git a/README.md b/README.md index c0a170a..137fb39 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Home Assistant Version Control provides complete version history for your setup. v1.7 adds API authentication for standalone Docker and cuts idle CPU. -* **API Authentication for Docker:** The standalone container now asks for HTTP Basic auth on `/api/*`. It is on by default there. The username is `havc`, and the password is generated on first start and saved to `/data/api-password`. Set `HAVC_USERNAME` and `HAVC_PASSWORD` to choose your own, or `HAVC_API_AUTH=off` behind an authenticating reverse proxy. The add-on behind Ingress is unchanged. +* **API Authentication for Docker:** The standalone container now asks for a username and password in the app. It is on by default there. The username is `havc`, and the password is generated on first start and saved to `/data/api-password`. Set `HAVC_USERNAME` and `HAVC_PASSWORD` to choose your own, or `HAVC_API_AUTH=off` behind an authenticating reverse proxy. The add-on behind Ingress is unchanged. * **Lower Idle CPU:** The watcher now ignores files HAVC can never commit, so polling no longer stats dependency folders, model caches, and other large directories. * **Faster Startup Recovery:** A stale `.git/index.lock` is cleared after ten minutes, and startup and watcher retries now share one initialization run instead of racing for the git lock. @@ -161,7 +161,7 @@ There are two ways to install Home Assistant Version Control: as a Home Assistan ### 2. Standalone Docker Installation > [!WARNING] -> The standalone container requires API authentication by default. Requests to `/api/*` need HTTP Basic auth, while static files and the `/` healthcheck stay open. If you don't set `HAVC_PASSWORD`, a password is generated on first start, printed in the container logs as `[auth] API password: ...`, and saved to `/data/api-password` inside the container. Set `HAVC_USERNAME` and `HAVC_PASSWORD` to use your own credentials. Set `HAVC_API_AUTH=off` only when an authenticating reverse proxy already sits in front of the container. For internet exposure, keep an authenticating reverse proxy in front and leave the port closed on untrusted networks. +> The standalone container requires API authentication by default. The app asks for a username and password when API auth is enabled. The login form works in the Home Assistant Companion app as well as a normal browser. The `/healthz` endpoint stays open for the container healthcheck. If you don't set `HAVC_PASSWORD`, a password is generated on first start, printed in the container logs as `[auth] API password: ...`, and saved to `/data/api-password` inside the container. Set `HAVC_USERNAME` and `HAVC_PASSWORD` to use your own credentials. Set `HAVC_API_AUTH=off` only when an authenticating reverse proxy already sits in front of the container. For internet exposure, keep an authenticating reverse proxy in front and leave the port closed on untrusted networks.
Click to expand Docker deployment options diff --git a/homeassistant-version-control/CHANGELOG.md b/homeassistant-version-control/CHANGELOG.md index 6960739..2455997 100644 --- a/homeassistant-version-control/CHANGELOG.md +++ b/homeassistant-version-control/CHANGELOG.md @@ -2,6 +2,11 @@ ## [Unreleased] +## [1.7.1] + +### Fixed & Improved +- **In-App Login for API Auth:** When API auth is enabled, the app now shows its own username and password form instead of relying on the browser's HTTP Basic dialog. This works in the Home Assistant Companion app and other embedded webviews, where the native dialog is unavailable. The container healthcheck moved to the unauthenticated `/healthz` endpoint. + ## [1.7.0] ### Security Hardening diff --git a/homeassistant-version-control/README.md b/homeassistant-version-control/README.md index 54a01ad..7bd68e9 100644 --- a/homeassistant-version-control/README.md +++ b/homeassistant-version-control/README.md @@ -18,7 +18,7 @@ Home Assistant Version Control provides complete version history for your setup. v1.7 adds API authentication for standalone Docker and cuts idle CPU. -* **API Authentication for Docker:** The standalone container now asks for HTTP Basic auth on `/api/*`. It is on by default there. The username is `havc`, and the password is generated on first start and saved to `/data/api-password`. Set `HAVC_USERNAME` and `HAVC_PASSWORD` to choose your own, or `HAVC_API_AUTH=off` behind an authenticating reverse proxy. The add-on behind Ingress is unchanged. +* **API Authentication for Docker:** The standalone container now asks for a username and password in the app. It is on by default there. The username is `havc`, and the password is generated on first start and saved to `/data/api-password`. Set `HAVC_USERNAME` and `HAVC_PASSWORD` to choose your own, or `HAVC_API_AUTH=off` behind an authenticating reverse proxy. The add-on behind Ingress is unchanged. * **Lower Idle CPU:** The watcher now ignores files HAVC can never commit, so polling no longer stats dependency folders, model caches, and other large directories. * **Faster Startup Recovery:** A stale `.git/index.lock` is cleared after ten minutes, and startup and watcher retries now share one initialization run instead of racing for the git lock. @@ -162,7 +162,7 @@ There are two ways to install Home Assistant Version Control: as a Home Assistan ### 2. Standalone Docker Installation > [!WARNING] -> The standalone container requires API authentication by default. Requests to `/api/*` need HTTP Basic auth, while static files and the `/` healthcheck stay open. If you don't set `HAVC_PASSWORD`, a password is generated on first start, printed in the container logs as `[auth] API password: ...`, and saved to `/data/api-password` inside the container. Set `HAVC_USERNAME` and `HAVC_PASSWORD` to use your own credentials. Set `HAVC_API_AUTH=off` only when an authenticating reverse proxy already sits in front of the container. For internet exposure, keep an authenticating reverse proxy in front and leave the port closed on untrusted networks. +> The standalone container requires API authentication by default. The app asks for a username and password when API auth is enabled. The login form works in the Home Assistant Companion app as well as a normal browser. The `/healthz` endpoint stays open for the container healthcheck. If you don't set `HAVC_PASSWORD`, a password is generated on first start, printed in the container logs as `[auth] API password: ...`, and saved to `/data/api-password` inside the container. Set `HAVC_USERNAME` and `HAVC_PASSWORD` to use your own credentials. Set `HAVC_API_AUTH=off` only when an authenticating reverse proxy already sits in front of the container. For internet exposure, keep an authenticating reverse proxy in front and leave the port closed on untrusted networks. For Docker users who aren't using the Home Assistant app, you have three deployment options: diff --git a/homeassistant-version-control/config.json b/homeassistant-version-control/config.json index 58920a7..c5ff618 100644 --- a/homeassistant-version-control/config.json +++ b/homeassistant-version-control/config.json @@ -1,6 +1,6 @@ { "name": "Home Assistant Version Control", - "version": "1.7.0", + "version": "1.7.1", "slug": "home-assistant-version-control", "description": "Git-powered version control and automated backup for your Home Assistant configuration files.", "url": "https://github.com/saihgupr/HomeAssistantVersionControl", @@ -95,7 +95,7 @@ "CMD", "node", "-e", - "fetch('http://localhost:54001/').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))" + "fetch('http://localhost:54001/healthz').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))" ], "interval": "5s", "timeout": "3s", diff --git a/homeassistant-version-control/package.json b/homeassistant-version-control/package.json index 76f9de0..cb6b9c1 100644 --- a/homeassistant-version-control/package.json +++ b/homeassistant-version-control/package.json @@ -1,6 +1,6 @@ { "name": "home-assistant-version-control", - "version": "1.7.0", + "version": "1.7.1", "description": "Home Assistant Version Control - Git-powered time travel for your HA config", "main": "server.js", "type": "module", diff --git a/homeassistant-version-control/public/auth.js b/homeassistant-version-control/public/auth.js new file mode 100644 index 0000000..f4329b4 --- /dev/null +++ b/homeassistant-version-control/public/auth.js @@ -0,0 +1,141 @@ +// In-app login for API auth. +// +// The browser's HTTP Basic dialog works in Safari and desktop browsers, but +// Home Assistant Ingress loads this app inside a frame in the Companion app, +// where iOS does not surface the dialog. This wrapper sends the credentials +// with every API request and shows a form when the server returns 401. +(function () { + 'use strict'; + + const AUTH_KEY = 'havc-api-auth'; + const FAILED_KEY = 'havc-api-auth-failed'; + const DEFAULT_USERNAME = 'havc'; + + function readStorage(key) { + try { + return sessionStorage.getItem(key) || ''; + } catch (error) { + return ''; + } + } + + function writeStorage(key, value) { + try { + sessionStorage.setItem(key, value); + } catch (error) { + // Private mode or a locked-down webview. The form still works for this page. + } + } + + function removeStorage(key) { + try { + sessionStorage.removeItem(key); + } catch (error) { + // Ignore. + } + } + + function getStoredAuth() { + return readStorage(AUTH_KEY); + } + + function isApiRequest(url) { + try { + const parsed = new URL(url, window.location.href); + // Never attach credentials to another origin, even if its path looks + // like an API route. + if (parsed.origin !== window.location.origin) return false; + return parsed.pathname === '/api' || parsed.pathname.startsWith('/api/'); + } catch (error) { + return false; + } + } + + function showLogin() { + const overlay = document.getElementById('authOverlay'); + if (!overlay) return; + + const form = document.getElementById('authForm'); + const username = document.getElementById('authUsername'); + const password = document.getElementById('authPassword'); + const errorBox = document.getElementById('authError'); + + overlay.hidden = false; + document.body.classList.add('auth-locked'); + + // Keep the error message across the app's several parallel 401s. Only the + // first call after a failed attempt sets it; later calls leave it alone. + const failed = readStorage(FAILED_KEY) === '1'; + if (failed) { + removeStorage(FAILED_KEY); + if (errorBox) errorBox.textContent = 'Wrong username or password.'; + } + if (username && !username.value) { + username.value = DEFAULT_USERNAME; + } + if (password) { + password.value = ''; + } + + if (form && !form.dataset.authBound) { + form.dataset.authBound = '1'; + form.addEventListener('submit', function (event) { + event.preventDefault(); + + const user = (username && username.value ? username.value : '').trim(); + const pass = password && password.value ? password.value : ''; + + if (!user || !pass) { + if (errorBox) errorBox.textContent = 'Enter a username and password.'; + return; + } + + writeStorage(AUTH_KEY, btoa(user + ':' + pass)); + window.location.reload(); + }); + } + + if (password) { + window.setTimeout(function () { + password.focus(); + }, 50); + } + } + + const nativeFetch = window.fetch.bind(window); + + window.fetch = function (input, init) { + const options = init ? Object.assign({}, init) : {}; + const url = typeof input === 'string' ? input : (input && input.url) || ''; + const apiRequest = isApiRequest(url); + + if (apiRequest) { + const headers = new Headers(options.headers || (input instanceof Request ? input.headers : undefined)); + headers.set('X-Requested-With', 'XMLHttpRequest'); + + const auth = getStoredAuth(); + if (auth) { + headers.set('Authorization', 'Basic ' + auth); + } + options.headers = headers; + } + + return nativeFetch(input, options).then(function (response) { + if (apiRequest && response.status === 401) { + if (getStoredAuth()) { + removeStorage(AUTH_KEY); + writeStorage(FAILED_KEY, '1'); + } + showLogin(); + } + return response; + }); + }; + + window.havcAuth = { + show: showLogin, + clear: function () { + removeStorage(AUTH_KEY); + } + }; +})(); diff --git a/homeassistant-version-control/public/css/auth.css b/homeassistant-version-control/public/css/auth.css new file mode 100644 index 0000000..1e5fbc6 --- /dev/null +++ b/homeassistant-version-control/public/css/auth.css @@ -0,0 +1,231 @@ +/* === In-app sign in === */ +/* Shown when the API returns 401. Home Assistant Ingress runs in an iframe + on iOS, where the native HTTP Basic dialog never appears, so this replaces it. */ + +.auth-overlay { + position: fixed; + inset: 0; + z-index: 20000; + display: flex; + align-items: center; + justify-content: center; + overflow-y: auto; + overflow-x: hidden; + overscroll-behavior: contain; + padding: max(16px, env(safe-area-inset-top, 0px)) max(16px, env(safe-area-inset-right, 0px)) max(16px, env(safe-area-inset-bottom, 0px)) max(16px, env(safe-area-inset-left, 0px)); + /* Soft accent glow over the scrim so the backdrop does not read as flat black */ + background: + radial-gradient(circle at 50% 42%, rgba(59, 130, 246, 0.1), transparent 62%), + rgba(0, 0, 0, 0.72); + -webkit-backdrop-filter: blur(8px); + backdrop-filter: blur(8px); + animation: authOverlayIn 0.18s ease-out; +} + +/* The hidden attribute must win over display: flex */ +.auth-overlay[hidden] { + display: none; +} + +body.auth-locked { + overflow: hidden; +} + +.auth-card { + position: relative; + z-index: 1; + width: 100%; + max-width: 360px; + margin: auto; + padding: 24px; + background: var(--bg-elevated); + border: 1px solid var(--border-subtle); + border-radius: var(--radius-lg); + box-shadow: 0 24px 60px rgba(0, 0, 0, 0.55), 0 2px 6px rgba(0, 0, 0, 0.35); + text-align: left; + animation: authCardIn 0.24s cubic-bezier(0.22, 1, 0.36, 1); +} + +.auth-title { + margin: 0 0 6px; + font-family: inherit; + font-size: 20px; + font-weight: 600; + letter-spacing: -0.02em; + color: var(--text-primary); +} + +.auth-subtitle { + margin: 0 0 18px; + font-size: 13px; + line-height: 1.45; + color: var(--text-secondary); +} + +.auth-label { + display: block; + margin-bottom: 6px; + font-size: 12px; + font-weight: 600; + letter-spacing: 0.02em; + color: var(--text-secondary); +} + +.auth-input { + width: 100%; + box-sizing: border-box; + margin-bottom: 14px; + padding: 10px 12px; + background: var(--bg-secondary); + border: 1px solid var(--border-medium); + border-radius: var(--radius-md); + color: var(--text-primary); + font-family: inherit; + font-size: 16px; /* 16px keeps iOS from zooming the field on focus */ + line-height: 1.4; + -webkit-appearance: none; + appearance: none; + transition: border-color 0.2s ease, box-shadow 0.2s ease, background 0.2s ease; +} + +.auth-input::placeholder { + color: var(--text-tertiary); +} + +.auth-input:focus { + outline: none; + border-color: var(--accent-primary); + box-shadow: 0 0 0 3px var(--accent-light, rgba(59, 130, 246, 0.15)); +} + +/* Keep autofilled fields inside the dark theme instead of a white block */ +.auth-input:-webkit-autofill, +.auth-input:-webkit-autofill:hover, +.auth-input:-webkit-autofill:focus { + -webkit-text-fill-color: var(--text-primary); + -webkit-box-shadow: 0 0 0 1000px var(--bg-secondary) inset; + caret-color: var(--text-primary); +} + +/* Autofill would otherwise hide the focus ring, so draw both shadows */ +.auth-input:-webkit-autofill:focus { + -webkit-box-shadow: 0 0 0 3px var(--accent-light, rgba(59, 130, 246, 0.15)), 0 0 0 1000px var(--bg-secondary) inset; +} + +.auth-error { + min-height: 18px; + margin: 0 0 12px; + font-size: 13px; + line-height: 1.35; + color: var(--danger, #ef4444); +} + +.auth-submit { + width: 100%; + display: flex; + align-items: center; + justify-content: center; + padding: 11px 16px; + background: var(--accent-primary); + color: #ffffff; + border: 0; + border-radius: var(--radius-md); + font-family: inherit; + font-size: 15px; + font-weight: 600; + letter-spacing: -0.01em; + cursor: pointer; + -webkit-appearance: none; + appearance: none; + -webkit-tap-highlight-color: transparent; + touch-action: manipulation; + transition: background 0.2s ease, filter 0.2s ease, transform 0.1s ease; +} + +.auth-submit:hover { + background: var(--accent-hover); +} + +.auth-submit:active { + background: var(--accent-hover); + transform: scale(0.995); +} + +.auth-submit:focus-visible { + outline: 2px solid var(--accent-primary); + outline-offset: 2px; +} + +.auth-submit:disabled { + background: var(--bg-tertiary); + color: var(--text-tertiary); + cursor: not-allowed; + transform: none; +} + +/* Light mode (body without dark-mode) needs a softer scrim */ +body:not(.dark-mode) .auth-overlay { + background: + radial-gradient(circle at 50% 42%, rgba(37, 99, 235, 0.08), transparent 62%), + rgba(15, 15, 15, 0.35); +} + +body:not(.dark-mode) .auth-card { + box-shadow: 0 24px 60px rgba(0, 0, 0, 0.18), 0 2px 6px rgba(0, 0, 0, 0.08); +} + +@keyframes authOverlayIn { + from { + opacity: 0; + } + + to { + opacity: 1; + } +} + +@keyframes authCardIn { + from { + opacity: 0; + transform: translateY(8px) scale(0.985); + } + + to { + opacity: 1; + transform: translateY(0) scale(1); + } +} + +/* Narrow phones (320px wide and below): keep the card inside the viewport */ +@media (max-width: 360px) { + .auth-card { + padding: 20px; + } +} + +/* Short landscape or split view: tighten spacing so nothing gets cut off */ +@media (max-height: 520px) { + .auth-card { + padding: 20px; + } + + .auth-subtitle { + margin-bottom: 12px; + } + + .auth-input { + margin-bottom: 10px; + } +} + +@media (prefers-reduced-motion: reduce) { + + .auth-overlay, + .auth-card { + animation: none; + } + + .auth-submit { + transition: none; + } +} diff --git a/homeassistant-version-control/public/index.html b/homeassistant-version-control/public/index.html index 1126a3e..28c6cb5 100644 --- a/homeassistant-version-control/public/index.html +++ b/homeassistant-version-control/public/index.html @@ -12,6 +12,7 @@ +