dsql-admin/dsql-app/.local_dist/server/pages/api/connect/handshake.js
Benjamin Toby e0a030f10d Updates
2025-01-13 09:00:21 +01:00

1 line
8.7 KiB
JavaScript

"use strict";(()=>{var e={};e.id=4065,e.ids=[4065],e.modules={75600:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},72583:e=>{e.exports=require("sanitize-html")},9973:e=>{e.exports=require("serverless-mysql")},79428:e=>{e.exports=require("buffer")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},33873:e=>{e.exports=require("path")},79551:e=>{e.exports=require("url")},28354:e=>{e.exports=require("util")},96762:(e,t)=>{Object.defineProperty(t,"M",{enumerable:!0,get:function(){return function e(t,r){return r in t?t[r]:"then"in t&&"function"==typeof t.then?t.then(t=>e(t,r)):"function"==typeof t&&"default"===r?t:void 0}}})},63177:(e,t,r)=>{r.r(t),r.d(t,{config:()=>y,default:()=>E,routeModule:()=>D});var n={};r.r(n),r.d(n,{default:()=>h});var s=r(89947),a=r(2706),o=r(96762),i=r(76856),l=r(29021),u=r.n(l),c=r(33873),d=r.n(c);let _=async({userId:e,dbSchema:t,targetDb:r})=>(await (0,i.A)({userId:e,targetDatabase:r}),`User ${e} Db Schema Updated`),f=async({userId:e,dbSchema:t,database:r})=>{let n=d().resolve(String(process.env.DSQL_USER_DB_SCHEMA_PATH),`user-${e}`,"main.json"),s=JSON.parse(u().readFileSync(n,"utf-8")),a=s.findIndex(e=>e.dbFullName==r.db_full_name);if(a<0)throw Error("Target Database not found in user schema data");let o={...s[a]};return s[a]=t,s[a].dbName=o.dbName,s[a].dbFullName=o.dbFullName,s[a].dbSlug=o.dbSlug,s[a].dbImage=o.dbImage,await _({dbSchema:s,userId:e,targetDb:r.db_full_name})};var p=r(67619),S=r(69842),g=r(30802);r(29021),r(28354);let{URL:m}=r(79551);async function h(e,t){if("POST"!==e.method)return t.json({msg:"Failed!"});let r=await (0,S.A)(e,t,!0);if(!r)return t.json({success:!1,msg:"Unauthorized"});let{url:n,key:s,database:a,dbSchema:o,type:i,remoteDbs:l,targetDb:u}=e.body;try{if(!u?.tables)throw Error("No Target Database");await (0,g.A)({tableName:"user_databases",data:{remote_connected:1,remote_connection_host:n,remote_connection_key:s,remote_connection_type:i,remote_db_full_name:u.dbFullName},identifierColumnName:"db_full_name",identifierValue:a.db_full_name}),await f({dbSchema:u,database:a,userId:r.id}),t.json({success:!0})}catch(e){console.log("Connect Handshake Error, =>",e.message),(0,p.default)({component:"/api/connect/verify/main-catch-error",message:e.message,user:r}),t.json({success:!1,msg:"Operation Failed!"})}}let E=(0,o.M)(n,"default"),y=(0,o.M)(n,"config"),D=new s.PagesAPIRouteModule({definition:{kind:a.A.PAGES_API,page:"/api/connect/handshake",pathname:"/api/connect/handshake",bundlePath:"",filename:""},userland:n})},69842:(e,t,r)=>{r.d(t,{A:()=>l});var n=r(48786),s=r(95969),a=r(29021),o=r.n(a),i=r(89159);async function l(e,t,r){let{keyCookieName:a,csrfCookieName:l}=(0,i.A)();if(!e.cookies?.[a]?.match(/./))return null;let u=(0,s.A)({encryptedString:e.cookies[a]});if(!u)return null;let c=JSON.parse(u);if(!c.csrf_k||r&&!Object.keys(e.headers).find(e=>e.includes(c.csrf_k)))return null;let d=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!d)return console.log("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable."),null;if(r&&!o().existsSync(`${d}/${c.csrf_k}`))return null;if(0==c.verification_status&&!r){let e=await (0,n.A)(`SELECT verification_status FROM users WHERE id='${c.id}'`);e&&e[0]&&1==e[0].verification_status&&t.setHeader("Set-Cookie",["user_refresh=1"])}return c?.date&&Date.now()-c.date>6048e5?null:c}},89159:(e,t,r)=>{r.d(t,{A:()=>n});function n(e){let t=process.env.DSQL_COOKIES_PREFIX||"dsql_",r=process.env.DSQL_COOKIES_KEY_NAME||"key",n=process.env.DSQL_COOKIES_CSRF_NAME||"csrf",s=process.env.DSQL_COOKIES_ONE_TIME_CODE_NAME||"one-time-code",a=e?.database?.replace(/^datasquirel_user_\d+_/,"")||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),o=t;e?.userId&&(o+=`user_${e.userId}_`),a&&(o+=`${a}_`),o+=r;let i=t;e?.userId&&(i+=`user_${e.userId}_`),a&&(i+=`${a}_`),i+=n;let l=t;return e?.userId&&(l+=`user_${e.userId}_`),a&&(l+=`${a}_`),{keyCookieName:o,csrfCookieName:i,oneTimeCodeName:l+=s}}},30802:(e,t,r)=>{r.d(t,{A:()=>c});var n=r(72583),s=r.n(n),a=r(92652),o=r(48786),i=r(37275),l=r(68829),u=r(7971);async function c({dbContext:e,paradigm:t,dbFullName:r,tableName:n,data:c,tableSchema:d,identifierColumnName:_,identifierValue:f,encryptionKey:p,encryptionSalt:S,useLocal:g}){if(!c||!Object.keys(c).length)return null;let m=!!g||!e?.match(/dsql.user/i)&&(!r||!!r.match(/^datasquirel$/)),h=g?u.A:m?o.A:i.default,E=Object.keys(c),y=[],D=[];for(let e=0;e<E.length;e++)try{let t=E[e],r=c[t],n=d?d?.fields?.filter(e=>e.fieldName===t):null,o=n&&n[0]?n[0]:null;if(null==r||void 0==r)continue;let i=/<[^>]+>/g;(o?.richText||String(r).match(i))&&(r=s()(r,a.A)),o?.encrypted&&(r=(0,l.default)({data:r,encryptionKey:p,encryptionSalt:S})),"object"==typeof r&&(r=JSON.stringify(r)),o?.pattern&&!new RegExp(o.pattern,o.patternFlags||"").test(r)&&(console.log("DSQL: Pattern not matched =>",r),r=""),"string"==typeof r&&r.match(/^null$/i)&&(r={toSqlString:function(){return"NULL"}}),"string"!=typeof r||r.match(/./i)||(r={toSqlString:function(){return"NULL"}}),y.push(`\`${t}\`=?`),"number"==typeof r?D.push(String(r)):D.push(r)}catch(e){console.log("DSQL: Error in parsing data keys in update function =>",e.message);continue}y.push(`date_updated='${Date()}'`),y.push(`date_updated_code='${Date.now()}'`);let b=`UPDATE \`${r}\`.\`${n}\` SET ${y.join(",")} WHERE \`${_}\`=?`;return D.push(f),m?await h(b,D):await h({paradigm:t,queryString:b,queryValues:D})}},92652:(e,t,r)=>{r.d(t,{A:()=>n});let n={allowedTags:["b","i","em","strong","a","p","span","ul","ol","li","h1","h2","h3","h4","h5","h6","img","div","button","pre","code","br"],allowedAttributes:{a:["href"],img:["src","alt","width","height","class","style"],"*":["style","class"]}}},68829:(e,t,r)=>{r.r(t),r.d(t,{default:()=>a});var n=r(55511),s=r(79428);function a({data:e,encryptionKey:t,encryptionSalt:r}){if(!e?.match(/./))return console.log("Encryption string is invalid"),e;let a=t||process.env.DSQL_ENCRYPTION_PASSWORD,o=r||process.env.DSQL_ENCRYPTION_SALT,i=process.env.DSQL_ENCRYPTION_KEY_LENGTH?Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH):24;if(!a?.match(/.{8,}/))return console.log("Encryption key is invalid"),e;if(!o?.match(/.{8,}/))return console.log("Encryption salt is invalid"),e;let l=(0,n.scryptSync)(a,o,i),u=s.Buffer.alloc(16,0),c=(0,n.createCipheriv)("aes-192-cbc",l,u);try{let t=c.update(e,"utf8","hex");return t+=c.final("hex")}catch(t){return console.log("Error in encrypting =>",t.message),e}}},48786:(e,t,r)=>{r.d(t,{A:()=>n});async function n(...e){let t=global.DSQL_DB_CONN;try{if(!t)throw Error("No Connection provided to DB_HANDLER function!");let r=await t.query(...e);return JSON.parse(JSON.stringify(r))}catch(e){return console.log("DB Error =>",e),{success:!1,error:e.message}}finally{await t?.end()}}},37275:(e,t,r)=>{async function n(e,t,r){try{if("string"==typeof t){let n=await e.query(s(t),r);return JSON.parse(JSON.stringify(n))}if("object"!=typeof t)return null;{let r=[];for(let n=0;n<t.length;n++)try{let a=t[n],o=await e.query(s(a.query),a.values);r.push(JSON.parse(JSON.stringify(o)))}catch(e){r.push(null)}return r}}catch(e){return null}finally{e.end()}}function s(e){return e.replace(/\n/gm,"").replace(/ {2,}/g,"").trim()}async function a({paradigm:e,queryString:t,queryValues:r}){try{switch(e){case"Read Only":return await n(global.DSQL_READ_ONLY_DB_CONN,t,r);case"Full Access":case"FA":return await n(global.DSQL_FULL_ACCESS_DB_CONN,t,r);default:return null}}catch(e){return console.log(`DSQL_USER_DB_HANDLER Error: ${e.message}`),null}}r.r(t),r.d(t,{default:()=>a})},7971:(e,t,r)=>{r.d(t,{A:()=>i});var n=r(9973),s=r.n(n),a=r(29021),o=r.n(a);async function i(...e){let t=s()({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_USERNAME,password:process.env.DSQL_DB_PASSWORD,database:process.env.DSQL_DB_NAME,port:process.env.DSQL_DB_PORT?Number(process.env.DSQL_DB_PORT):void 0,charset:"utf8mb4",ssl:function(){let e=process.env.DSQL_SSL_DIR;if(!e?.match(/./))return;let t=`${e}/ca-cert.pem`;if(!o().existsSync(t)){console.log(`${t} does not exist`);return}return{ca:o().readFileSync(`${e}/ca-cert.pem`),rejectUnauthorized:!1}}()},onConnect:()=>{console.log("Connection Successful!")},onConnectError:e=>{console.log("Connection Error",e.message)},onError:e=>{console.log("Client Error",e.message)}});console.log("Querying ...");try{let r=await t.query(...e);return await t.end(),JSON.parse(JSON.stringify(r))}catch(e){return console.log("DB Error =>",e.message),{success:!1,error:e.message}}}},2706:(e,t)=>{Object.defineProperty(t,"A",{enumerable:!0,get:function(){return r}});var r=function(e){return e.PAGES="PAGES",e.PAGES_API="PAGES_API",e.APP_PAGE="APP_PAGE",e.APP_ROUTE="APP_ROUTE",e.IMAGE="IMAGE",e}({})},89947:(e,t,r)=>{e.exports=r(75600)}};var t=require("../../../webpack-api-runtime.js");t.C(e);var r=e=>t(t.s=e),n=t.X(0,[2282],()=>r(63177));module.exports=n})();