@nsshunt/stsutils 1.4.0 → 1.5.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.
Files changed (2) hide show
  1. package/authutils.js +183 -0
  2. package/package.json +3 -2
package/authutils.js CHANGED
@@ -1,12 +1,20 @@
1
+ const debug = require('debug')(`stsutils`);
1
2
  const jwt = require('jsonwebtoken');
2
3
  const { status } = require('./status');
3
4
  const fs = require('fs');
4
5
  const goptions = require('@nsshunt/stsconfig').$options;
6
+ const tough = require('tough-cookie');
7
+ const Cookie = tough.Cookie;
8
+ const cookiejar = new tough.CookieJar();
9
+ const colors = require('colors');
10
+ const axios = require('axios');
11
+ const http = require('http');
5
12
 
6
13
  class AuthUtils
7
14
  {
8
15
  #privateKey = null;
9
16
  #publicKey = null;
17
+ #httpAgent = null;
10
18
 
11
19
  constructor()
12
20
  {
@@ -105,6 +113,181 @@ class AuthUtils
105
113
  }
106
114
  }
107
115
  }
116
+
117
+ #GetDurationColour(duration)
118
+ {
119
+ if (duration > 250) {
120
+ return colors.red;
121
+ } else if (duration > 150) {
122
+ return colors.magenta;
123
+ } else if (duration > 50) {
124
+ return colors.blue;
125
+ } else if (duration > 10) {
126
+ return colors.green;
127
+ } else {
128
+ return colors.black;
129
+ }
130
+ }
131
+
132
+ LoginBrowser = async (options) => {
133
+ const { authendpoint, authUserName, authUserEMail, authUserPassword, defaultTimeout, publishDebug } = options;
134
+ try {
135
+ let processStart = performance.now();
136
+ let duration = 0;
137
+ let accessToken = null;
138
+ let payload = { name: authUserName, password: authUserPassword, email: authUserEMail }
139
+ let retVal = await axios({
140
+ url: `${authendpoint}/login`
141
+ ,method: 'post'
142
+ ,data: payload
143
+ ,timeout: defaultTimeout
144
+ });
145
+ duration = (performance.now() - processStart).toFixed(4);
146
+ if (publishDebug) debug(this.#GetDurationColour(duration)(`AuthUtils.LoginBrowser request duration: [${duration}]`));
147
+ accessToken = retVal.data.detail.token;
148
+ return {
149
+ accessToken: accessToken,
150
+ duration: duration
151
+ }
152
+ } catch (error) {
153
+ if (publishDebug) debug(`Error (AuthUtils:LoginBrowser): ${error}`.red);
154
+ throw error;
155
+ }
156
+ };
157
+
158
+ // https://stackoverflow.com/questions/43002444/make-axios-send-cookies-in-its-requests-automatically
159
+ // axios.get('some api url', {withCredentials: true});
160
+ // https://medium.com/@adityasrivast/handling-cookies-with-axios-872790241a9b
161
+ // https://www.codegrepper.com/code-examples/javascript/axios+send+cookies
162
+ // http only cookies
163
+ RefreshAuthTokenBrowser = async (options) => {
164
+ const { authendpoint, defaultTimeout, publishDebug } = options;
165
+ try {
166
+ let processStart = performance.now();
167
+ let duration = 0;
168
+ let accessToken = null;
169
+ // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials
170
+ // https://stackoverflow.com/questions/43002444/make-axios-send-cookies-in-its-requests-automatically
171
+ // axios.get('some api url', {withCredentials: true});
172
+ let retVal = await axios({
173
+ url: `${authendpoint}/refreshtoken`
174
+ ,method: 'post'
175
+ ,timeout: defaultTimeout
176
+ ,withCredentials: true
177
+ });
178
+ duration = (performance.now() - processStart).toFixed(4);
179
+ if (publishDebug) debug(this.#GetDurationColour(duration)(`AuthUtils.RefreshAuthTokenBrowser request duration: [${duration}]`));
180
+ accessToken = retVal.data.detail.token;
181
+ return {
182
+ accessToken: accessToken,
183
+ duration: duration
184
+ }
185
+ } catch (error) {
186
+ if (publishDebug) debug(`Error (AuthUtils:RefreshAuthTokenBrowser): ${error}`.red);
187
+ throw error;
188
+ }
189
+ }
190
+
191
+ #setCookiesToJar = (authendpoint, headers) =>
192
+ {
193
+ let cookies = null;
194
+ if (headers['set-cookie'] instanceof Array)
195
+ cookies = headers['set-cookie'].map(Cookie.parse);
196
+ else
197
+ cookies = [Cookie.parse(headers['set-cookie'])];
198
+
199
+ return cookiejar.setCookie(cookies[0], `${authendpoint}`);
200
+ };
201
+
202
+ #getCookiesFromJar = (authendpoint) =>
203
+ {
204
+ return cookiejar.getCookies(`${authendpoint}`);
205
+ };
206
+
207
+ #getHttpAgent = () =>
208
+ {
209
+ if (this.#httpAgent === null) {
210
+ // https://nodejs.org/api/http.html#class-httpagent
211
+ this.#httpAgent = new http.Agent({
212
+ keepAlive: true,
213
+ maxSockets: 10,
214
+ maxTotalSockets: 10,
215
+ maxFreeSockets: 10,
216
+ timeout: 5000
217
+ });
218
+ }
219
+ return this.#httpAgent;
220
+ }
221
+
222
+ LoginNode = async (options) =>
223
+ {
224
+ const { authendpoint, authUserName, authUserEMail, authUserPassword, defaultTimeout, publishDebug } = options;
225
+ try {
226
+ let processStart = performance.now();
227
+ let duration = 0;
228
+ let accessToken = null;
229
+ let payload = { name: authUserName, password: authUserPassword, email: authUserEMail }
230
+ let retVal = await axios({
231
+ url: `${authendpoint}/login`
232
+ ,method: 'post'
233
+ ,data: payload
234
+ ,timeout: defaultTimeout
235
+ ,httpAgent: this.#getHttpAgent()
236
+ // Use below if using a socket endpoint
237
+ //,socketPath: '/var/run/sts/stsrest01.sock'
238
+ });
239
+ duration = (performance.now() - processStart).toFixed(4);
240
+ if (publishDebug) debug(this.#GetDurationColour(duration)(`AuthUtils.LoginNode request duration: [${duration}]`));
241
+ accessToken = retVal.data.detail.token;
242
+ await this.#setCookiesToJar(authendpoint, retVal.headers);
243
+ return {
244
+ accessToken: accessToken,
245
+ duration: duration
246
+ }
247
+ } catch (error)
248
+ {
249
+ if (publishDebug) debug(`Error (AuthUtils:LoginNode): ${error}`.red);
250
+ throw error;
251
+ }
252
+ }
253
+
254
+ // https://stackoverflow.com/questions/43002444/make-axios-send-cookies-in-its-requests-automatically
255
+ // axios.get('some api url', {withCredentials: true});
256
+ // https://medium.com/@adityasrivast/handling-cookies-with-axios-872790241a9b
257
+ // https://www.codegrepper.com/code-examples/javascript/axios+send+cookies
258
+ // http only cookies
259
+ RefreshAuthTokenNode = async (options) =>
260
+ {
261
+ const { authendpoint, defaultTimeout, publishDebug } = options;
262
+ try {
263
+ let processStart = performance.now();
264
+ let duration = 0;
265
+ let accessToken = null;
266
+ let cookies = await this.#getCookiesFromJar(authendpoint);
267
+ let retVal = await axios({
268
+ url: `${authendpoint}/refreshtoken`
269
+ ,method: 'post'
270
+ ,headers: {
271
+ Cookie: cookies
272
+ }
273
+ ,timeout: defaultTimeout
274
+ ,httpAgent: this.#httpAgent,
275
+ // Use below for socket connections
276
+ //,socketPath: '/var/run/sts/stsrest01.sock'
277
+ });
278
+ duration = (performance.now() - processStart).toFixed(4);
279
+ if (publishDebug) debug(this.#GetDurationColour(duration)(`AuthUtils.RefreshAuthTokenBrowser request duration: [${duration}]`));
280
+ accessToken = retVal.data.detail.token;
281
+ await this.#setCookiesToJar(authendpoint, retVal.headers);
282
+ return {
283
+ accessToken: accessToken,
284
+ duration: duration
285
+ }
286
+ } catch (error) {
287
+ if (publishDebug) debug(`Error (AuthUtils:RefreshAuthTokenBrowser): ${error}`.red);
288
+ throw error;
289
+ }
290
+ }
108
291
  }
109
292
 
110
293
  module.exports = { AuthUtils };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nsshunt/stsutils",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "",
5
5
  "main": "stsutils.js",
6
6
  "scripts": {
@@ -34,6 +34,7 @@
34
34
  "colors": "^1.4.0",
35
35
  "debug": "^4.3.3",
36
36
  "express": "^4.17.1",
37
- "jsonwebtoken": "^8.5.1"
37
+ "jsonwebtoken": "^8.5.1",
38
+ "tough-cookie": "^4.0.0"
38
39
  }
39
40
  }