2 lines
2.4 KiB
JavaScript
2 lines
2.4 KiB
JavaScript
function l(e){const n=new Map(e.map(t=>[t.id,{...t,children:[]}])),r=[];for(const t of n.values()){const i=new Set([t.id]);let o=n.get(t.parent_id),a=!1;for(;o;){if(i.has(o.id)){a=!0;break}i.add(o.id),o=n.get(o.parent_id)}const s=n.get(t.parent_id);s&&!a?s.children.push(t):r.push(t)}return r}function u(e,n){return{company_id:e,admin_id:n.id,member_status:Number(n.member_status),can_login:Number(n.can_login)}}function d(e,n){if(/[\u0000-\u0020\u007f\\]/.test(e))throw new Error("系统入口地址无效,请联系管理员");const r=new URL(e),t=["localhost","127.0.0.1","[::1]"].includes(r.hostname);if(r.protocol!=="https:"&&!(r.protocol==="http:"&&(t||r.origin===n))||r.username||r.password||r.hash)throw new Error("系统入口地址无效,请联系管理员");return r}function h(e){if(!e.ready||!e.can_login)throw new Error(e.status_message||"当前账号暂不能进入此系统");const n=e.id==="kefu"&&e.http_entry_allowed===!0?new URL(e.entry_url).origin:void 0,r=d(e.entry_url,n);if(r.search)throw new Error("系统入口地址无效,请联系管理员");return r.href}function f(e,n){const r=["client_id","redirect_uri","state","code_challenge","code_challenge_method"];if(Object.keys(e).some(s=>!r.includes(s)))throw new Error("登录请求包含未知参数");const t=Object.fromEntries(r.map(s=>{const c=e[s];if(typeof c!="string"||!c||c.length>2048)throw new Error("登录请求参数缺失或重复");return[s,c]})),i=n.find(s=>s.id===t.client_id);if(!i||!["internet-hospital","kefu"].includes(t.client_id))throw new Error("未注册的应用登录请求");const o=new URL(h(i)),a=d(t.redirect_uri,i.id==="kefu"&&i.http_entry_allowed===!0?o.origin:void 0);if(a.href!==o.href||a.search||t.code_challenge_method!=="S256"||!/^[A-Za-z0-9_-]{43}$/.test(t.code_challenge)||!/^[A-Za-z0-9_-]{32,128}$/.test(t.state))throw new Error("应用登录请求校验失败");return t}function w(e,n,r,t=!1){if(typeof e!="string")throw new Error("未获取到有效的应用回调");const i=t?new URL(n).origin:void 0,o=d(e,i),a=d(n,i);if(o.origin!==a.origin||o.pathname!==a.pathname||a.search||o.searchParams.getAll("code").length!==1||o.searchParams.getAll("state").length!==1||!/^[A-Za-z0-9_-]{32,256}$/.test(o.searchParams.get("code")||"")||o.searchParams.get("state")!==r||[...o.searchParams.keys()].some(s=>s!=="code"&&s!=="state"))throw new Error("应用回调地址校验失败");return o.href}export{h as a,f as b,l as c,w as d,u as m};
|