bjx-auth 1.25.1 → 1.26.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.
- package/dist/bjx-auth-api.esm.js +1 -1
- package/dist/bjx-auth-api.umd.js +1 -1
- package/dist/bjx-auth.esm.js +1 -1
- package/dist/bjx-auth.umd.js +1 -1
- package/dist/index-BWc8egXM.js +2 -0
- package/dist/index-TA8vHl0I.js +1 -1
- package/dist/index.cjs +2 -2
- package/dist/request.cjs +1 -1
- package/dist/strategy.cjs +2 -2
- package/dist/web.mjs +1 -1
- package/package.json +1 -1
- package/dist/index-47Ugpz6x.js +0 -2
package/dist/index-47Ugpz6x.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
/*! version: 1.25.1 */
|
|
2
|
-
"use strict";var e,t,s,r,o,n=require("./index-TA8vHl0I.js"),i=require("passport"),a={exports:{}};function h(){if(r)return s;r=1;const{Strategy:o}=i,{setConfig:a,getConfig:h,getTokenApi:c,getUserInfoApi:u}=function(){if(t)return e;t=1;const{setConfig:s,getConfig:r,getToken:o,getUserInfo:i}=n.requireRequest();return e={setConfig:s,getConfig:r,getTokenApi:async function(e,t,{headers:s,ctx:r}){return o({__isRefresh__:t,headers:{Cookie:Object.entries(e).map(e=>e.join("=")).join("; "),...s}},{ctx:r}).then(({data:t,headers:s})=>{if(!1===t.isError)return{token_type:"",access_token:t.data.authToken,expires_at:t.data.expiresAt||Date.now()/1e3+14400|0,__flag__:e["idsrv.session"]};throw t.set_cookie=s["set-cookie"],new Error("getTokenApi invalid data",{cause:t})})},getUserInfoApi:async function(e,t,{headers:s,ctx:r}){const o={};return e.startsWith("Bearer ")?o.Authorization=e:o.AuthToken=e,i({__type__:t,headers:{...s,...o}},{ctx:r}).then(({data:e})=>{if(!1===e.IsError)return e.Data;throw new Error("getUserInfoApi invalid data",{cause:e})})}}}(),{errorLogger:f,debugLogger:d}=n.requireLogger();return s={BjxStrategy:class extends o{constructor(e={},t){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=t||((e,t)=>t(null,e)),a(e.authConfig),e.authConfig.debug&&d("配置项",h())}normalizeExpires(e){return"number"==typeof e&&e>0?60*e*1e3:18e5}authenticate(e,t){const s=e.cookies&&e.cookies.get("idsrv.session")&&e.cookies.get(".AspNetCore.Identity.Application"),r=e.headers&&(e.headers.authtoken||e.headers.authorization);if(!(s||this.handleHeadrToken&&r))return d("认证失败",s,"(",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.fail(e)}):s?this.executeAuthentication(e).then(e=>this.verify(e,(e,t)=>{if(e)return this.error(e);this.success(t)})).catch(e=>{"__goto_next__"===e.message?this.pass():this.fail(e)}):this.pass()}async executeCosplayAuthentication(e){let t="",s=!1;e.headers?.authtoken?t=e.headers.authtoken:e.headers?.authorization&&(t=e.headers.authorization.replace("Bearer ",""),s=!0);const r=e?.session?.passport?.user||{};if(r.token?.access_token===t)throw new Error("__goto_next__");r.info=null;const o=await this.getUserInfoWithRefresh((s?"Bearer ":"")+t,e,{});if(o)return d(`通过${s?"Authorization":"AuthToken"}头信息登录系统`),s?{token:{token_type:"Bearer",access_token:t,expires_at:Date.now()/1e3+3600|0,__source__:"header"},info:this.loadUserInfo?o:void 0}:{token:{token_type:"",access_token:t,expires_at:Date.now()/1e3+3600|0,__flag__:"This token from AuthToken header",__source__:"header"},info:this.loadUserInfo?o:void 0};throw new Error("Invalid headers token")}async executeAuthentication(e){const t=this.extractHeaders(e),s={"idsrv.session":e.cookies.get("idsrv.session"),".AspNetCore.Identity.Application":e.cookies.get(".AspNetCore.Identity.Application")},r=await this.getTokenWithRefresh(s,e,t);if(!r)throw new Error("Empty token data");let o;if(this.loadUserInfo){const s=r.access_token;if(o=await this.getUserInfoWithRefresh(s,e,t),!o)throw new Error("Empty userInfo data")}return{token:r,info:o}}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,t,s){const r=t?.session?.passport?.user||{},o=Date.now();if(r.token?.__flag__!==e["idsrv.session"]&&(delete r.token,delete r.info,d(r.token?.__flag__?"令牌缓存标识和cookie内的不一致":"令牌缓存不存在")),r.token&&1e3*r.token.expires_at>o)throw new Error("__goto_next__");const n=!!r.token;let i;n&&d("刷新令牌");try{i=await c(e,n,{ctx:t.ctx,headers:s})}catch(e){try{e?.cause?.set_cookie&&t?.ctx?.response?.set&&(t.ctx.response.set("Set-Cookie",e.cause.set_cookie),d("转发授权服务器cookie: 成功",e.cause.set_cookie))}catch(e){f(e)}throw new Error("Get token failed",{cause:e})}return d("获取“新”令牌: "+(i?"成功":"失败"),e,n,i||"无"),i}async getUserInfoWithRefresh(e,t,s){const r=t?.session?.passport?.user||{},o=Date.now();if(r.info&&r.info.__expires_at__>o)throw new Error("__goto_next__");let n;try{n=await u(e,this.userInfoType,{ctx:t.ctx,headers:s})}catch(e){throw f(e),new Error("Get userInfo failed",{cause:e})}return n&&(n.__expires_at__=o+this.userInfoExpires),d("获取“新”用户信息: "+(n?"成功":"失败"),e,n||"无"),n}},createBjxAuthMiddleware:function(e,t){const{excludeRouterReg:s=/^\/(oidc|sso|proxy|css|img|js)(\/.*|$)/i,app:r}=t||{};let o=!1;return r&&(o=!0,a("app",r)),async(t,r)=>{if(o||h("app")||a("app",t.app),s.test(t.path)||"/favicon.ico"===t.path)return r();await new Promise((s,r)=>{e.authenticate("bjx",(e,r,o,n)=>{e?(d("this.error() is called"),f(e,t),t.logout()):!1===r?(d("this.fail() is called"),o&&f(o,t),t.logout()):r&&(d("this.success() is called"),t.login(r)),s()})(t,()=>{d("this.pass() is called"),s()})}),await r()}}}}exports.requireStrategy=function(){return o||(o=1,function(e){const{setConfig:t,getConfig:s}=n.requireConfig(),r=h(),o=n.requireUtils();e.exports=Object.assign({setConfig:t,getConfig:s},r,o)}(a)),a.exports};
|