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