dsql-admin/dsql-app/.local_dist/server/chunks/9971.js

231 lines
8.8 KiB
JavaScript
Raw Normal View History

2024-11-05 11:12:42 +00:00
exports.id = 9971;
exports.ids = [9971];
exports.modules = {
/***/ 6069:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
"use strict";
// @ts-check
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
const fs = __webpack_require__(7147);
const { hashPassword , } = __webpack_require__(5382);
const serverError = __webpack_require__(2163);
const httpsRequest = __webpack_require__(722);
const DB_HANDLER = __webpack_require__(2224);
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
/**
*
* @typedef {object} GithubUserPayload
* @property {string} login - Full name merged eg. "JohnDoe"
* @property {number} id - github user id
* @property {string} node_id - Some other id
* @property {string} avatar_url - profile picture
* @property {string} gravatar_id - some other id
* @property {string} url - Github user URL
* @property {string} html_url - User html URL - whatever that means
* @property {string} followers_url - Followers URL
* @property {string} following_url - Following URL
* @property {string} gists_url - Gists URL
* @property {string} starred_url - Starred URL
* @property {string} subscriptions_url - Subscriptions URL
* @property {string} organizations_url - Organizations URL
* @property {string} repos_url - Repositories URL
* @property {string} received_events_url - Received Events URL
* @property {string} type - Common value => "User"
* @property {boolean} site_admin - Is site admin or not? Boolean
* @property {string} name - More like "username"
* @property {string} company - User company
* @property {string} blog - User blog URL
* @property {string} location - User Location
* @property {string} email - User Email
* @property {string} hireable - Is user hireable
* @property {string} bio - User bio
* @property {string} twitter_username - User twitter username
* @property {number} public_repos - Number of public repositories
* @property {number} public_gists - Number of public gists
* @property {number} followers - Number of followers
* @property {number} following - Number of following
* @property {string} created_at - Date created
* @property {string} updated_at - Date updated
*/ /**
* Login/signup a github user
* ==============================================================================
* @async
*
* @param {Object} params - foundUser if any
* @param {string} params.code - github auth token
* @param {string} params.clientId - github client Id
* @param {string} params.clientSecret - github client Secret
*
* @returns {Promise<GithubUserPayload|null|undefined>}
*/ module.exports = async function githubLogin({ code , clientId , clientSecret }) {
/** @type {GithubUserPayload | undefined} */ let gitHubUser;
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
try {
/**
* Create new user folder and file
*
* @description Create new user folder and file
*/ // const response = await fetch(`https://github.com/login/oauth/access_token?client_id=${process.env.DSQL_GITHUB_ID}`);
const response = await httpsRequest({
method: "POST",
hostname: "github.com",
path: `/login/oauth/access_token?client_id=${clientId}&client_secret=${clientSecret}&code=${code}`,
headers: {
Accept: "application/json",
"User-Agent": "*"
},
scheme: "https"
});
// `https://github.com/login/oauth/access_token?client_id=${process.env.DSQL_GITHUB_ID}&client_secret=${process.env.DSQL_GITHUB_SECRET}&code=${code}`,
// body: JSON.stringify({
// client_id: process.env.DSQL_GITHUB_ID,
// client_secret: process.env.DSQL_GITHUB_SECRET,
// code: code,
// }),
const accessTokenObject = JSON.parse(response);
if (!accessTokenObject?.access_token) {
return gitHubUser;
}
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
const userDataResponse = await httpsRequest({
method: "GET",
hostname: "api.github.com",
path: "/user",
headers: {
Authorization: `Bearer ${accessTokenObject.access_token}`,
"User-Agent": "*"
},
scheme: "https"
});
gitHubUser = JSON.parse(userDataResponse);
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
if (!gitHubUser?.email && gitHubUser) {
const existingGithubUser = await DB_HANDLER(`SELECT email FROM users WHERE social_login='1' AND social_platform='github' AND social_id='${gitHubUser.id}'`);
if (existingGithubUser && existingGithubUser[0]) {
gitHubUser.email = existingGithubUser[0].email;
}
}
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
} catch (/** @type {any} */ error) {
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
console.log("ERROR in githubLogin.js backend function =>", error.message);
// serverError({
// component: "/api/social-login/github-auth/catch-error",
// message: error.message,
// user: user,
// });
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
}
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
return gitHubUser;
};
/***/ }),
/***/ 5382:
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {
// @ts-check
const { createHmac } = __webpack_require__(6113);
//
/**
* # Password Hash function
* @param {string} password
* @returns
*/ function hashPassword(password) {
const hmac = createHmac("sha512", process.env.DSQL_ENCRYPTION_PASSWORD || "");
hmac.update(password);
let hashed = hmac.digest("base64");
return hashed;
}
exports.hashPassword = hashPassword; // export const comparePasswords = async (password) => {
// const hmac = createHmac("sha512", process.env.DSQL_ENCRYPTION_PASSWORD);
// hmac.update(password);
// let hashed = hmac.digest("base64");
// let dbPass = await global.DB_HANDLER(`SELECT * FROM users WHERE password = '${hashed}'`);
// console.log(dbPass);
// return dbPass;
// };
/***/ }),
/***/ 2127:
/***/ ((module) => {
"use strict";
// @ts-check
/**
* Convert Camel Joined Text to Camel Spaced Text
* ==============================================================================
* @description this function takes a camel cased text without spaces, and returns
* a camel-case-spaced text
*
* @param {string} text - text string without spaces
*
* @returns {string | null}
*/
module.exports = function camelJoinedtoCamelSpace(text) {
if (!text?.match(/./)) {
return "";
}
if (text?.match(/ /)) {
return text;
}
if (text) {
let textArray = text.split("");
let capIndexes = [];
for(let i = 0; i < textArray.length; i++){
const char = textArray[i];
if (i === 0) continue;
if (char.match(/[A-Z]/)) {
capIndexes.push(i);
}
}
let textChunks = [
`${textArray[0].toUpperCase()}${text.substring(1, capIndexes[0])}`,
];
for(let j = 0; j < capIndexes.length; j++){
const capIndex = capIndexes[j];
if (capIndex === 0) continue;
const startIndex = capIndex + 1;
const endIndex = capIndexes[j + 1];
textChunks.push(`${textArray[capIndex].toUpperCase()}${text.substring(startIndex, endIndex)}`);
}
return textChunks.join(" ");
} else {
return null;
}
};
/***/ })
};
;