dsql-admin/dsql-app/.local_dist/server/pages/api/addApiKey.js

16 lines
5.3 KiB
JavaScript
Raw Normal View History

2024-12-09 12:27:08 +00:00
"use strict";(()=>{var e={};e.id=1385,e.ids=[1385],e.modules={47795:e=>{e.exports=require("generate-password")},27825:e=>{e.exports=require("lodash")},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")},79646:e=>{e.exports=require("child_process")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},81630:e=>{e.exports=require("http")},33873:e=>{e.exports=require("path")},87256:(e,r,t)=>{t.r(r),t.d(r,{config:()=>h,default:()=>E,routeModule:()=>v});var s={};t.r(s),t.d(s,{default:()=>y});var n=t(89947),i=t(2706),a=t(96762),o=t(20503),l=t.n(o),u=t(48225),c=t.n(u),d=t(6420),p=t.n(d),_=t(47670),f=t.n(_);let m=t(29021);t(33873);let{execSync:S}=t(79646),g=t(47795);async function y(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let t=await p()(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let{api_key_name:s,api_key_slug:n,api_key_scope:i,target_database:a,target_table:o}=e.body;if(!s?.match(/./)||!n?.match(/./)||!i?.match(/./))return r.json({success:!1,msg:"Wrong Input"});try{let e=process.env.DSQL_API_KEYS_PATH;if(!e)throw Error("process.env.DSQL_API_KEYS_PATH variable not found");m.existsSync(e)||m.mkdirSync(e,{recursive:!0});let u=g.generate({length:24,numbers:!0,uppercase:!0,lowercase:!0,symbols:"-"})+"-"+Date.now(),c={user_id:t.id,date_code:Date.now(),sign:u,target_database:a,target_table:o};"fullAccess"==i&&(c={...c,full_access:!0});let d=l()({data:JSON.stringify(c)});if(!d)return r.json({success:!1,msg:"Couldn't encrypt API key"});let p=l()({data:d}),_={dbFullName:"datasquirel",tableName:"api_keys",data:{user_id:t.id,name:s,slug:n,key:p,scope:i}};u&&(_.data.csrf=u);let S=await f()(_);if(!S.insertId)throw Error(S?.error||"Api Key Couldn't be added");m.writeFileSync(`${e}/${u}`,JSON.stringify(c),"utf-8"),r.json({success:!0})}catch(e){c()({component:"/api/addApiKey/catch-error",message:e.message,user:t}),r.json({success:!1,msg:"Database Files could not be written!"})}}let E=(0,a.M)(s,"default"),h=(0,a.M)(s,"config"),v=new n.PagesAPIRouteModule({definition:{kind:i.A.PAGES_API,page:"/api/addApiKey",pathname:"/api/addApiKey",bundlePath:"",filename:""},userland:s})},6420:(e,r,t)=>{t(81630);let s=t(51348),n=t(72835),i=t(29021),a=t(31341);async function o(e,r,t,o){let{keyCookieName:l,csrfCookieName:u}=a();if(!e.cookies?.[l]?.match(/./))return null;let c=n({encryptedString:e.cookies[l]});if(!c)return null;let d=JSON.parse(c);if(!d.csrf_k||t&&!e.headers["x-csrf-auth"]?.match(RegExp(`${d.csrf_k}`)))return null;let p=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!p)return console.log("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable."),null;if(t&&!i.existsSync(`${p}/${d.csrf_k}`))return null;if(0==d.verification_status&&!t){let e=await s(`SELECT verification_status FROM users WHERE id='${d.id}'`);e&&e[0]&&1==e[0].verification_status&&r.setHeader("Set-Cookie",["user_refresh=1"])}return d?.date&&Date.now()-d.date>6048e5?null:d}e.exports=o},31341:e=>{e.exports=function(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",n=e?.database||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),i=r;e?.userId&&(i+=`user_${e.userId}_`),n&&(i+=`${n}_`),i+=t;let a=r;return e?.userId&&(a+=`user_${e.userId}_`),n&&(a+=`${n}_`),{keyCookieName:i,csrfCookieName:a+=s}}},48225:(e,r,t)=>{let s=t(29021),{IncomingMessage:n}=t(81630);e.exports=async function({user:e,message:r,component:t,noMail:n,req:i}){let a=new Date,o=(()=>{if(!i)return null;try{let e=i.headers["x-forwarded-for"],r=i.headers["x-real-ip"],t=i.headers["cf-connecting-ip"],s=Array.isArray(e)?e[0]:e?.split(",")[0],n=t||s||r||i.socket.remoteAddress;if(!n)return null;return String(n)}catch(e){return null}})();try{let n=`🚀 SERVER ERROR ===========================
2024-12-08 19:56:57 +00:00
Error Message: ${r}
2024-12-09 12:27:08 +00:00
Component: ${t}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(n+=`
2024-12-08 19:56:57 +00:00
User Id: ${e?.id}
User Name: ${e?.first_name} ${e?.last_name}
2024-12-09 12:27:08 +00:00
User Email: ${e?.email}`),i?.url&&(n+=`
URL: ${i.url}`),i?.body&&(n+=`
Request Body: ${JSON.stringify(i.body,null,4)}`),o&&(n+=`
IP: ${o}`),n+=`
Date: ${a.toDateString()}
========================================`,s.existsSync("./.tmp/error.log")||s.writeFileSync("./.tmp/error.log","","utf-8");let l=s.readFileSync("./.tmp/error.log","utf-8");s.writeFileSync("./.tmp/error.log",n),s.appendFileSync("./.tmp/error.log",`
2024-12-08 19:56:57 +00:00
2024-12-09 12:27:08 +00:00
${l}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},72835:(e,r,t)=>{let{scryptSync:s,createDecipheriv:n}=t(55511),{Buffer:i}=t(79428);e.exports=({encryptedString:e,encryptionKey:r,encryptionSalt:t})=>{if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let a=r||process.env.DSQL_ENCRYPTION_PASSWORD,o=t||process.env.DSQL_ENCRYPTION_SALT,l=process.env.DSQL_ENCRYPTION_KEY_LENGTH?Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH):24;if(!a?.match(/.{8,}/))return console.log("Decrption key is invalid"),e;if(!o?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let u=n("aes-192-cbc",s(a,o,l),i.alloc(16,0));try{let r=u.update(e,"hex","utf8");return r+=u.final("utf8")}catch(r){return console.log("Error in decrypting =>",r.message),e}}}};var r=require("../../webpack-api-runtime.js");r.C(e);var t=e=>r(r.s=e),s=r.X(0,[4050],()=>t(87256));module.exports=s})();