1 line
11 KiB
JavaScript
1 line
11 KiB
JavaScript
"use strict";(()=>{var e={};e.id=4371,e.ids=[4371],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")},96762:(e,r)=>{Object.defineProperty(r,"M",{enumerable:!0,get:function(){return function e(r,t){return t in r?r[t]:"then"in r&&"function"==typeof r.then?r.then(r=>e(r,t)):"function"==typeof r&&"default"===t?r:void 0}}})},81147:(e,r,t)=>{t.r(r),t.d(r,{config:()=>h,default:()=>S,routeModule:()=>b});var s={};t.r(s),t.d(s,{default:()=>E});var a=t(89947),n=t(2706),o=t(96762),i=t(29021),l=t.n(i),u=t(48786),c=t(31495),d=t(60125),_=t(67619),f=t(69842),p=t(30802),g=t(76856);async function E(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let t=await (0,f.A)(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let{dbFullName:s,dbSlug:a,dbId:n,tableNameOld:o,payload:i,delegated:E}=e.body,{tableName:S,tableFullName:h,tableDescription:b}=i;if(!S?.match(/./)||S.match(/[^a-zA-Z0-9-_]/))return r.json({success:!1,msg:"Bad Input"});try{let i=await (0,d.A)({databaseSlug:a,request:e,user:t,query:e.query});if(!await (0,c.A)({user:t,confirmedDelegetedUser:i,database:a,table:o,dbId:n,priviledgeRegex:/Edit Table/i}))return console.log("User Unauthorized"),r.json({success:!1,msg:"Unauthorized"});let f=i?.rootUserId?i.rootUserId:t.id,m=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${f}/main.json`,A=JSON.parse(l().readFileSync(m,"utf8")),y=A.filter(e=>e.dbFullName===s)[0].tables.filter((e,r)=>{if(e.tableName===o)return!0});if(y&&y[0])y[0].tableName=S,y[0].tableNameOld=o,y[0].tableFullName=h,b&&(y[0].tableDescription=b),y[0].childrenTables&&y[0].childrenTables.forEach(e=>{try{A.filter(r=>r.dbFullName===e.dbNameFull)[0].tables.filter(r=>{if(r.tableName===e.tableName)return!0})[0].childTableName=S}catch(e){(0,_.A)({component:"/api/updateUserTable/lines-131-138",message:e.message,user:t})}});else throw Error("Table Does not exist");l().writeFileSync(`${m}`,JSON.stringify(A),"utf8"),await (0,p.A)({dbFullName:"datasquirel",tableName:"user_database_tables",identifierColumnName:"table_slug",identifierValue:o,data:{table_name:h,table_slug:S,table_description:b}}),E&&i&&await (0,u.A)("UPDATE delegated_user_tables SET `table`=? WHERE delegated_user_id=? AND root_user_id=? AND `table`=? AND `database`=?",[S,t.id,i.rootUserId,o,a]);let N=s.replace(/[^a-z\_0-9]/g,"");await (0,g.A)({userId:f,targetDatabase:N}),r.json({success:!0})}catch(e){(0,_.A)({component:"/api/updateUserTable/main-catch-error",message:e.message,user:t}),r.json({success:!1,msg:"Database Files could not be written!"})}}let S=(0,o.M)(s,"default"),h=(0,o.M)(s,"config"),b=new a.PagesAPIRouteModule({definition:{kind:n.A.PAGES_API,page:"/api/updateUserTable",pathname:"/api/updateUserTable",bundlePath:"",filename:""},userland:s})},31495:(e,r,t)=>{t.d(r,{A:()=>n});var s=t(48786),a=t(67619);async function n({user:e,confirmedDelegetedUser:r,database:t,table:n,priviledgeRegex:o,dbId:i}){let l,u;try{r?.delegated?(l=await (0,s.A)("SELECT priviledge FROM delegated_user_tables WHERE root_user_id=? AND delegated_user_id=? AND `database`=? AND `table`=?",[r.rootUserId,e.id,t,n]),u=l[0]?.priviledge?.match(o)):(l=await (0,s.A)("SELECT table_slug FROM user_database_tables WHERE user_id=? AND table_slug=? AND db_id=?",[e.id,n,i]),u=!0)}catch(r){(0,a.A)({component:"checkUserRights",message:r.message,user:e})}return!!l&&!!l[0]&&!!u}},60125:(e,r,t)=>{t.d(r,{A:()=>a});var s=t(95969);async function a({request:e,databaseSlug:r,user:t,query:a}){try{let n=t.id,o=null;if(!a?.delegated)return{dbUserId:n};let i=a.dbUserId,l=`${process.env.DSQL_USER_DB_PREFIX}${i}_${r}`,u=`${process.env.DSQL_USER_DELEGATED_DB_COOKIE_PREFIX}${l}`;try{if(!e.cookies?.[u])throw Error("Cookie not present");let t=(0,s.A)({encryptedString:e.cookies[u]});if(!t)throw Error("Invalid Token");if((o=JSON.parse(t)).databaseSlug===r)return{dbUserId:n=o.rootUserId,rootUserId:o.rootUserId,rootUserName:o.rootUserName,rootUserEmail:o.rootUserEmail,rootUserImage:o.rootUserImage,databaseFullName:o.databaseFullName,databaseSlug:o.databaseSlug,allowedTables:o.allowedTables,priviledges:o.priviledges,database:o.databaseSlug,delegated:!0}}catch(e){}return{dbUserId:n}}catch(e){return null}}},69842:(e,r,t)=>{t.d(r,{A:()=>l});var s=t(48786),a=t(95969),n=t(29021),o=t.n(n),i=t(89159);async function l(e,r,t){try{let{keyCookieName:n,csrfCookieName:l}=(0,i.A)();if(!e.cookies?.[n]?.match(/./))throw Error("Request Cookies not Found!");let u=(0,a.A)({encryptedString:e.cookies[n]});if(!u)throw Error("Couldn't decrypt auth cookie!");let c=JSON.parse(u);if(!c.csrf_k)throw Error("No `csrf_k` present in user Object!");if(t&&!Object.keys(e.headers).find(e=>e.includes(c.csrf_k)))throw console.log("req.headers",e.headers),console.log("userObject",c),Error("Deep Auth Failed. `csrf` key not found in request headers!");let d=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!d)throw Error("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable.");if(t&&!o().existsSync(`${d}/${c.csrf_k}`))throw Error("Key not found in Auth Keys Folder!");if(0==c.verification_status&&!t){let e=await (0,s.A)(`SELECT verification_status FROM users WHERE id='${c.id}'`);e&&e[0]&&1==e[0].verification_status&&r.setHeader("Set-Cookie",["user_refresh=1"])}if(c?.date&&Date.now()-c.date>6048e5)throw Error("Key expired!");return c}catch(e){return console.log(`userAuth Error: ${e.message}`),null}}},89159:(e,r,t)=>{t.d(r,{A:()=>s});function s(e){let r=process.env.DSQL_COOKIES_PREFIX||"dsql_",t=process.env.DSQL_COOKIES_KEY_NAME||"key",s=process.env.DSQL_COOKIES_CSRF_NAME||"csrf",a=process.env.DSQL_COOKIES_ONE_TIME_CODE_NAME||"one-time-code",n=e?.database?.replace(/^datasquirel_user_\d+_/,"")||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),o=r;e?.userId&&(o+=`user_${e.userId}_`),n&&(o+=`${n}_`),o+=t;let i=r;e?.userId&&(i+=`user_${e.userId}_`),n&&(i+=`${n}_`),i+=s;let l=r;return e?.userId&&(l+=`user_${e.userId}_`),n&&(l+=`${n}_`),{keyCookieName:o,csrfCookieName:i,oneTimeCodeName:l+=a}}},30802:(e,r,t)=>{t.d(r,{A:()=>c});var s=t(72583),a=t.n(s),n=t(92652),o=t(48786),i=t(37275),l=t(68829),u=t(7971);async function c({dbContext:e,paradigm:r,dbFullName:t,tableName:s,data:c,tableSchema:d,identifierColumnName:_,identifierValue:f,encryptionKey:p,encryptionSalt:g,useLocal:E}){if(!c||!Object.keys(c).length)return null;let S=!!E||!e?.match(/dsql.user/i)&&(!t||!!t.match(/^datasquirel$/)),h=E?u.A:S?o.A:i.A,b=Object.keys(c),m=[],A=[];for(let e=0;e<b.length;e++)try{let r=b[e],t=c[r],s=d?d?.fields?.filter(e=>e.fieldName===r):null,o=s&&s[0]?s[0]:null;if(null==t||void 0==t)continue;let i=/<[^>]+>/g;(o?.richText||String(t).match(i))&&(t=a()(t,n.A)),o?.encrypted&&(t=(0,l.A)({data:t,encryptionKey:p,encryptionSalt:g})),"object"==typeof t&&(t=JSON.stringify(t)),o?.pattern&&!new RegExp(o.pattern,o.patternFlags||"").test(t)&&(console.log("DSQL: Pattern not matched =>",t),t=""),"string"==typeof t&&t.match(/^null$/i)&&(t={toSqlString:function(){return"NULL"}}),"string"!=typeof t||t.match(/./i)||(t={toSqlString:function(){return"NULL"}}),m.push(`\`${r}\`=?`),"number"==typeof t?A.push(String(t)):A.push(t)}catch(e){console.log("DSQL: Error in parsing data keys in update function =>",e.message);continue}m.push(`date_updated='${Date()}'`),m.push(`date_updated_code='${Date.now()}'`);let y=`UPDATE \`${t}\`.\`${s}\` SET ${m.join(",")} WHERE \`${_}\`=?`;return A.push(f),S?await h(y,A):await h({paradigm:r,queryString:y,queryValues:A})}},92652:(e,r,t)=>{t.d(r,{A:()=>s});let s={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,r,t)=>{t.d(r,{A:()=>n});var s=t(55511),a=t(79428);function n({data:e,encryptionKey:r,encryptionSalt:t}){if(!e?.match(/./))return console.log("Encryption string is invalid"),e;let n=r||process.env.DSQL_ENCRYPTION_PASSWORD,o=t||process.env.DSQL_ENCRYPTION_SALT,i=process.env.DSQL_ENCRYPTION_KEY_LENGTH?Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH):24;if(!n?.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,s.scryptSync)(n,o,i),u=a.Buffer.alloc(16,0),c=(0,s.createCipheriv)("aes-192-cbc",l,u);try{let r=c.update(e,"utf8","hex");return r+=c.final("hex")}catch(r){return console.log("Error in encrypting =>",r.message),e}}},48786:(e,r,t)=>{t.d(r,{A:()=>s});async function s(...e){let r=global.DSQL_DB_CONN;try{if(!r)throw Error("No Connection provided to DB_HANDLER function!");let t=await r.query(...e);return JSON.parse(JSON.stringify(t))}catch(e){return console.log("DB Error =>",e),{success:!1,error:e.message}}finally{await r?.end()}}},37275:(e,r,t)=>{async function s(e,r,t){try{if("string"==typeof r){let s=await e.query(a(r),t);return JSON.parse(JSON.stringify(s))}if("object"!=typeof r)return null;{let t=[];for(let s=0;s<r.length;s++)try{let n=r[s],o=await e.query(a(n.query),n.values);t.push(JSON.parse(JSON.stringify(o)))}catch(e){t.push(null)}return t}}catch(e){return null}finally{e.end()}}function a(e){return e.replace(/\n/gm,"").replace(/ {2,}/g,"").trim()}async function n({paradigm:e,queryString:r,queryValues:t}){try{switch(e){case"Read Only":return await s(global.DSQL_READ_ONLY_DB_CONN,r,t);case"Full Access":case"FA":return await s(global.DSQL_FULL_ACCESS_DB_CONN,r,t);default:return null}}catch(e){return console.log(`DSQL_USER_DB_HANDLER Error: ${e.message}`),null}}t.d(r,{A:()=>n})},7971:(e,r,t)=>{t.d(r,{A:()=>i});var s=t(9973),a=t.n(s),n=t(29021),o=t.n(n);async function i(...e){let r=a()({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 r=`${e}/ca-cert.pem`;if(!o().existsSync(r)){console.log(`${r} 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 t=await r.query(...e);return await r.end(),JSON.parse(JSON.stringify(t))}catch(e){return console.log("DB Error =>",e.message),{success:!1,error:e.message}}}},2706:(e,r)=>{Object.defineProperty(r,"A",{enumerable:!0,get:function(){return t}});var t=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,r,t)=>{e.exports=t(75600)}};var r=require("../../webpack-api-runtime.js");r.C(e);var t=e=>r(r.s=e),s=r.X(0,[2282],()=>t(81147));module.exports=s})(); |