Simple auth
This commit is contained in:
parent
0b901a8a9f
commit
f3f3a6e22f
2 changed files with 5 additions and 25 deletions
24
app.js
24
app.js
|
|
@ -1,9 +1,7 @@
|
|||
const STORAGE_KEY = "routes.links.v1";
|
||||
const AUTH_SESSION_KEY = "routes.authenticated.v1";
|
||||
// SHA-256 hash of the secret. Default secret is "change-me".
|
||||
// Replace this hash before publishing. Generate a new one with:
|
||||
// printf 'your-secret' | sha256sum
|
||||
const AUTH_SECRET_HASH = "e2186dbdb1bb4193608605e84f33208765b5693b55edd4f730a719a100eeea6f";
|
||||
// Simple shared secret. Change this before publishing if needed.
|
||||
const AUTH_SECRET = "change-me";
|
||||
|
||||
const authScreen = document.querySelector("#auth-screen");
|
||||
const authForm = document.querySelector("#auth-form");
|
||||
|
|
@ -41,19 +39,11 @@ if (sessionStorage.getItem(AUTH_SESSION_KEY) === "yes") {
|
|||
lockApp();
|
||||
}
|
||||
|
||||
async function authenticate(event) {
|
||||
function authenticate(event) {
|
||||
event.preventDefault();
|
||||
authMessage.textContent = "";
|
||||
|
||||
let hash;
|
||||
try {
|
||||
hash = await sha256(authSecretInput.value.trim());
|
||||
} catch {
|
||||
authMessage.textContent = "Authentication needs HTTPS, localhost, or a modern browser.";
|
||||
return;
|
||||
}
|
||||
|
||||
if (hash !== AUTH_SECRET_HASH) {
|
||||
if (authSecretInput.value.trim() !== AUTH_SECRET) {
|
||||
authMessage.textContent = "Wrong secret.";
|
||||
authSecretInput.select();
|
||||
return;
|
||||
|
|
@ -79,12 +69,6 @@ function lockApp() {
|
|||
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() {
|
||||
try {
|
||||
return JSON.parse(localStorage.getItem(STORAGE_KEY)) ?? [];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue