Fix biased random API key generation and improve review feedback
This commit is contained in:
parent
8e69420295
commit
a1ee29a1b8
|
|
@ -10,11 +10,12 @@ function generatePlaceId() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generates a random alphanumeric+symbol API key of the given length (default 64 characters).
|
// Generates a random alphanumeric+symbol API key of the given length (default 64 characters).
|
||||||
|
// Uses crypto.randomInt for unbiased selection of each character (rather than bytes % length,
|
||||||
|
// which would skew towards characters at the start of the alphabet).
|
||||||
function generateApiKey(length = API_KEY_LENGTH) {
|
function generateApiKey(length = API_KEY_LENGTH) {
|
||||||
const bytes = crypto.randomBytes(length);
|
|
||||||
let key = '';
|
let key = '';
|
||||||
for (let i = 0; i < length; i++) {
|
for (let i = 0; i < length; i++) {
|
||||||
key += API_KEY_CHARS[bytes[i] % API_KEY_CHARS.length];
|
key += API_KEY_CHARS[crypto.randomInt(API_KEY_CHARS.length)];
|
||||||
}
|
}
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -241,11 +241,16 @@ document.getElementById('toggle-api-key-btn').addEventListener('click', () => {
|
||||||
document.getElementById('copy-api-key-btn').addEventListener('click', async () => {
|
document.getElementById('copy-api-key-btn').addEventListener('click', async () => {
|
||||||
const value = document.getElementById('place-api-key-display').value;
|
const value = document.getElementById('place-api-key-display').value;
|
||||||
if (!value) return;
|
if (!value) return;
|
||||||
|
|
||||||
|
const btn = document.getElementById('copy-api-key-btn');
|
||||||
|
const originalLabel = btn.textContent;
|
||||||
try {
|
try {
|
||||||
await navigator.clipboard.writeText(value);
|
await navigator.clipboard.writeText(value);
|
||||||
|
btn.textContent = 'Copied!';
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Clipboard API may be unavailable; fail silently.
|
btn.textContent = 'Copy failed';
|
||||||
}
|
}
|
||||||
|
setTimeout(() => { btn.textContent = originalLabel; }, 1500);
|
||||||
});
|
});
|
||||||
|
|
||||||
document.getElementById('regenerate-api-key-btn').addEventListener('click', async () => {
|
document.getElementById('regenerate-api-key-btn').addEventListener('click', async () => {
|
||||||
|
|
|
||||||
|
|
@ -103,8 +103,13 @@ router.post('/places', (req, res) => {
|
||||||
return res.status(403).json({ success: false, message: "Only admins can set a custom place id or API key" });
|
return res.status(403).json({ success: false, message: "Only admins can set a custom place id or API key" });
|
||||||
}
|
}
|
||||||
|
|
||||||
id = canSetCustomValues && id ? String(id).trim() : '';
|
if (!canSetCustomValues) {
|
||||||
apiKey = canSetCustomValues && apiKey ? String(apiKey).trim() : '';
|
id = '';
|
||||||
|
apiKey = '';
|
||||||
|
} else {
|
||||||
|
id = id ? String(id).trim() : '';
|
||||||
|
apiKey = apiKey ? String(apiKey).trim() : '';
|
||||||
|
}
|
||||||
|
|
||||||
if (!id) id = generatePlaceId();
|
if (!id) id = generatePlaceId();
|
||||||
if (!apiKey) apiKey = generateApiKey();
|
if (!apiKey) apiKey = generateApiKey();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue