Spaces:
Running
Running
Create login-handler.js
Browse files- login-handler.js +129 -0
login-handler.js
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { KeyLock } from '../dist/keylock.js';
|
2 |
+
|
3 |
+
document.addEventListener('DOMContentLoaded', () => {
|
4 |
+
const keylock = new KeyLock();
|
5 |
+
|
6 |
+
// --- DOM Elements ---
|
7 |
+
const imageUploadInput = document.getElementById('image-upload-input');
|
8 |
+
const imageUploadBox = document.getElementById('image-upload-box');
|
9 |
+
const imagePreview = document.getElementById('image-preview');
|
10 |
+
const uploadPrompt = document.getElementById('upload-prompt');
|
11 |
+
const statusDisplay = document.getElementById('status-display');
|
12 |
+
const privateKeyInputDecode = document.getElementById('private-key-input-decode');
|
13 |
+
|
14 |
+
const generateImgButton = document.getElementById('generate-img-button');
|
15 |
+
const payloadInput = document.getElementById('payload-input');
|
16 |
+
const publicKeyInputEncode = document.getElementById('public-key-input-encode');
|
17 |
+
const generatedImageArea = document.getElementById('generated-image-area');
|
18 |
+
const generatedImagePreview = document.getElementById('generated-image-preview');
|
19 |
+
const downloadGeneratedImage = document.getElementById('download-generated-image');
|
20 |
+
|
21 |
+
const generateKeysButton = document.getElementById('generate-keys-button');
|
22 |
+
const outputPublicKey = document.getElementById('output-public-key');
|
23 |
+
const outputPrivateKey = document.getElementById('output-private-key');
|
24 |
+
|
25 |
+
// --- Helper Functions ---
|
26 |
+
function setStatus(message, type = 'info') {
|
27 |
+
statusDisplay.innerHTML = `<p>${message}</p>`;
|
28 |
+
statusDisplay.className = type;
|
29 |
+
}
|
30 |
+
|
31 |
+
// --- Event Listeners ---
|
32 |
+
|
33 |
+
// 1. DECODER: Handle image upload
|
34 |
+
imageUploadInput.addEventListener('change', async (event) => {
|
35 |
+
const file = event.target.files[0];
|
36 |
+
if (!file) return;
|
37 |
+
|
38 |
+
const privateKey = privateKeyInputDecode.value;
|
39 |
+
if (!privateKey) {
|
40 |
+
setStatus('<strong>Error:</strong> Please provide your Private Key to decode the image.', 'error');
|
41 |
+
return;
|
42 |
+
}
|
43 |
+
|
44 |
+
// Show preview
|
45 |
+
const reader = new FileReader();
|
46 |
+
reader.onload = (e) => {
|
47 |
+
imagePreview.src = e.target.result;
|
48 |
+
imagePreview.classList.remove('hidden');
|
49 |
+
uploadPrompt.classList.add('hidden');
|
50 |
+
};
|
51 |
+
reader.readAsDataURL(file);
|
52 |
+
|
53 |
+
setStatus('Decoding in progress...', 'info');
|
54 |
+
|
55 |
+
try {
|
56 |
+
const payload = await keylock.decode(file, privateKey);
|
57 |
+
let payloadHtml = "<strong>Authentication Success</strong><ul>";
|
58 |
+
for (const [key, value] of Object.entries(payload)) {
|
59 |
+
// Obfuscate sensitive values like passwords/keys
|
60 |
+
const isSensitive = ['pass', 'secret', 'key', 'token'].some(k => key.toLowerCase().includes(k));
|
61 |
+
const displayValue = isSensitive ? '•'.repeat(8) : value;
|
62 |
+
payloadHtml += `<li><strong>${key}:</strong> ${displayValue}</li>`;
|
63 |
+
}
|
64 |
+
payloadHtml += "</ul>";
|
65 |
+
setStatus(payloadHtml, 'success');
|
66 |
+
} catch (error) {
|
67 |
+
console.error('Decoding failed:', error);
|
68 |
+
setStatus(`<strong>Login Failed:</strong> ${error.message}`, 'error');
|
69 |
+
}
|
70 |
+
});
|
71 |
+
|
72 |
+
// Allow drag-and-drop
|
73 |
+
imageUploadBox.addEventListener('dragover', (e) => { e.preventDefault(); e.stopPropagation(); });
|
74 |
+
imageUploadBox.addEventListener('drop', (e) => {
|
75 |
+
e.preventDefault();
|
76 |
+
e.stopPropagation();
|
77 |
+
imageUploadInput.files = e.dataTransfer.files;
|
78 |
+
imageUploadInput.dispatchEvent(new Event('change'));
|
79 |
+
});
|
80 |
+
imageUploadBox.addEventListener('click', () => imageUploadInput.click());
|
81 |
+
|
82 |
+
|
83 |
+
// 2. ENCODER: Handle image generation
|
84 |
+
generateImgButton.addEventListener('click', async () => {
|
85 |
+
const publicKey = publicKeyInputEncode.value;
|
86 |
+
const payloadStr = payloadInput.value;
|
87 |
+
|
88 |
+
if (!publicKey || !payloadStr) {
|
89 |
+
alert('Please provide the recipient\'s public key and a JSON payload.');
|
90 |
+
return;
|
91 |
+
}
|
92 |
+
|
93 |
+
try {
|
94 |
+
const payload = JSON.parse(payloadStr);
|
95 |
+
generateImgButton.textContent = 'Generating...';
|
96 |
+
generateImgButton.disabled = true;
|
97 |
+
|
98 |
+
const imageUrl = await keylock.encode(payload, publicKey);
|
99 |
+
|
100 |
+
generatedImagePreview.src = imageUrl;
|
101 |
+
downloadGeneratedImage.href = imageUrl;
|
102 |
+
generatedImageArea.classList.remove('hidden');
|
103 |
+
|
104 |
+
} catch (error) {
|
105 |
+
console.error('Encoding failed:', error);
|
106 |
+
alert(`Encoding failed: ${error.message}. Make sure the payload is valid JSON.`);
|
107 |
+
} finally {
|
108 |
+
generateImgButton.textContent = 'Generate Encrypted Image';
|
109 |
+
generateImgButton.disabled = false;
|
110 |
+
}
|
111 |
+
});
|
112 |
+
|
113 |
+
// 3. KEYGEN: Handle key pair generation
|
114 |
+
generateKeysButton.addEventListener('click', async () => {
|
115 |
+
generateKeysButton.textContent = 'Generating...';
|
116 |
+
generateKeysButton.disabled = true;
|
117 |
+
try {
|
118 |
+
const keys = await KeyLock.generateKeys();
|
119 |
+
outputPublicKey.value = keys.publicKey;
|
120 |
+
outputPrivateKey.value = keys.privateKey;
|
121 |
+
} catch (error) {
|
122 |
+
console.error('Key generation failed:', error);
|
123 |
+
alert(`Key generation failed: ${error.message}`);
|
124 |
+
} finally {
|
125 |
+
generateKeysButton.textContent = 'Generate Keys';
|
126 |
+
generateKeysButton.disabled = false;
|
127 |
+
}
|
128 |
+
});
|
129 |
+
});
|