location update optimized with schema changes and geoJson Compatability testing endpoint with encrypted key authorization

This commit is contained in:
Shibi Chakkaravarthy
2026-04-17 14:15:11 +05:30
parent b44abf9c88
commit 92769cfd38
19 changed files with 459 additions and 27 deletions
+57 -1
View File
@@ -2,14 +2,42 @@ import jwt from "jsonwebtoken";
import crypto from "node:crypto";
import User from "../models/User/model.js";
import Access from "../models/Access/model.js";
import dotenv from "dotenv";
dotenv.config();
const { ECT_KEY, ECT_IV, ECT_METHOD } = process.env;
export const getAccessToken = (payload) => {
return jwt.sign(payload, process.env.ECT_KEY, {
return jwt.sign(payload, ECT_KEY, {
algorithm: "HS256",
expiresIn: "7m",
});
};
export const getEncryptedPayload = (payload) => {
const cipher = crypto.createCipheriv(ECT_METHOD, ECT_KEY, ECT_IV);
let encrypted = cipher.update(JSON.stringify(payload), "utf8", "hex");
encrypted += cipher.final("hex");
const tag = cipher.getAuthTag().toString("hex");
return `${ECT_IV}:${tag}:${encrypted}`;
};
export const getDecryptedPayload = (payload) => {
const [ivHex, authTagHex, encryptedHex] = payload.split(":");
const iv = Buffer.from(ivHex, "hex");
const authTag = Buffer.from(authTagHex, "hex");
const decipher = crypto.createDecipheriv(ECT_METHOD, ECT_KEY, ECT_IV);
decipher.setAuthTag(authTag); // Verify integrity
let decrypted = decipher.update(encryptedHex, "hex", "utf8");
decrypted += decipher.final("utf8");
return JSON.parse(decrypted);
};
export const getRefreshToken = () => {
return crypto.randomBytes(32).toString("hex");
};
@@ -68,3 +96,31 @@ export const authorize = (requiredModule, requiredPermission) => {
}
};
};
export const authorizeWithEncryptedKey = (
requiredModule,
requiredPermission,
) => {
return async (req, res, next) => {
try {
const { key } = req.query;
if (!key) {
throw { code: 401, message: "Encrypted Auth Key Missing" };
}
const verifiedPayload = getDecryptedPayload(key);
const userExists = await User.exists({ _id: verifiedPayload.id });
if (!userExists) {
throw { code: 404, message: "User not found" };
}
res.locals.user = verifiedPayload;
next();
} catch (error) {
console.error("AUTH_ERROR", error);
res.status(error.code || 401).json(error);
}
};
};