Simple auth
This commit is contained in:
parent
0b901a8a9f
commit
f3f3a6e22f
2 changed files with 5 additions and 25 deletions
|
|
@ -15,8 +15,4 @@ Open `index.html` in a browser to use it.
|
||||||
|
|
||||||
## Secret
|
## Secret
|
||||||
|
|
||||||
The current demo secret is `change-me`. Before publishing, replace `AUTH_SECRET_HASH` in `app.js` with the SHA-256 hash of your real secret. This is a simple client-side gate, not strong server-side security.
|
The current demo secret is `change-me`. Before publishing, replace `AUTH_SECRET` in `app.js` with your real secret. This is a simple client-side gate, not strong server-side security.
|
||||||
|
|
||||||
```sh
|
|
||||||
printf 'your-secret' | sha256sum
|
|
||||||
```
|
|
||||||
|
|
|
||||||
24
app.js
24
app.js
|
|
@ -1,9 +1,7 @@
|
||||||
const STORAGE_KEY = "routes.links.v1";
|
const STORAGE_KEY = "routes.links.v1";
|
||||||
const AUTH_SESSION_KEY = "routes.authenticated.v1";
|
const AUTH_SESSION_KEY = "routes.authenticated.v1";
|
||||||
// SHA-256 hash of the secret. Default secret is "change-me".
|
// Simple shared secret. Change this before publishing if needed.
|
||||||
// Replace this hash before publishing. Generate a new one with:
|
const AUTH_SECRET = "change-me";
|
||||||
// printf 'your-secret' | sha256sum
|
|
||||||
const AUTH_SECRET_HASH = "e2186dbdb1bb4193608605e84f33208765b5693b55edd4f730a719a100eeea6f";
|
|
||||||
|
|
||||||
const authScreen = document.querySelector("#auth-screen");
|
const authScreen = document.querySelector("#auth-screen");
|
||||||
const authForm = document.querySelector("#auth-form");
|
const authForm = document.querySelector("#auth-form");
|
||||||
|
|
@ -41,19 +39,11 @@ if (sessionStorage.getItem(AUTH_SESSION_KEY) === "yes") {
|
||||||
lockApp();
|
lockApp();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function authenticate(event) {
|
function authenticate(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
authMessage.textContent = "";
|
authMessage.textContent = "";
|
||||||
|
|
||||||
let hash;
|
if (authSecretInput.value.trim() !== AUTH_SECRET) {
|
||||||
try {
|
|
||||||
hash = await sha256(authSecretInput.value.trim());
|
|
||||||
} catch {
|
|
||||||
authMessage.textContent = "Authentication needs HTTPS, localhost, or a modern browser.";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hash !== AUTH_SECRET_HASH) {
|
|
||||||
authMessage.textContent = "Wrong secret.";
|
authMessage.textContent = "Wrong secret.";
|
||||||
authSecretInput.select();
|
authSecretInput.select();
|
||||||
return;
|
return;
|
||||||
|
|
@ -79,12 +69,6 @@ function lockApp() {
|
||||||
authSecretInput.focus();
|
authSecretInput.focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function sha256(value) {
|
|
||||||
const data = new TextEncoder().encode(value);
|
|
||||||
const digest = await crypto.subtle.digest("SHA-256", data);
|
|
||||||
return [...new Uint8Array(digest)].map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
||||||
}
|
|
||||||
|
|
||||||
function loadRoutes() {
|
function loadRoutes() {
|
||||||
try {
|
try {
|
||||||
return JSON.parse(localStorage.getItem(STORAGE_KEY)) ?? [];
|
return JSON.parse(localStorage.getItem(STORAGE_KEY)) ?? [];
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue