Files
Sharpbin/SharpbinV2.Server/wwwroot/js/index.js
T
whoswhip 3eb7374a07 Enhance user authentication and paste management
- Updated `AuthController.cs` to include token in `CreateSession` and improved validation for username and email.
- Refactored paste creation logic from `Program.cs` to a new `PasteController.cs`.
- Added email validation method in `HelperService.cs`.
- Introduced `CompressionService.cs` for handling string compression and decompression.
- Improved user experience with autocomplete and spellcheck in HTML forms.
- Enhanced date display in `archive.js` and refactored date formatting in `dash.js`.
- Updated API endpoints in `login.js` and `register.js` to reflect changes.
2025-07-06 22:55:29 -04:00

104 lines
3.1 KiB
JavaScript

const siteInfo = [];
document.addEventListener("DOMContentLoaded", async function () {
var visibility = document.getElementById("visibility");
visibility.value = "0";
getSiteInfo();
});
document.getElementById("create-form").addEventListener("submit", async function (event) {
event.preventDefault();
var title = document.getElementById("title").value;
var content = document.getElementById("content").value;
var visibility = document.getElementById("visibility").value;
var password = document.getElementById("password").value;
var syntax = document.getElementById("syntax").value;
if (content.length > siteInfo.maxFileSize) {
showNotification("Content exceeds maximum size.", "error");
return;
}
if (content.length === 0) {
showNotification("Content cannot be empty.", "error");
return;
}
if (visibility === "2" && password !== "" && password.length >= 6) {
content = await encryptAES(content, password);
}
else if (visibility === "2") {
showNotification("Password must be at least 6 characters long.", "error");
return;
}
fetch(`/api/pastes/create?title=${title}&visibility=${visibility}&syntax=${syntax}`, {
method: "POST",
headers: {
'Content-Type': 'text/plain'
},
body: content
})
.then(response => response.json())
.then(data => {
const paste = data.paste;
window.location.href = `/${paste.id}`;
});
});
function changeVisibility() {
var visibility = document.getElementById("visibility").value;
var password = document.getElementById("password");
if (visibility === "2") {
password.style.display = "block";
} else {
password.style.display = "none";
}
}
function getSiteInfo() {
fetch("/api/site/info")
.then(response => response.json())
.then(data => {
siteInfo = data.info;
var content = document.getElementById("content")
content.setAttribute("maxlength", siteInfo.maxFileSize);
});
}
async function encryptAES(content, password) {
const enc = new TextEncoder();
const keyMaterial = await window.crypto.subtle.importKey(
"raw",
enc.encode(password),
{ name: "PBKDF2" },
false,
["deriveKey"]
);
const key = await window.crypto.subtle.deriveKey(
{
name: "PBKDF2",
salt: enc.encode("salt"),
iterations: 100000,
hash: "SHA-256"
},
keyMaterial,
{ name: "AES-GCM", length: 256 },
false,
["encrypt"]
);
const iv = window.crypto.getRandomValues(new Uint8Array(12));
const encryptedContent = await window.crypto.subtle.encrypt(
{
name: "AES-GCM",
iv: iv
},
key,
enc.encode(content)
);
const encryptedArray = new Uint8Array(encryptedContent);
const encryptedString = btoa(String.fromCharCode(...iv) + String.fromCharCode(...encryptedArray));
return encryptedString;
}