Updates
This commit is contained in:
+24
-22
@@ -25,6 +25,7 @@ type Param = {
|
||||
dsqlUserId?: string | number;
|
||||
expiry?: number;
|
||||
csrfHeaderName?: string;
|
||||
debug?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -45,31 +46,36 @@ export default function userAuth({
|
||||
expiry = weekInMilliseconds,
|
||||
cookieString,
|
||||
csrfHeaderName,
|
||||
debug,
|
||||
}: Param): AuthenticatedUser {
|
||||
try {
|
||||
const finalRequest = req || request;
|
||||
|
||||
const finalEncryptionKey =
|
||||
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
|
||||
const finalEncryptionSalt =
|
||||
encryptionSalt || process.env.DSQL_ENCRYPTION_SALT;
|
||||
|
||||
const cookies = parseCookies({
|
||||
request: finalRequest,
|
||||
cookieString,
|
||||
});
|
||||
|
||||
if (debug) {
|
||||
console.log("userAuth:cookies:", cookies);
|
||||
}
|
||||
|
||||
const keyNames = getAuthCookieNames({
|
||||
userId: dsqlUserId || process.env.DSQL_API_USER_ID,
|
||||
database: database || process.env.DSQL_DB_NAME,
|
||||
});
|
||||
|
||||
const authKeyName = keyNames.keyCookieName;
|
||||
const csrfName = keyNames.csrfCookieName;
|
||||
if (debug) {
|
||||
console.log("userAuth:keyNames:", keyNames);
|
||||
}
|
||||
|
||||
const key = encryptedUserString
|
||||
? encryptedUserString
|
||||
: cookies[authKeyName];
|
||||
: cookies[keyNames.keyCookieName];
|
||||
|
||||
if (debug) {
|
||||
console.log("userAuth:key:", key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Grab the payload
|
||||
@@ -78,10 +84,14 @@ export default function userAuth({
|
||||
*/
|
||||
let userPayloadJSON = decrypt({
|
||||
encryptedString: key,
|
||||
encryptionKey: finalEncryptionKey,
|
||||
encryptionSalt: finalEncryptionSalt,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
});
|
||||
|
||||
if (debug) {
|
||||
console.log("userAuth:userPayloadJSON:", userPayloadJSON);
|
||||
}
|
||||
|
||||
/**
|
||||
* Grab the payload
|
||||
*
|
||||
@@ -105,6 +115,10 @@ export default function userAuth({
|
||||
let userObject: import("../package-shared/types").DATASQUIREL_LoggedInUser =
|
||||
JSON.parse(userPayloadJSON);
|
||||
|
||||
if (debug) {
|
||||
console.log("userAuth:userObject:", userObject);
|
||||
}
|
||||
|
||||
if (!userObject.csrf_k) {
|
||||
return {
|
||||
success: false,
|
||||
@@ -137,18 +151,6 @@ export default function userAuth({
|
||||
msg: "CSRF_K mismatch",
|
||||
};
|
||||
}
|
||||
|
||||
// const targetCsrfHeaderKey = Object.keys(finalRequest.headers)
|
||||
// .map((k) => k.replace(/[^a-zA-Z0-9\-]/g, ""))
|
||||
// .find((k) => k == userObject.csrf_k);
|
||||
|
||||
// if (csrfHeaderIsValue && !targetCsrfHeaderKey) {
|
||||
// return {
|
||||
// success: false,
|
||||
// payload: null,
|
||||
// msg: "CSRF_K Header Key mismatch",
|
||||
// };
|
||||
// }
|
||||
}
|
||||
|
||||
const payloadCreationDate = Number(userObject.date);
|
||||
|
||||
Reference in New Issue
Block a user