Edit: /tmp/tsx-0/17887-b367035b2bcd0862edab858b69ba008d3d693cbf (13635B)
{"code":"__filename=\"/opt/canhelp/node_modules/better-auth/dist/plugins/oidc-provider/authorize.mjs\";(()=>{\nvar __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;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 __toCommonJS=mod=>__copyProps(__defProp({},\"__esModule\",{value:true}),mod);var authorize_exports={};__export(authorize_exports,{authorize:()=>authorize});module.exports=__toCommonJS(authorize_exports);var import_random=require(\"../../crypto/random.mjs\");var import_crypto=require(\"../../crypto/index.mjs\");var import_session=require(\"../../api/routes/session.mjs\");var import_api=require(\"../../api/index.mjs\");var import_error=require(\"./error.mjs\");var import_prompt=require(\"./utils/prompt.mjs\");var import_index=require(\"./index.mjs\");var import_error2=require(\"@better-auth/core/error\");var import_fetch_metadata=require(\"@better-auth/core/utils/fetch-metadata\");function formatErrorURL(url,error,description){return`${url}${url.includes(\"?\")?\"&\":\"?\"}error=${error}&error_description=${description}`}__name(formatErrorURL,\"formatErrorURL\");function getErrorURL(ctx,error,description){return formatErrorURL(ctx.context.options.onAPIError?.errorURL||`${ctx.context.baseURL}/error`,error,description)}__name(getErrorURL,\"getErrorURL\");async function authorize(ctx,options){const handleRedirect=__name(url=>{if((0,import_fetch_metadata.isBrowserFetchRequest)(ctx.request?.headers))return ctx.json({redirect:true,url});else throw ctx.redirect(url)},\"handleRedirect\");const opts={codeExpiresIn:600,defaultScope:\"openid\",...options,scopes:[\"openid\",\"profile\",\"email\",\"offline_access\",...options?.scopes||[]]};if(!ctx.request)throw new import_error2.APIError(\"UNAUTHORIZED\",{error_description:\"request not found\",error:\"invalid_request\"});const query=ctx.query;const session=await(0,import_session.getSessionFromCtx)(ctx);if(!session){if((0,import_prompt.parsePrompt)(query.prompt??\"\").has(\"none\")){if(!query.redirect_uri)throw new import_error.InvalidRequest(\"redirect_uri is required when prompt=none and must be usable to return errors without displaying UI\");if(!query.client_id)throw new import_error.InvalidClient(\"client_id is required\");const client2=await(0,import_index.getClient)(query.client_id,options.trustedClients||[]);if(!client2)throw new import_error.InvalidClient(\"client_id is required\");const validRedirectURI=client2.redirectUrls.find(url=>url===query.redirect_uri);if(!validRedirectURI)throw new import_error.InvalidRequest(\"redirect_uri is invalid or not registered for this client\");return handleRedirect(formatErrorURL(validRedirectURI,\"login_required\",\"Authentication required but prompt is none\"))}await ctx.setSignedCookie(\"oidc_login_prompt\",JSON.stringify(ctx.query),ctx.context.secret,{maxAge:600,path:\"/\",sameSite:\"lax\"});const queryFromURL=ctx.request.url?.split(\"?\")[1];return handleRedirect(`${options.loginPage}?${queryFromURL}`)}if(!query.client_id){const errorURL=getErrorURL(ctx,\"invalid_client\",\"client_id is required\");throw ctx.redirect(errorURL)}if(!query.response_type){const errorURL=getErrorURL(ctx,\"invalid_request\",\"response_type is required\");throw ctx.redirect(errorURL)}const client=await(0,import_index.getClient)(ctx.query.client_id,options.trustedClients||[]);if(!client){const errorURL=getErrorURL(ctx,\"invalid_client\",\"client_id is required\");throw ctx.redirect(errorURL)}const redirectURI=client.redirectUrls.find(url=>url===ctx.query.redirect_uri);if(!redirectURI||!query.redirect_uri)throw new import_error2.APIError(\"BAD_REQUEST\",{message:\"Invalid redirect URI\"});if(client.disabled){const errorURL=getErrorURL(ctx,\"client_disabled\",\"client is disabled\");throw ctx.redirect(errorURL)}if(query.response_type!==\"code\"){const errorURL=getErrorURL(ctx,\"unsupported_response_type\",\"unsupported response type\");throw ctx.redirect(errorURL)}const requestScope=query.scope?.split(\" \").filter(s=>s)||opts.defaultScope?.split(\" \")||[];const invalidScopes=requestScope.filter(scope=>{return!opts.scopes.includes(scope)});if(invalidScopes.length)return handleRedirect(formatErrorURL(query.redirect_uri,\"invalid_scope\",`The following scopes are invalid: ${invalidScopes.join(\", \")}`));if((!query.code_challenge||!query.code_challenge_method)&&options.requirePKCE)return handleRedirect(formatErrorURL(query.redirect_uri,\"invalid_request\",\"pkce is required\"));if(!query.code_challenge_method)query.code_challenge_method=\"plain\";if(![\"s256\",options.allowPlainCodeChallengeMethod?\"plain\":\"s256\"].includes(query.code_challenge_method?.toLowerCase()||\"\"))return handleRedirect(formatErrorURL(query.redirect_uri,\"invalid_request\",\"invalid code_challenge method\"));const code=(0,import_random.generateRandomString)(32,\"a-z\",\"A-Z\",\"0-9\");const codeExpiresInMs=opts.codeExpiresIn*1e3;const expiresAt=new Date(Date.now()+codeExpiresInMs);const skipConsentForTrustedClient=client.skipConsent;const hasAlreadyConsented=await ctx.context.adapter.findOne({model:\"oauthConsent\",where:[{field:\"clientId\",value:client.clientId},{field:\"userId\",value:session.user.id}]}).then(res=>{if(!res?.consentGiven)return false;const consentedScopes=res.scopes?res.scopes.split(\" \"):[];return requestScope.every(scope=>consentedScopes.includes(scope))});const promptSet=(0,import_prompt.parsePrompt)(query.prompt??\"\");if(promptSet.has(\"none\")){if(!skipConsentForTrustedClient&&!hasAlreadyConsented)return handleRedirect(formatErrorURL(query.redirect_uri,\"consent_required\",\"Consent required but prompt is none\"))}let requireLogin=promptSet.has(\"login\");if(query.max_age!==void 0){const maxAge=Number(query.max_age);if(Number.isInteger(maxAge)&&maxAge>=0){if((Date.now()-new Date(session.session.createdAt).getTime())/1e3>maxAge)requireLogin=true}}const requireConsent=!skipConsentForTrustedClient&&(!hasAlreadyConsented||promptSet.has(\"consent\"));try{await ctx.context.internalAdapter.createVerificationValue({value:JSON.stringify({clientId:client.clientId,redirectURI:query.redirect_uri,scope:requestScope,userId:session.user.id,authTime:new Date(session.session.createdAt).getTime(),requireConsent,state:requireConsent?query.state:null,codeChallenge:query.code_challenge,codeChallengeMethod:query.code_challenge_method,nonce:query.nonce}),identifier:code,expiresAt})}catch{return handleRedirect(formatErrorURL(query.redirect_uri,\"server_error\",\"An error occurred while processing the request\"))}if(requireLogin){await ctx.setSignedCookie(\"oidc_login_prompt\",JSON.stringify(ctx.query),ctx.context.secret,{maxAge:600,path:\"/\",sameSite:\"lax\"});await ctx.setSignedCookie(\"oidc_consent_prompt\",code,ctx.context.secret,{maxAge:600,path:\"/\",sameSite:\"lax\"});return handleRedirect(`${options.loginPage}?${new URLSearchParams({client_id:client.clientId,code,state:query.state}).toString()}`)}if(!requireConsent){const redirectURIWithCode=new URL(redirectURI);redirectURIWithCode.searchParams.set(\"code\",code);redirectURIWithCode.searchParams.set(\"state\",ctx.query.state);return handleRedirect(redirectURIWithCode.toString())}if(options?.consentPage){await ctx.setSignedCookie(\"oidc_consent_prompt\",code,ctx.context.secret,{maxAge:600,path:\"/\",sameSite:\"lax\"});const urlParams=new URLSearchParams;urlParams.set(\"consent_code\",code);urlParams.set(\"client_id\",client.clientId);urlParams.set(\"scope\",requestScope.join(\" \"));return handleRedirect(`${options.consentPage}?${urlParams.toString()}`)}const htmlFn=options?.getConsentHTML;if(!htmlFn)throw new import_error2.APIError(\"INTERNAL_SERVER_ERROR\",{message:\"No consent page provided\"});return new Response(htmlFn({scopes:requestScope,clientMetadata:client.metadata,clientIcon:client?.icon,clientId:client.clientId,clientName:client.name,code}),{headers:{\"content-type\":\"text/html\"}})}__name(authorize,\"authorize\");0&&(module.exports={authorize});\n})()\n","warnings":[],"map":{"version":3,"mappings":";suBAAA,gJAAqC,mCACrC,kBAAO,kCACP,mBAAkC,wCAClC,eAAO,+BACP,iBAA8C,uBAC9C,kBAA4B,8BAC5B,iBAA0B,uBAC1B,IAAAA,cAAyB,mCACzB,0BAAsC,kDAGtC,SAAS,eAAe,IAAK,MAAO,YAAa,CAChD,MAAO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I,QAAS,IAAI,MAAM,KAAK,EAC7D,OAAO,eAAe,oBAAoB,SAAS,CAAC,CACrD,CACA,GAAI,SAAS,YAAa,CACzB,MAAM,IAAI,gBAAgB,sBAAuB,KAAM,IAAI,QAAQ,OAAQ,CAC1E,OAAQ,IACR,KAAM,IACN,SAAU,KACX,CAAC,EACD,MAAM,UAAY,IAAI,gBACtB,UAAU,IAAI,eAAgB,IAAI,EAClC,UAAU,IAAI,YAAa,OAAO,QAAQ,EAC1C,UAAU,IAAI,QAAS,aAAa,KAAK,GAAG,CAAC,EAC7C,OAAO,eAAe,GAAG,QAAQ,WAAW,IAAI,UAAU,SAAS,CAAC,EAAE,CACvE,CACA,MAAM,OAAS,SAAS,eACxB,GAAI,CAAC,OAAQ,MAAM,IAAI,uBAAS,wBAAyB,CAAE,QAAS,0BAA2B,CAAC,EAChG,OAAO,IAAI,SAAS,OAAO,CAC1B,OAAQ,aACR,eAAgB,OAAO,SACvB,WAAY,QAAQ,KACpB,SAAU,OAAO,SACjB,WAAY,OAAO,KACnB,IACD,CAAC,EAAG,CAAE,QAAS,CAAE,eAAgB,WAAY,CAAE,CAAC,CACjD,CArLe","names":["import_error","client"],"ignoreList":[],"sources":["/opt/canhelp/node_modules/better-auth/dist/plugins/oidc-provider/authorize.mjs"],"sourcesContent":[null]}}