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

16 lines
7.1 KiB
JavaScript
Raw Normal View History

2025-01-14 07:24:54 +00:00
"use strict";(()=>{var e={};e.id=2998,e.ids=[2998],e.modules={75600:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},79428:e=>{e.exports=require("buffer")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},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}}})},55896:(e,r,t)=>{t.r(r),t.d(r,{config:()=>m,default:()=>S,routeModule:()=>g});var n={};t.r(n),t.d(n,{default:()=>E});var s=t(89947),i=t(2706),o=t(96762),a=t(69842),l=t(29021),c=t.n(l),u=t(67619),_=t(48786),d=t(68829),f=t(89159);async function p({userId:e,res:r,csrf:t}){try{let n=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!n)return console.log("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable."),null;let s=await (0,_.A)("SELECT * FROM users WHERE id=?",[e]);c().unlinkSync(`${n}/${t}`);let i=Math.random().toString(36).substring(2)+"-"+Math.random().toString(36).substring(2),o={id:s[0].id,stripe_id:s[0].stripe_id||"",first_name:s[0].first_name,last_name:s[0].last_name,email:s[0].email,social_id:s[0].social_id,image:s[0].image,image_thumbnail:s[0].image_thumbnail,verification_status:s[0].verification_status,social_login:s[0].social_login,social_platform:s[0].social_platform,csrf_k:i,logged_in_status:!0,date:Date.now()},a=(0,d.A)({data:JSON.stringify(o)});c().writeFileSync(`${n}/${i}`,JSON.stringify(o));let{keyCookieName:l,csrfCookieName:u}=(0,f.A)();return r.setHeader("Set-Cookie",[`${l}=${a};samesite=strict;path=/;HttpOnly=true;Secure=true`,`${u}=${i};samesite=strict;path=/;HttpOnly=true;Secure=true`,"user_refresh=1"]),o}catch(r){return(0,u.A)({component:"reAuthUser",message:r.message,user:{id:e}}),null}}async function E(e,r){"POST"!==e.method&&r.json({msg:"Failed!"});let t=await (0,a.A)(e,r,!0);if(!t){console.log("No user Found"),r.redirect(307,"/logout");return}let n=await p({userId:t.id,res:r,csrf:t.csrf_k}),s={status:!!n,csrf:n?.csrf_k,user:n};r.json(s)}let S=(0,o.M)(n,"default"),m=(0,o.M)(n,"config"),g=new s.PagesAPIRouteModule({definition:{kind:i.A.PAGES_API,page:"/api/reAuthenticateUser",pathname:"/api/reAuthenticateUser",bundlePath:"",filename:""},userland:n})},69842:(e,r,t)=>{t.d(r,{A:()=>l});var n=t(48786),s=t(95969),i=t(29021),o=t.n(i),a=t(89159);async function l(e,r,t){try{let{keyCookieName:i,csrfCookieName:l}=(0,a.A)();if(!e.cookies?.[i]?.match(/./))throw Error("Request Cookies not Found!");let c=(0,s.A)({encryptedString:e.cookies[i]});if(!c)throw Error("Couldn't decrypt auth cookie!");let u=JSON.parse(c);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 _=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!_)throw Error("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable.");if(t&&!o().existsSync(`${_}/${u.csrf_k}`))throw Error("Key not found in Auth Keys Folder!");if(0==u.verification_status&&!t){let e=await (0,n.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:()=>n});function n(e){let r=process.env.DSQL_COOKIES_PREFIX||"dsql_",t=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",i=e?.database?.replace(/^datasquirel_user_\d+_/,"")||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),o=r;e?.userId&&(o+=`user_${e.userId}_`),i&&(o+=`${i}_`),o+=t;let a=r;e?.userId&&(a+=`user_${e.userId}_`),i&&(a+=`${i}_`),a+=n;let l=r;return e?.userId&&(l+=`user_${e.userId}_`),i&&(l+=`${i}_`),{keyCookieName:o,csrfCookieName:a,oneTimeCodeName:l+=s}}},67619:(e,r,t)=>{t.d(r,{A:()=>i});var n=t(
2024-12-08 19:56:57 +00:00
Error Message: ${r}
Component: ${t}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(n+=`
User Id: ${e?.id}
User Name: ${e?.first_name} ${e?.last_name}
User Email: ${e?.email}`),i?.url&&(n+=`
URL: ${i.url}`),i?.body&&(n+=`
2025-01-14 06:19:19 +00:00
Request Body: ${JSON.stringify(i.body,null,4)}`),a&&(n+=`
IP: ${a}`),n+=`
Date: ${o.toDateString()}
2025-01-13 08:00:21 +00:00
========================================`,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
2025-01-14 06:42:13 +00:00
${l}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},95969:(e,r,t)=>{t.d(r,{A:()=>i});var n=t(55511),s=t(79428);function i({encryptedString:e,encryptionKey:r,encryptionSalt:t}){if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let i=r||process.env.DSQL_ENCRYPTION_PASSWORD,o=t||process.env.DSQL_ENCRYPTION_SALT,a=process.env.DSQL_ENCRYPTION_KEY_LENGTH?Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH):24;if(!i?.match(/.{8,}/))return console.log("Decrption key is invalid"),e;if(!o?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let l=(0,n.scryptSync)(i,o,a),c=s.Buffer.alloc(16,0),u=(0,n.createDecipheriv)("aes-192-cbc",l,c);try{let r=u.update(e,"hex","utf8");return r+=u.final("utf8")}catch(r){return console.log("Error in decrypting =>",r.message),e}}},68829:(e,r,t)=>{t.d(r,{A:()=>i});var n=t(55511),s=t(79428);function i({data:e,encryptionKey:r,encryptionSalt:t}){if(!e?.match(/./))return console.log("Encryption string is invalid"),e;let i=r||process.env.DSQL_ENCRYPTION_PASSWORD,o=t||process.env.DSQL_ENCRYPTION_SALT,a=process.env.DSQL_ENCRYPTION_KEY_LENGTH?Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH):24;if(!i?.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)(i,o,a),c=s.Buffer.alloc(16,0),u=(0,n.createCipheriv)("aes-192-cbc",l,c);try{let r=u.update(e,"utf8","hex");return r+=u.final("hex")}catch(r){return console.log("Error in encrypting =>",r.message),e}}},48786:(e,r,t)=>{t.d(r,{A:()=>n});async function n(...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()}}},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=r(r.s=55896);module.exports=t})();