import{L as x,k as g,H as b,y as A,b as T,h as e,x as P}from"./entry.30e55a5e.js";import{d as k}from"./component.bb17d696.js";import{u as f}from"./api.9517b989.js";import"./composables.a8da7a35.js";function S(t,a){const n=Array.isArray(a)?a:[a];for(let i=0;i<n.length;i++)if(x(t,n[i]))return!0;return!1}function z(t){if(typeof t.client_id!="string"||!t.client_id||t.client_id.length===0)throw new Error("The client_id is invalid");if(typeof t.redirect_uri!="string"||!t.redirect_uri||t.redirect_uri.length===0)throw new Error("The redirect_uri is invalid");if(typeof t.response_type>"u"&&(t.response_type="code"),typeof t.response_type!="string"||t.response_type.length===0)throw new Error("The response_type is invalid");return typeof t.scope>"u"&&(t.scope=""),Array.isArray(t.scope)&&(t.scope=t.scope.join(" ")),{client_id:decodeURIComponent(t.client_id),response_type:t.response_type,redirect_uri:decodeURIComponent(t.redirect_uri),scope:decodeURIComponent(t.scope||"")}}const R=k({async setup(){const t=g();if(!b().loggedIn)return A({path:"/login",query:{...t.query,redirect:t.path}});const n=s=>e("div",{class:"d-flex align-items-center justify-content-center h-100"},[e("div",{class:"oauth2-wrapper panel-card p-3 flex-column"},[e("div",{class:"text-center"},[e("i",{class:"fa-solid fa-exclamation fa-10x text-danger"})]),e("div",{class:"text-center fs-6 p-3"},[s])])]),i=z(t.query),r=T(null);try{r.value=await f().client.getOne(i.client_id)}catch{return()=>n(["The",e("strong",{class:"ps-1 pe-1"},"client_id"),"is invalid"])}const v=(r.value.redirect_uri||"").split(",");if(!S(i.redirect_uri,v))return()=>n(["The",e("strong",{class:"ps-1 pe-1"},"redirect_uri"),"does not match."]);let c=[];typeof i.scope=="string"&&i.scope.length>0&&(c=i.scope.split(" "));const d={filter:{client_id:r.value.id},relations:["scope"]};d.filter&&c.length>0&&(d.filter.scope={name:c});const{data:u}=await f().clientScope.getMany(d),m=()=>{const s=new URL(`${i.redirect_uri}`);s.searchParams.set("error","access_denied"),s.searchParams.set("error_description","The resource owner or authorization server denied the request"),t.query.state&&typeof t.query.state=="string"&&s.searchParams.set("state",t.query.state),window.location.href=s.href},h=async()=>{try{const s=await f().post("authorize",{response_type:i.response_type,client_id:r.value.id,redirect_uri:i.redirect_uri,...t.query.state?{state:t.query.state}:{},scope:u.map(p=>p.scope.name).join(" ")}),{url:l}=s.data;window.location.href=l}catch(s){s instanceof Error&&P().warning(s.message)}},_=()=>{const s=e("div",{class:"flex-column d-flex"},[e("div",{class:"text-center text-secondary"},[e("div","An external application"),e("div",{class:"fs-5 fw-bold"},r.value.name),e("div","wants to access your account")])]);r.value.id,c.length>0;const l=u.map(o=>e("div",{class:"d-flex flex-row"},[e("div",{style:{width:"15px"},class:"text-center"},[e("i",{class:"fa-solid fa-check text-success"})]),e("div",{class:"ms-1"},[e("small",[o.scope.name])])])),p=e("div",{class:"mt-2 mb-2"},[e("div",["This will allow the",e("strong",{class:"ps-1 pe-1"},r.value.name),"developer to:"]),e("div",{class:"flex-column"},[l])]),y=e("div",{class:"mt-auto"},[e("div",{class:"d-flex flex-row"},[e("div",[e("i",{class:"fa-solid fa-link"})]),e("div",{class:"ms-1"},[e("small",["Once authorized, you will be redirected to:"," ",i.redirect_uri])])]),e("div",{class:"d-flex flex-row"},[e("div",[e("i",{class:"fa-solid fa-lock"})]),e("div",{class:"ms-1"},[e("small",["This application is governed by the",e("strong",{class:"ps-1 pe-1"},r.value.name),"developer's Privacy Policy and Terms of Service."])])]),e("div",{class:"d-flex flex-row"},[e("div",[e("i",{class:"fa-solid fa-clock"})]),e("div",{class:"ms-1"},[e("small",["Active since"," ",`${r.value.created_at}`])])])]),w=e("div",{class:"d-flex justify-content-evenly mt-auto"},[e("div",[e("button",{type:"button",class:"btn btn-sm btn-secondary",onClick:o=>(o.preventDefault(),m())},["Abort"])]),e("div",[e("button",{type:"button",class:"btn btn-sm btn-primary",onClick:o=>(o.preventDefault(),h())},["Authorize"])])]);return e("div",{class:"d-flex align-items-center justify-content-center h-100"},[e("div",{class:"oauth2-wrapper panel-card p-3 flex-column"},[s,p,y,w])])};return()=>_()}});export{R as default};
