This commit is contained in:
Benjamin Toby
2024-12-08 09:58:57 +01:00
parent 8a8257d17e
commit 7bd4b2fe65
76 changed files with 1783 additions and 1106 deletions
+2 -2
View File
@@ -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 {
+1 -1
View File
@@ -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> }
+12 -14
View File
@@ -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
View File
@@ -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;