Introduce a new logger (modules/logger.js) that prints colorized console output, writes timestamped logs to a file in a logs/ directory, strips ANSI colors for file output, and handles process exit/SIGINT cleanup. Add service stubs (services/security.js, services/storage.js, services/smtp/actions.js, services/smtp/authenticate.js) and a test.js. Add package.json and package-lock.json with runtime deps (colors, dotenv, zod) and dev tooling (eslint, nodemon) to support the new code.
47 lines
1.2 KiB
JavaScript
47 lines
1.2 KiB
JavaScript
const crypto = require("node:crypto")
|
|
|
|
function decodeSASL(base64Payload) {
|
|
try {
|
|
const buffer = Buffer.from(base64Payload, "base64")
|
|
const parts = [];
|
|
let lastPos = 0;
|
|
|
|
for (let i = 0; i < buffer.length; i++) {
|
|
if (buffer[i] === 0) {
|
|
parts.push(buffer.slice(lastPos, i).toString("utf8"))
|
|
lastPos = i + 1
|
|
}
|
|
}
|
|
parts.push(buffer.slice(lastPos).toString("utf8"))
|
|
|
|
return { user: parts[1], pass: parts[2] }
|
|
} catch (e) {
|
|
return null
|
|
}
|
|
}
|
|
|
|
function safeCompare(input, actual) {
|
|
if (!input || !actual || input.length !== actual.length) {
|
|
return false
|
|
}
|
|
return crypto.timingSafeEqual(Buffer.from(input), Buffer.from(actual))
|
|
}
|
|
|
|
function authenticate(identity, secret, type = "PASSWORD") {
|
|
const mockUser = {
|
|
username: "azures",
|
|
password: "Password123@",
|
|
token: "tanit_tk_998877"
|
|
};
|
|
|
|
if (type === "TOKEN") {
|
|
return safeCompare(secret, mockUser.token)
|
|
}
|
|
|
|
return safeCompare(identity, mockUser.username) && safeCompare(secret, mockUser.password)
|
|
}
|
|
|
|
module.exports = {
|
|
decodeSASL,
|
|
authenticate
|
|
} |