@excofy/utils 2.1.0 → 2.1.1
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.cjs +26 -2
- package/dist/index.d.cts +20 -1
- package/dist/index.d.ts +20 -1
- package/dist/index.js +24 -2
- package/package.json +1 -1
- package/src/errors/crypto/CryptoError.ts +29 -0
- package/src/errors/index.ts +1 -0
- package/src/helpers/crypto.ts +7 -2
- package/src/index.ts +3 -0
package/dist/index.cjs
CHANGED
|
@@ -30,6 +30,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
+
ExpiredTokenError: () => ExpiredTokenError,
|
|
34
|
+
InvalidTokenError: () => InvalidTokenError,
|
|
33
35
|
createValidator: () => createValidator,
|
|
34
36
|
cryptoUtils: () => cryptoUtils,
|
|
35
37
|
generateCode: () => generateCode,
|
|
@@ -684,6 +686,26 @@ var stringUtils = {
|
|
|
684
686
|
removeFileExtension: (fileName) => fileName.replace(/\.[^/.]+$/, "")
|
|
685
687
|
};
|
|
686
688
|
|
|
689
|
+
// src/errors/crypto/CryptoError.ts
|
|
690
|
+
var CryptoError = class extends Error {
|
|
691
|
+
constructor(message) {
|
|
692
|
+
super(message);
|
|
693
|
+
this.name = "CryptoError";
|
|
694
|
+
}
|
|
695
|
+
};
|
|
696
|
+
var InvalidTokenError = class extends CryptoError {
|
|
697
|
+
constructor(message = "Invalid or malformed token") {
|
|
698
|
+
super(message);
|
|
699
|
+
this.name = "InvalidTokenError";
|
|
700
|
+
}
|
|
701
|
+
};
|
|
702
|
+
var ExpiredTokenError = class extends CryptoError {
|
|
703
|
+
constructor(message = "Token has expired") {
|
|
704
|
+
super(message);
|
|
705
|
+
this.name = "ExpiredTokenError";
|
|
706
|
+
}
|
|
707
|
+
};
|
|
708
|
+
|
|
687
709
|
// src/helpers/crypto.ts
|
|
688
710
|
var encoder = new TextEncoder();
|
|
689
711
|
var decoder = new TextDecoder();
|
|
@@ -809,11 +831,11 @@ var cryptoUtils = {
|
|
|
809
831
|
);
|
|
810
832
|
if (!valid) throw new Error("Invalid access token");
|
|
811
833
|
} catch (err) {
|
|
812
|
-
throw new
|
|
834
|
+
throw new InvalidTokenError("Invalid or malformed access token");
|
|
813
835
|
}
|
|
814
836
|
const { expiresAt } = JSON.parse(payloadDecrypted);
|
|
815
837
|
if (Date.now() > new Date(expiresAt).getTime()) {
|
|
816
|
-
throw new
|
|
838
|
+
throw new ExpiredTokenError("Access token has expired");
|
|
817
839
|
}
|
|
818
840
|
return payloadDecrypted;
|
|
819
841
|
},
|
|
@@ -866,6 +888,8 @@ var toDecimal = (value) => {
|
|
|
866
888
|
};
|
|
867
889
|
// Annotate the CommonJS export names for ESM import in node:
|
|
868
890
|
0 && (module.exports = {
|
|
891
|
+
ExpiredTokenError,
|
|
892
|
+
InvalidTokenError,
|
|
869
893
|
createValidator,
|
|
870
894
|
cryptoUtils,
|
|
871
895
|
generateCode,
|
package/dist/index.d.cts
CHANGED
|
@@ -200,4 +200,23 @@ declare namespace number {
|
|
|
200
200
|
export { number_toCents as toCents, number_toDecimal as toDecimal };
|
|
201
201
|
}
|
|
202
202
|
|
|
203
|
-
|
|
203
|
+
/**
|
|
204
|
+
* Classe base para erros de criptografia
|
|
205
|
+
*/
|
|
206
|
+
declare class CryptoError extends Error {
|
|
207
|
+
constructor(message: string);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Token inválido, malformado ou corrompido
|
|
211
|
+
*/
|
|
212
|
+
declare class InvalidTokenError extends CryptoError {
|
|
213
|
+
constructor(message?: string);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Token válido mas expirado
|
|
217
|
+
*/
|
|
218
|
+
declare class ExpiredTokenError extends CryptoError {
|
|
219
|
+
constructor(message?: string);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export { ExpiredTokenError, InvalidTokenError, createValidator, cryptoUtils, generateCode, htmlEntityDecode, number as numberUtils, slug as slugUtils, stringUtils };
|
package/dist/index.d.ts
CHANGED
|
@@ -200,4 +200,23 @@ declare namespace number {
|
|
|
200
200
|
export { number_toCents as toCents, number_toDecimal as toDecimal };
|
|
201
201
|
}
|
|
202
202
|
|
|
203
|
-
|
|
203
|
+
/**
|
|
204
|
+
* Classe base para erros de criptografia
|
|
205
|
+
*/
|
|
206
|
+
declare class CryptoError extends Error {
|
|
207
|
+
constructor(message: string);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Token inválido, malformado ou corrompido
|
|
211
|
+
*/
|
|
212
|
+
declare class InvalidTokenError extends CryptoError {
|
|
213
|
+
constructor(message?: string);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Token válido mas expirado
|
|
217
|
+
*/
|
|
218
|
+
declare class ExpiredTokenError extends CryptoError {
|
|
219
|
+
constructor(message?: string);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export { ExpiredTokenError, InvalidTokenError, createValidator, cryptoUtils, generateCode, htmlEntityDecode, number as numberUtils, slug as slugUtils, stringUtils };
|
package/dist/index.js
CHANGED
|
@@ -648,6 +648,26 @@ var stringUtils = {
|
|
|
648
648
|
removeFileExtension: (fileName) => fileName.replace(/\.[^/.]+$/, "")
|
|
649
649
|
};
|
|
650
650
|
|
|
651
|
+
// src/errors/crypto/CryptoError.ts
|
|
652
|
+
var CryptoError = class extends Error {
|
|
653
|
+
constructor(message) {
|
|
654
|
+
super(message);
|
|
655
|
+
this.name = "CryptoError";
|
|
656
|
+
}
|
|
657
|
+
};
|
|
658
|
+
var InvalidTokenError = class extends CryptoError {
|
|
659
|
+
constructor(message = "Invalid or malformed token") {
|
|
660
|
+
super(message);
|
|
661
|
+
this.name = "InvalidTokenError";
|
|
662
|
+
}
|
|
663
|
+
};
|
|
664
|
+
var ExpiredTokenError = class extends CryptoError {
|
|
665
|
+
constructor(message = "Token has expired") {
|
|
666
|
+
super(message);
|
|
667
|
+
this.name = "ExpiredTokenError";
|
|
668
|
+
}
|
|
669
|
+
};
|
|
670
|
+
|
|
651
671
|
// src/helpers/crypto.ts
|
|
652
672
|
var encoder = new TextEncoder();
|
|
653
673
|
var decoder = new TextDecoder();
|
|
@@ -773,11 +793,11 @@ var cryptoUtils = {
|
|
|
773
793
|
);
|
|
774
794
|
if (!valid) throw new Error("Invalid access token");
|
|
775
795
|
} catch (err) {
|
|
776
|
-
throw new
|
|
796
|
+
throw new InvalidTokenError("Invalid or malformed access token");
|
|
777
797
|
}
|
|
778
798
|
const { expiresAt } = JSON.parse(payloadDecrypted);
|
|
779
799
|
if (Date.now() > new Date(expiresAt).getTime()) {
|
|
780
|
-
throw new
|
|
800
|
+
throw new ExpiredTokenError("Access token has expired");
|
|
781
801
|
}
|
|
782
802
|
return payloadDecrypted;
|
|
783
803
|
},
|
|
@@ -829,6 +849,8 @@ var toDecimal = (value) => {
|
|
|
829
849
|
return new Big(value).div(100).round(2).toNumber();
|
|
830
850
|
};
|
|
831
851
|
export {
|
|
852
|
+
ExpiredTokenError,
|
|
853
|
+
InvalidTokenError,
|
|
832
854
|
createValidator,
|
|
833
855
|
cryptoUtils,
|
|
834
856
|
generateCode,
|
package/package.json
CHANGED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Classe base para erros de criptografia
|
|
3
|
+
*/
|
|
4
|
+
export class CryptoError extends Error {
|
|
5
|
+
constructor(message: string) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = 'CryptoError';
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Token inválido, malformado ou corrompido
|
|
13
|
+
*/
|
|
14
|
+
export class InvalidTokenError extends CryptoError {
|
|
15
|
+
constructor(message: string = 'Invalid or malformed token') {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = 'InvalidTokenError';
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Token válido mas expirado
|
|
23
|
+
*/
|
|
24
|
+
export class ExpiredTokenError extends CryptoError {
|
|
25
|
+
constructor(message: string = 'Token has expired') {
|
|
26
|
+
super(message);
|
|
27
|
+
this.name = 'ExpiredTokenError';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { InvalidTokenError, ExpiredTokenError } from './crypto/CryptoError';
|
package/src/helpers/crypto.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ExpiredTokenError,
|
|
3
|
+
InvalidTokenError,
|
|
4
|
+
} from '../errors/crypto/CryptoError';
|
|
5
|
+
|
|
1
6
|
interface IGenerateAccessToken {
|
|
2
7
|
AUTH_SIGN_SECRET: string;
|
|
3
8
|
AUTH_PAYLOAD_SECRET: string;
|
|
@@ -228,13 +233,13 @@ export const cryptoUtils: ICrypto = {
|
|
|
228
233
|
if (!valid) throw new Error('Invalid access token');
|
|
229
234
|
} catch (err) {
|
|
230
235
|
// qualquer falha na decriptação ou verificação -> token inválido
|
|
231
|
-
throw new
|
|
236
|
+
throw new InvalidTokenError('Invalid or malformed access token');
|
|
232
237
|
}
|
|
233
238
|
|
|
234
239
|
// verifica expiração **fora do try/catch**, para não ser capturada como token inválido
|
|
235
240
|
const { expiresAt } = JSON.parse(payloadDecrypted);
|
|
236
241
|
if (Date.now() > new Date(expiresAt).getTime()) {
|
|
237
|
-
throw new
|
|
242
|
+
throw new ExpiredTokenError('Access token has expired');
|
|
238
243
|
}
|
|
239
244
|
|
|
240
245
|
return payloadDecrypted;
|