Updates
This commit is contained in:
Vendored
+2
-2
@@ -5,7 +5,7 @@ export = addUser;
|
||||
* @async
|
||||
*
|
||||
* @param {object} param - Single object passed
|
||||
* @param {string} param.key - FULL ACCESS API Key
|
||||
* @param {string} [param.key] - FULL ACCESS API Key
|
||||
* @param {string} param.database - Database Name
|
||||
* @param {import("../package-shared/types").UserDataPayload} param.payload - User Data Payload
|
||||
* @param {string} [param.encryptionKey]
|
||||
@@ -17,7 +17,7 @@ export = addUser;
|
||||
* @returns { Promise<import("../package-shared/types").AddUserFunctionReturn> }
|
||||
*/
|
||||
declare function addUser({ key, payload, database, encryptionKey, user_id, useLocal, apiUserId, }: {
|
||||
key: string;
|
||||
key?: string;
|
||||
database: string;
|
||||
payload: import("../package-shared/types").UserDataPayload;
|
||||
encryptionKey?: string;
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ const apiCreateUser = require("../package-shared/functions/api/users/api-create-
|
||||
* @async
|
||||
*
|
||||
* @param {object} param - Single object passed
|
||||
* @param {string} param.key - FULL ACCESS API Key
|
||||
* @param {string} [param.key] - FULL ACCESS API Key
|
||||
* @param {string} param.database - Database Name
|
||||
* @param {import("../package-shared/types").UserDataPayload} param.payload - User Data Payload
|
||||
* @param {string} [param.encryptionKey]
|
||||
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
export = deleteUser;
|
||||
/**
|
||||
* # Update User
|
||||
* @async
|
||||
*
|
||||
* @param {object} params - API Key
|
||||
* @param {String} [params.key] - API Key
|
||||
* @param {String} params.database - Target Database
|
||||
* @param {String | number} params.deletedUserId - Target Database
|
||||
* @param {boolean} [params.user_id] - User ID
|
||||
* @param {boolean} [params.useLocal]
|
||||
*
|
||||
* @returns { Promise<import("../package-shared/types").UpdateUserFunctionReturn>}
|
||||
*/
|
||||
declare function deleteUser({ key, database, user_id, useLocal, deletedUserId }: {
|
||||
key?: string;
|
||||
database: string;
|
||||
deletedUserId: string | number;
|
||||
user_id?: boolean;
|
||||
useLocal?: boolean;
|
||||
}): Promise<import("../package-shared/types").UpdateUserFunctionReturn>;
|
||||
@@ -0,0 +1,127 @@
|
||||
// @ts-check
|
||||
|
||||
const http = require("http");
|
||||
const https = require("https");
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
const grabHostNames = require("../package-shared/utils/grab-host-names");
|
||||
const apiUpdateUser = require("../package-shared/functions/api/users/api-update-user");
|
||||
const apiDeleteUser = require("../package-shared/functions/api/users/api-delete-user");
|
||||
|
||||
/**
|
||||
* # Update User
|
||||
* @async
|
||||
*
|
||||
* @param {object} params - API Key
|
||||
* @param {String} [params.key] - API Key
|
||||
* @param {String} params.database - Target Database
|
||||
* @param {String | number} params.deletedUserId - Target Database
|
||||
* @param {boolean} [params.user_id] - User ID
|
||||
* @param {boolean} [params.useLocal]
|
||||
*
|
||||
* @returns { Promise<import("../package-shared/types").UpdateUserFunctionReturn>}
|
||||
*/
|
||||
async function deleteUser({ key, database, user_id, useLocal, deletedUserId }) {
|
||||
/**
|
||||
* Check for local DB settings
|
||||
*
|
||||
* @description Look for local db settings in `.env` file and by pass the http request if available
|
||||
*/
|
||||
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME } = process.env;
|
||||
|
||||
const grabedHostNames = grabHostNames();
|
||||
const { host, port, scheme } = grabedHostNames;
|
||||
|
||||
if (
|
||||
DSQL_HOST?.match(/./) &&
|
||||
DSQL_USER?.match(/./) &&
|
||||
DSQL_PASS?.match(/./) &&
|
||||
DSQL_DB_NAME?.match(/./) &&
|
||||
useLocal
|
||||
) {
|
||||
/** @type {import("../package-shared/types").DSQL_DatabaseSchemaType | undefined} */
|
||||
let dbSchema;
|
||||
|
||||
try {
|
||||
const localDbSchemaPath = path.resolve(
|
||||
process.cwd(),
|
||||
"dsql.schema.json"
|
||||
);
|
||||
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
|
||||
} catch (error) {}
|
||||
|
||||
if (dbSchema) {
|
||||
return await apiDeleteUser({
|
||||
dbFullName: DSQL_DB_NAME,
|
||||
useLocal,
|
||||
deletedUserId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Make https request
|
||||
*
|
||||
* @description make a request to datasquirel.com
|
||||
*/
|
||||
const httpResponse = await new Promise((resolve, reject) => {
|
||||
const reqPayload = JSON.stringify({
|
||||
database,
|
||||
deletedUserId,
|
||||
});
|
||||
|
||||
const httpsRequest = scheme.request(
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Content-Length": Buffer.from(reqPayload).length,
|
||||
Authorization:
|
||||
process.env.DSQL_FULL_ACCESS_API_KEY ||
|
||||
process.env.DSQL_API_KEY ||
|
||||
key,
|
||||
},
|
||||
port,
|
||||
hostname: host,
|
||||
path: `/api/user/${
|
||||
user_id || grabedHostNames.user_id
|
||||
}/delete-user`,
|
||||
},
|
||||
|
||||
/**
|
||||
* Callback Function
|
||||
*
|
||||
* @description https request callback
|
||||
*/
|
||||
(response) => {
|
||||
var str = "";
|
||||
|
||||
response.on("data", function (chunk) {
|
||||
str += chunk;
|
||||
});
|
||||
|
||||
response.on("end", function () {
|
||||
resolve(JSON.parse(str));
|
||||
});
|
||||
|
||||
response.on("error", (err) => {
|
||||
reject(err);
|
||||
});
|
||||
}
|
||||
);
|
||||
httpsRequest.write(reqPayload);
|
||||
httpsRequest.end();
|
||||
});
|
||||
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
|
||||
return httpResponse;
|
||||
}
|
||||
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
|
||||
module.exports = deleteUser;
|
||||
Vendored
+15
-11
@@ -5,7 +5,7 @@ export = loginUser;
|
||||
* @async
|
||||
*
|
||||
* @param {object} params - Single Param object containing params
|
||||
* @param {String} params.key - FULL ACCESS API Key
|
||||
* @param {String} [params.key] - FULL ACCESS API Key
|
||||
* @param {String} params.database - Target Database
|
||||
* @param {{
|
||||
* email?: string,
|
||||
@@ -13,9 +13,9 @@ export = loginUser;
|
||||
* password: string,
|
||||
* }} params.payload Login Email/Username and Password
|
||||
* @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object
|
||||
* @param {http.ServerResponse} params.response - Http response object
|
||||
* @param {String} params.encryptionKey - Encryption Key
|
||||
* @param {String} params.encryptionSalt - Encryption Salt
|
||||
* @param {http.ServerResponse & Object<string, any>} [params.response] - Http response object
|
||||
* @param {String} [params.encryptionKey] - Encryption Key
|
||||
* @param {String} [params.encryptionSalt] - Encryption Salt
|
||||
* @param {boolean} [params.email_login] - Email only Login
|
||||
* @param {string} [params.email_login_code] - Email login code
|
||||
* @param {string} [params.temp_code_field] - Database table field name for temporary code
|
||||
@@ -23,11 +23,12 @@ export = loginUser;
|
||||
* @param {boolean} [params.user_id] - User ID
|
||||
* @param {boolean} [params.skipPassword]
|
||||
* @param {boolean} [params.useLocal]
|
||||
* @param {string | number} [params.apiUserID] - Required for setting of cookies
|
||||
*
|
||||
* @returns { Promise<import("../package-shared/types").AuthenticatedUser>}
|
||||
* @returns { Promise<import("../package-shared/types").APILoginFunctionReturn>}
|
||||
*/
|
||||
declare function loginUser({ key, payload, database, additionalFields, response, encryptionKey, encryptionSalt, email_login, email_login_code, temp_code_field, token, user_id, skipPassword, useLocal, }: {
|
||||
key: string;
|
||||
declare function loginUser({ key, payload, database, additionalFields, response, encryptionKey, encryptionSalt, email_login, email_login_code, temp_code_field, token, user_id, skipPassword, useLocal, apiUserID, }: {
|
||||
key?: string;
|
||||
database: string;
|
||||
payload: {
|
||||
email?: string;
|
||||
@@ -35,9 +36,11 @@ declare function loginUser({ key, payload, database, additionalFields, response,
|
||||
password: string;
|
||||
};
|
||||
additionalFields?: string[];
|
||||
response: http.ServerResponse;
|
||||
encryptionKey: string;
|
||||
encryptionSalt: string;
|
||||
response?: http.ServerResponse & {
|
||||
[x: string]: any;
|
||||
};
|
||||
encryptionKey?: string;
|
||||
encryptionSalt?: string;
|
||||
email_login?: boolean;
|
||||
email_login_code?: string;
|
||||
temp_code_field?: string;
|
||||
@@ -45,5 +48,6 @@ declare function loginUser({ key, payload, database, additionalFields, response,
|
||||
user_id?: boolean;
|
||||
skipPassword?: boolean;
|
||||
useLocal?: boolean;
|
||||
}): Promise<import("../package-shared/types").AuthenticatedUser>;
|
||||
apiUserID?: string | number;
|
||||
}): Promise<import("../package-shared/types").APILoginFunctionReturn>;
|
||||
import http = require("http");
|
||||
|
||||
+57
-47
@@ -13,6 +13,9 @@ const encrypt = require("../package-shared/functions/dsql/encrypt");
|
||||
const grabHostNames = require("../package-shared/utils/grab-host-names");
|
||||
const apiLoginUser = require("../package-shared/functions/api/users/api-login");
|
||||
const getAuthCookieNames = require("../package-shared/functions/backend/cookies/get-auth-cookie-names");
|
||||
const {
|
||||
writeAuthFile,
|
||||
} = require("../package-shared/functions/backend/auth/write-auth-files");
|
||||
|
||||
/**
|
||||
* Login A user
|
||||
@@ -20,7 +23,7 @@ const getAuthCookieNames = require("../package-shared/functions/backend/cookies/
|
||||
* @async
|
||||
*
|
||||
* @param {object} params - Single Param object containing params
|
||||
* @param {String} params.key - FULL ACCESS API Key
|
||||
* @param {String} [params.key] - FULL ACCESS API Key
|
||||
* @param {String} params.database - Target Database
|
||||
* @param {{
|
||||
* email?: string,
|
||||
@@ -28,9 +31,9 @@ const getAuthCookieNames = require("../package-shared/functions/backend/cookies/
|
||||
* password: string,
|
||||
* }} params.payload Login Email/Username and Password
|
||||
* @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object
|
||||
* @param {http.ServerResponse} params.response - Http response object
|
||||
* @param {String} params.encryptionKey - Encryption Key
|
||||
* @param {String} params.encryptionSalt - Encryption Salt
|
||||
* @param {http.ServerResponse & Object<string, any>} [params.response] - Http response object
|
||||
* @param {String} [params.encryptionKey] - Encryption Key
|
||||
* @param {String} [params.encryptionSalt] - Encryption Salt
|
||||
* @param {boolean} [params.email_login] - Email only Login
|
||||
* @param {string} [params.email_login_code] - Email login code
|
||||
* @param {string} [params.temp_code_field] - Database table field name for temporary code
|
||||
@@ -38,8 +41,9 @@ const getAuthCookieNames = require("../package-shared/functions/backend/cookies/
|
||||
* @param {boolean} [params.user_id] - User ID
|
||||
* @param {boolean} [params.skipPassword]
|
||||
* @param {boolean} [params.useLocal]
|
||||
* @param {string | number} [params.apiUserID] - Required for setting of cookies
|
||||
*
|
||||
* @returns { Promise<import("../package-shared/types").AuthenticatedUser>}
|
||||
* @returns { Promise<import("../package-shared/types").APILoginFunctionReturn>}
|
||||
*/
|
||||
async function loginUser({
|
||||
key,
|
||||
@@ -56,6 +60,7 @@ async function loginUser({
|
||||
user_id,
|
||||
skipPassword,
|
||||
useLocal,
|
||||
apiUserID,
|
||||
}) {
|
||||
const grabedHostNames = grabHostNames();
|
||||
const { host, port, scheme } = grabedHostNames;
|
||||
@@ -67,6 +72,28 @@ async function loginUser({
|
||||
: defaultTempLoginFieldName
|
||||
: undefined;
|
||||
|
||||
const finalEncryptionKey =
|
||||
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
|
||||
const finalEncryptionSalt =
|
||||
encryptionSalt || process.env.DSQL_ENCRYPTION_SALT;
|
||||
|
||||
if (!finalEncryptionKey?.match(/.{8,}/)) {
|
||||
console.log("Encryption key is invalid");
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
msg: "Encryption key is invalid",
|
||||
};
|
||||
}
|
||||
if (!finalEncryptionSalt?.match(/.{8,}/)) {
|
||||
console.log("Encryption salt is invalid");
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
msg: "Encryption salt is invalid",
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Check required fields
|
||||
*
|
||||
@@ -80,43 +107,14 @@ async function loginUser({
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Check Encryption Keys
|
||||
*
|
||||
* @description Check Encryption Keys
|
||||
*/
|
||||
if (!encryptionKey?.match(/./))
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
msg: "Encryption Key Required",
|
||||
};
|
||||
|
||||
if (!encryptionSalt?.match(/./))
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
msg: "Encryption Salt Required",
|
||||
};
|
||||
|
||||
if (encryptionKey.length < 24)
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
msg: "Encryption Key must be at least 24 characters",
|
||||
};
|
||||
|
||||
if (encryptionSalt.length < 8)
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
msg: "Encryption Salt must be at least 8 characters",
|
||||
};
|
||||
|
||||
/**
|
||||
* Initialize HTTP response variable
|
||||
*/
|
||||
let httpResponse;
|
||||
|
||||
/** @type {import("../package-shared/types").APILoginFunctionReturn} */
|
||||
let httpResponse = {
|
||||
success: false,
|
||||
};
|
||||
|
||||
/**
|
||||
* Check for local DB settings
|
||||
@@ -150,7 +148,7 @@ async function loginUser({
|
||||
username: payload.username,
|
||||
password: payload.password,
|
||||
skipPassword,
|
||||
encryptionKey,
|
||||
encryptionKey: finalEncryptionKey,
|
||||
additionalFields,
|
||||
email_login,
|
||||
email_login_code,
|
||||
@@ -170,7 +168,7 @@ async function loginUser({
|
||||
httpResponse = await new Promise((resolve, reject) => {
|
||||
/** @type {import("../package-shared/types").PackageUserLoginRequestBody} */
|
||||
const reqPayload = {
|
||||
encryptionKey,
|
||||
encryptionKey: finalEncryptionKey,
|
||||
payload,
|
||||
database,
|
||||
additionalFields,
|
||||
@@ -236,22 +234,34 @@ async function loginUser({
|
||||
if (httpResponse?.success) {
|
||||
let encryptedPayload = encrypt({
|
||||
data: JSON.stringify(httpResponse.payload),
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
encryptionKey: finalEncryptionKey,
|
||||
encryptionSalt: finalEncryptionSalt,
|
||||
});
|
||||
|
||||
try {
|
||||
if (token) httpResponse["token"] = encryptedPayload;
|
||||
if (token && encryptedPayload)
|
||||
httpResponse["token"] = encryptedPayload;
|
||||
} catch (error) {}
|
||||
|
||||
const { userId } = httpResponse;
|
||||
const cookieNames = getAuthCookieNames({
|
||||
database,
|
||||
userId: apiUserID || process.env.DSQL_API_USER_ID,
|
||||
});
|
||||
|
||||
const cookieNames = getAuthCookieNames();
|
||||
if (httpResponse.csrf) {
|
||||
writeAuthFile(
|
||||
httpResponse.csrf,
|
||||
JSON.stringify(httpResponse.payload)
|
||||
);
|
||||
}
|
||||
|
||||
httpResponse["cookieNames"] = cookieNames;
|
||||
httpResponse["key"] = String(encryptedPayload);
|
||||
|
||||
const authKeyName = cookieNames.keyCookieName;
|
||||
const csrfName = cookieNames.csrfCookieName;
|
||||
|
||||
response.setHeader("Set-Cookie", [
|
||||
response?.setHeader("Set-Cookie", [
|
||||
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`,
|
||||
`${csrfName}=${httpResponse.payload?.csrf_k};samesite=strict;path=/;HttpOnly=true`,
|
||||
]);
|
||||
|
||||
Vendored
+12
-9
@@ -3,20 +3,23 @@ export = logoutUser;
|
||||
* Logout user
|
||||
* ==============================================================================
|
||||
* @param {object} params - Single Param object containing params
|
||||
* @param {http.IncomingMessage} params.request - Http request object
|
||||
* @param {http.ServerResponse} params.response - Http response object
|
||||
* @param {string} [params.database] - Target database name(slug): optional => If you don't
|
||||
* include this you will be logged out of all datasquirel websites instead of just the target
|
||||
* database
|
||||
* @param {string} params.encryptedUserString - Encrypted User String
|
||||
* @param {http.ServerResponse & Object<string, any>} [params.response] - Http response object
|
||||
* @param {string} [params.database] - Target database name(slug): optional
|
||||
* @param {string | number} [params.dsqlUserId]
|
||||
*
|
||||
* @returns {{success: boolean, payload: string}}
|
||||
* @returns {{success: boolean, payload: string, cookieNames?: any}}
|
||||
*/
|
||||
declare function logoutUser({ request, response, database }: {
|
||||
request: http.IncomingMessage;
|
||||
response: http.ServerResponse;
|
||||
declare function logoutUser({ response, database, dsqlUserId, encryptedUserString }: {
|
||||
encryptedUserString: string;
|
||||
response?: http.ServerResponse & {
|
||||
[x: string]: any;
|
||||
};
|
||||
database?: string;
|
||||
dsqlUserId?: string | number;
|
||||
}): {
|
||||
success: boolean;
|
||||
payload: string;
|
||||
cookieNames?: any;
|
||||
};
|
||||
import http = require("http");
|
||||
|
||||
+35
-46
@@ -1,72 +1,61 @@
|
||||
// @ts-check
|
||||
|
||||
const http = require("http");
|
||||
const parseCookies = require("../utils/functions/parseCookies");
|
||||
const getAuthCookieNames = require("../package-shared/functions/backend/cookies/get-auth-cookie-names");
|
||||
const decrypt = require("../package-shared/functions/dsql/decrypt");
|
||||
const EJSON = require("../package-shared/utils/ejson");
|
||||
const {
|
||||
deleteAuthFile,
|
||||
} = require("../package-shared/functions/backend/auth/write-auth-files");
|
||||
|
||||
/**
|
||||
* Logout user
|
||||
* ==============================================================================
|
||||
* @param {object} params - Single Param object containing params
|
||||
* @param {http.IncomingMessage} params.request - Http request object
|
||||
* @param {http.ServerResponse} params.response - Http response object
|
||||
* @param {string} [params.database] - Target database name(slug): optional => If you don't
|
||||
* include this you will be logged out of all datasquirel websites instead of just the target
|
||||
* database
|
||||
* @param {string} params.encryptedUserString - Encrypted User String
|
||||
* @param {http.ServerResponse & Object<string, any>} [params.response] - Http response object
|
||||
* @param {string} [params.database] - Target database name(slug): optional
|
||||
* @param {string | number} [params.dsqlUserId]
|
||||
*
|
||||
* @returns {{success: boolean, payload: string}}
|
||||
* @returns {{success: boolean, payload: string, cookieNames?: any}}
|
||||
*/
|
||||
function logoutUser({ request, response, database }) {
|
||||
function logoutUser({ response, database, dsqlUserId, encryptedUserString }) {
|
||||
/**
|
||||
* Check Encryption Keys
|
||||
*
|
||||
* @description Check Encryption Keys
|
||||
*/
|
||||
try {
|
||||
const cookies = parseCookies({ request });
|
||||
const cookiesKeys = Object.keys(cookies);
|
||||
|
||||
const keyNames = getAuthCookieNames();
|
||||
|
||||
const keyRegexp = new RegExp(keyNames.keyCookieName);
|
||||
const csrfRegexp = new RegExp(keyNames.csrfCookieName);
|
||||
|
||||
const authKeyName = cookiesKeys.filter((cookieKey) =>
|
||||
cookieKey.match(keyRegexp)
|
||||
)[0];
|
||||
const csrfName = cookiesKeys.filter((cookieKey) =>
|
||||
cookieKey.match(csrfRegexp)
|
||||
)[0];
|
||||
|
||||
if (authKeyName && csrfName) {
|
||||
response.setHeader("Set-Cookie", [
|
||||
`${authKeyName}=null;max-age=0`,
|
||||
`${csrfName}=null;max-age=0`,
|
||||
]);
|
||||
} else {
|
||||
const allKeys = cookiesKeys.filter((cookieKey) =>
|
||||
cookieKey.match(/datasquirel_.*_auth_key/)
|
||||
);
|
||||
const allCsrfs = cookiesKeys.filter((cookieKey) =>
|
||||
cookieKey.match(/datasquirel_.*_csrf/)
|
||||
const decryptedUserJSON = decrypt({
|
||||
encryptedString: encryptedUserString,
|
||||
});
|
||||
const userObject =
|
||||
/** @type {import("../package-shared/types").DATASQUIREL_LoggedInUser | undefined} */ (
|
||||
EJSON.parse(decryptedUserJSON)
|
||||
);
|
||||
|
||||
response.setHeader("Set-Cookie", [
|
||||
...allKeys.map(
|
||||
(key) =>
|
||||
`${key}=null;samesite=strict;path=/;HttpOnly=true;Secure=true`
|
||||
),
|
||||
...allCsrfs.map(
|
||||
(csrf) =>
|
||||
`${csrf}=null;samesite=strict;path=/;HttpOnly=true`
|
||||
),
|
||||
`dsqluid=null;samesite=strict;path=/;HttpOnly=true`,
|
||||
]);
|
||||
}
|
||||
if (!userObject?.csrf_k)
|
||||
throw new Error("Invalid User. Please check key");
|
||||
|
||||
const cookieNames = getAuthCookieNames({
|
||||
database,
|
||||
userId: dsqlUserId || process.env.DSQL_API_USER_ID,
|
||||
});
|
||||
const authKeyName = cookieNames.keyCookieName;
|
||||
const csrfName = cookieNames.csrfCookieName;
|
||||
|
||||
response?.setHeader("Set-Cookie", [
|
||||
`${authKeyName}=null;max-age=0`,
|
||||
`${csrfName}=null;max-age=0`,
|
||||
]);
|
||||
|
||||
const csrf = userObject.csrf_k;
|
||||
deleteAuthFile(csrf);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
payload: "User Logged Out",
|
||||
cookieNames,
|
||||
};
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
|
||||
Vendored
+15
-15
@@ -12,31 +12,31 @@ export = reauthUser;
|
||||
* @async
|
||||
*
|
||||
* @param {object} params - Single Param object containing params
|
||||
* @param {String} params.key - API Key
|
||||
* @param {String} [params.key] - API Key
|
||||
* @param {String} params.database - Target Database
|
||||
* @param {http.ServerResponse} params.response - Http response object
|
||||
* @param {http.IncomingMessage} params.request - Http request object
|
||||
* @param {http.ServerResponse} [params.response] - Http response object
|
||||
* @param {http.IncomingMessage} [params.request] - Http request object
|
||||
* @param {("deep" | "normal")} [params.level] - Authentication level
|
||||
* @param {String} params.encryptionKey - Encryption Key
|
||||
* @param {String} params.encryptionSalt - Encryption Salt
|
||||
* @param {String} [params.encryptionKey] - Encryption Key
|
||||
* @param {String} [params.encryptionSalt] - Encryption Salt
|
||||
* @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object
|
||||
* @param {string} [params.token] - access token to use instead of getting from cookie header
|
||||
* @param {string} [params.encryptedUserString] - encrypted user string to use instead of getting from cookie header
|
||||
* @param {boolean} [params.user_id] - User ID
|
||||
* @param {boolean} [params.useLocal]
|
||||
*
|
||||
* @returns { Promise<import("../package-shared/types").ReauthUserFunctionReturn> }
|
||||
* @returns { Promise<import("../package-shared/types").APILoginFunctionReturn> }
|
||||
*/
|
||||
declare function reauthUser({ key, database, response, request, level, encryptionKey, encryptionSalt, additionalFields, token, user_id, useLocal, }: {
|
||||
key: string;
|
||||
declare function reauthUser({ key, database, response, request, level, encryptionKey, encryptionSalt, additionalFields, encryptedUserString, user_id, useLocal, }: {
|
||||
key?: string;
|
||||
database: string;
|
||||
response: http.ServerResponse;
|
||||
request: http.IncomingMessage;
|
||||
response?: http.ServerResponse;
|
||||
request?: http.IncomingMessage;
|
||||
level?: ("deep" | "normal");
|
||||
encryptionKey: string;
|
||||
encryptionSalt: string;
|
||||
encryptionKey?: string;
|
||||
encryptionSalt?: string;
|
||||
additionalFields?: string[];
|
||||
token?: string;
|
||||
encryptedUserString?: string;
|
||||
user_id?: boolean;
|
||||
useLocal?: boolean;
|
||||
}): Promise<import("../package-shared/types").ReauthUserFunctionReturn>;
|
||||
}): Promise<import("../package-shared/types").APILoginFunctionReturn>;
|
||||
import http = require("http");
|
||||
|
||||
+37
-20
@@ -14,6 +14,11 @@ const encrypt = require("../package-shared/functions/dsql/encrypt");
|
||||
const userAuth = require("./user-auth");
|
||||
const grabHostNames = require("../package-shared/utils/grab-host-names");
|
||||
const apiReauthUser = require("../package-shared/functions/api/users/api-reauth-user");
|
||||
const {
|
||||
writeAuthFile,
|
||||
deleteAuthFile,
|
||||
} = require("../package-shared/functions/backend/auth/write-auth-files");
|
||||
const getAuthCookieNames = require("../package-shared/functions/backend/cookies/get-auth-cookie-names");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
@@ -29,19 +34,19 @@ const apiReauthUser = require("../package-shared/functions/api/users/api-reauth-
|
||||
* @async
|
||||
*
|
||||
* @param {object} params - Single Param object containing params
|
||||
* @param {String} params.key - API Key
|
||||
* @param {String} [params.key] - API Key
|
||||
* @param {String} params.database - Target Database
|
||||
* @param {http.ServerResponse} params.response - Http response object
|
||||
* @param {http.IncomingMessage} params.request - Http request object
|
||||
* @param {http.ServerResponse} [params.response] - Http response object
|
||||
* @param {http.IncomingMessage} [params.request] - Http request object
|
||||
* @param {("deep" | "normal")} [params.level] - Authentication level
|
||||
* @param {String} params.encryptionKey - Encryption Key
|
||||
* @param {String} params.encryptionSalt - Encryption Salt
|
||||
* @param {String} [params.encryptionKey] - Encryption Key
|
||||
* @param {String} [params.encryptionSalt] - Encryption Salt
|
||||
* @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object
|
||||
* @param {string} [params.token] - access token to use instead of getting from cookie header
|
||||
* @param {string} [params.encryptedUserString] - encrypted user string to use instead of getting from cookie header
|
||||
* @param {boolean} [params.user_id] - User ID
|
||||
* @param {boolean} [params.useLocal]
|
||||
*
|
||||
* @returns { Promise<import("../package-shared/types").ReauthUserFunctionReturn> }
|
||||
* @returns { Promise<import("../package-shared/types").APILoginFunctionReturn> }
|
||||
*/
|
||||
async function reauthUser({
|
||||
key,
|
||||
@@ -52,7 +57,7 @@ async function reauthUser({
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
additionalFields,
|
||||
token,
|
||||
encryptedUserString,
|
||||
user_id,
|
||||
useLocal,
|
||||
}) {
|
||||
@@ -64,13 +69,18 @@ async function reauthUser({
|
||||
const grabedHostNames = grabHostNames();
|
||||
const { host, port, scheme } = grabedHostNames;
|
||||
|
||||
const finalEncryptionKey =
|
||||
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
|
||||
const finalEncryptionSalt =
|
||||
encryptionSalt || process.env.DSQL_ENCRYPTION_SALT;
|
||||
|
||||
const existingUser = userAuth({
|
||||
database,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
encryptionKey: finalEncryptionKey,
|
||||
encryptionSalt: finalEncryptionSalt,
|
||||
level,
|
||||
request,
|
||||
token,
|
||||
encryptedUserString,
|
||||
});
|
||||
|
||||
if (!existingUser?.payload?.id) {
|
||||
@@ -189,23 +199,30 @@ async function reauthUser({
|
||||
if (httpResponse?.success) {
|
||||
let encryptedPayload = encrypt({
|
||||
data: JSON.stringify(httpResponse.payload),
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
encryptionKey: finalEncryptionKey,
|
||||
encryptionSalt: finalEncryptionSalt,
|
||||
});
|
||||
|
||||
const { userId } = httpResponse;
|
||||
const cookieNames = getAuthCookieNames({ database, userId });
|
||||
|
||||
const authKeyName = `datasquirel_${userId}_${database}_auth_key`;
|
||||
const csrfName = `datasquirel_${userId}_${database}_csrf`;
|
||||
httpResponse["cookieNames"] = cookieNames;
|
||||
httpResponse["key"] = String(encryptedPayload);
|
||||
|
||||
response.setHeader("Set-Cookie", [
|
||||
const authKeyName = cookieNames.keyCookieName;
|
||||
const csrfName = cookieNames.csrfCookieName;
|
||||
|
||||
response?.setHeader("Set-Cookie", [
|
||||
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`,
|
||||
`${csrfName}=${httpResponse.payload.csrf_k};samesite=strict;path=/;HttpOnly=true`,
|
||||
`dsqluid=${userId};samesite=strict;path=/;HttpOnly=true`,
|
||||
`${csrfName}=${httpResponse.payload?.csrf_k};samesite=strict;path=/;HttpOnly=true`,
|
||||
]);
|
||||
|
||||
if (token) {
|
||||
httpResponse.token = encryptedPayload;
|
||||
if (httpResponse.csrf) {
|
||||
deleteAuthFile(String(existingUser.payload.csrf_k));
|
||||
writeAuthFile(
|
||||
httpResponse.csrf,
|
||||
JSON.stringify(httpResponse.payload)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Vendored
+2
-2
@@ -28,7 +28,7 @@ export = githubAuth;
|
||||
* @param {http.ServerResponse} params.response - HTTPS response object
|
||||
* @param {string} params.encryptionKey - Encryption key
|
||||
* @param {string} params.encryptionSalt - Encryption salt
|
||||
* @param {object} [params.additionalFields] - Additional Fields to be added to the user object
|
||||
* @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object
|
||||
* @param {boolean} [params.user_id] - User ID
|
||||
*
|
||||
* @returns { Promise<FunctionReturn | undefined> }
|
||||
@@ -43,7 +43,7 @@ declare function githubAuth({ key, code, email, database, clientId, clientSecret
|
||||
response: http.ServerResponse;
|
||||
encryptionKey: string;
|
||||
encryptionSalt: string;
|
||||
additionalFields?: object;
|
||||
additionalFields?: string[];
|
||||
user_id?: boolean;
|
||||
}): Promise<FunctionReturn | undefined>;
|
||||
declare namespace githubAuth {
|
||||
|
||||
@@ -44,7 +44,7 @@ const apiGithubLogin = require("../../package-shared/functions/api/users/social/
|
||||
* @param {http.ServerResponse} params.response - HTTPS response object
|
||||
* @param {string} params.encryptionKey - Encryption key
|
||||
* @param {string} params.encryptionSalt - Encryption salt
|
||||
* @param {object} [params.additionalFields] - Additional Fields to be added to the user object
|
||||
* @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object
|
||||
* @param {boolean} [params.user_id] - User ID
|
||||
*
|
||||
* @returns { Promise<FunctionReturn | undefined> }
|
||||
|
||||
Vendored
+12
-14
@@ -19,29 +19,27 @@ export = googleAuth;
|
||||
* @async
|
||||
*
|
||||
* @param {object} params - main params object
|
||||
* @param {string} params.key - API full access key
|
||||
* @param {string} [params.key] - API full access key
|
||||
* @param {string} params.token - Google access token gotten from the client side
|
||||
* @param {string} params.database - Target database name(slug)
|
||||
* @param {string} params.clientId - Google client id
|
||||
* @param {http.ServerResponse} params.response - HTTPS response object
|
||||
* @param {string} params.encryptionKey - Encryption key
|
||||
* @param {string} params.encryptionSalt - Encryption salt
|
||||
* @param {object} [params.additionalFields] - Additional Fields to be added to the user object
|
||||
* @param {http.ServerResponse} [params.response] - HTTPS response object
|
||||
* @param {string} [params.encryptionKey] - Encryption key
|
||||
* @param {string} [params.encryptionSalt] - Encryption salt
|
||||
* @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object
|
||||
* @param {boolean} [params.user_id] - User ID
|
||||
* @param {string | number} [params.apiUserID] - Required for Local
|
||||
* @param {string | number} [params.apiUserID] - Required for setting of cookies
|
||||
* @param {boolean} [params.useLocal] - Whether to use a remote database instead of API
|
||||
*
|
||||
* @returns { Promise<FunctionReturn> }
|
||||
*/
|
||||
declare function googleAuth({ key, token, database, clientId, response, encryptionKey, encryptionSalt, additionalFields, user_id, apiUserID, useLocal, }: {
|
||||
key: string;
|
||||
declare function googleAuth({ key, token, database, response, encryptionKey, encryptionSalt, additionalFields, user_id, apiUserID, useLocal, }: {
|
||||
key?: string;
|
||||
token: string;
|
||||
database: string;
|
||||
clientId: string;
|
||||
response: http.ServerResponse;
|
||||
encryptionKey: string;
|
||||
encryptionSalt: string;
|
||||
additionalFields?: object;
|
||||
response?: http.ServerResponse;
|
||||
encryptionKey?: string;
|
||||
encryptionSalt?: string;
|
||||
additionalFields?: string[];
|
||||
user_id?: boolean;
|
||||
apiUserID?: string | number;
|
||||
useLocal?: boolean;
|
||||
|
||||
+61
-84
@@ -6,12 +6,15 @@
|
||||
* ==============================================================================
|
||||
*/
|
||||
const http = require("http");
|
||||
const https = require("https");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const encrypt = require("../../package-shared/functions/dsql/encrypt");
|
||||
const grabHostNames = require("../../package-shared/utils/grab-host-names");
|
||||
const apiGoogleLogin = require("../../package-shared/functions/api/users/social/api-google-login");
|
||||
const getAuthCookieNames = require("../../package-shared/functions/backend/cookies/get-auth-cookie-names");
|
||||
const {
|
||||
writeAuthFile,
|
||||
} = require("../../package-shared/functions/backend/auth/write-auth-files");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
@@ -35,16 +38,15 @@ const apiGoogleLogin = require("../../package-shared/functions/api/users/social/
|
||||
* @async
|
||||
*
|
||||
* @param {object} params - main params object
|
||||
* @param {string} params.key - API full access key
|
||||
* @param {string} [params.key] - API full access key
|
||||
* @param {string} params.token - Google access token gotten from the client side
|
||||
* @param {string} params.database - Target database name(slug)
|
||||
* @param {string} params.clientId - Google client id
|
||||
* @param {http.ServerResponse} params.response - HTTPS response object
|
||||
* @param {string} params.encryptionKey - Encryption key
|
||||
* @param {string} params.encryptionSalt - Encryption salt
|
||||
* @param {object} [params.additionalFields] - Additional Fields to be added to the user object
|
||||
* @param {http.ServerResponse} [params.response] - HTTPS response object
|
||||
* @param {string} [params.encryptionKey] - Encryption key
|
||||
* @param {string} [params.encryptionSalt] - Encryption salt
|
||||
* @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object
|
||||
* @param {boolean} [params.user_id] - User ID
|
||||
* @param {string | number} [params.apiUserID] - Required for Local
|
||||
* @param {string | number} [params.apiUserID] - Required for setting of cookies
|
||||
* @param {boolean} [params.useLocal] - Whether to use a remote database instead of API
|
||||
*
|
||||
* @returns { Promise<FunctionReturn> }
|
||||
@@ -53,7 +55,6 @@ async function googleAuth({
|
||||
key,
|
||||
token,
|
||||
database,
|
||||
clientId,
|
||||
response,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
@@ -65,18 +66,33 @@ async function googleAuth({
|
||||
const grabedHostNames = grabHostNames();
|
||||
const { host, port, scheme } = grabedHostNames;
|
||||
|
||||
const finalEncryptionKey =
|
||||
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
|
||||
const finalEncryptionSalt =
|
||||
encryptionSalt || process.env.DSQL_ENCRYPTION_SALT;
|
||||
|
||||
if (!finalEncryptionKey?.match(/.{8,}/)) {
|
||||
console.log("Encryption key is invalid");
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
msg: "Encryption key is invalid",
|
||||
};
|
||||
}
|
||||
if (!finalEncryptionSalt?.match(/.{8,}/)) {
|
||||
console.log("Encryption salt is invalid");
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
msg: "Encryption salt is invalid",
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Check inputs
|
||||
*
|
||||
* @description Check inputs
|
||||
*/
|
||||
if (!key || key?.match(/ /)) {
|
||||
return {
|
||||
success: false,
|
||||
user: null,
|
||||
msg: "Please enter API full access Key",
|
||||
};
|
||||
}
|
||||
|
||||
if (!token || token?.match(/ /)) {
|
||||
return {
|
||||
@@ -94,46 +110,14 @@ async function googleAuth({
|
||||
};
|
||||
}
|
||||
|
||||
if (!clientId || clientId?.match(/ /)) {
|
||||
return {
|
||||
success: false,
|
||||
user: null,
|
||||
msg: "Please enter Google OAUTH client ID",
|
||||
};
|
||||
}
|
||||
|
||||
if (!response || !response?.setHeader) {
|
||||
return {
|
||||
success: false,
|
||||
user: null,
|
||||
msg: "Please provide a valid HTTPS response object",
|
||||
};
|
||||
}
|
||||
|
||||
if (!encryptionKey || encryptionKey?.match(/ /)) {
|
||||
return {
|
||||
success: false,
|
||||
user: null,
|
||||
msg: "Please provide a valid encryption key",
|
||||
};
|
||||
}
|
||||
|
||||
if (!encryptionSalt || encryptionSalt?.match(/ /)) {
|
||||
return {
|
||||
success: false,
|
||||
user: null,
|
||||
msg: "Please provide a valid encryption salt",
|
||||
};
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Initialize HTTP response variable
|
||||
*/
|
||||
let httpResponse;
|
||||
|
||||
/** @type {import("../../package-shared/types").APILoginFunctionReturn} */
|
||||
let httpResponse = {
|
||||
success: false,
|
||||
};
|
||||
|
||||
/**
|
||||
* Check for local DB settings
|
||||
@@ -163,18 +147,11 @@ async function googleAuth({
|
||||
if (dbSchema && apiUserID) {
|
||||
httpResponse = await apiGoogleLogin({
|
||||
token,
|
||||
clientId,
|
||||
additionalFields,
|
||||
res: response,
|
||||
database: DSQL_DB_NAME,
|
||||
userId: apiUserID,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Make https request
|
||||
*
|
||||
@@ -184,7 +161,6 @@ async function googleAuth({
|
||||
httpResponse = await new Promise((resolve, reject) => {
|
||||
const reqPayload = JSON.stringify({
|
||||
token,
|
||||
clientId,
|
||||
database,
|
||||
additionalFields,
|
||||
});
|
||||
@@ -233,44 +209,45 @@ async function googleAuth({
|
||||
});
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Make https request
|
||||
*
|
||||
* @description make a request to datasquirel.com
|
||||
*/
|
||||
if (httpResponse?.success && httpResponse?.user) {
|
||||
if (httpResponse?.success && httpResponse?.payload) {
|
||||
let encryptedPayload = encrypt({
|
||||
data: JSON.stringify(httpResponse.user),
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
data: JSON.stringify(httpResponse.payload),
|
||||
encryptionKey: finalEncryptionKey,
|
||||
encryptionSalt: finalEncryptionSalt,
|
||||
});
|
||||
|
||||
const { user, dsqlUserId } = httpResponse;
|
||||
const cookieNames = getAuthCookieNames({
|
||||
database,
|
||||
userId: apiUserID || process.env.DSQL_API_USER_ID,
|
||||
});
|
||||
|
||||
const authKeyName = `datasquirel_${dsqlUserId}_${database}_auth_key`;
|
||||
const csrfName = `datasquirel_${dsqlUserId}_${database}_csrf`;
|
||||
console.log("apiUserID", apiUserID);
|
||||
|
||||
response.setHeader("Set-Cookie", [
|
||||
if (httpResponse.csrf) {
|
||||
writeAuthFile(
|
||||
httpResponse.csrf,
|
||||
JSON.stringify(httpResponse.payload)
|
||||
);
|
||||
}
|
||||
|
||||
httpResponse["cookieNames"] = cookieNames;
|
||||
httpResponse["key"] = String(encryptedPayload);
|
||||
|
||||
const authKeyName = cookieNames.keyCookieName;
|
||||
const csrfName = cookieNames.csrfCookieName;
|
||||
|
||||
response?.setHeader("Set-Cookie", [
|
||||
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`,
|
||||
`${csrfName}=${user.csrf_k};samesite=strict;path=/;HttpOnly=true`,
|
||||
`dsqluid=${dsqlUserId};samesite=strict;path=/;HttpOnly=true`,
|
||||
`datasquirel_social_id=${user.social_id};samesite=strict;path=/`,
|
||||
`${csrfName}=${httpResponse.payload?.csrf_k};samesite=strict;path=/;HttpOnly=true`,
|
||||
]);
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
return httpResponse;
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
module.exports = googleAuth;
|
||||
|
||||
Vendored
+6
-6
@@ -4,20 +4,20 @@ export = updateUser;
|
||||
* @async
|
||||
*
|
||||
* @param {object} params - API Key
|
||||
* @param {String} params.key - API Key
|
||||
* @param {String} [params.key] - API Key
|
||||
* @param {String} params.database - Target Database
|
||||
* @param {{ id: number } & Object.<string, any>} params.payload - User Object: ID is required
|
||||
* @param {String | number} params.updatedUserId - Target Database
|
||||
* @param {Object.<string, any>} params.payload - User Object: ID is required
|
||||
* @param {boolean} [params.user_id] - User ID
|
||||
* @param {boolean} [params.useLocal]
|
||||
*
|
||||
* @returns { Promise<import("../package-shared/types").UpdateUserFunctionReturn>}
|
||||
*/
|
||||
declare function updateUser({ key, payload, database, user_id, useLocal }: {
|
||||
key: string;
|
||||
declare function updateUser({ key, payload, database, user_id, useLocal, updatedUserId, }: {
|
||||
key?: string;
|
||||
database: string;
|
||||
updatedUserId: string | number;
|
||||
payload: {
|
||||
id: number;
|
||||
} & {
|
||||
[x: string]: any;
|
||||
};
|
||||
user_id?: boolean;
|
||||
|
||||
+14
-3
@@ -12,15 +12,23 @@ const apiUpdateUser = require("../package-shared/functions/api/users/api-update-
|
||||
* @async
|
||||
*
|
||||
* @param {object} params - API Key
|
||||
* @param {String} params.key - API Key
|
||||
* @param {String} [params.key] - API Key
|
||||
* @param {String} params.database - Target Database
|
||||
* @param {{ id: number } & Object.<string, any>} params.payload - User Object: ID is required
|
||||
* @param {String | number} params.updatedUserId - Target Database
|
||||
* @param {Object.<string, any>} params.payload - User Object: ID is required
|
||||
* @param {boolean} [params.user_id] - User ID
|
||||
* @param {boolean} [params.useLocal]
|
||||
*
|
||||
* @returns { Promise<import("../package-shared/types").UpdateUserFunctionReturn>}
|
||||
*/
|
||||
async function updateUser({ key, payload, database, user_id, useLocal }) {
|
||||
async function updateUser({
|
||||
key,
|
||||
payload,
|
||||
database,
|
||||
user_id,
|
||||
useLocal,
|
||||
updatedUserId,
|
||||
}) {
|
||||
/**
|
||||
* Check for local DB settings
|
||||
*
|
||||
@@ -54,6 +62,8 @@ async function updateUser({ key, payload, database, user_id, useLocal }) {
|
||||
payload: payload,
|
||||
dbFullName: DSQL_DB_NAME,
|
||||
useLocal,
|
||||
updatedUserId,
|
||||
dbSchema,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -67,6 +77,7 @@ async function updateUser({ key, payload, database, user_id, useLocal }) {
|
||||
const reqPayload = JSON.stringify({
|
||||
payload,
|
||||
database,
|
||||
updatedUserId,
|
||||
});
|
||||
|
||||
const httpsRequest = scheme.request(
|
||||
|
||||
Vendored
+21
-11
@@ -6,21 +6,31 @@ export = userAuth;
|
||||
* with the user's data
|
||||
*
|
||||
* @param {Object} params - Arg
|
||||
* @param {http.IncomingMessage} params.request - Http request object
|
||||
* @param {string} params.encryptionKey - Encryption Key
|
||||
* @param {string} params.encryptionSalt - Encryption Salt
|
||||
* @param {http.IncomingMessage & Object<string, any>} [params.request] - Http request object
|
||||
* @param {http.IncomingMessage & Object<string, any>} [params.req] - Http request object
|
||||
* @param {string} [params.encryptedUserString] - Encrypted user string to use instead of getting from cookie header
|
||||
* @param {string} [params.encryptionKey] - Encryption Key: alt env: DSQL_ENCRYPTION_PASSWORD
|
||||
* @param {string} [params.encryptionSalt] - Encryption Salt: alt env: DSQL_ENCRYPTION_SALT
|
||||
* @param {("deep" | "normal")} [params.level] - Optional. "Deep" value indicates an extra layer of security
|
||||
* @param {string} params.database - Database Name
|
||||
* @param {string} [params.token] - access token to use instead of getting from cookie header
|
||||
* @param {string} [params.database] - Database Name (slug)
|
||||
* @param {string | number} [params.dsqlUserId] - alt env: DSQL_API_USER_ID
|
||||
* @param {number} [params.expiry] - Expiry time in milliseconds
|
||||
*
|
||||
* @returns { import("../package-shared/types").AuthenticatedUser }
|
||||
*/
|
||||
declare function userAuth({ request, encryptionKey, encryptionSalt, level, database, token, }: {
|
||||
request: http.IncomingMessage;
|
||||
encryptionKey: string;
|
||||
encryptionSalt: string;
|
||||
declare function userAuth({ request, req, encryptionKey, encryptionSalt, level, database, dsqlUserId, encryptedUserString, expiry, }: {
|
||||
request?: http.IncomingMessage & {
|
||||
[x: string]: any;
|
||||
};
|
||||
req?: http.IncomingMessage & {
|
||||
[x: string]: any;
|
||||
};
|
||||
encryptedUserString?: string;
|
||||
encryptionKey?: string;
|
||||
encryptionSalt?: string;
|
||||
level?: ("deep" | "normal");
|
||||
database: string;
|
||||
token?: string;
|
||||
database?: string;
|
||||
dsqlUserId?: string | number;
|
||||
expiry?: number;
|
||||
}): import("../package-shared/types").AuthenticatedUser;
|
||||
import http = require("http");
|
||||
|
||||
+77
-19
@@ -4,6 +4,16 @@ const http = require("http");
|
||||
const decrypt = require("../package-shared/functions/dsql/decrypt");
|
||||
const parseCookies = require("../utils/functions/parseCookies");
|
||||
const getAuthCookieNames = require("../package-shared/functions/backend/cookies/get-auth-cookie-names");
|
||||
const {
|
||||
checkAuthFile,
|
||||
} = require("../package-shared/functions/backend/auth/write-auth-files");
|
||||
|
||||
const minuteInMilliseconds = 60000;
|
||||
const hourInMilliseconds = minuteInMilliseconds * 60;
|
||||
const dayInMilliseconds = hourInMilliseconds * 24;
|
||||
const weekInMilliseconds = dayInMilliseconds * 7;
|
||||
const monthInMilliseconds = dayInMilliseconds * 30;
|
||||
const yearInMilliseconds = dayInMilliseconds * 365;
|
||||
|
||||
/**
|
||||
* Authenticate User from request
|
||||
@@ -12,37 +22,48 @@ const getAuthCookieNames = require("../package-shared/functions/backend/cookies/
|
||||
* with the user's data
|
||||
*
|
||||
* @param {Object} params - Arg
|
||||
* @param {http.IncomingMessage} params.request - Http request object
|
||||
* @param {string} params.encryptionKey - Encryption Key
|
||||
* @param {string} params.encryptionSalt - Encryption Salt
|
||||
* @param {http.IncomingMessage & Object<string, any>} [params.request] - Http request object
|
||||
* @param {http.IncomingMessage & Object<string, any>} [params.req] - Http request object
|
||||
* @param {string} [params.encryptedUserString] - Encrypted user string to use instead of getting from cookie header
|
||||
* @param {string} [params.encryptionKey] - Encryption Key: alt env: DSQL_ENCRYPTION_PASSWORD
|
||||
* @param {string} [params.encryptionSalt] - Encryption Salt: alt env: DSQL_ENCRYPTION_SALT
|
||||
* @param {("deep" | "normal")} [params.level] - Optional. "Deep" value indicates an extra layer of security
|
||||
* @param {string} params.database - Database Name
|
||||
* @param {string} [params.token] - access token to use instead of getting from cookie header
|
||||
* @param {string} [params.database] - Database Name (slug)
|
||||
* @param {string | number} [params.dsqlUserId] - alt env: DSQL_API_USER_ID
|
||||
* @param {number} [params.expiry] - Expiry time in milliseconds
|
||||
*
|
||||
* @returns { import("../package-shared/types").AuthenticatedUser }
|
||||
*/
|
||||
function userAuth({
|
||||
request,
|
||||
req,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
level,
|
||||
database,
|
||||
token,
|
||||
dsqlUserId,
|
||||
encryptedUserString,
|
||||
expiry = weekInMilliseconds,
|
||||
}) {
|
||||
try {
|
||||
/**
|
||||
* Grab the payload
|
||||
*
|
||||
* @description Grab the payload
|
||||
*/
|
||||
const cookies = parseCookies({ request });
|
||||
const finalEncryptionKey =
|
||||
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
|
||||
const finalEncryptionSalt =
|
||||
encryptionSalt || process.env.DSQL_ENCRYPTION_SALT;
|
||||
|
||||
const keyNames = getAuthCookieNames();
|
||||
const cookies = parseCookies({ request: request || req });
|
||||
|
||||
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;
|
||||
|
||||
const key = token ? token : cookies[authKeyName];
|
||||
const key = encryptedUserString
|
||||
? encryptedUserString
|
||||
: cookies[authKeyName];
|
||||
const csrf = cookies[csrfName];
|
||||
|
||||
/**
|
||||
@@ -50,10 +71,10 @@ function userAuth({
|
||||
*
|
||||
* @description Grab the payload
|
||||
*/
|
||||
let userPayload = decrypt({
|
||||
let userPayloadJSON = decrypt({
|
||||
encryptedString: key,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
encryptionKey: finalEncryptionKey,
|
||||
encryptionSalt: finalEncryptionSalt,
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -61,7 +82,7 @@ function userAuth({
|
||||
*
|
||||
* @description Grab the payload
|
||||
*/
|
||||
if (!userPayload) {
|
||||
if (!userPayloadJSON) {
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
@@ -74,7 +95,9 @@ function userAuth({
|
||||
*
|
||||
* @description Grab the payload
|
||||
*/
|
||||
let userObject = JSON.parse(userPayload);
|
||||
|
||||
/** @type {import("../package-shared/types").DATASQUIREL_LoggedInUser} */
|
||||
let userObject = JSON.parse(userPayloadJSON);
|
||||
|
||||
if (!userObject.csrf_k) {
|
||||
return {
|
||||
@@ -84,6 +107,14 @@ function userAuth({
|
||||
};
|
||||
}
|
||||
|
||||
if (!checkAuthFile(userObject.csrf_k)) {
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
msg: "Auth file doesn't exist",
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Grab the payload
|
||||
*
|
||||
@@ -100,6 +131,33 @@ function userAuth({
|
||||
};
|
||||
}
|
||||
|
||||
const payloadCreationDate = Number(userObject.date);
|
||||
|
||||
if (
|
||||
Number.isNaN(payloadCreationDate) ||
|
||||
typeof payloadCreationDate !== "number"
|
||||
) {
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
msg: "Payload Creation Date is not a number",
|
||||
};
|
||||
}
|
||||
|
||||
const timeElapsed = Date.now() - payloadCreationDate;
|
||||
|
||||
const finalExpiry = process.env.DSQL_SESSION_EXPIRY_TIME
|
||||
? Number(process.env.DSQL_SESSION_EXPIRY_TIME)
|
||||
: expiry;
|
||||
|
||||
if (timeElapsed > finalExpiry) {
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
msg: "Session has expired",
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Return User Object
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user