@questwork/q-utilities 0.1.0 → 0.1.2
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/index.min.cjs +1154 -0
- package/dist/index.min.js +1140 -0
- package/package.json +29 -13
- package/index.js +0 -3
- package/lib/authenticator/authenticator.js +0 -56
- package/lib/authenticator/clientApp.js +0 -46
- package/lib/authenticator/index.js +0 -5
- package/lib/convertString/convertString.js +0 -20
- package/lib/convertString/index.js +0 -1
- package/lib/cryptoHelper/cryptoHelper.js +0 -93
- package/lib/cryptoHelper/index.js +0 -5
- package/lib/getValidation/getValidation.js +0 -117
- package/lib/getValidation/index.js +0 -1
- package/lib/getValueByKeys/getValueByKeys.js +0 -20
- package/lib/getValueByKeys/index.js +0 -1
- package/lib/index.js +0 -14
- package/lib/jwtHelper/index.js +0 -5
- package/lib/jwtHelper/jwtHelper.js +0 -254
- package/lib/keyValueObject/index.js +0 -1
- package/lib/keyValueObject/keyValueObject.js +0 -163
package/package.json
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@questwork/q-utilities",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Questwork QUtilities",
|
|
5
|
-
"main": "index.js",
|
|
5
|
+
"main": "dist/index.min.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"require": "./dist/index.min.cjs",
|
|
10
|
+
"default": "./dist/index.min.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
6
13
|
"author": {
|
|
7
14
|
"name": "Questwork Consulting Limited",
|
|
8
15
|
"email": "info@questwork.com",
|
|
@@ -10,24 +17,33 @@
|
|
|
10
17
|
},
|
|
11
18
|
"license": "MIT",
|
|
12
19
|
"dependencies": {
|
|
13
|
-
"axios": "^0.28.1"
|
|
14
|
-
"buffer": "^6.0.3"
|
|
20
|
+
"axios": "^0.28.1"
|
|
15
21
|
},
|
|
16
22
|
"devDependencies": {
|
|
17
|
-
"
|
|
18
|
-
"eslint": "
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
23
|
+
"@babel/core": "^7.17.8",
|
|
24
|
+
"@babel/eslint-parser": "^7.17.0",
|
|
25
|
+
"babel-loader": "^8.2.4",
|
|
26
|
+
"babel-plugin-component": "^1.1.1",
|
|
27
|
+
"chai": "^4.5.0",
|
|
28
|
+
"clean-webpack-plugin": "^4.0.0",
|
|
29
|
+
"cross-env": "^7.0.3",
|
|
30
|
+
"eslint": "^6.8.0",
|
|
31
|
+
"eslint-config-airbnb-base": "^14.2.1",
|
|
32
|
+
"eslint-plugin-import": "^2.31.0",
|
|
33
|
+
"eslint-plugin-mocha": "^6.3.0",
|
|
34
|
+
"gulp": "^4.0.2",
|
|
35
|
+
"gulp-rename": "^2.0.0",
|
|
36
|
+
"mocha": "^7.2.0",
|
|
37
|
+
"webpack": "^5.75.0",
|
|
38
|
+
"webpack-cli": "^4.9.2",
|
|
39
|
+
"webpack-node-externals": "^3.0.0"
|
|
24
40
|
},
|
|
25
41
|
"engines": {
|
|
26
42
|
"node": ">=10.0.0"
|
|
27
43
|
},
|
|
28
44
|
"scripts": {
|
|
45
|
+
"build": "cross-env NODE_ENV=production minimize=false gulp",
|
|
29
46
|
"lint": "eslint .",
|
|
30
|
-
"test": "mocha --exit '
|
|
31
|
-
"test-models": "NODE_ENV=test mocha --exit 'test/test.setup.js' 'test/**/*.spec.js'"
|
|
47
|
+
"test:models": "NODE_ENV=test mocha --exit 'lib/models/test.setup.js' 'lib/models/**/*.spec.js'"
|
|
32
48
|
}
|
|
33
49
|
}
|
package/index.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
const axios = require('axios')
|
|
2
|
-
const { ClientApp } = require('./clientApp')
|
|
3
|
-
|
|
4
|
-
async function getAuthToken({ id, appSecret, name, service, authServerHost, authServerPath, authPath, additionalData }) {
|
|
5
|
-
try {
|
|
6
|
-
const clientApp = _initClientApp({ id, appSecret, name })
|
|
7
|
-
return _getAuthToken({
|
|
8
|
-
clientApp, service, authServerHost, authServerPath, authPath, additionalData
|
|
9
|
-
})
|
|
10
|
-
} catch (err) {
|
|
11
|
-
throw err
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function _getAuthCredentials({ clientApp, service, additionalData }) {
|
|
16
|
-
const jwt = clientApp.getJwtToken()
|
|
17
|
-
const data = {
|
|
18
|
-
service,
|
|
19
|
-
...additionalData
|
|
20
|
-
}
|
|
21
|
-
const headers = { Authorization: `Bearer ${jwt}` }
|
|
22
|
-
return {
|
|
23
|
-
data,
|
|
24
|
-
headers
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
async function _getAuthToken({ clientApp, service, authServerHost, authServerPath, authPath, additionalData }) {
|
|
29
|
-
try {
|
|
30
|
-
const path = authServerPath || authPath
|
|
31
|
-
if (!!path && !!authServerHost) {
|
|
32
|
-
const { data, headers } = _getAuthCredentials({ clientApp, service, additionalData })
|
|
33
|
-
const response = await axios.post(`${authServerHost}${path}`, data, { headers })
|
|
34
|
-
return response.data
|
|
35
|
-
}
|
|
36
|
-
throw new Error('missing auth server')
|
|
37
|
-
} catch (err) {
|
|
38
|
-
const message = err.response && err.response.data ? err.response.data.message : err.message
|
|
39
|
-
throw new Error(`failed to get ServiceApp token: ${message}`)
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function _initClientApp({ id, appSecret, name } = {}) {
|
|
44
|
-
try {
|
|
45
|
-
const clientApp = ClientApp.init({
|
|
46
|
-
id, appSecret, name
|
|
47
|
-
})
|
|
48
|
-
return clientApp
|
|
49
|
-
} catch (err) {
|
|
50
|
-
throw new Error(`invalid ClientApp: ${err.message}`)
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
module.exports = {
|
|
55
|
-
getAuthToken
|
|
56
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
const { jwtHelper } = require('../jwtHelper')
|
|
2
|
-
|
|
3
|
-
const TOKEN_EXPIRY = 300
|
|
4
|
-
|
|
5
|
-
class ClientApp {
|
|
6
|
-
constructor(options) {
|
|
7
|
-
options = options || {}
|
|
8
|
-
this.id = options.id
|
|
9
|
-
this.appSecret = options.appSecret
|
|
10
|
-
this.name = options.name
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// Class method
|
|
14
|
-
static init(options = {}) {
|
|
15
|
-
const clientApp = new ClientApp(options)
|
|
16
|
-
return clientApp.isValid ? clientApp : null
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// Getter
|
|
20
|
-
get isValid() {
|
|
21
|
-
return !!this.name && !!this.appSecret && !!this.id
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// instance methods
|
|
25
|
-
getJwtPayload() {
|
|
26
|
-
return {
|
|
27
|
-
clientApp: {
|
|
28
|
-
id: this.id,
|
|
29
|
-
name: this.name
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
getJwtToken({ tokenExpiry } = {}) {
|
|
34
|
-
const iExpiry = tokenExpiry || Math.floor(Date.now() / 1000) + TOKEN_EXPIRY // must be second
|
|
35
|
-
return jwtHelper.create(
|
|
36
|
-
this.getJwtPayload(),
|
|
37
|
-
{
|
|
38
|
-
secret: this.appSecret,
|
|
39
|
-
expiry: iExpiry
|
|
40
|
-
})
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
module.exports = {
|
|
45
|
-
ClientApp
|
|
46
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
function convertString(string, patternMatch = /\$\{(.+?)\}/g, value, getValueByKeys) {
|
|
2
|
-
if (!string || typeof getValueByKeys !== 'function') {
|
|
3
|
-
return ''
|
|
4
|
-
}
|
|
5
|
-
const reg = new RegExp(patternMatch, 'g')
|
|
6
|
-
return string.replace(reg, (match, key) => {
|
|
7
|
-
const result = getValueByKeys({ keys: key.split('.'), obj: value })
|
|
8
|
-
if (result === null || result === undefined) {
|
|
9
|
-
return ''
|
|
10
|
-
}
|
|
11
|
-
return typeof result === 'object' ? JSON.stringify(result) : result
|
|
12
|
-
})
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export default {
|
|
16
|
-
convertString
|
|
17
|
-
}
|
|
18
|
-
export {
|
|
19
|
-
convertString
|
|
20
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { convertString } from './convertString'
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
// const crypto = require('crypto-browserify')
|
|
2
|
-
const crypto = require('crypto')
|
|
3
|
-
// const { subtle } = globalThis.crypto
|
|
4
|
-
|
|
5
|
-
const cryptoHelper = {
|
|
6
|
-
createHash(algorithm, options) {
|
|
7
|
-
return crypto.createHash(algorithm, options)
|
|
8
|
-
},
|
|
9
|
-
|
|
10
|
-
createHmac(algorithm, key, options) {
|
|
11
|
-
return crypto.createHmac(algorithm, key, options)
|
|
12
|
-
},
|
|
13
|
-
|
|
14
|
-
createSign(algorithm, options) {
|
|
15
|
-
return crypto.createSign(algorithm, options)
|
|
16
|
-
},
|
|
17
|
-
|
|
18
|
-
createVerify(algorithm, options) {
|
|
19
|
-
return crypto.createVerify(algorithm, options)
|
|
20
|
-
},
|
|
21
|
-
|
|
22
|
-
digest(instance, outputEncoding) {
|
|
23
|
-
if (!(typeof instance.digest === 'function')) {
|
|
24
|
-
throw new Error('no digest method')
|
|
25
|
-
}
|
|
26
|
-
return instance.digest(outputEncoding)
|
|
27
|
-
},
|
|
28
|
-
|
|
29
|
-
sign({ instance, privateKey, outputEncoding }) {
|
|
30
|
-
if (!(typeof instance.sign === 'function')) {
|
|
31
|
-
throw new Error('no sign method')
|
|
32
|
-
}
|
|
33
|
-
return instance.sign(privateKey, outputEncoding)
|
|
34
|
-
},
|
|
35
|
-
|
|
36
|
-
verify({ instance, object, signature, signatureEncoding }) {
|
|
37
|
-
if (!(typeof instance.verify === 'function')) {
|
|
38
|
-
throw new Error('no verify method')
|
|
39
|
-
}
|
|
40
|
-
return instance.verify(object, signature, signatureEncoding)
|
|
41
|
-
},
|
|
42
|
-
|
|
43
|
-
update({ instance, data, inputEncoding }) {
|
|
44
|
-
if (!(typeof instance.update === 'function')) {
|
|
45
|
-
throw new Error('no update method')
|
|
46
|
-
}
|
|
47
|
-
return instance.update(data, inputEncoding)
|
|
48
|
-
},
|
|
49
|
-
|
|
50
|
-
createStringHmac({ algorithm, options, key, data, inputEncoding, outputEncoding }) {
|
|
51
|
-
try {
|
|
52
|
-
const hmac = this.createHmac(algorithm, key, options)
|
|
53
|
-
const instance = this.update({ instance: hmac, data, inputEncoding })
|
|
54
|
-
return this.digest(instance, outputEncoding)
|
|
55
|
-
} catch (err) {
|
|
56
|
-
throw err
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
|
|
60
|
-
createStringVerify({ algorithm, options, data, object, signature, inputEncoding, signatureEncoding }) {
|
|
61
|
-
try {
|
|
62
|
-
const verify = this.createVerify(algorithm, options)
|
|
63
|
-
const instance = this.update({ instance: verify, data, inputEncoding })
|
|
64
|
-
return this.verify({ instance, object, signature, signatureEncoding })
|
|
65
|
-
} catch (err) {
|
|
66
|
-
throw err
|
|
67
|
-
}
|
|
68
|
-
},
|
|
69
|
-
|
|
70
|
-
createSignature({ algorithm, options, data, privateKey, inputEncoding, outputEncoding }) {
|
|
71
|
-
try {
|
|
72
|
-
const sign = this.createSign(algorithm, options)
|
|
73
|
-
const instance = this.update({ instance: sign, data, inputEncoding })
|
|
74
|
-
return this.sign({ instance, privateKey, outputEncoding })
|
|
75
|
-
} catch (err) {
|
|
76
|
-
throw err
|
|
77
|
-
}
|
|
78
|
-
},
|
|
79
|
-
|
|
80
|
-
createStringHash({ algorithm, options, data, inputEncoding, outputEncoding }) {
|
|
81
|
-
try {
|
|
82
|
-
const hash = this.createHash(algorithm, options)
|
|
83
|
-
const instance = this.update({ instance: hash, data, inputEncoding })
|
|
84
|
-
return this.digest(instance, outputEncoding)
|
|
85
|
-
} catch (err) {
|
|
86
|
-
throw err
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
module.exports = {
|
|
92
|
-
cryptoHelper
|
|
93
|
-
}
|
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
function getValidation(rule, data, getDataByKey, KeyValueObject) {
|
|
2
|
-
if (!rule) {
|
|
3
|
-
return true
|
|
4
|
-
}
|
|
5
|
-
if (typeof getDataByKey !== 'function' || typeof KeyValueObject !== 'function') {
|
|
6
|
-
return false
|
|
7
|
-
}
|
|
8
|
-
const { key = '', value, keyValuePath = '' } = rule
|
|
9
|
-
const [valueAttribute] = Object.keys(value)
|
|
10
|
-
|
|
11
|
-
if (!key) {
|
|
12
|
-
switch (valueAttribute) {
|
|
13
|
-
case '$and': {
|
|
14
|
-
const arr = value['$and']
|
|
15
|
-
return arr.reduce((acc, item) => (acc && getValidation(item, data, getDataByKey)), true)
|
|
16
|
-
}
|
|
17
|
-
case '$or': {
|
|
18
|
-
const arr = value['$or']
|
|
19
|
-
return arr.reduce((acc, item) => (acc || getValidation(item, data, getDataByKey)), false)
|
|
20
|
-
}
|
|
21
|
-
default:
|
|
22
|
-
return false
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
let rowValue = getDataByKey(key, data)
|
|
27
|
-
|
|
28
|
-
// debugger
|
|
29
|
-
|
|
30
|
-
// if KeyValue object
|
|
31
|
-
if (keyValuePath) {
|
|
32
|
-
console.log('keyValuePath', keyValuePath)
|
|
33
|
-
const rowValueData = KeyValueObject.toObject(rowValue)
|
|
34
|
-
rowValue = getDataByKey(keyValuePath, rowValueData)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
switch (valueAttribute) {
|
|
38
|
-
case '$empty': {
|
|
39
|
-
const isEmpty = rowValue === null || rowValue === undefined
|
|
40
|
-
return isEmpty === value['$empty']
|
|
41
|
-
}
|
|
42
|
-
case '$eq': {
|
|
43
|
-
return rowValue === value['$eq']
|
|
44
|
-
}
|
|
45
|
-
case '$gt': {
|
|
46
|
-
return rowValue > value['$gt']
|
|
47
|
-
}
|
|
48
|
-
case '$gte': {
|
|
49
|
-
return rowValue >= value['$gte']
|
|
50
|
-
}
|
|
51
|
-
case '$lt': {
|
|
52
|
-
return rowValue < value['$lt']
|
|
53
|
-
}
|
|
54
|
-
case '$lte': {
|
|
55
|
-
return rowValue <= value['$lte']
|
|
56
|
-
}
|
|
57
|
-
case '$in': {
|
|
58
|
-
if (Array.isArray(rowValue)) {
|
|
59
|
-
return !!rowValue.find((e) => (value['$in'].includes(e)))
|
|
60
|
-
}
|
|
61
|
-
if (typeof rowValue !== 'object') {
|
|
62
|
-
return !!value['$in'].includes(rowValue)
|
|
63
|
-
}
|
|
64
|
-
return false
|
|
65
|
-
}
|
|
66
|
-
case '$inValue': {
|
|
67
|
-
const result = getDataByKey(value['$inValue'], data)
|
|
68
|
-
const _value = Array.isArray(result) ? result : []
|
|
69
|
-
if (Array.isArray(rowValue)) {
|
|
70
|
-
return !!rowValue.find((e) => (_value.includes(e)))
|
|
71
|
-
}
|
|
72
|
-
if (typeof rowValue === 'string') {
|
|
73
|
-
return !!_value.includes(rowValue)
|
|
74
|
-
}
|
|
75
|
-
return false
|
|
76
|
-
}
|
|
77
|
-
case '$ne': {
|
|
78
|
-
return rowValue !== value['$ne']
|
|
79
|
-
}
|
|
80
|
-
case '$notIn': {
|
|
81
|
-
if (Array.isArray(rowValue)) {
|
|
82
|
-
return !rowValue.find((e) => (value['$notIn'].includes(e)))
|
|
83
|
-
}
|
|
84
|
-
if (typeof rowValue !== 'object') {
|
|
85
|
-
return !value['$notIn'].includes(rowValue)
|
|
86
|
-
}
|
|
87
|
-
return false
|
|
88
|
-
}
|
|
89
|
-
case '$notInValue': {
|
|
90
|
-
const result = getDataByKey(value['$notInValue'], data)
|
|
91
|
-
const _value = Array.isArray(result) ? result : []
|
|
92
|
-
if (Array.isArray(rowValue)) {
|
|
93
|
-
return !rowValue.find((e) => (_value.includes(e)))
|
|
94
|
-
}
|
|
95
|
-
if (typeof rowValue !== 'object') {
|
|
96
|
-
return !_value.includes(rowValue)
|
|
97
|
-
}
|
|
98
|
-
return false
|
|
99
|
-
}
|
|
100
|
-
case '$range': {
|
|
101
|
-
const [min, max] = value['$range']
|
|
102
|
-
if (typeof min === 'number' && typeof max === 'number' && rowValue >= min && rowValue <= max) {
|
|
103
|
-
return true
|
|
104
|
-
}
|
|
105
|
-
return false
|
|
106
|
-
}
|
|
107
|
-
default:
|
|
108
|
-
return false
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export default {
|
|
113
|
-
getValidation
|
|
114
|
-
}
|
|
115
|
-
export {
|
|
116
|
-
getValidation
|
|
117
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { getValidation } from './getValidation'
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
function getValueByKeys(keys, data) {
|
|
2
|
-
if (keys.length === 0) {
|
|
3
|
-
return data
|
|
4
|
-
}
|
|
5
|
-
const firstKey = keys.shift()
|
|
6
|
-
if (data && Object.prototype.hasOwnProperty.call(data, firstKey)) {
|
|
7
|
-
return getValueByKeys(keys, data[firstKey])
|
|
8
|
-
}
|
|
9
|
-
if (data && firstKey) {
|
|
10
|
-
return data[firstKey]
|
|
11
|
-
}
|
|
12
|
-
return data
|
|
13
|
-
}
|
|
14
|
-
export default {
|
|
15
|
-
getValueByKeys
|
|
16
|
-
}
|
|
17
|
-
export {
|
|
18
|
-
getValueByKeys
|
|
19
|
-
}
|
|
20
|
-
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { getValueByKeys } from './getValueByKeys'
|
package/lib/index.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
// const { authenticator } = require('./authenticator')
|
|
2
|
-
// const { cryptoHelper } = require('./cryptoHelper')
|
|
3
|
-
// const { jwtHelper } = require('./jwtHelper')
|
|
4
|
-
|
|
5
|
-
// module.exports = {
|
|
6
|
-
// authenticator,
|
|
7
|
-
// cryptoHelper,
|
|
8
|
-
// jwtHelper
|
|
9
|
-
// }
|
|
10
|
-
|
|
11
|
-
export * from './convertString'
|
|
12
|
-
export * from './getValidation'
|
|
13
|
-
export * from './getValueByKeys'
|
|
14
|
-
export * from './keyValueObject'
|