Edit: /tmp/tsx-0/17887-24d623cb034d5a71761ca63ba7b901ffd63ffa9f (9601B)
{"code":"__filename=\"/opt/canhelp/node_modules/better-auth/dist/plugins/two-factor/totp/index.mjs\";(()=>{\nvar __create=Object.create;var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __getProtoOf=Object.getPrototypeOf;var __hasOwnProp=Object.prototype.hasOwnProperty;var __name=(target,value)=>__defProp(target,\"name\",{value,configurable:true});var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:true})};var __copyProps=(to,from,except,desc)=>{if(from&&typeof from===\"object\"||typeof from===\"function\"){for(let key of __getOwnPropNames(from))if(!__hasOwnProp.call(to,key)&&key!==except)__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable})}return to};var __toESM=(mod,isNodeMode,target)=>(target=mod!=null?__create(__getProtoOf(mod)):{},__copyProps(isNodeMode||!mod||!mod.__esModule?__defProp(target,\"default\",{value:mod,enumerable:true}):target,mod));var __toCommonJS=mod=>__copyProps(__defProp({},\"__esModule\",{value:true}),mod);var totp_exports={};__export(totp_exports,{totp2fa:()=>totp2fa});module.exports=__toCommonJS(totp_exports);var import_crypto=require(\"../../../crypto/index.mjs\");var import_cookies=require(\"../../../cookies/index.mjs\");var import_session=require(\"../../../api/routes/session.mjs\");var import_api=require(\"../../../api/index.mjs\");var import_error_code=require(\"../error-code.mjs\");var import_verify_two_factor=require(\"../verify-two-factor.mjs\");var import_error=require(\"@better-auth/core/error\");var import_api2=require(\"@better-auth/core/api\");var z=__toESM(require(\"zod\"));var import_otp=require(\"@better-auth/utils/otp\");const generateTOTPBodySchema=z.object({secret:z.string().meta({description:\"The secret to generate the TOTP code\"})});const getTOTPURIBodySchema=z.object({password:z.string().meta({description:\"User password\"})});const verifyTOTPBodySchema=z.object({code:z.string().meta({description:'The otp code to verify. Eg: \"012345\"'}),trustDevice:z.boolean().meta({description:\"If true, the device will be trusted for 30 days. It'll be refreshed on every sign in request within this time. Eg: true\"}).optional()});const totp2fa=__name(options=>{const opts={...options,digits:options?.digits||6,period:options?.period||30};const twoFactorTable=\"twoFactor\";return{id:\"totp\",endpoints:{generateTOTP:(0,import_api2.createAuthEndpoint)({method:\"POST\",body:generateTOTPBodySchema,metadata:{openapi:{summary:\"Generate TOTP code\",description:\"Use this endpoint to generate a TOTP code\",responses:{200:{description:\"Successful response\",content:{\"application/json\":{schema:{type:\"object\",properties:{code:{type:\"string\"}}}}}}}}}},async ctx=>{if(options?.disable){ctx.context.logger.error(\"totp isn't configured. please pass totp option on two factor plugin to enable totp\");throw import_error.APIError.from(\"BAD_REQUEST\",{message:\"totp isn't configured\",code:\"TOTP_NOT_CONFIGURED\"})}return{code:await(0,import_otp.createOTP)(ctx.body.secret,{period:opts.period,digits:opts.digits}).totp()}}),getTOTPURI:(0,import_api2.createAuthEndpoint)(\"/two-factor/get-totp-uri\",{method:\"POST\",use:[import_session.sessionMiddleware],body:getTOTPURIBodySchema,metadata:{openapi:{summary:\"Get TOTP URI\",description:\"Use this endpoint to get the TOTP URI\",responses:{200:{description:\"Successful response\",content:{\"application/json\":{schema:{type:\"object\",properties:{totpURI:{type:\"string\"}}}}}}}}}},async ctx=>{if(options?.disable){ctx.context.logger.error(\"totp isn't configured. please pass totp option on two factor plugin to enable totp\");throw import_error.APIError.from(\"BAD_REQUEST\",{message:\"totp isn't configured\",code:\"TOTP_NOT_CONFIGURED\"})}const user=ctx.context.session.user;const twoFactor=await ctx.context.adapter.findOne({model:twoFactorTable,where:[{field:\"userId\",value:user.id}]});if(!twoFactor)throw import_error.APIError.from(\"BAD_REQUEST\",import_error_code.TWO_FACTOR_ERROR_CODES.TOTP_NOT_ENABLED);const secret=await(0,import_crypto.symmetricDecrypt)({key:ctx.context.secretConfig,data:twoFactor.secret});await ctx.context.password.checkPassword(user.id,ctx);return{totpURI:(0,import_otp.createOTP)(secret,{digits:opts.digits,period:opts.period}).url(options?.issuer||ctx.context.appName,user.email)}}),verifyTOTP:(0,import_api2.createAuthEndpoint)(\"/two-factor/verify-totp\",{method:\"POST\",body:verifyTOTPBodySchema,metadata:{openapi:{summary:\"Verify two factor TOTP\",description:\"Verify two factor TOTP\",responses:{200:{description:\"Successful response\",content:{\"application/json\":{schema:{type:\"object\",properties:{status:{type:\"boolean\"}}}}}}}}}},async ctx=>{if(options?.disable){ctx.context.logger.error(\"totp isn't configured. please pass totp option on two factor plugin to enable totp\");throw import_error.APIError.from(\"BAD_REQUEST\",{message:\"totp isn't configured\",code:\"TOTP_NOT_CONFIGURED\"})}const{session,valid,invalid}=await(0,import_verify_two_factor.verifyTwoFactor)(ctx);const user=session.user;const twoFactor=await ctx.context.adapter.findOne({model:twoFactorTable,where:[{field:\"userId\",value:user.id}]});if(!twoFactor)throw import_error.APIError.from(\"BAD_REQUEST\",import_error_code.TWO_FACTOR_ERROR_CODES.TOTP_NOT_ENABLED);if(!await(0,import_otp.createOTP)(await(0,import_crypto.symmetricDecrypt)({key:ctx.context.secretConfig,data:twoFactor.secret}),{period:opts.period,digits:opts.digits}).verify(ctx.body.code))return invalid(\"INVALID_CODE\");if(!user.twoFactorEnabled){if(!session.session)throw import_error.APIError.from(\"BAD_REQUEST\",import_error.BASE_ERROR_CODES.FAILED_TO_CREATE_SESSION);const updatedUser=await ctx.context.internalAdapter.updateUser(user.id,{twoFactorEnabled:true});const newSession=await ctx.context.internalAdapter.createSession(user.id,false,session.session).catch(e=>{throw e});await ctx.context.internalAdapter.deleteSession(session.session.token);await(0,import_cookies.setSessionCookie)(ctx,{session:newSession,user:updatedUser})}return valid(ctx)})}}},\"totp2fa\");0&&(module.exports={totp2fa});\n})()\n","warnings":[],"map":{"version":3,"mappings":";i/BAAA,6HAAiC,qCACjC,mBAAiC,sCACjC,mBAAkC,2CAClC,eAAO,kCACP,sBAAuC,6BACvC,6BAAgC,oCAChC,iBAA2C,mCAC3C,IAAAA,YAAmC,iCACnC,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