"use strict";(()=>{var e={};e.id=11,e.ids=[11],e.modules={5529:e=>{e.exports=require("@moduletrace/datasquirel/dist/package-shared/functions/dsql/hashPassword")},8804:e=>{e.exports=require("@moduletrace/nsqlite")},1635:e=>{e.exports=require("dayjs")},145:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},2048:e=>{e.exports=require("fs")},6249:(e,s)=>{Object.defineProperty(s,"l",{enumerable:!0,get:function(){return function e(s,r){return r in s?s[r]:"then"in s&&"function"==typeof s.then?s.then(s=>e(s,r)):"function"==typeof s&&"default"===r?s:void 0}}})},7809:(e,s,r)=>{r.r(s),r.d(s,{config:()=>_,default:()=>b,routeModule:()=>w});var t={};r.r(t),r.d(t,{default:()=>y});var i=r(1802),n=r(7153),a=r(6249),o=r(4751),u=r(5529),d=r.n(u),l=r(8804),c=r.n(l),m=r(2632),p=r(2253);let h=require("child_process");var f=r(2048);async function g({user_id:e}){let s=(await c().select({table:"users",targetId:(0,p._n)(e)})).singleRes;if(!s?.id)return;let{username:r}=s,t=`/home/${r}`,i=`${t}/.ssh`,n=`${i}/${r}`,a=`/etc/ssh/sshd_config.d/${r}`,o=`/usr/local/bin/turboci-deployment-user-${r}`,u=`/bin/bash << 'TURBOCIHEREDOC'
`;(0,f.existsSync)(t)||(u+=`useradd --create-home --shell /bin/bash --comment "TurboCI Deployment user ${r}" ${r}
passwd --lock "${r}"
mkdir -p "${i}"
ssh-keygen -t ed25519 -f "${n}" -N ""
cp "${n}.pub" "${i}/authorized_keys"
chown -R "${r}:${r}" "${i}"
chmod 700 "${i}"
chmod 600 "${n}"
chmod 644 "${n}.pub"
chmod 600 "${i}/authorized_keys"
`),u+=`cat << 'EOF' > ${o}
#!/bin/bash
case "$SSH_ORIGINAL_COMMAND" in
    "shell")
        exec /bin/bash
        ;;
    *)
        echo "Access denied."
        exit 1
        ;;
esac
EOF

chmod +x ${o}

cat << 'EOF' > ${a}
Match User ${r}
    PasswordAuthentication no
    PubkeyAuthentication yes
    AuthenticationMethods publickey

    AllowTcpForwarding yes
    X11Forwarding no

    ForceCommand ${o}
EOF
TURBOCIHEREDOC
`,(0,h.execSync)(u)}async function y(e,s){try{let{singleRes:r}=await (0,m.Z)({req:e});if("POST"!==e.method)return s.json({success:!1,msg:"Wrong Method"});let{new_user:t}=e.body;if(r?.id&&!t?.username?.match(/^[a-z0-9\-]{3,}$/))throw Error("Please pass a valid username");if(!t)throw Error("No new User Object Passed!");if(!t.password)throw Error("New User Password is required.");let i=await c().select({table:"users"});if(i.payload?.[0]?.id&&!r?.id)return s.json({success:!1,msg:"Super Admin User already exists. Other Users can be created by this user."});if(r?.id&&!r.super_admin)return s.json({success:!1,msg:"Operation not allowed!"});let{first_name:n,email:a,last_name:u,password:l,username:h}=t,f=d()({password:l}),y=await c().insert({data:[{first_name:n,last_name:u,email:a,password:f,is_super_admin:r?.id?0:1,username:(0,p.lV)(h)}],table:"users"});if(!y.postInsertReturn?.insertId)throw Error("Couldn't create New User.");let b=(await c().select({table:"users",query:{query:{is_super_admin:{value:"1"}}}})).singleRes;if(!b?.id)throw Error("Couldn't Find Newly inserted user.");if(r?.id)return await g({user_id:b.id}),s.json({success:!0,singleRes:b});{let e=await (0,o.Z)({res:s,user_id:b.id});return s.json(e)}}catch(e){return s.json({success:!1,msg:e.message})}}let b=(0,a.l)(t,"default"),_=(0,a.l)(t,"config"),w=new i.PagesAPIRouteModule({definition:{kind:n.x.PAGES_API,page:"/api/auth/signup",pathname:"/api/auth/signup",bundlePath:"",filename:""},userland:t})},2253:(e,s,r)=>{r.d(s,{dF:()=>a(),_n:()=>l,lV:()=>u()});let t=require("@moduletrace/datasquirel/dist/client");var i=r.n(t);let n=require("@moduletrace/datasquirel/dist/package-shared/utils/ejson");var a=r.n(n);let o=require("@moduletrace/datasquirel/dist/package-shared/utils/slugify");var u=r.n(o);let d=require("@moduletrace/datasquirel/dist/package-shared/utils/numberfy"),l=r.n(d)();i().utils.serializeQuery,i().utils.deserializeQuery},4751:(e,s,r)=>{r.d(s,{Z:()=>h});var t=r(8804),i=r.n(t),n=r(6941),a=r(2253);let o=require("@moduletrace/datasquirel/dist/package-shared/functions/dsql/encrypt");var u=r.n(o),d=r(5529),l=r.n(d),c=r(1635),m=r.n(c),p=r(9786);async function h({res:e,user_id:s,password:r,email_or_username:t}){let o;if(s){let e=await i().select({table:"users",targetId:s});if(!e.singleRes?.id)throw Error("Couldn't Find user for login");o=e.singleRes}if(t){let e=await i().select({table:"users",query:{query:{email:{value:t},username:{value:t}},searchOperator:"OR"}});if(!e.singleRes?.id)throw Error("Couldn't Find user for login");o=e.singleRes}if(!o)return{success:!1,msg:"User Not Found!"};if(r&&l()({password:r})!==o.password)return{success:!1,msg:"Invalid Password."};let d=Date.now(),c=Math.random().toString(36).substring(2)+"-"+Math.random().toString(36).substring(2),h={first_name:o.first_name,last_name:o.last_name,date:d,email:o.email,csrf_k:c,id:o.id,logged_in_status:!0,image:o.image,image_thumbnail:o.image,super_admin:!!o.is_super_admin},f=a.dF.stringify(h),g=u()({data:f||""}),y=m()(Date.now()).add(7,"days");y.add(7,"days");let{auth_key_cookie_name:b,csrf_cookie_name:_}=(0,p.Z)();return(0,n.d8)(e,[{name:b,value:g||"",options:{secure:"localhost"!==process.env.DOMAIN,path:"/",expires:y,domain:process.env.DOMAIN}},{name:_,value:c,options:{path:"/",expires:y,domain:process.env.DOMAIN}}]),{success:!0,singleRes:h}}},6941:(e,s,r)=>{function t(e,s){let r=[];for(let e=0;e<s.length;e++){let{name:t,options:i,value:n}=s[e],a=[`${encodeURIComponent(t)}=${encodeURIComponent(n)}`];i.expires&&a.push(`Expires=${i.expires.toDate().toUTCString()}`),void 0!==i.maxAge&&a.push(`Max-Age=${i.maxAge}`),i.path&&a.push(`Path=${i.path}`),i.domain&&a.push(`Domain=${i.domain}`),i.secure&&a.push("Secure"),i.httpOnly&&a.push("HttpOnly"),r.push(a)}let t=r.map(e=>e.join("; "));e.setHeader("Set-Cookie",t)}function i(e,s){return e.split(";").reduce((e,s)=>{let[r,t]=s.trim().split("=").map(decodeURIComponent);return e[r]=t,e},{})[s]||null}r.d(s,{d8:()=>t,ej:()=>i}),r(1635)},9786:(e,s,r)=>{r.d(s,{Z:()=>a});var t=r(2048);let i=require("path");var n=r.n(i);function a(){let{TURBOCI_DEPLOYMENT_ID_FILE:e}=function(){let e=n().resolve(__dirname,"../../"),s="/root/.turboci",r=n().join(s,".config"),t=n().join(r,"turboci.json"),i=n().join(s,"deployment_id"),a=n().join(s,".ssh"),o=n().join(a,"turboci");return{APP_DIR:e,TURBOCI_CONFIG_DIR:r,TURBOCI_CONFIG_JSON_FILE:t,TURBOCI_DIR:s,TURBOCI_SSH_DIR:a,TURBOCI_SSH_KEY_FILE:o,TURBOCI_DEPLOYMENT_ID_FILE:i}}();if(!(0,t.existsSync)(e))throw Error(`\`${e}\` does not exist.`);let s=(0,t.readFileSync)(e,"utf-8").split("-").shift();if(!s)throw Error("Invalid deployment_id");return{auth_key_cookie_name:`turboci-admin-${s}-auth-key`,csrf_cookie_name:`turboci-admin-${s}-csrf`}}},2632:(e,s,r)=>{r.d(s,{Z:()=>u});var t=r(6941);let i=require("@moduletrace/datasquirel/dist/package-shared/functions/dsql/decrypt");var n=r.n(i),a=r(2253),o=r(9786);async function u({req:e,bun_req:s}){try{let{auth_key_cookie_name:r,csrf_cookie_name:i}=(0,o.Z)(),u=e?.headers.cookie||s?.headers.get("cookie");if(!u)return{success:!1,msg:"Couldn't grab cookie string"};let d=(0,t.ej)(u,r);if(!d)return{success:!1,msg:`No ${r} found in request object.`};let l=n()({encryptedString:d}),c=a.dF.parse(l);if(!c?.id)return{success:!1,msg:"Invalid Auth Key"};let m=(0,t.ej)(u,i);if(!m)return{success:!1,msg:`No ${i} found in request object.`};if(m!==c.csrf_k)return{success:!1,msg:"CSRF mismatch"};return{success:!0,singleRes:c}}catch(e){return{success:!1}}}},7153:(e,s)=>{var r;Object.defineProperty(s,"x",{enumerable:!0,get:function(){return r}}),function(e){e.PAGES="PAGES",e.PAGES_API="PAGES_API",e.APP_PAGE="APP_PAGE",e.APP_ROUTE="APP_ROUTE"}(r||(r={}))},1802:(e,s,r)=>{e.exports=r(145)}};var s=require("../../../webpack-api-runtime.js");s.C(e);var r=s(s.s=7809);module.exports=r})();