@blazedpath/commons 0.0.4

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.
Files changed (224) hide show
  1. package/README.md +3 -0
  2. package/blz-base/health/index.js +215 -0
  3. package/blz-base/index.js +1466 -0
  4. package/blz-cache/LruCache.js +44 -0
  5. package/blz-cache/index.js +29 -0
  6. package/blz-config/index.js +434 -0
  7. package/blz-core/index.js +364 -0
  8. package/blz-cryptography/index.js +54 -0
  9. package/blz-datetimes/index.js +356 -0
  10. package/blz-file/example.dat +2545 -0
  11. package/blz-file/fileService.js +205 -0
  12. package/blz-file/index.js +94 -0
  13. package/blz-file/index.test.js +31 -0
  14. package/blz-file/lab.js +33 -0
  15. package/blz-hazelcast/index.js +189 -0
  16. package/blz-hazelcast/lib/credentials.js +25 -0
  17. package/blz-hazelcast/lib/credentialsFactory.js +12 -0
  18. package/blz-hazelcast/lib/hazelcastCache.js +234 -0
  19. package/blz-iterable/index.js +446 -0
  20. package/blz-json-schema/index.js +11 -0
  21. package/blz-jwt/index.js +121 -0
  22. package/blz-kafka/index.js +522 -0
  23. package/blz-math/index.js +131 -0
  24. package/blz-mongodb/index.js +326 -0
  25. package/blz-rds/__test__/scape.test.js +58 -0
  26. package/blz-rds/blz-rds-executor.js +578 -0
  27. package/blz-rds/blz-rds-helper.js +310 -0
  28. package/blz-rds/commands/core/add.js +13 -0
  29. package/blz-rds/commands/core/and.js +18 -0
  30. package/blz-rds/commands/core/asc.js +10 -0
  31. package/blz-rds/commands/core/avg.js +10 -0
  32. package/blz-rds/commands/core/column-ref.js +8 -0
  33. package/blz-rds/commands/core/count-distinct.js +10 -0
  34. package/blz-rds/commands/core/count.js +10 -0
  35. package/blz-rds/commands/core/decimal.js +8 -0
  36. package/blz-rds/commands/core/desc.js +10 -0
  37. package/blz-rds/commands/core/distinct.js +10 -0
  38. package/blz-rds/commands/core/divide.js +11 -0
  39. package/blz-rds/commands/core/embedded-exists.js +17 -0
  40. package/blz-rds/commands/core/embedded-select.js +17 -0
  41. package/blz-rds/commands/core/equals.js +9 -0
  42. package/blz-rds/commands/core/false.js +8 -0
  43. package/blz-rds/commands/core/greater-or-equal.js +9 -0
  44. package/blz-rds/commands/core/greater.js +9 -0
  45. package/blz-rds/commands/core/in.js +9 -0
  46. package/blz-rds/commands/core/integer.js +8 -0
  47. package/blz-rds/commands/core/is-not-null.js +11 -0
  48. package/blz-rds/commands/core/is-null-or-value.js +10 -0
  49. package/blz-rds/commands/core/is-null.js +11 -0
  50. package/blz-rds/commands/core/less-or-equal.js +9 -0
  51. package/blz-rds/commands/core/less-unary.js +12 -0
  52. package/blz-rds/commands/core/less.js +9 -0
  53. package/blz-rds/commands/core/like.js +12 -0
  54. package/blz-rds/commands/core/max.js +10 -0
  55. package/blz-rds/commands/core/min.js +10 -0
  56. package/blz-rds/commands/core/multiply.js +13 -0
  57. package/blz-rds/commands/core/not-equals.js +9 -0
  58. package/blz-rds/commands/core/not-in.js +9 -0
  59. package/blz-rds/commands/core/not.js +13 -0
  60. package/blz-rds/commands/core/null.js +8 -0
  61. package/blz-rds/commands/core/nvl.js +11 -0
  62. package/blz-rds/commands/core/or.js +13 -0
  63. package/blz-rds/commands/core/parameter.js +34 -0
  64. package/blz-rds/commands/core/remainder.js +16 -0
  65. package/blz-rds/commands/core/string.js +8 -0
  66. package/blz-rds/commands/core/subtract.js +13 -0
  67. package/blz-rds/commands/core/sum.js +10 -0
  68. package/blz-rds/commands/core/true.js +8 -0
  69. package/blz-rds/commands/core/tuple.js +13 -0
  70. package/blz-rds/commands/datetimes/add-days.js +11 -0
  71. package/blz-rds/commands/datetimes/add-hours.js +11 -0
  72. package/blz-rds/commands/datetimes/add-milliseconds.js +11 -0
  73. package/blz-rds/commands/datetimes/add-minutes.js +11 -0
  74. package/blz-rds/commands/datetimes/add-months.js +11 -0
  75. package/blz-rds/commands/datetimes/add-seconds.js +11 -0
  76. package/blz-rds/commands/datetimes/add-years.js +11 -0
  77. package/blz-rds/commands/datetimes/date-diff.js +11 -0
  78. package/blz-rds/commands/datetimes/date.js +12 -0
  79. package/blz-rds/commands/datetimes/datetime-diff.js +11 -0
  80. package/blz-rds/commands/datetimes/datetime.js +15 -0
  81. package/blz-rds/commands/datetimes/day.js +10 -0
  82. package/blz-rds/commands/datetimes/hour.js +10 -0
  83. package/blz-rds/commands/datetimes/millisecond.js +10 -0
  84. package/blz-rds/commands/datetimes/minute.js +10 -0
  85. package/blz-rds/commands/datetimes/month-text.js +10 -0
  86. package/blz-rds/commands/datetimes/month.js +10 -0
  87. package/blz-rds/commands/datetimes/now.js +9 -0
  88. package/blz-rds/commands/datetimes/second.js +10 -0
  89. package/blz-rds/commands/datetimes/subtract-days.js +11 -0
  90. package/blz-rds/commands/datetimes/subtract-hours.js +11 -0
  91. package/blz-rds/commands/datetimes/subtract-milliseconds.js +11 -0
  92. package/blz-rds/commands/datetimes/subtract-minutes.js +11 -0
  93. package/blz-rds/commands/datetimes/subtract-seconds.js +11 -0
  94. package/blz-rds/commands/datetimes/time-diff.js +11 -0
  95. package/blz-rds/commands/datetimes/time.js +13 -0
  96. package/blz-rds/commands/datetimes/today.js +9 -0
  97. package/blz-rds/commands/datetimes/week-day-text.js +10 -0
  98. package/blz-rds/commands/datetimes/week-day.js +10 -0
  99. package/blz-rds/commands/datetimes/week.js +10 -0
  100. package/blz-rds/commands/datetimes/year.js +10 -0
  101. package/blz-rds/commands/math/abs.js +10 -0
  102. package/blz-rds/commands/math/acos.js +10 -0
  103. package/blz-rds/commands/math/asin.js +10 -0
  104. package/blz-rds/commands/math/atan.js +10 -0
  105. package/blz-rds/commands/math/atan2.js +11 -0
  106. package/blz-rds/commands/math/ceil.js +10 -0
  107. package/blz-rds/commands/math/cos.js +10 -0
  108. package/blz-rds/commands/math/cosh.js +10 -0
  109. package/blz-rds/commands/math/exp.js +10 -0
  110. package/blz-rds/commands/math/floor.js +10 -0
  111. package/blz-rds/commands/math/log.js +18 -0
  112. package/blz-rds/commands/math/log10.js +10 -0
  113. package/blz-rds/commands/math/pow.js +11 -0
  114. package/blz-rds/commands/math/random.js +9 -0
  115. package/blz-rds/commands/math/round.js +18 -0
  116. package/blz-rds/commands/math/sign.js +10 -0
  117. package/blz-rds/commands/math/sin.js +10 -0
  118. package/blz-rds/commands/math/sinh.js +10 -0
  119. package/blz-rds/commands/math/sqrt.js +10 -0
  120. package/blz-rds/commands/math/tan.js +10 -0
  121. package/blz-rds/commands/math/tanh.js +10 -0
  122. package/blz-rds/commands/math/trunc.js +18 -0
  123. package/blz-rds/commands/strings/concat.js +20 -0
  124. package/blz-rds/commands/strings/contains.js +12 -0
  125. package/blz-rds/commands/strings/ends-with.js +12 -0
  126. package/blz-rds/commands/strings/index-of.js +11 -0
  127. package/blz-rds/commands/strings/is-null-or-empty.js +11 -0
  128. package/blz-rds/commands/strings/is-null-or-white-space.js +11 -0
  129. package/blz-rds/commands/strings/join.js +22 -0
  130. package/blz-rds/commands/strings/last-index-of.js +11 -0
  131. package/blz-rds/commands/strings/length.js +10 -0
  132. package/blz-rds/commands/strings/pad-left.js +20 -0
  133. package/blz-rds/commands/strings/pad-right.js +20 -0
  134. package/blz-rds/commands/strings/replace.js +12 -0
  135. package/blz-rds/commands/strings/starts-with.js +12 -0
  136. package/blz-rds/commands/strings/substring.js +12 -0
  137. package/blz-rds/commands/strings/to-lower.js +10 -0
  138. package/blz-rds/commands/strings/to-upper.js +10 -0
  139. package/blz-rds/commands/strings/trim-end.js +10 -0
  140. package/blz-rds/commands/strings/trim-start.js +10 -0
  141. package/blz-rds/commands/strings/trim.js +10 -0
  142. package/blz-rds/index.js +744 -0
  143. package/blz-rds-mysql/base.js +857 -0
  144. package/blz-rds-mysql/connection-manager.js +129 -0
  145. package/blz-rds-mysql/execute-bulk-insert.js +35 -0
  146. package/blz-rds-mysql/execute-bulk-merge.js +45 -0
  147. package/blz-rds-mysql/execute-non-query.js +34 -0
  148. package/blz-rds-mysql/execute-query.js +50 -0
  149. package/blz-rds-mysql/index.js +41 -0
  150. package/blz-rds-mysql/stored-procedure.js +207 -0
  151. package/blz-rds-mysql/syntaxis.json +114 -0
  152. package/blz-rds-mysqlx/base.js +846 -0
  153. package/blz-rds-mysqlx/connection-manager.js +141 -0
  154. package/blz-rds-mysqlx/execute-bulk-insert.js +35 -0
  155. package/blz-rds-mysqlx/execute-bulk-merge.js +45 -0
  156. package/blz-rds-mysqlx/execute-non-query.js +29 -0
  157. package/blz-rds-mysqlx/execute-query.js +39 -0
  158. package/blz-rds-mysqlx/index.js +41 -0
  159. package/blz-rds-mysqlx/stored-procedure.js +179 -0
  160. package/blz-rds-mysqlx/syntaxis.json +105 -0
  161. package/blz-rds-oracle/index.js +540 -0
  162. package/blz-rds-oracle/syntaxis.json +112 -0
  163. package/blz-rds-postgres/base.js +861 -0
  164. package/blz-rds-postgres/connection-manager.js +225 -0
  165. package/blz-rds-postgres/execute-bulk-insert.js +81 -0
  166. package/blz-rds-postgres/execute-bulk-merge.js +93 -0
  167. package/blz-rds-postgres/execute-non-query.js +23 -0
  168. package/blz-rds-postgres/execute-query.js +37 -0
  169. package/blz-rds-postgres/index.js +41 -0
  170. package/blz-rds-postgres/result-set.js +51 -0
  171. package/blz-rds-postgres/stored-procedure.js +116 -0
  172. package/blz-rds-postgres/syntaxis.json +114 -0
  173. package/blz-redis/index.js +217 -0
  174. package/blz-redis/lib/redisCache.js +265 -0
  175. package/blz-regex/index.js +25 -0
  176. package/blz-security/.eslintrc.js +15 -0
  177. package/blz-security/__test__/AuthorizationKpn.yaml +1043 -0
  178. package/blz-security/__test__/FinancingSetting.yaml +177 -0
  179. package/blz-security/__test__/KpnConfigPortal.yaml +330 -0
  180. package/blz-security/__test__/OrderManagement.yaml +5190 -0
  181. package/blz-security/__test__/Security.yaml +128 -0
  182. package/blz-security/__test__/autorization.test.js +105 -0
  183. package/blz-security/__test__/orderManagement.test.js +26 -0
  184. package/blz-security/__test__/secureUrl.test.js +79 -0
  185. package/blz-security/__test__/solveMergeRule.test.js +109 -0
  186. package/blz-security/__test__/sqlInjectionGuard.test.js +203 -0
  187. package/blz-security/__test__/xssGuard.test.js +204 -0
  188. package/blz-security/authorizationService.js +536 -0
  189. package/blz-security/config/global.js +8 -0
  190. package/blz-security/config/welcome +8 -0
  191. package/blz-security/doc/README.md +75 -0
  192. package/blz-security/filescanner/index.js +46 -0
  193. package/blz-security/helpers/consts.js +229 -0
  194. package/blz-security/helpers/utils.js +267 -0
  195. package/blz-security/implementations/cache.js +90 -0
  196. package/blz-security/implementations/oidc.js +404 -0
  197. package/blz-security/implementations/pkceCacheStore.js +23 -0
  198. package/blz-security/implementations/saml.js +10 -0
  199. package/blz-security/implementations/uma.js +63 -0
  200. package/blz-security/implementations/webAuthn.js +9 -0
  201. package/blz-security/implementations/wstg.js +72 -0
  202. package/blz-security/index.js +77 -0
  203. package/blz-security/lab/index.js +27 -0
  204. package/blz-security/middleware/HapiServerAzureAd.js +641 -0
  205. package/blz-security/middleware/HapiServerKeycloak.js +840 -0
  206. package/blz-security/middleware/HapiServerSimToken.js +247 -0
  207. package/blz-security/middleware/hapi.js +515 -0
  208. package/blz-security/middleware/hapiServer.js +974 -0
  209. package/blz-security/navigationMemoryRepository.js +15 -0
  210. package/blz-security/navigationMongoDbRepository.js +73 -0
  211. package/blz-security/secureUrlService.js +47 -0
  212. package/blz-security/securityService.js +409 -0
  213. package/blz-security/sqlInjectionGuard.js +162 -0
  214. package/blz-security/templates/forbidden.html +0 -0
  215. package/blz-security/templates/session-iframe-azure-ad.html +7 -0
  216. package/blz-security/templates/session-iframe.html +73 -0
  217. package/blz-security/templates/unauthorized.html +1 -0
  218. package/blz-security/xssGuard.js +87 -0
  219. package/blz-strings/index.js +167 -0
  220. package/blz-uuid/index.js +7 -0
  221. package/blz-yaml/index.js +19 -0
  222. package/index.js +84 -0
  223. package/package.json +97 -0
  224. package/process-managers/index.js +422 -0
