bjx-auth 1.2.0 → 1.4.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,59 +0,0 @@
1
- const reqSign = require('./reqSign')
2
- const reqEncrypt = require('./reqEncrypt')
3
- const reqSginOld = require('./reqSginOld')
4
-
5
- async function getReqBody(data, isEncrypt, opts) {
6
- let {
7
- eqp = '',
8
- os = '',
9
- ba = '',
10
- bp = '',
11
- ver = '',
12
- apiVersion = '',
13
- signVersion = '',
14
- clientId = '',
15
- clientSecret = '',
16
- ctx,
17
- } = opts || {}
18
- if (ctx) {
19
- if (!eqp) {
20
- eqp = ctx.uuid
21
- }
22
- if (!os) {
23
- eqp = ctx.isMobi ? 2 : 1
24
- }
25
- if (!ba) {
26
- ba = ctx.query.ba
27
- }
28
- if (!bp) {
29
- bp = ctx.query.bp
30
- }
31
- }
32
-
33
- const obj = {
34
- eqp,
35
- os,
36
- ba,
37
- bp,
38
- ver,
39
- apiVersion,
40
- signVersion,
41
- clientId,
42
- clientSecret,
43
- }
44
-
45
- if (isEncrypt && data && Object.keys(data).length > 0) {
46
- obj.data = await reqEncrypt(data)
47
- } else {
48
- obj.data = data || {}
49
- }
50
-
51
- return reqSign(obj)
52
- }
53
-
54
- exports = module.exports = {
55
- reqSign,
56
- reqEncrypt,
57
- getReqBody,
58
- ...reqSginOld,
59
- }
@@ -1,45 +0,0 @@
1
- const reqSign = require('./reqSign')
2
- const { passportapi } = require('../axios')
3
- const { errorLogger } = require('../../logger')
4
-
5
- // 获取公钥
6
- let publicPemCache = null
7
- async function getPublicPem() {
8
- if (publicPemCache && publicPemCache.expiresAt > Date.now()) {
9
- return publicPemCache.value
10
- }
11
- let body = reqSign()
12
- try {
13
- const { data } = await passportapi({
14
- url: '/api/v1/common/config/security',
15
- method: 'POST',
16
- data: body,
17
- })
18
-
19
- if (data.isError === false) {
20
- publicPemCache = {
21
- value: `-----BEGIN PUBLIC KEY-----\n${data.data.rsaPubkey}\n-----END PUBLIC KEY-----`,
22
- expiresAt: Date.now() + 8 * 3600 * 1000,
23
- }
24
- return publicPemCache.value
25
- } else {
26
- throw new Error(data.error)
27
- }
28
- } catch (error) {
29
- errorLogger(error)
30
- }
31
- }
32
-
33
- function clearPublicPemCache() {
34
- publicPemCache = null
35
- }
36
-
37
- function hasPublicPemCache() {
38
- return !!publicPemCache
39
- }
40
-
41
- exports = module.exports = {
42
- getPublicPem,
43
- clearPublicPemCache,
44
- hasPublicPemCache,
45
- }
@@ -1,16 +0,0 @@
1
- const { getPublicPem } = require('./publicPem')
2
- const { encryptRSA, encryptAES } = require('./encrypt')
3
-
4
- async function getReqEncrypt(data) {
5
- if (!data) throw new Error('data is required')
6
- const publicPem = await getPublicPem()
7
- const dataStr = JSON.stringify(data)
8
- const { encrypted, iv } = encryptAES(dataStr)
9
- const encryptedIv = encryptRSA(iv, publicPem)
10
- return {
11
- akaParams: encrypted,
12
- secretKey: encryptedIv,
13
- }
14
- }
15
-
16
- exports = module.exports = getReqEncrypt
@@ -1,89 +0,0 @@
1
- const { hashMD5 } = require('./hash')
2
- const config = require('../../config')
3
-
4
- function getReqSign(params, ...moreParams) {
5
- if (typeof params === 'string') {
6
- params = {
7
- EQP: params || '',
8
- OS: moreParams[0] || '',
9
- BA: moreParams[1] || '',
10
- BP: moreParams[2] || '',
11
- }
12
- }
13
- let { EQP, OS, BA, BP, Ver, ctx } = params || {}
14
- if (ctx) {
15
- if (!EQP) {
16
- EQP = ctx.uuid
17
- }
18
- if (!OS) {
19
- OS = ctx.isMobi ? 2 : 1
20
- }
21
- if (!BA) {
22
- BA = ctx.query.ba
23
- }
24
- if (!BP) {
25
- BP = ctx.query.bp
26
- }
27
- }
28
-
29
- const obj = {
30
- EQP: EQP || config.eqp || '',
31
- OS: OS || config.os * 1 || 1,
32
- BA: BA || config.ba || '',
33
- BP: BP || config.bp || '',
34
- Ver: Ver || config.ver || '1.0.0',
35
- TS: Date.now(),
36
- }
37
-
38
- const secret = 'bjxstat2019'
39
- const signStr = `EQP=${obj.EQP}&OS=${obj.OS}&BA=${obj.BA}&BP=${obj.BP}&Ver=${obj.Ver}&TS=${obj.TS}+${secret}`
40
- const Sign = hashMD5(signStr).toUpperCase()
41
- return Object.assign(obj, { Sign })
42
- }
43
-
44
- function getReqSign2(params, ...moreParams) {
45
- if (typeof params === 'string') {
46
- params = {
47
- EQP: params || '',
48
- OS: moreParams[0] || '',
49
- BA: moreParams[1] || '',
50
- BP: moreParams[2] || '',
51
- XAppId: moreParams[3] || '',
52
- }
53
- }
54
- let { EQP, OS, BA, BP, Ver, XAppId, ctx } = params || {}
55
- if (ctx) {
56
- if (!EQP) {
57
- EQP = ctx.uuid
58
- }
59
- if (!OS) {
60
- OS = ctx.isMobi ? 2 : 1
61
- }
62
- if (!BA) {
63
- BA = ctx.query.ba
64
- }
65
- if (!BP) {
66
- BP = ctx.query.bp
67
- }
68
- }
69
-
70
- const obj = {
71
- EQP: EQP || config.eqp || '',
72
- OS: OS || config.os * 1 || 1,
73
- BA: BA || config.ba || '',
74
- BP: BP || config.bp || '',
75
- Ver: Ver || config.ver || '1.0.0',
76
- TS: Date.now(),
77
- XAppId: XAppId || config.clientId || '',
78
- }
79
-
80
- const secret = '%SiGn2021!@#'
81
- const signStr = `EQP=${obj.EQP}&OS=${obj.OS}&BA=${obj.BA}&BP=${obj.BP}&Ver=${obj.Ver}&TS=${obj.TS}&XAppId=${obj.XAppId}+${secret}`
82
- const Sign = hashMD5(signStr).toUpperCase()
83
- return Object.assign(obj, { Sign })
84
- }
85
-
86
- exports = module.exports = {
87
- getReqSign,
88
- getReqSign2,
89
- }
@@ -1,80 +0,0 @@
1
- const { hashHS256 } = require('./hash')
2
- const config = require('../../config')
3
-
4
- // 请求签名相关
5
- function sortData(data) {
6
- if (Object.prototype.toString.call(data) === '[object Object]') {
7
- return Object.keys(data)
8
- .sort() // 默认情况下 sort 会按字典顺序(ASCII 顺序)进行排序
9
- .reduce((newObj, key) => {
10
- newObj[key] = sortData(data[key])
11
- return newObj
12
- }, {})
13
- } else if (Object.prototype.toString.call(data) === '[object Array]') {
14
- return data.map((item) => sortData(item))
15
- } else {
16
- return data
17
- }
18
- }
19
- function objToStr(data) {
20
- let dataArr = []
21
- Object.entries(data).map(([key, value]) => {
22
- if (value === undefined || value === '' || value === null) {
23
- } else {
24
- if (
25
- Object.prototype.toString.call(value) === '[object Array]' ||
26
- Object.prototype.toString.call(value) === '[object Object]'
27
- ) {
28
- dataArr.push(`${key}=${encodeURIComponent(JSON.stringify(value))}`)
29
- } else {
30
- dataArr.push(`${key}=${encodeURIComponent(value)}`)
31
- }
32
- }
33
- })
34
- return dataArr.join('&')
35
- }
36
-
37
- function signFn(data, signKey) {
38
- const sorted = sortData(data)
39
- const dataStr = objToStr(sorted)
40
- const sign = hashHS256(dataStr, signKey)
41
- return {
42
- ...data,
43
- sign,
44
- }
45
- }
46
-
47
- function getReqSign(params, signKey) {
48
- const {
49
- eqp,
50
- os,
51
- ba,
52
- bp,
53
- ver,
54
- apiVersion,
55
- signVersion,
56
- clientId,
57
- clientSecret,
58
- data = {},
59
- } = params || {}
60
-
61
- const obj = {
62
- eqp: eqp || config.eqp || '',
63
- os: os * 1 || config.os * 1 || 1,
64
- ba: ba || config.ba || '',
65
- bp: bp || config.bp || '',
66
- ver: ver || config.ver || '1.0.0',
67
- ts: Date.now(),
68
- apiVersion: apiVersion || config.apiVersion || '1.0.0',
69
- signVersion: signVersion || config.signVersion || 'V1',
70
- clientId: clientId || config.clientId || '',
71
- clientSecret: clientSecret || config.clientSecret || '',
72
- data,
73
- }
74
- signKey = signKey || config.signKey
75
-
76
- const res = signFn(obj, signKey)
77
- return res
78
- }
79
-
80
- exports = module.exports = getReqSign
@@ -1,72 +0,0 @@
1
- const { auth, enterpriseapi } = require('./axios')
2
- const { getReqSign } = require('./sign')
3
-
4
- /**
5
- * nodejs授权
6
- */
7
- async function baseInfo(opts, signObj = {}) {
8
- opts.data = { ...opts.data, ...getReqSign(signObj) }
9
- return auth({
10
- url: '/api/User/' + opts.__type__,
11
- method: 'POST',
12
- ...opts,
13
- })
14
- }
15
-
16
- async function userInfo(opts, signObj = {}) {
17
- opts.data = { ...opts.data, ...getReqSign(signObj) }
18
- return enterpriseapi({
19
- url: '/enterprise/user/userInfo',
20
- method: 'POST',
21
- ...opts,
22
- }).then((res) => {
23
- const { data: rsp_obj } = res
24
- res.data = {
25
- HttpStatusCode: rsp_obj.code,
26
- Error: rsp_obj.errMsg,
27
- IsError: !rsp_obj.success,
28
- Data:
29
- rsp_obj.data && Object.keys(rsp_obj.data).length
30
- ? {
31
- HasCompany: rsp_obj.data.hasCompanyState > 0, // BaseInfoV1和BaseInfo的差别
32
- Id: rsp_obj.data.id || '',
33
- UId: rsp_obj.data.userId || 0,
34
- UserName: rsp_obj.data.userName || '',
35
- NickName: rsp_obj.data.nickName || '',
36
- Email: rsp_obj.data.userEmail || '',
37
- EmailIsCheck: rsp_obj.data.emailCheckState > 0,
38
- RegionCode: rsp_obj.data.regionCode || '',
39
- Phone: rsp_obj.data.userPhone || '',
40
- PhoneIsCheck: rsp_obj.data.phoneCheckState > 0,
41
- HeadUrl: rsp_obj.data.headUrl || '',
42
- BackImage: rsp_obj.data.backImage || '',
43
- BriefIntro: rsp_obj.data.briefIntro || '',
44
- Industry: rsp_obj.data.industryId || 0,
45
- Source: rsp_obj.data.registerSource || 0,
46
- ShowName: rsp_obj.data.showName || '',
47
- HeadIsDef: rsp_obj.data.headDefState > 0,
48
- Nick: rsp_obj.data.nick || '',
49
- NickShowName: rsp_obj.data.nickShowName || '',
50
- RegDate: rsp_obj.data.registerDate || '',
51
- }
52
- : {},
53
- }
54
- return res
55
- })
56
- }
57
-
58
- async function getUserInfo(opts, signArr) {
59
- const typeArr = ['BaseInfo', 'BaseInfoV1', 'ExtendInfo', 'userInfo']
60
- if (!typeArr.includes(opts.__type__)) {
61
- opts.__type__ = typeArr[0]
62
- }
63
- if (opts.__type__ === 'userInfo') {
64
- return userInfo(opts, signArr)
65
- } else {
66
- return baseInfo(opts, signArr)
67
- }
68
- }
69
-
70
- exports = module.exports = {
71
- getUserInfo,
72
- }
@@ -1,66 +0,0 @@
1
- const {
2
- setConfig,
3
- getConfig,
4
- getToken: getTokenApi,
5
- getUserInfo: getUserInfoApi,
6
- } = require('../request')
7
-
8
- async function getToken(cookies, isRefresh, { headers, ctx }) {
9
- return getTokenApi(
10
- {
11
- __isRefresh__: isRefresh,
12
- headers: {
13
- Cookie: Object.entries(cookies)
14
- .map((v) => v.join('='))
15
- .join('; '),
16
- ...headers,
17
- },
18
- },
19
- {
20
- ctx,
21
- },
22
- ).then(({ data }) => {
23
- if (data.isError === false) {
24
- return {
25
- bjx_token_flag: cookies['idsrv.session'],
26
- token_type: '',
27
- access_token: data.data.authToken,
28
- expires_at: data.data.expiresAt || (Date.now() / 1000 + 4 * 3600) | 0, // 4小时过期
29
- }
30
- }
31
- })
32
- }
33
-
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
-
43
- return getUserInfoApi(
44
- {
45
- __type__: type,
46
- headers: {
47
- ...headers,
48
- ...tokenHeader,
49
- },
50
- },
51
- {
52
- ctx,
53
- },
54
- ).then(({ data }) => {
55
- if (data.IsError === false) {
56
- return data.Data
57
- }
58
- })
59
- }
60
-
61
- module.exports = {
62
- setConfig,
63
- getConfig,
64
- getToken,
65
- getUserInfo,
66
- }
@@ -1,49 +0,0 @@
1
- async function setConfig(obj) {
2
- console.log('设置配置方法', obj)
3
- }
4
-
5
- async function getToken(cookie, { headers }) {
6
- console.log('令牌方法', cookie, headers)
7
- return {
8
- is_bjx_token: true,
9
- token_type: '',
10
- access_token:
11
- 'xn4d9DmczNYqDDHt1kHA95l923HSjqNbaYUNVzDNw9FTkxZGg7tsZutqFgfg0gTXoxH9LCxs5uuQ_sBBi54WWg01gESgzSMQV7a1f2utaBr90QPSCy-oLu5YUdHzCPDszfx2P8M4xm4LRMNehjXl8cG0Xph80FdJ7-OntP_ZdGE8ERWAppZATpSZML1oOMj2AKg_Z8YReHTqdp6BuFJqpk2qdCtsFyrqsCbh87pRcghj93eDB4sbSXmKOoc-96LO',
12
- expires_at: 1756375244,
13
- }
14
- }
15
-
16
- async function getUserInfo(token, type, { headers }) {
17
- console.log('用户信息方法', token, type, headers)
18
- return {
19
- ts: new Date().toLocaleString('sv-SE'),
20
- Id: '4316B40F-DBB6-4AA5-8242-5425C7D36DFB',
21
- UId: 1100000010,
22
- UserName: 'bjxadmin',
23
- NickName: '徐薇',
24
- Email: 'caoce5158@qq.com',
25
- EmailIsCheck: true,
26
- RegionCode: '+86',
27
- Phone: '13911112222',
28
- PhoneIsCheck: true,
29
- HeadUrl:
30
- 'https://static.bjx.com.cn/EnterpriseNew/SeekerImg/1100000010/2023050813520237_74810.jpeg',
31
- BackImage:
32
- 'http://img01.mybjx.net/webupload/image/20231109/3e06d701bc00002.png',
33
- BriefIntro: '',
34
- Industry: 1300,
35
- Source: 9,
36
- ShowName: '徐薇',
37
- HeadIsDef: false,
38
- Nick: '',
39
- NickShowName: '星友2222',
40
- RegDate: '2016-05-09T17:14:56.473',
41
- PwdIsSet: false,
42
- }
43
- }
44
-
45
- module.exports = {
46
- setConfig,
47
- getToken,
48
- getUserInfo,
49
- }
@@ -1,9 +0,0 @@
1
- const { setConfig, getConfig } = require('../config')
2
- const strategy = require('./strategy')
3
- const utils = require('./utils')
4
-
5
- exports = module.exports = Object.assign(
6
- { setConfig, getConfig },
7
- strategy,
8
- utils,
9
- )