@navservice/core 1.93.0 → 1.95.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/build/es/helpers.js +43 -50
- package/build/lib/helpers.cjs +7 -6
- package/package.json +1 -1
package/build/es/helpers.js
CHANGED
|
@@ -264,65 +264,24 @@ const set_response = class {
|
|
|
264
264
|
};
|
|
265
265
|
};
|
|
266
266
|
const _set_response = set_response;
|
|
267
|
-
const scryptAsync = promisify(scrypt);
|
|
268
|
-
class _secret {
|
|
269
|
-
static SALT_LENGTH = 22;
|
|
270
|
-
static KEY_LENGTH = 35;
|
|
271
|
-
static async gerar_hash_senha(password) {
|
|
272
|
-
const salt = randomBytes(this.SALT_LENGTH);
|
|
273
|
-
const derivedKey = await scryptAsync(password, salt, this.KEY_LENGTH);
|
|
274
|
-
return salt.toString('base64') + '.' + derivedKey.toString('base64');
|
|
275
|
-
}
|
|
276
|
-
static async verify({ password, dashed_senha }) {
|
|
277
|
-
try {
|
|
278
|
-
const [saltBase64, hashBase64] = dashed_senha.split('.');
|
|
279
|
-
if (!saltBase64 || !hashBase64) return false;
|
|
280
|
-
const salt = Buffer.from(saltBase64, 'base64');
|
|
281
|
-
const storedHash = Buffer.from(hashBase64, 'base64');
|
|
282
|
-
const derivedKey = await scryptAsync(password, salt, this.KEY_LENGTH);
|
|
283
|
-
return timingSafeEqual(storedHash, derivedKey);
|
|
284
|
-
} catch {
|
|
285
|
-
return false;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
const helpers_secret = _secret;
|
|
290
|
-
class _data {
|
|
291
|
-
static get get_now_format_number() {
|
|
292
|
-
return Math.floor(Date.now() / 1000);
|
|
293
|
-
}
|
|
294
|
-
static verificar_data(value) {
|
|
295
|
-
const data = new Date(1000 * value);
|
|
296
|
-
if (isNaN(data.getTime())) throw new Error("Timestamp está inválido.");
|
|
297
|
-
return value;
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
const helpers_data = _data;
|
|
301
|
-
const helpers = {
|
|
302
|
-
set_response: _set_response,
|
|
303
|
-
token: helpers_token,
|
|
304
|
-
secret: helpers_secret,
|
|
305
|
-
data: helpers_data
|
|
306
|
-
};
|
|
307
|
-
const src_helpers = helpers;
|
|
308
267
|
const _token = class {
|
|
309
268
|
static async verificar_token(c, next) {
|
|
310
269
|
try {
|
|
311
270
|
const authHeader = c.req.header("Authorization");
|
|
312
|
-
if (!authHeader) return
|
|
271
|
+
if (!authHeader) return _set_response.c.INVALID_TOKEN({
|
|
313
272
|
message: 'token não enviado!!!',
|
|
314
273
|
c: c,
|
|
315
274
|
results: []
|
|
316
275
|
});
|
|
317
276
|
const token = authHeader.split(" ")[1];
|
|
318
|
-
if (!token) return
|
|
277
|
+
if (!token) return _set_response.c.INVALID_TOKEN({
|
|
319
278
|
message: 'Acesso negado!!!',
|
|
320
279
|
c: c,
|
|
321
280
|
results: []
|
|
322
281
|
});
|
|
323
282
|
const secret = new TextEncoder().encode(c.env.JSON_WEB_TOKEN_AUTH_USER);
|
|
324
283
|
const { payload } = await jwtVerify(token, secret);
|
|
325
|
-
if (!payload._id || !payload.email) return
|
|
284
|
+
if (!payload._id || !payload.email) return _set_response.c.INVALID_TOKEN({
|
|
326
285
|
message: 'Token inválido!!!!',
|
|
327
286
|
c: c,
|
|
328
287
|
results: []
|
|
@@ -339,7 +298,7 @@ const _token = class {
|
|
|
339
298
|
c.set("usuario_auth", setar_token);
|
|
340
299
|
return await next();
|
|
341
300
|
} catch (error) {
|
|
342
|
-
return
|
|
301
|
+
return _set_response.c.INVALID_TOKEN({
|
|
343
302
|
message: 'Token inválido!!!',
|
|
344
303
|
c: c,
|
|
345
304
|
results: []
|
|
@@ -347,7 +306,7 @@ const _token = class {
|
|
|
347
306
|
}
|
|
348
307
|
}
|
|
349
308
|
static async criar_token_login_usuario({ _id, email, app, usuario_tipo, ativo, data_criacao, nome, c }) {
|
|
350
|
-
if (!c.env.JSON_WEB_TOKEN_AUTH_USER) return
|
|
309
|
+
if (!c.env.JSON_WEB_TOKEN_AUTH_USER) return _set_response.error.WARNING({
|
|
351
310
|
message: "Erro ao gerar token!!",
|
|
352
311
|
results: []
|
|
353
312
|
});
|
|
@@ -361,7 +320,7 @@ const _token = class {
|
|
|
361
320
|
nome: nome
|
|
362
321
|
}).setProtectedHeader({
|
|
363
322
|
alg: "HS256"
|
|
364
|
-
}).setIssuedAt().sign(new TextEncoder().encode(c.env.JSON_WEB_TOKEN_AUTH_USER)).catch((error)=>
|
|
323
|
+
}).setIssuedAt().sign(new TextEncoder().encode(c.env.JSON_WEB_TOKEN_AUTH_USER)).catch((error)=>_set_response.error.WARNING({
|
|
365
324
|
message: "Erro ao gerar token!",
|
|
366
325
|
results: error?.message
|
|
367
326
|
}));
|
|
@@ -369,11 +328,45 @@ const _token = class {
|
|
|
369
328
|
}
|
|
370
329
|
};
|
|
371
330
|
const helpers_token = _token;
|
|
372
|
-
const
|
|
331
|
+
const scryptAsync = promisify(scrypt);
|
|
332
|
+
class _secret {
|
|
333
|
+
static SALT_LENGTH = 22;
|
|
334
|
+
static KEY_LENGTH = 35;
|
|
335
|
+
static async gerar_hash_senha(password) {
|
|
336
|
+
const salt = randomBytes(this.SALT_LENGTH);
|
|
337
|
+
const derivedKey = await scryptAsync(password, salt, this.KEY_LENGTH);
|
|
338
|
+
return salt.toString('base64') + '.' + derivedKey.toString('base64');
|
|
339
|
+
}
|
|
340
|
+
static async verify({ password, dashed_senha }) {
|
|
341
|
+
try {
|
|
342
|
+
const [saltBase64, hashBase64] = dashed_senha.split('.');
|
|
343
|
+
if (!saltBase64 || !hashBase64) return false;
|
|
344
|
+
const salt = Buffer.from(saltBase64, 'base64');
|
|
345
|
+
const storedHash = Buffer.from(hashBase64, 'base64');
|
|
346
|
+
const derivedKey = await scryptAsync(password, salt, this.KEY_LENGTH);
|
|
347
|
+
return timingSafeEqual(storedHash, derivedKey);
|
|
348
|
+
} catch {
|
|
349
|
+
return false;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
const helpers_secret = _secret;
|
|
354
|
+
class _data {
|
|
355
|
+
static get get_now_format_number() {
|
|
356
|
+
return Math.floor(Date.now() / 1000);
|
|
357
|
+
}
|
|
358
|
+
static verificar_data(value) {
|
|
359
|
+
const data = new Date(1000 * value);
|
|
360
|
+
if (isNaN(data.getTime())) throw new Error("Timestamp está inválido.");
|
|
361
|
+
return value;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
const helpers_data = _data;
|
|
365
|
+
const helpers = {
|
|
373
366
|
set_response: _set_response,
|
|
374
367
|
token: helpers_token,
|
|
375
368
|
secret: helpers_secret,
|
|
376
369
|
data: helpers_data
|
|
377
370
|
};
|
|
378
|
-
const
|
|
379
|
-
export {
|
|
371
|
+
const src_helpers = helpers;
|
|
372
|
+
export { src_helpers as default };
|
package/build/lib/helpers.cjs
CHANGED
|
@@ -327,18 +327,19 @@ const set_response = class set_response {
|
|
|
327
327
|
const external_jose_namespaceObject = require("jose");
|
|
328
328
|
;// CONCATENATED MODULE: ./src/helpers/_token.ts
|
|
329
329
|
|
|
330
|
+
// Tem que importar assim para evitar dependencia circular
|
|
330
331
|
|
|
331
332
|
const _token = class _token {
|
|
332
333
|
static async verificar_token(c, next) {
|
|
333
334
|
try {
|
|
334
335
|
const authHeader = c.req.header("Authorization");
|
|
335
|
-
if (!authHeader) return
|
|
336
|
+
if (!authHeader) return _set_response.c.INVALID_TOKEN({
|
|
336
337
|
message: 'token não enviado!!!',
|
|
337
338
|
c: c,
|
|
338
339
|
results: []
|
|
339
340
|
});
|
|
340
341
|
const token = authHeader.split(" ")[1];
|
|
341
|
-
if (!token) return
|
|
342
|
+
if (!token) return _set_response.c.INVALID_TOKEN({
|
|
342
343
|
message: 'Acesso negado!!!',
|
|
343
344
|
c: c,
|
|
344
345
|
results: []
|
|
@@ -346,7 +347,7 @@ const _token = class _token {
|
|
|
346
347
|
const secret = new TextEncoder().encode(c.env.JSON_WEB_TOKEN_AUTH_USER);
|
|
347
348
|
const { payload } = await external_jose_namespaceObject.jwtVerify(token, secret);
|
|
348
349
|
if (!payload._id || !payload.email) {
|
|
349
|
-
return
|
|
350
|
+
return _set_response.c.INVALID_TOKEN({
|
|
350
351
|
message: 'Token inválido!!!!',
|
|
351
352
|
c: c,
|
|
352
353
|
results: []
|
|
@@ -364,7 +365,7 @@ const _token = class _token {
|
|
|
364
365
|
c.set("usuario_auth", setar_token);
|
|
365
366
|
return await next();
|
|
366
367
|
} catch (error) {
|
|
367
|
-
return
|
|
368
|
+
return _set_response.c.INVALID_TOKEN({
|
|
368
369
|
message: 'Token inválido!!!',
|
|
369
370
|
c: c,
|
|
370
371
|
results: []
|
|
@@ -373,7 +374,7 @@ const _token = class _token {
|
|
|
373
374
|
}
|
|
374
375
|
static async criar_token_login_usuario({ _id, email, app, usuario_tipo, ativo, data_criacao, nome, c }) {
|
|
375
376
|
if (!c.env.JSON_WEB_TOKEN_AUTH_USER) {
|
|
376
|
-
return
|
|
377
|
+
return _set_response.error.WARNING({
|
|
377
378
|
message: "Erro ao gerar token!!",
|
|
378
379
|
results: []
|
|
379
380
|
});
|
|
@@ -390,7 +391,7 @@ const _token = class _token {
|
|
|
390
391
|
alg: "HS256"
|
|
391
392
|
}).setIssuedAt()//.setExpirationTime("24h")
|
|
392
393
|
.sign(new TextEncoder().encode(c.env.JSON_WEB_TOKEN_AUTH_USER)).catch((error)=>{
|
|
393
|
-
return
|
|
394
|
+
return _set_response.error.WARNING({
|
|
394
395
|
message: "Erro ao gerar token!",
|
|
395
396
|
results: error?.message
|
|
396
397
|
});
|