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).
|
||||
// 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) {
|
||||
const bytes = crypto.randomBytes(length);
|
||||
let key = '';
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -241,11 +241,16 @@ document.getElementById('toggle-api-key-btn').addEventListener('click', () => {
|
|||
document.getElementById('copy-api-key-btn').addEventListener('click', async () => {
|
||||
const value = document.getElementById('place-api-key-display').value;
|
||||
if (!value) return;
|
||||
|
||||
const btn = document.getElementById('copy-api-key-btn');
|
||||
const originalLabel = btn.textContent;
|
||||
try {
|
||||
await navigator.clipboard.writeText(value);
|
||||
btn.textContent = 'Copied!';
|
||||
} 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 () => {
|
||||
|
|
|
|||
|
|
@ -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" });
|
||||
}
|
||||
|
||||
id = canSetCustomValues && id ? String(id).trim() : '';
|
||||
apiKey = canSetCustomValues && apiKey ? String(apiKey).trim() : '';
|
||||
if (!canSetCustomValues) {
|
||||
id = '';
|
||||
apiKey = '';
|
||||
} else {
|
||||
id = id ? String(id).trim() : '';
|
||||
apiKey = apiKey ? String(apiKey).trim() : '';
|
||||
}
|
||||
|
||||
if (!id) id = generatePlaceId();
|
||||
if (!apiKey) apiKey = generateApiKey();
|
||||
|
|
|
|||
Loading…
Reference in a new issue