bjx-auth 1.1.0 → 1.3.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.1.0",
3
+ "version": "1.3.0",
4
4
  "exports": {
5
5
  ".": {
6
6
  "require": "./dist/bjx-auth-api.umd.js",
@@ -23,12 +23,14 @@
23
23
  "license": "ISC",
24
24
  "description": "",
25
25
  "dependencies": {
26
+ "@rollup/plugin-babel": "^6.0.4",
26
27
  "axios": "^1.11.0",
27
28
  "crypto-js": "^4.2.0",
28
29
  "jsencrypt": "^3.5.4",
29
30
  "passport": "^0.4.0"
30
31
  },
31
32
  "devDependencies": {
33
+ "@babel/preset-env": "^7.28.3",
32
34
  "@rollup/plugin-commonjs": "^28.0.6",
33
35
  "@rollup/plugin-node-resolve": "^16.0.1",
34
36
  "@rollup/plugin-replace": "^6.0.2",
package/src/config.js CHANGED
@@ -20,6 +20,9 @@ const config = {
20
20
  // logger.js
21
21
  debug: false,
22
22
  debugApiUrl: '',
23
+
24
+ // 缓存koa的app对象
25
+ app: null,
23
26
  }
24
27
 
25
28
  function setKey(key, val) {
@@ -32,12 +32,20 @@ async function getToken(cookies, isRefresh, { headers, ctx }) {
32
32
  }
33
33
 
34
34
  async function getUserInfo(token, type, { headers, ctx }) {
35
+ // 兼容老的token 以支持老版本APP嵌入页面
36
+ const tokenHeader = {}
37
+ if (token.startsWith('Bearer ')) {
38
+ tokenHeader['Authorization'] = token
39
+ } else {
40
+ tokenHeader['AuthToken'] = token
41
+ }
42
+
35
43
  return getUserInfoApi(
36
44
  {
37
45
  __type__: type,
38
46
  headers: {
39
- AuthToken: token,
40
47
  ...headers,
48
+ ...tokenHeader,
41
49
  },
42
50
  },
43
51
  {
@@ -44,11 +44,12 @@ class BjxStrategy extends Strategy {
44
44
 
45
45
  // 假如不存在 则认证失败
46
46
  if (!hasRequiredCookies && !(this.handleHeadrToken && hasRequiredHeaders)) {
47
- return this.fail(new Error('Missing required cookies or headers'))
47
+ debugLogger('认证失败', hasRequiredCookies, '(', this.handleHeadrToken, hasRequiredHeaders, ')')
48
+ return this.fail()
48
49
  }
49
50
 
50
51
  if (this.handleHeadrToken && hasRequiredHeaders) {
51
- this.executeCosplayAuthentication(req, hasRequiredHeaders)
52
+ this.executeCosplayAuthentication(req)
52
53
  .then((user) => {
53
54
  this.success(user)
54
55
  })
@@ -81,8 +82,17 @@ class BjxStrategy extends Strategy {
81
82
  }
82
83
  }
83
84
 
84
- async executeCosplayAuthentication(req, token) {
85
- token = token.replace('Bearer ', '')
85
+ async executeCosplayAuthentication(req) {
86
+ // Bearer开始的 为Authorization头 否则为Authtoken头
87
+ let token = ''
88
+ let isAuthorization = false
89
+ if (req.headers?.authtoken) {
90
+ token = req.headers.authtoken
91
+ } else if (req.headers?.authorization) {
92
+ token = req.headers.authorization.replace('Bearer ', '')
93
+ isAuthorization = true
94
+ }
95
+
86
96
  const su = req?.session?.passport?.user || {}
87
97
  if (su.token?.access_token === token) {
88
98
  throw new Error('__goto_next__')
@@ -90,16 +100,34 @@ class BjxStrategy extends Strategy {
90
100
 
91
101
  // 假如session里面没有不是该token说明换了 需要重新获取
92
102
  su.info = null
93
- const userInfo = await this.getUserInfoWithRefresh(token, req, {})
103
+ const userInfo = await this.getUserInfoWithRefresh(
104
+ (isAuthorization ? 'Bearer ' : '') + token,
105
+ req,
106
+ {},
107
+ )
94
108
  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,
109
+ debugLogger(
110
+ `通过${isAuthorization ? 'Authorization' : 'Authtoken'}头信息登录系统`,
111
+ )
112
+ if (isAuthorization) {
113
+ return {
114
+ token: {
115
+ token_type: 'Bearer',
116
+ access_token: token,
117
+ expires_at: (Date.now() / 1000 + 4 * 600) | 0,
118
+ },
119
+ info: userInfo,
120
+ }
121
+ } else {
122
+ return {
123
+ token: {
124
+ bjx_token_flag: 'This token from authtoken/authorization header',
125
+ token_type: '',
126
+ access_token: token,
127
+ expires_at: (Date.now() / 1000 + 4 * 600) | 0,
128
+ },
129
+ info: userInfo,
130
+ }
103
131
  }
104
132
  } else {
105
133
  throw new Error('Invalid headers token')
@@ -163,7 +191,7 @@ class BjxStrategy extends Strategy {
163
191
  // 假如存在token 还到这一步 说明是刷新token
164
192
  const isRefresh = !!su.token
165
193
  if (isRefresh) {
166
- debugLogger('更新令牌')
194
+ debugLogger('刷新令牌')
167
195
  }
168
196
 
169
197
  // 获取新token
@@ -204,22 +232,28 @@ class BjxStrategy extends Strategy {
204
232
  // Koa中间件
205
233
  function createBjxAuthMiddleware(passport) {
206
234
  return async (ctx, next) => {
235
+ // 缓存app对象 方便打印日志
236
+ if (!getConfig('app')) {
237
+ setConfig('app', ctx.app)
238
+ }
239
+
240
+ // 等待每次鉴权结果
207
241
  await new Promise((resolve) => {
208
242
  passport.authenticate('bjx', (err, user, info, status) => {
209
243
  if (err) {
210
- // this.error()
244
+ // this.error()进到这里
211
245
  errorLogger(err)
212
246
  ctx.logout()
213
247
  } else if (user) {
214
- // this.success()
248
+ // this.success()进到这里
215
249
  ctx.login(user)
216
250
  } else {
217
- // this.fail()
251
+ // this.fail()进到这里
218
252
  ctx.logout()
219
253
  }
220
254
  resolve()
221
255
  })(ctx, () => {
222
- // this.pass()
256
+ // this.pass()进到这里
223
257
  resolve()
224
258
  })
225
259
  })