@@ -0,0 +1,247 @@
1
+ /**
2
+ * @author Blazedpath Team
3
+ * @implements Protecting all resources through hapi middleware
4
+ * @description Hapi.js (derived from Http-API) is an open-source Node.js
5
+ * framework used to build powerful and scalable web applications.
6
+ * @see https://hapi.dev/api/
7
+ */
8
+ const {
9
+ Exception,
10
+ getFullUrl,
11
+ getHost,
12
+ getPathname,
13
+ getTokenTolerance,
14
+ trace
15
+ } = require('../helpers/utils')
16
+
17
+ // Uses Issue to cache manage and logout (generators/customs not sure why yet)
18
+ const {
19
+ Issuer
20
+ } = require('openid-client') // OpenID Certified Relying Party.
21
+
22
+ // Self sign library
23
+ const jwToken = require('jsonwebtoken');
24
+
25
+ let securityService = null
26
+
27
+ class HapiServerSimToken {
28
+ constructor(openIdConnect, cookiesName, cache) {
29
+ this.openIdConnect = openIdConnect
30
+ this.COOKIE_NAMES = cookiesName
31
+ this.authServerConfig = null;
32
+ // This cache stores locally the jwt token set for refresh and logout.
33
+ this.cache = cache;
34
+ // To terminate sessions
35
+ this.clientOidc = null;
36
+ }
37
+
38
+ async connect(_securityService, hapiServer, config) {
39
+ this.authServerConfig = config;
40
+ securityService = _securityService
41
+ const stateOption = {
42
+ clearInvalid: true,
43
+ encoding: 'base64',
44
+ isSecure: true,
45
+ isHttpOnly: true,
46
+ isSameSite: 'Lax',
47
+ path: '/',
48
+ strictHeader: true
49
+ }
50
+ hapiServer.config = config
51
+ hapiServer.state(this.COOKIE_NAMES.ACCESS_TOKEN, stateOption)
52
+ this.authServerSimulation(hapiServer)
53
+ }
54
+
55
+ authServerSimulation(hapiServer) {
56
+ if (!hapiServer.config || !hapiServer.config.accessTokenSimulation) {
57
+ throw new Exception('Error parsing metadata for simulation', 'ConfigurationError', 404)
58
+ }
59
+ let {
60
+ simaAlgorithm,
61
+ payload,
62
+ secret
63
+ } = hapiServer.config.accessTokenSimulation
64
+ const me = this
65
+ hapiServer.ext('onPreAuth', async function(request, h) {
66
+ if (request.state && request.state[me.COOKIE_NAMES.ACCESS_TOKEN]) {
67
+ return h.continue
68
+ } else {
69
+ switch (simaAlgorithm) {
70
+ case 'HMAC-SHA384': {
71
+ simaAlgorithm = 'HS384'
72
+ break
73
+ }
74
+ case 'HMAC-SHA512': {
75
+ simaAlgorithm = 'HS512'
76
+ break
77
+ }
78
+ default: {
79
+ simaAlgorithm = 'HS256'
80
+ }
81
+ }
82
+ const token = jwToken.sign(payload, secret, { expiresIn: '1h', algorithm: simaAlgorithm });
83
+ return h
84
+ .response()
85
+ .state(me.COOKIE_NAMES.ACCESS_TOKEN, token)
86
+ .redirect(getFullUrl(request))
87
+ .takeover()
88
+ }
89
+ })
90
+ // /get-authorization
91
+ hapiServer.route({
92
+ path: '/get-authorization',
93
+ method: 'GET',
94
+ handler: async function(_request, h) {
95
+ return h
96
+ .response('[]')
97
+ .code(200)
98
+ }
99
+ })
100
+ // /get-security-rules
101
+ hapiServer.route({
102
+ path: '/get-security-rules',
103
+ method: 'GET',
104
+ handler: async function(_request, h) {
105
+ let securityRules = []
106
+ if (securityService && hapiServer.config.accessTokenSimulation.playload) {
107
+ const groups = securityService.getGroups(hapiServer.config.accessTokenSimulation.playload)
108
+ securityRules = securityService.getFrontendSecurityRules([groups])
109
+ }
110
+ return h
111
+ .response(JSON.stringify(securityRules))
112
+ .code(200)
113
+ }
114
+ })
115
+ // /get-permissions
116
+ hapiServer.route({
117
+ path: '/get-permissions',
118
+ method: 'GET',
119
+ handler: async function(_request, h) {
120
+ const permissions = (securityService) ? securityService.getPermissions() : []
121
+ return h
122
+ .response(JSON.stringify(permissions))
123
+ .code(200)
124
+ }
125
+ })
126
+ // /get-user-info
127
+ hapiServer.route({
128
+ path: '/get-user-info',
129
+ method: 'GET',
130
+ handler: async function(_request, h) {
131
+ return h
132
+ .response(JSON.stringify(payload))
133
+ .code(200)
134
+ }
135
+ })
136
+ // /logout
137
+ hapiServer.route({
138
+ path: '/logout',
139
+ method: 'GET',
140
+ handler: async function(_request, h) {
141
+ return h
142
+ .response()
143
+ .unstate(me.COOKIE_NAMES.ACCESS_TOKEN)
144
+ .takeover()
145
+ }
146
+ })
147
+ }
148
+ async authenticate(h, scope) {
149
+ const {
150
+ request
151
+ } = h
152
+ const pkceCode = await this.openIdConnect.pkceCode()
153
+ const requestUrl = getFullUrl(request)
154
+ let oidcMetadata = await this.openIdConnect.oidcMetadata()
155
+ if (!oidcMetadata || !oidcMetadata.openid_configuration) {
156
+ oidcMetadata = await this.configuration(this.authServerConfig.authServer)
157
+ }
158
+ if (requestUrl.match(new RegExp(/^(https?:\/{2}.*):?(\d*)/.source + getHost(request) + /\/?$/.source))) {
159
+ const authorizationUrl = await this.openIdConnect.authorizationUrl({
160
+ scope,
161
+ redirectUri: getFullUrl(request),
162
+ pkceCode
163
+ })
164
+ trace('INFO', `Authenticate redirecting to ${authorizationUrl}`)
165
+ return h
166
+ .response()
167
+ .state(this.COOKIE_NAMES.SID, pkceCode)
168
+ .redirect(authorizationUrl)
169
+ .takeover()
170
+ } else if (getPathname(request) === '/logout') {
171
+ return h.continue
172
+ } else {
173
+ const tokenSet = await this.openIdConnect.tokenSet()
174
+ const {
175
+ state
176
+ } = request
177
+ if (tokenSet && state && state[this.COOKIE_NAMES.SESSION_STATE]) {
178
+ const tokens = await tokenSet.tokens(state[this.COOKIE_NAMES.SESSION_STATE])
179
+ if (!tokens || tokens.refresh_expires_in <= getTokenTolerance(0)) {
180
+ throw new Exception('Error when getting token', 'ExpirationError', 403)
181
+ }
182
+ return h.continue
183
+ } else {
184
+ return h
185
+ .response()
186
+ .code(401)
187
+ .takeover()
188
+ }
189
+ }
190
+ }
191
+ oidcMetadataKey() {
192
+ return this.authServerConfig.authServer.sessionCookiesDomain || 'oidcMetadata'
193
+ }
194
+ async configuration(authServer) {
195
+ if (!authServer) {
196
+ throw new Exception('Error when getting configuration attributes ')
197
+ }
198
+ const {
199
+ clientId,
200
+ clientSecret
201
+ } = authServer
202
+ await this.openIdConnect.client({
203
+ clientId,
204
+ clientSecret
205
+ })
206
+ if (authServer.openIdConfigurationEndpoint) {
207
+ return await this.openIdConnect.configuration(authServer.openIdConfigurationEndpoint)
208
+ } else {
209
+ // If configuration uri does not exist but the auth server form has been filled in.
210
+ return await this.openIdConnect.configuration({
211
+ issuer: authServer.issuer,
212
+ authorization_endpoint: authServer.authorizationEndpoint,
213
+ token_endpoint: authServer.tokenEndpoint,
214
+ userinfo_endpoint: authServer.userinfoEndpoint,
215
+ end_session_endpoint: authServer.endSessionEndpoint,
216
+ jwks_uri: authServer.jwksUri
217
+ })
218
+ }
219
+ }
220
+ async configuration(context) {
221
+ let metadata = await this.cache.get(this.oidcMetadataKey())
222
+ if (typeof context === 'string' && !context.match(/(https?:\/\/.*):?(\d*)\/?(.*)/gi)) {
223
+ throw new Exception('Wrong OpenId Provider configuration URI entered', 'AttributeError', 403)
224
+ }
225
+ if (!metadata || !metadata.issuer) {
226
+ if (context.issuer) {
227
+ metadata = {
228
+ ...(metadata || {}),
229
+ ...context
230
+ }
231
+ } else {
232
+ metadata = metadata || {}
233
+ metadata.openid_configuration = context
234
+ metadata = {
235
+ ...metadata,
236
+ ...(await Issuer.discover(context.issuer))
237
+ } // Discover an issuer configuration, must be an url
238
+ }
239
+ await this.cache.set(this.oidcMetadataKey(), metadata, 864e5) // 1 day of cache
240
+ }
241
+ return new Iss(metadata)
242
+ }
243
+ }
244
+
245
+ module.exports = {
246
+ HapiServerSimToken
247
+ }