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

16 lines
5.4 KiB
JavaScript
Raw Normal View History

2025-01-13 08:00:21 +00:00
"use strict";(()=>{var e={};e.id=1385,e.ids=[1385],e.modules={47795:e=>{e.exports=require("generate-password")},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")},15420:(e,r,t)=>{t.r(r),t.d(r,{config:()=>y,default:()=>S,routeModule:()=>E});var s={};t.r(s),t.d(s,{default:()=>m});var n=t(89947),a=t(2706),i=t(96762),o=t(29021),l=t.n(o),u=t(47795),c=t.n(u),d=t(68829),_=t(67619),p=t(69842),f=t(98e3);async function m(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let t=await (0,p.A)(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let{api_key_name:s,api_key_slug:n,api_key_scope:a,target_database:i,target_table:o}=e.body;if(!s?.match(/./)||!n?.match(/./)||!a?.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");l().existsSync(e)||l().mkdirSync(e,{recursive:!0});let u=c().generate({length:24,numbers:!0,uppercase:!0,lowercase:!0,symbols:"-"})+"-"+Date.now(),_={user_id:t.id,date_code:Date.now(),sign:u,target_database:i,target_table:o};"fullAccess"==a&&(_={..._,full_access:!0});let p=(0,d.default)({data:JSON.stringify(_)});if(!p)return r.json({success:!1,msg:"Couldn't encrypt API key"});let m=(0,d.default)({data:p}),S={dbFullName:"datasquirel",tableName:"api_keys",data:{user_id:t.id,name:s,slug:n,key:m,scope:a}};u&&(S.data.csrf=u);let y=await (0,f.A)(S);if(!y.insertId)throw Error(y?.error||"Api Key Couldn't be added");l().writeFileSync(`${e}/${u}`,JSON.stringify(_),"utf-8"),r.json({success:!0})}catch(e){(0,_.default)({component:"/api/addApiKey/catch-error",message:e.message,user:t}),r.json({success:!1,msg:"Database Files could not be written!"})}}let S=(0,i.M)(s,"default"),y=(0,i.M)(s,"config"),E=new n.PagesAPIRouteModule({definition:{kind:a.A.PAGES_API,page:"/api/addApiKey",pathname:"/api/addApiKey",bundlePath:"",filename:""},userland:s})},69842:(e,r,t)=>{t.d(r,{A:()=>l});var s=t(48786),n=t(95969),a=t(29021),i=t.n(a),o=t(89159);async function l(e,r,t){let{keyCookieName:a,csrfCookieName:l}=(0,o.A)();if(!e.cookies?.[a]?.match(/./))return null;let u=(0,n.A)({encryptedString:e.cookies[a]});if(!u)return null;let c=JSON.parse(u);if(!c.csrf_k||t&&!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(t&&!i().existsSync(`${d}/${c.csrf_k}`))return null;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"])}return c?.date&&Date.now()-c.date>6048e5?null:c}},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",n=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+_/,""),i=r;e?.userId&&(i+=`user_${e.userId}_`),a&&(i+=`${a}_`),i+=t;let o=r;e?.userId&&(o+=`user_${e.userId}_`),a&&(o+=`${a}_`),o+=s;let l=r;return e?.userId&&(l+=`user_${e.userId}_`),a&&(l+=`${a}_`),{keyCookieName:i,csrfCookieName:o,oneTimeCodeName:l+=n}}},67619:(e,r,t)=>{t.r(r),t.d(r,{default:()=>a});var s=t(29021),n=t.n(s);async function a({user:e,message:r,component:t,noMail:s,req:a}){let i=new Date,o=(()=>{if(!a)return null;try{let e=a.headers["x-forwarded-for"],r=a.headers["x-real-ip"],t=a.headers["cf-connecting-ip"],s=Array.isArray(e)?e[0]:e?.split(",")[0],n=t||s||r||a.socket.remoteAddress;if(!n)return null;return String(n)}catch(e){return null}})();try{let s=`🚀 SERVER ERROR ===========================
2024-12-08 19:56:57 +00:00
Error Message: ${r}
2025-01-13 08:00:21 +00:00
Component: ${t}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(s+=`
2024-12-08 19:56:57 +00:00
User Id: ${e?.id}
User Name: ${e?.first_name} ${e?.last_name}
2025-01-13 08:00:21 +00:00
User Email: ${e?.email}`),a?.url&&(s+=`
URL: ${a.url}`),a?.body&&(s+=`
Request Body: ${JSON.stringify(a.body,null,4)}`),o&&(s+=`
IP: ${o}`),s+=`
2024-12-15 11:27:16 +00:00
Date: ${i.toDateString()}
2025-01-13 08:00:21 +00:00
========================================`,n().existsSync("./.tmp/error.log")||n().writeFileSync("./.tmp/error.log","","utf-8");let l=n().readFileSync("./.tmp/error.log","utf-8");n().writeFileSync("./.tmp/error.log",s),n().appendFileSync("./.tmp/error.log",`
2024-12-08 19:56:57 +00:00
2025-01-13 08:00:21 +00:00
${l}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},95969:(e,r,t)=>{t.d(r,{A:()=>a});var s=t(55511),n=t(79428);function a({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,i=t||process.env.DSQL_ENCRYPTION_SALT,o=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(!i?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let l=(0,s.scryptSync)(a,i,o),u=n.Buffer.alloc(16,0),c=(0,s.createDecipheriv)("aes-192-cbc",l,u);try{let r=c.update(e,"hex","utf8");return r+=c.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,[1251],()=>t(15420));module.exports=s})();