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

16 lines
5.7 KiB
JavaScript
Raw Normal View History

2025-01-14 07:24:54 +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:()=>E,routeModule:()=>S});var s={};t.r(s),t.d(s,{default:()=>m});var o=t(89947),n=t(2706),i=t(96762),a=t(29021),c=t.n(a),l=t(47795),u=t.n(l),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:o,api_key_scope:n,target_database:i,target_table:a}=e.body;if(!s?.match(/./)||!o?.match(/./)||!n?.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");c().existsSync(e)||c().mkdirSync(e,{recursive:!0});let l=u().generate({length:24,numbers:!0,uppercase:!0,lowercase:!0,symbols:"-"})+"-"+Date.now(),_={user_id:t.id,date_code:Date.now(),sign:l,target_database:i,target_table:a};"fullAccess"==n&&(_={..._,full_access:!0});let p=(0,d.A)({data:JSON.stringify(_)});if(!p)return r.json({success:!1,msg:"Couldn't encrypt API key"});let m=(0,d.A)({data:p}),E={dbFullName:"datasquirel",tableName:"api_keys",data:{user_id:t.id,name:s,slug:o,key:m,scope:n}};l&&(E.data.csrf=l);let y=await (0,f.A)(E);if(!y.insertId)throw Error(y?.error||"Api Key Couldn't be added");c().writeFileSync(`${e}/${l}`,JSON.stringify(_),"utf-8"),r.json({success:!0})}catch(e){(0,_.A)({component:"/api/addApiKey/catch-error",message:e.message,user:t}),r.json({success:!1,msg:"Database Files could not be written!"})}}let E=(0,i.M)(s,"default"),y=(0,i.M)(s,"config"),S=new o.PagesAPIRouteModule({definition:{kind:n.A.PAGES_API,page:"/api/addApiKey",pathname:"/api/addApiKey",bundlePath:"",filename:""},userland:s})},69842:(e,r,t)=>{t.d(r,{A:()=>c});var s=t(48786),o=t(95969),n=t(29021),i=t.n(n),a=t(89159);async function c(e,r,t){try{let{keyCookieName:n,csrfCookieName:c}=(0,a.A)();if(!e.cookies?.[n]?.match(/./))throw Error("Request Cookies not Found!");let l=(0,o.A)({encryptedString:e.cookies[n]});if(!l)throw Error("Couldn't decrypt auth cookie!");let u=JSON.parse(l);if(!u.csrf_k)throw Error("No `csrf_k` present in user Object!");if(t&&!Object.keys(e.headers).find(e=>e.includes(u.csrf_k)))throw 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&&!i().existsSync(`${d}/${u.csrf_k}`))throw Error("Key not found in Auth Keys Folder!");if(0==u.verification_status&&!t){let e=await (0,s.A)(`SELECT verification_status FROM users WHERE id='${u.id}'`);e&&e[0]&&1==e[0].verification_status&&r.setHeader("Set-Cookie",["user_refresh=1"])}if(u?.date&&Date.now()-u.date>6048e5)throw Error("Key expired!");return u}catch(r){return console.log(`userAuth Error for ${e.url}: ${r.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",o=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+_/,""),i=r;e?.userId&&(i+=`user_${e.userId}_`),n&&(i+=`${n}_`),i+=t;let a=r;e?.userId&&(a+=`user_${e.userId}_`),n&&(a+=`${n}_`),a+=s;let c=r;return e?.userId&&(c+=`user_${e.userId}_`),n&&(c+=`${n}_`),{keyCookieName:i,csrfCookieName:a,oneTimeCodeName:c+=o}}},67619:(e,r,t)=>{t.d(r,{A:()=>n});var s=t(29021),o=t.n(s);async function n({user:e,message:r,component:t,noMail:s,req:n}){let i=new Date,a=(()=>{if(!n)return null;try{let e=n.headers["x-forwarded-for"],r=n.headers["x-real
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-14 06:19:19 +00:00
User Email: ${e?.email}`),n?.url&&(s+=`
URL: ${n.url}`),n?.body&&(s+=`
2025-01-14 06:42:13 +00:00
Request Body: ${JSON.stringify(n.body,null,4)}`),a&&(s+=`
IP: ${a}`),s+=`
Date: ${i.toDateString()}
========================================`,o().existsSync("./.tmp/error.log")||o().writeFileSync("./.tmp/error.log","","utf-8");let c=o().readFileSync("./.tmp/error.log","utf-8");o().writeFileSync("./.tmp/error.log",s),o().appendFileSync("./.tmp/error.log",`
2024-12-08 19:56:57 +00:00
2025-01-14 06:42:13 +00:00
${c}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},95969:(e,r,t)=>{t.d(r,{A:()=>n});var s=t(55511),o=t(79428);function n({encryptedString:e,encryptionKey:r,encryptionSalt:t}){if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let n=r||process.env.DSQL_ENCRYPTION_PASSWORD,i=t||process.env.DSQL_ENCRYPTION_SALT,a=process.env.DSQL_ENCRYPTION_KEY_LENGTH?Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH):24;if(!n?.match(/.{8,}/))return console.log("Decrption key is invalid"),e;if(!i?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let c=(0,s.scryptSync)(n,i,a),l=o.Buffer.alloc(16,0),u=(0,s.createDecipheriv)("aes-192-cbc",c,l);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,[1251],()=>t(15420));module.exports=s})();