bjx-auth 1.6.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +0,0 @@
1
- /*! version: 1.6.0 */
2
- "use strict";var e=require("./index-n4gNgnia.js"),o=require("passport");require("crypto"),require("axios");var t,r,n,s,i,a,c,h={exports:{}};function d(){if(s)return n;s=1;const{Strategy:i}=o,{setConfig:a,getConfig:c,getToken:h,getUserInfo:d}=function(){if(r)return t;r=1;const{setConfig:o,getConfig:n,getToken:s,getUserInfo:i}=e.requireRequest();return t={setConfig:o,getConfig:n,getToken:async function(e,o,{headers:t,ctx:r}){return s({__isRefresh__:o,headers:{Cookie:Object.entries(e).map(e=>e.join("=")).join("; "),...t}},{ctx:r}).then(({data:o})=>{if(!1===o.isError)return{token_type:"",access_token:o.data.authToken,expires_at:o.data.expiresAt||Date.now()/1e3+14400|0,__flag__:e["idsrv.session"]}})},getUserInfo:async function(e,o,{headers:t,ctx:r}){const n={};return e.startsWith("Bearer ")?n.Authorization=e:n.AuthToken=e,i({__type__:o,headers:{...t,...n}},{ctx:r}).then(({data:e})=>{if(!1===e.IsError)return e.Data})}}}(),{errorLogger:u,debugLogger:j}=e.requireLogger();return n={BjxStrategy:class extends i{constructor(e={},o){super(),this.name="bjx",this.loadUserInfo=e.loadUserInfo||!1,this.userInfoExpires=this.normalizeExpires(e.loadUserInfo),this.userInfoType=e.userInfoType||"",this.handleHeadrToken=e.handleHeadrToken||!1,this.verify=o||((e,o)=>o(null,e)),a(e.authConfig),e.authConfig.debug&&j("配置项",c())}normalizeExpires(e){return"number"==typeof e&&e>0?60*e*1e3:18e5}authenticate(e,o){const t=e.cookies&&e.cookies.get("idsrv.session")&&e.cookies.get(".AspNetCore.Identity.Application"),r=e.headers&&(e.headers.authtoken||e.headers.authorization);if(!(t||this.handleHeadrToken&&r))return j("认证失败",t,"(",this.handleHeadrToken,r,")"),this.fail();this.handleHeadrToken&&r?this.executeCosplayAuthentication(e).then(e=>{this.success(e)}).catch(e=>{"__goto_next__"===e.message?this.pass():this.error(e)}):t?this.executeAuthentication(e).then(e=>this.verify(e,(e,o)=>{if(e)return this.error(e);this.success(o)})).catch(e=>{"__goto_next__"===e.message?this.pass():this.error(e)}):this.pass()}async executeCosplayAuthentication(e){let o="",t=!1;e.headers?.authtoken?o=e.headers.authtoken:e.headers?.authorization&&(o=e.headers.authorization.replace("Bearer ",""),t=!0);const r=e?.session?.passport?.user||{};if(r.token?.access_token===o)throw new Error("__goto_next__");r.info=null;const n=await this.getUserInfoWithRefresh((t?"Bearer ":"")+o,e,{});if(n)return j(`通过${t?"Authorization":"AuthToken"}头信息登录系统`),t?{token:{token_type:"Bearer",access_token:o,expires_at:Date.now()/1e3+3600|0,__source__:"header"},info:this.loadUserInfo?n:void 0}:{token:{token_type:"",access_token:o,expires_at:Date.now()/1e3+3600|0,__flag__:"This token from AuthToken header",__source__:"header"},info:this.loadUserInfo?n:void 0};throw new Error("Invalid headers token")}async executeAuthentication(e){const o=this.extractHeaders(e),t={"idsrv.session":e.cookies.get("idsrv.session"),".AspNetCore.Identity.Application":e.cookies.get(".AspNetCore.Identity.Application")},r=await this.getTokenWithRefresh(t,e,o);if(!r)throw new Error("Get token failed");let n;if(this.loadUserInfo){const t=r.access_token;n=await this.getUserInfoWithRefresh(t,e,o)}return{token:r,info:n}}extractHeaders(e){return{"X-Forwarded-For":e.headers["x-forwarded-for"],"X-Forwarded-Host":e.headers["x-forwarded-host"],"User-Agent":e.headers["user-agent"],Referer:e.headers.referer}}async getTokenWithRefresh(e,o,t){const r=o?.session?.passport?.user||{},n=Date.now();if(r.token?.__flag__!==e["idsrv.session"]&&(r.token=void 0,r.info=void 0,j("令牌标识和cookie内的不一致")),r.token&&1e3*r.token.expires_at>n)throw new Error("__goto_next__");const s=!!r.token;s&&j("刷新令牌");const i=await h(e,s,{ctx:o.ctx,headers:t});return j("获取“新”令牌",e,s,i||"无"),i}async getUserInfoWithRefresh(e,o,t){const r=o?.session?.passport?.user||{},n=Date.now();if(r.info&&r.info.__expires_at__>n)throw new Error("__goto_next__");const s=await d(e,this.userInfoType,{ctx:o.ctx,headers:t});return s&&(s.__expires_at__=n+this.userInfoExpires),j("获取“新”用户信息",e,s||"无"),s}},createBjxAuthMiddleware:function(e){return async(o,t)=>{c("app")||a("app",o.app),await new Promise(t=>{e.authenticate("bjx",(e,r,n,s)=>{e?(u(e),o.logout()):r?o.login(r):o.logout(),t()})(o,()=>{t()})}),await t()}}}}function u(){if(a)return i;a=1;const o=e.requireConfig();function t(e){if(!e||0===Object.keys(e).length)return"";const o=[];for(const t in e)e[t]&&o.push(`${t}=${encodeURIComponent(e[t])}`);return o.join("&")}return i={getLoginCenterUrl:function(e,r=""){const{ctx:n,site:s="",returnUrl:i="",BA:a="",BP:c="",OS:h="",EQP:d="",...u}=e||{};if(!s&&!n)throw new Error("site is required");if(!i&&!n)throw new Error("returnUrl is required");const{clientId:j,login:b}=o,f=t({BA:a||n?.query?.ba||o.ba||"",BP:c||n?.query?.bp||o.bp||"",OS:h||o.os||1,EQP:d||n?.uuid||o.eqp||""});let g;!n||s&&i||(g=function(e,o){let t=e.query.f||"";const r=e.origin,n=new RegExp("^"+r);if(/^\//.test(t))t=r+t;else if(t&&!n.test(t))t=r;else if(!t){const o=e.headers.referer;t=n.test(o)&&o!==e.href?o:r}const s={returnUrl:t};if(o){const e=t.replace(/^https?:\/\//,"").replace(/\/.*/,"");s.site=function(e){const o={hr:3e4,dljob:30100,hdjob:30101,fdjob:30102,sdjob:30103,hedjob:30104,gfjob:30105,zhnyfwjob:30106,shdjob:30107,tanjob:30108,qnjob:30109,dqjob:30200,cnjob:30201,spdjob:30202,zdhjob:30203,xxhjob:30204,hbjob:30300,scljob:30301,gfcljob:30302,dqzljob:30303,jchpjob:30304,hbfdjob:30305,hbgcjob:30306,hbsbjob:30307,hjxfjob:30308,jnjob:30309,gcjob:30400,dlgcjob:30401,jzjob:30402,szlqjob:30403,gdjob:30404,jdjob:30406,sjjob:30407,jljob:30408,gczjjob:30409,dcjob:30500,dcscjob:30501,dcyyjob:30502,dccljob:30503,dchsjob:30504,dcjsjob:30507,spdsbjob:30601,pdywjob:30602,pwpdgcjob:30603,znwdwjob:30604,zlpdwjob:30605,xndcjob:30606,dlscjob:30700,mhr:4e4,mdljob:40100,mhdjob:40101,mfdjob:40102,msdjob:40103,mhedjob:40104,mgfjob:40105,mzhnyfwjob:40106,mshdjob:40107,mtanjob:40108,mqnjob:40109,mdqjob:40200,mcnjob:40201,mspdjob:40202,mzdhjob:40203,mxxhjob:40204,mhbjob:40300,mscljob:40301,mgfcljob:40302,mdqzljob:40303,mjchpjob:40304,mhbfdjob:40305,mhbgcjob:40306,mhbsbjob:40307,mhjxfjob:40308,mjnjob:40309,mgcjob:40400,mdlgcjob:40401,mjzjob:40402,mszlqjob:40403,mgdjob:40404,mjdjob:40406,msjjob:40407,mjljob:40408,mgczjjob:40409,mdcjob:40500,mdcscjob:40501,mdcyyjob:40502,mdccljob:40503,mdchsjob:40504,mdcjsjob:40507,mspdsbjob:40601,mpdywjob:40602,mpwpdgcjob:40603,mznwdwjob:40604,mzlpdwjob:40605,mxndcjob:40606,mdlscjob:40700,yun:3099,yun:30991,yun:30992,yun:30993,yun:30994,yun:30995,yun:30996,sxh:3094,msxh:3093,xiaoyuan:3091,mxiaoyuan:4091,xxsxh:3090,mxxsxh:4090};for(const t in o){if(e.includes(t))return t+".bjx.com.cn";if(e.includes(o[t]))return t+".bjx.com.cn"}return"hr.bjx.com.cn"}(e)}return s}(n,!s));const l=t({site:s||g.site,clientId:j,pp:f,...u,returnUrl:i||g.returnUrl}),p=`/Account/Login?${l}`,_={login:`${b}${p}`,register:`${b}${`/Account/Register?${l}`}`,logout:`${b}${`/Account/Logout?${l}`}`,bind:`${b}${`/External/UserBind?${l}`}`,logout2:`${b}${`/Account/Logout?${l.replace("returnUrl=.+$","")+encodeURIComponent(p)}`}`,returnUrl:g.returnUrl};if(r){let e=[];if(Array.isArray(r)?e=r:"string"==typeof r&&(e=r.split(",").map(e=>e.trim())),!e.length)return _;const o={};return e.forEach(e=>{const t=_[e];t&&(o[e]=t)}),1===Object.keys(o).length?Object.values(o)[0]:Object.keys(o).length?o:_}return _}}}var j=(c||(c=1,function(o){const{setConfig:t,getConfig:r}=e.requireConfig(),n=d(),s=u();o.exports=Object.assign({setConfig:t,getConfig:r},n,s)}(h)),h.exports),b=e.getDefaultExportFromCjs(j);module.exports=b;