"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")},1282:e=>{e.exports=require("child_process")},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}}})},9055:(e,s,r)=>{r.r(s),r.d(s,{config:()=>y,default:()=>g,routeModule:()=>_});var t={};r.r(t),r.d(t,{default:()=>f});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),h=r(2253),p=r(246);async function f(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:f}=t,g=d()({password:l}),y=await c().insert({data:[{first_name:n,last_name:u,email:a,password:g,is_super_admin:r?.id?0:1,username:f?(0,h.lV)(f):void 0}],table:"users"}),_=y.postInsertReturn?.insertId;if(!_)throw Error("Couldn't create New User.");let b=await c().select({table:"users",targetId:_}),w=b.singleRes;if(!w?.id)throw console.log("newly_inserted_user_res",b),Error("Couldn't Find Newly inserted user.");if(r?.id&&!w.username)throw await c().delete({table:"users",targetId:w?.id}),Error("Couldn't set Newly inserted user username.");if(r?.id)return console.log("newly_inserted_user",w),await (0,p.Z)({user_id:w.id}),s.json({success:!0,singleRes:w});{let r=await (0,o.Z)({res:s,user_id:w.id,req:e});return s.json(r)}}catch(e){return s.json({success:!1,msg:e.message})}}let g=(0,a.l)(t,"default"),y=(0,a.l)(t,"config"),_=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:()=>p});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),h=r(9786);async function p({req:e,res:s,user_id:r,password:t,email_or_username:o}){let d;if(r){let e=await i().select({table:"users",targetId:r});if(!e.singleRes?.id)throw Error("Couldn't Find user for login");d=e.singleRes}if(o){let e=await i().select({table:"users",query:{query:{email:{value:o},username:{value:o}},searchOperator:"OR"}});if(!e.singleRes?.id)throw Error("Couldn't Find user for login");d=e.singleRes}if(!d)return{success:!1,msg:"User Not Found!"};if(t&&l()({password:t})!==d.password)return{success:!1,msg:"Invalid Password."};let c=Date.now(),p=Math.random().toString(36).substring(2)+"-"+Math.random().toString(36).substring(2),f={first_name:d.first_name,last_name:d.last_name,date:c,email:d.email,csrf_k:p,id:d.id,logged_in_status:!0,image:d.image,image_thumbnail:d.image,super_admin:!!d.is_super_admin},g=a.dF.stringify(f),y=u()({data:g||""}),_=m()(Date.now()).add(7,"days");_.add(7,"days");let{auth_key_cookie_name:b,csrf_cookie_name:w}=(0,h.Z)(),$=e.headers.host;return(0,n.d8)(s,[{name:b,value:y||"",options:{secure:"localhost"!==$,path:"/",expires:_,domain:$}},{name:w,value:p,options:{path:"/",expires:_,domain:$}}]),{success:!0,singleRes:f}}},246:(e,s,r)=>{r.d(s,{Z:()=>d});var t=r(2253),i=r(2481),n=r(8804),a=r.n(n),o=r(1282),u=r(2048);async function d({user_id:e}){let s=(await a().select({table:"users",targetId:(0,t._n)(e)})).singleRes;if(!s?.id||!s.username||s.is_super_admin)return;let{username:r}=s,{force_command_file:n,ssh_dir:d,ssh_key_file:l,sshd_config_file:c,user_dir:m}=(0,i.Z)({user:s}),h=`/bin/bash << 'TURBOCIHEREDOC'
`;(0,u.existsSync)(m)||(h+=`useradd --create-home --shell /bin/bash --comment "TurboCI Deployment user ${r}" ${r}
passwd --lock "${r}"
mkdir -p "${d}"
ssh-keygen -t ed25519 -f "${l}" -N "" -C "${r}@$(hostname)"
cp "${l}.pub" "${d}/authorized_keys"
chown -R "${r}:${r}" "${d}"
chmod 700 "${d}"
chmod 600 "${l}"
chmod 644 "${l}.pub"
chmod 600 "${d}/authorized_keys"
`),h+=`cat << 'EOF' > ${n}
#!/bin/bash
case "$SSH_ORIGINAL_COMMAND" in
    "shell")
        exec /bin/bash
        ;;
    *)
        echo "Access denied."
        exit 1
        ;;
esac
EOF

chmod +x ${n}

cat << 'EOF' > ${c}
Match User ${r}
    PasswordAuthentication no
    PubkeyAuthentication yes
    AuthenticationMethods publickey
    AllowTcpForwarding yes
    X11Forwarding no
    ForceCommand ${n}
    PermitOpen localhost:80
    PermitTTY no
EOF
TURBOCIHEREDOC
`,(0,o.execSync)(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`}}},2481:(e,s,r)=>{r.d(s,{Z:()=>t});function t({user:e}){let{username:s}=e,r=`/home/${s}`,t=`${r}/.ssh`,i=`${t}/${s}`;return{user_dir:r,ssh_dir:t,ssh_key_file:i,sshd_config_file:`/etc/ssh/sshd_config.d/${s}.conf`,force_command_file:`/usr/local/bin/turboci-deployment-user-${s}`}}},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=9055);module.exports=r})();