import{setCookie as b}from"hono/cookie";import{withPathPrefix as l}from"@redocly/theme/core/utils";import{DEV_LOGIN_SLUG as A}from"../../constants/common.js";import{CACHE_CONTROL_NO_STORE_HEADER_VALUE as y}from"../constants/common.js";import{getAuthProviderLoginParams as C,buildLoginUrl as O}from"./auth.js";import{renderPage as R}from"../ssr/index.js";import{telemetry as _}from"../../cli/telemetry/index.js";async function T(r,t,o,n){const{isAuthenticated:a}=r.get("auth"),i=r.req.raw.headers.get("x-forwarded-host"),e=r.req.raw.headers.get("x-forwarded-proto"),w=i?`${e==="http"||e==="https"?e:"https"}://${i}`:new URL(r.req.url).origin,g=t.getConfig().ssoDirect,p=Object.keys(g||{}),s=n||p[0],m=g?.[s];if(a)return v(r,t,{slug:o},403);const d=s&&m?await C(s,m):void 0,f=d?{...d,extraParams:{...d.extraParams,prompt:"login"}}:void 0,{loginUrl:E,cookies:h={}}=f&&O(f,w,l(o))||{},L=t.globalData.auth?.devLogin||p.length>1?z(o):E;return Object.keys(h).forEach(c=>{b(r,c,h[c].value,h[c].options)}),L?r.newResponse(null,302,{Location:L}):r.text("Unauthorized",401)}const P={};async function v(r,t,o,n,a){const i=a||String(n);let e=P[i];if(!e){const u={templateId:String(a||n),fsPath:"/",...o,baseSlug:o.slug};e=(await R(u,{},r,t,_)).html,P[i]=e}return r.html(e,n,{"Cache-Control":y})}function z(r){const t=new URLSearchParams({redirectTo:l(r)});return`${l(A)}?${t}`}async function j(r){return r.text("Forbidden",U(r))}function k(r){return r.json({message:"Forbidden"},U(r))}function U(r){const{isAuthenticated:t}=r.get("auth");return t?403:401}function G(r){const t=r?.match(/(?:^|:)(\d{1,3}(?:\.\d{1,3}){3})$/);return t?t[1]:r}export{z as getLoginUrlWithRedirect,v as handleErrorPageRender,T as handleUnauthorized,k as handleUnauthorizedApiRequest,j as handleUnauthorizedAsset,G as normalizeIpAddress};
