2024-11-05 11:12:42 +00:00
"use strict" ;
( ( ) => {
var exports = { } ;
exports . id = 1146 ;
exports . ids = [ 1146 ] ;
exports . modules = {
/***/ 2261 :
/***/ ( ( module ) => {
module . exports = require ( "serverless-mysql" ) ;
/***/ } ) ,
/***/ 4300 :
/***/ ( ( module ) => {
module . exports = require ( "buffer" ) ;
/***/ } ) ,
/***/ 6113 :
/***/ ( ( module ) => {
module . exports = require ( "crypto" ) ;
/***/ } ) ,
/***/ 7147 :
/***/ ( ( module ) => {
module . exports = require ( "fs" ) ;
/***/ } ) ,
/***/ 3685 :
/***/ ( ( module ) => {
module . exports = require ( "http" ) ;
/***/ } ) ,
/***/ 1017 :
/***/ ( ( module ) => {
module . exports = require ( "path" ) ;
/***/ } ) ,
/***/ 5989 :
/***/ ( ( _ _unused _webpack _module , _ _webpack _exports _ _ , _ _webpack _require _ _ ) => {
_ _webpack _require _ _ . r ( _ _webpack _exports _ _ ) ;
/* harmony export */ _ _webpack _require _ _ . d ( _ _webpack _exports _ _ , {
/* harmony export */ "default" : ( ) => ( /* binding */ handler )
/* harmony export */ } ) ;
/* harmony import */ var _package _shared _utils _backend _global _db _DB _HANDLER _ _WEBPACK _IMPORTED _MODULE _0 _ _ = _ _webpack _require _ _ ( 2224 ) ;
/* harmony import */ var _package _shared _utils _backend _global _db _DB _HANDLER _ _WEBPACK _IMPORTED _MODULE _0 _ _ _default = /*#__PURE__*/ _ _webpack _require _ _ . n ( _package _shared _utils _backend _global _db _DB _HANDLER _ _WEBPACK _IMPORTED _MODULE _0 _ _ ) ;
/* harmony import */ var _package _shared _functions _backend _encrypt _ _WEBPACK _IMPORTED _MODULE _1 _ _ = _ _webpack _require _ _ ( 7547 ) ;
/* harmony import */ var _package _shared _functions _backend _encrypt _ _WEBPACK _IMPORTED _MODULE _1 _ _ _default = /*#__PURE__*/ _ _webpack _require _ _ . n ( _package _shared _functions _backend _encrypt _ _WEBPACK _IMPORTED _MODULE _1 _ _ ) ;
/* harmony import */ var _functions _backend _serverError _ _WEBPACK _IMPORTED _MODULE _2 _ _ = _ _webpack _require _ _ ( 2163 ) ;
/* harmony import */ var _functions _backend _serverError _ _WEBPACK _IMPORTED _MODULE _2 _ _ _default = /*#__PURE__*/ _ _webpack _require _ _ . n ( _functions _backend _serverError _ _WEBPACK _IMPORTED _MODULE _2 _ _ ) ;
/* harmony import */ var _functions _backend _userAuth _ _WEBPACK _IMPORTED _MODULE _3 _ _ = _ _webpack _require _ _ ( 6825 ) ;
/* harmony import */ var _functions _backend _userAuth _ _WEBPACK _IMPORTED _MODULE _3 _ _ _default = /*#__PURE__*/ _ _webpack _require _ _ . n ( _functions _backend _userAuth _ _WEBPACK _IMPORTED _MODULE _3 _ _ ) ;
// @ts-check
/ * *
* === === === === === === === === === === === === === === === === === === === === === === === === === ===
* Imports
* === === === === === === === === === === === === === === === === === === === === === === === === === ===
* /
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ / * *
* API handler
* === === === === === === === === === === === === === === === === === === === === === === === === === ===
* @ type { import ( "next" ) . NextApiHandler }
* / a s y n c f u n c t i o n h a n d l e r ( r e q , r e s ) {
/ * *
* Check method
*
* @ description Check request method and return if invalid
* / i f ( r e q . m e t h o d ! = = " P O S T " ) r e t u r n r e s . j s o n ( {
msg : "Failed!"
} ) ;
/ * *
* User auth
*
* @ description Authenticate user
* / c o n s t u s e r = a w a i t _ f u n c t i o n s _ b a c k e n d _ u s e r A u t h _ _ W E B P A C K _ I M P O R T E D _ M O D U L E _ 3 _ _ _ d e f a u l t ( ) ( r e q , r e s , t r u e ) ;
if ( ! user ) {
return res . json ( {
success : false ,
msg : "Unauthorized"
} ) ;
}
/ * *
* User auth
*
* @ description Authenticate user
* / / / const { rootUserId , priviledges , database } = req . body ;
const rootUserId = req . body . rootUserId ;
const priviledges = req . body . priviledges ;
const database = req . body . database ;
/ * *
* Input Validation
*
* @ description Input Validation
* / t r y {
const foundUserUser = await _package _shared _utils _backend _global _db _DB _HANDLER _ _WEBPACK _IMPORTED _MODULE _0 _ _ _default ( ) ( ` SELECT * FROM user_users WHERE user_id=? AND invited_user_id=? AND MATCH(database_access) AGAINST(?) ` , [
rootUserId ,
user . id ,
database
] ) ;
if ( ! foundUserUser || ! foundUserUser [ 0 ] ) throw new Error ( ` user with id ${ user . id } has no access to ${ database } database belonging to user ${ rootUserId } ` ) ;
const foundUserTables = ( await _package _shared _utils _backend _global _db _DB _HANDLER _ _WEBPACK _IMPORTED _MODULE _0 _ _ _default ( ) ( ` SELECT * FROM delegated_user_tables WHERE root_user_id=? AND delegated_user_id=? AND \` database \` =? ` , [
rootUserId ,
user . id ,
database
] ) ) . map ( ( /** @type {any} */ tableObject ) => {
return tableObject . table ;
} ) ;
const rootUserinfo = await _package _shared _utils _backend _global _db _DB _HANDLER _ _WEBPACK _IMPORTED _MODULE _0 _ _ _default ( ) ( ` SELECT * FROM users WHERE id=? ` , [
rootUserId
] ) ;
const payload = {
rootUserId : rootUserId ,
rootUserName : ` ${ rootUserinfo [ 0 ] . first _name } ${ rootUserinfo [ 0 ] . last _name } ` ,
rootUserEmail : rootUserinfo [ 0 ] . email ,
rootUserImage : rootUserinfo [ 0 ] . image _thumbnail ,
delegatedUserId : user . id ,
databaseFullName : ` datasquirel_user_ ${ rootUserId } _ ${ database } ` ,
databaseSlug : database ,
allowedTables : foundUserTables ,
priviledges : priviledges ,
dateCreated : Date . now ( )
} ;
const encryptedPayload = _package _shared _functions _backend _encrypt _ _WEBPACK _IMPORTED _MODULE _1 _ _ _default ( ) ( JSON . stringify ( payload ) ) ;
const dbFullName = ` ${ process . env . DSQL _USER _DB _PREFIX } ${ payload . rootUserId } _ ${ database } ` ;
const tokenName = ` ${ process . env . DSQL _USER _DELEGATED _DB _COOKIE _PREFIX } ${ dbFullName } ` ;
res . setHeader ( "Set-Cookie" , [
` ${ tokenName } = ${ encryptedPayload } ;path=/ ` ,
] ) ;
res . json ( {
success : true ,
msg : "Processed Successfully"
} ) ;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
} catch ( /** @type {any} */ error ) {
console . log ( "ERROR:" , error . message ) ;
_functions _backend _serverError _ _WEBPACK _IMPORTED _MODULE _2 _ _ _default ( ) ( {
component : "/api/getDelegatedDatabaseToken/main-catch-error" ,
message : error . message ,
user : user
} ) ;
return res . json ( {
success : false ,
msg : "Unauthorized"
} ) ;
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
}
/***/ } )
} ;
;
// load runtime
var _ _webpack _require _ _ = require ( "../../webpack-api-runtime.js" ) ;
_ _webpack _require _ _ . C ( exports ) ;
var _ _webpack _exec _ _ = ( moduleId ) => ( _ _webpack _require _ _ ( _ _webpack _require _ _ . s = moduleId ) )
2024-11-05 14:18:40 +00:00
var _ _webpack _exports _ _ = _ _webpack _require _ _ . X ( 0 , [ 2224 , 2163 , 6825 , 3017 , 7547 ] , ( ) => ( _ _webpack _exec _ _ ( 5989 ) ) ) ;
2024-11-05 11:12:42 +00:00
module . exports = _ _webpack _exports _ _ ;
} ) ( ) ;