bjx-auth 1.0.3 → 1.1.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bjx-auth",
3
- "version": "1.0.3",
3
+ "version": "1.1.0",
4
4
  "exports": {
5
5
  ".": {
6
6
  "require": "./dist/bjx-auth-api.umd.js",
package/src/config.js CHANGED
@@ -32,7 +32,7 @@ async function setConfig(key, val) {
32
32
  for (const k in key) {
33
33
  setKey(k, key[k])
34
34
  }
35
- } else {
35
+ } else if (key) {
36
36
  setKey(key, val)
37
37
  }
38
38
  }
@@ -1,5 +1,6 @@
1
1
  const {
2
2
  setConfig,
3
+ getConfig,
3
4
  getToken: getTokenApi,
4
5
  getUserInfo: getUserInfoApi,
5
6
  } = require('../request')
@@ -51,6 +52,7 @@ async function getUserInfo(token, type, { headers, ctx }) {
51
52
 
52
53
  module.exports = {
53
54
  setConfig,
55
+ getConfig,
54
56
  getToken,
55
57
  getUserInfo,
56
58
  }
@@ -1,5 +1,5 @@
1
1
  const { Strategy } = require('passport')
2
- const { setConfig, getToken, getUserInfo } = require('./handle')
2
+ const { setConfig, getConfig, getToken, getUserInfo } = require('./handle')
3
3
  const { errorLogger, debugLogger } = require('../logger')
4
4
 
5
5
  class BjxStrategy extends Strategy {
@@ -13,11 +13,14 @@ class BjxStrategy extends Strategy {
13
13
  this.loadUserInfo = options.loadUserInfo || false
14
14
  this.userInfoDuration = this.normalizeDuration(options.loadUserInfo)
15
15
  this.userInfoType = options.userInfoType || ''
16
- this.authConfig = options.authConfig || {}
16
+ this.handleHeadrToken = options.handleHeadrToken || false
17
17
  this.verify = verify || ((user, done) => done(null, user))
18
18
 
19
19
  // 设置配置缓存
20
- setConfig(this.authConfig)
20
+ setConfig(options.authConfig)
21
+ if (options.authConfig.debug) {
22
+ debugLogger('配置项', getConfig())
23
+ }
21
24
  }
22
25
 
23
26
  normalizeDuration(val) {
@@ -35,27 +38,72 @@ class BjxStrategy extends Strategy {
35
38
  req.cookies.get('idsrv.session') &&
36
39
  req.cookies.get('.AspNetCore.Identity.Application')
37
40
 
41
+ // 检查必要的header
42
+ const hasRequiredHeaders =
43
+ req.headers && (req.headers.authtoken || req.headers.authorization)
44
+
38
45
  // 假如不存在 则认证失败
39
- if (!hasRequiredCookies) {
40
- return this.fail(new Error('Missing required cookies'))
46
+ if (!hasRequiredCookies && !(this.handleHeadrToken && hasRequiredHeaders)) {
47
+ return this.fail(new Error('Missing required cookies or headers'))
41
48
  }
42
49
 
43
- // 否则 执行认证流程
44
- this.executeAuthentication(req)
45
- .then((user) =>
46
- // 创建策略时 可传入回调函数 已追加认证逻辑
47
- this.verify(user, (err, verifiedUser) => {
48
- if (err) return this.error(err)
49
- this.success(verifiedUser)
50
- }),
51
- )
52
- .catch((err) => {
53
- if (err.message === '__goto_next__') {
54
- this.pass()
55
- } else {
56
- this.error(err)
57
- }
58
- })
50
+ if (this.handleHeadrToken && hasRequiredHeaders) {
51
+ this.executeCosplayAuthentication(req, hasRequiredHeaders)
52
+ .then((user) => {
53
+ this.success(user)
54
+ })
55
+ .catch((err) => {
56
+ if (err.message === '__goto_next__') {
57
+ this.pass()
58
+ } else {
59
+ this.error(err)
60
+ }
61
+ })
62
+ } else if (hasRequiredCookies) {
63
+ // 否则 执行认证流程
64
+ this.executeAuthentication(req)
65
+ .then((user) =>
66
+ // 创建策略时 可传入回调函数 已追加认证逻辑
67
+ this.verify(user, (err, verifiedUser) => {
68
+ if (err) return this.error(err)
69
+ this.success(verifiedUser)
70
+ }),
71
+ )
72
+ .catch((err) => {
73
+ if (err.message === '__goto_next__') {
74
+ this.pass()
75
+ } else {
76
+ this.error(err)
77
+ }
78
+ })
79
+ } else {
80
+ this.pass()
81
+ }
82
+ }
83
+
84
+ async executeCosplayAuthentication(req, token) {
85
+ token = token.replace('Bearer ', '')
86
+ const su = req?.session?.passport?.user || {}
87
+ if (su.token?.access_token === token) {
88
+ throw new Error('__goto_next__')
89
+ }
90
+
91
+ // 假如session里面没有不是该token说明换了 需要重新获取
92
+ su.info = null
93
+ const userInfo = await this.getUserInfoWithRefresh(token, req, {})
94
+ if (userInfo) {
95
+ return {
96
+ token: {
97
+ bjx_token_flag: 'This token from authtoken/authorization header',
98
+ token_type: '',
99
+ access_token: token,
100
+ expires_at: (Date.now() / 1000 + 4 * 600) | 0,
101
+ },
102
+ info: userInfo,
103
+ }
104
+ } else {
105
+ throw new Error('Invalid headers token')
106
+ }
59
107
  }
60
108
 
61
109
  async executeAuthentication(req) {
@@ -188,13 +188,16 @@ function getLoginCenterUrl(opts, type = '') {
188
188
  } = opts || {}
189
189
  if (!site && !ctx) throw new Error('site is required')
190
190
  if (!returnUrl && !ctx) throw new Error('returnUrl is required')
191
+
191
192
  const { clientId, login: loginCenter } = config
193
+
192
194
  const pp = objToQs({
193
195
  BA: BA || ctx?.query?.ba || config.ba || '',
194
196
  BP: BP || ctx?.query?.bp || config.bp || '',
195
197
  OS: OS || config.os || 1,
196
198
  EQP: EQP || config.eqp || '',
197
199
  })
200
+
198
201
  let sr
199
202
  if (ctx && (!site || !returnUrl)) {
200
203
  sr = handleCtx(ctx, !site)