jsonauthtoken 3.0.2 → 3.0.4-beta-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/node.index.d.mts +53 -0
- package/dist/node.index.d.ts +53 -0
- package/dist/node.index.js +767 -0
- package/dist/node.index.mjs +728 -0
- package/dist/types.d-D_Cze-FV.d.mts +32 -0
- package/dist/types.d-D_Cze-FV.d.ts +32 -0
- package/dist/types.d.ts +68 -0
- package/dist/web.index.d.mts +52 -0
- package/dist/web.index.d.ts +52 -0
- package/dist/web.index.js +599 -0
- package/dist/web.index.mjs +571 -0
- package/package.json +36 -5
- package/types.d.ts +54 -53
- package/dist/config/algo.config.d.ts +0 -2
- package/dist/config/algo.config.js +0 -28
- package/dist/config/name.config.d.ts +0 -1
- package/dist/config/name.config.js +0 -4
- package/dist/config/runtime.config.d.ts +0 -1
- package/dist/config/runtime.config.js +0 -15
- package/dist/index.d.ts +0 -34
- package/dist/index.js +0 -133
- package/dist/lib/decoading.lib.d.ts +0 -2
- package/dist/lib/decoading.lib.js +0 -9
- package/dist/lib/encoading.lib.d.ts +0 -2
- package/dist/lib/encoading.lib.js +0 -8
- package/dist/lib/functions.lib.d.ts +0 -12
- package/dist/lib/functions.lib.js +0 -85
- package/dist/lib/timeformat.d.ts +0 -1
- package/dist/lib/timeformat.js +0 -42
- package/dist/runtime/node.runtime.d.ts +0 -31
- package/dist/runtime/node.runtime.js +0 -150
- package/dist/runtime/runtime.d.ts +0 -12
- package/dist/runtime/runtime.js +0 -152
- package/dist/runtime/web.runtime.d.ts +0 -38
- package/dist/runtime/web.runtime.js +0 -180
package/types.d.ts
CHANGED
|
@@ -1,67 +1,68 @@
|
|
|
1
|
-
|
|
1
|
+
export interface WebJATInstance {
|
|
2
|
+
create: ({ key, exp, algo }: { key: string, exp: JsonAuthTokenExpiry, algo: RuntimeWiseAlgorithmMap['web'] }, payload: any) => Promise<string>
|
|
3
|
+
verify: <T>(token: string, key: string) => Promise<T>
|
|
4
|
+
}
|
|
2
5
|
|
|
3
|
-
|
|
6
|
+
export interface NodeJATInstance {
|
|
7
|
+
create: ({ key, exp, algo }: { key: string, exp: JsonAuthTokenExpiry, algo: RuntimeWiseAlgorithmMap['node'] }, payload: any) => Promise<string>
|
|
8
|
+
verify: <T>(token: string, key: string) => Promise<T>
|
|
9
|
+
}
|
|
4
10
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
11
|
+
export declare function JAT(config?: { runtime?: 'web' | 'node', dev?: boolean }): NodeJATInstance | WebJATInstance;
|
|
12
|
+
export declare function JAT(config: { runtime: 'web', dev?: boolean }): WebJATInstance;
|
|
13
|
+
export declare function JAT(config: { runtime: 'node', dev?: boolean }): NodeJATInstance;
|
|
8
14
|
|
|
9
|
-
interface GenerateKeyPair {
|
|
10
|
-
privateKey: string
|
|
11
|
-
publicKey: string
|
|
12
|
-
}
|
|
13
15
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
//timeformat type
|
|
17
|
+
export type TimeUnit = 'S' | 'MIN' | 'H' | 'D' | 'M' | 'Y';
|
|
18
|
+
export type ExpirationString = `${number}${TimeUnit}` | `${number}${Lowercase<TimeUnit>}`;
|
|
19
|
+
export type JsonAuthTokenExpiry = number | ExpirationString;
|
|
16
20
|
|
|
17
|
-
interface AlgorithmDetails {
|
|
18
|
-
name: string;
|
|
19
|
-
value: string
|
|
20
|
-
type: EncryptionAlgorithmType;
|
|
21
|
-
}
|
|
22
21
|
|
|
23
|
-
interface RuntimeWiseAlgorithmMap {
|
|
24
|
-
node: 'AES-256-GCM' | 'RSA+A256GCM'
|
|
25
|
-
edge: 'AES-GCM' | 'RSA+AES-GCM'
|
|
26
|
-
web: 'AES-GCM' | 'RSA+AES-GCM'
|
|
27
|
-
}
|
|
28
22
|
|
|
29
|
-
|
|
23
|
+
export interface GenerateKeyPair {
|
|
24
|
+
privateKey: string
|
|
25
|
+
publicKey: string
|
|
26
|
+
}
|
|
30
27
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
dev?: boolean
|
|
34
|
-
}
|
|
28
|
+
export type Runtimes = 'node' | 'web';
|
|
29
|
+
export type EncryptionAlgorithmType = 'symmetric' | 'asymmetric';
|
|
35
30
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
exp?: JATTime
|
|
39
|
-
algo?: RuntimeWiseAlgorithm<R>
|
|
40
|
-
}
|
|
31
|
+
export type WebRuntime = 'web'
|
|
32
|
+
export type NodeRuntime = 'node' | 'web'
|
|
41
33
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
34
|
+
export interface AlgorithmDetails {
|
|
35
|
+
name: string;
|
|
36
|
+
value: string
|
|
37
|
+
type: EncryptionAlgorithmType;
|
|
38
|
+
}
|
|
48
39
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
| { type: 'asymmetric'; encryptedKey: string }
|
|
56
|
-
))
|
|
57
|
-
| (BaseTokenMetaData<R> & {
|
|
58
|
-
runtime: 'web' | 'edge';
|
|
59
|
-
tag?: never;
|
|
60
|
-
} & (
|
|
61
|
-
| { type: 'symmetric'; encryptedKey?: never }
|
|
62
|
-
| { type: 'asymmetric'; encryptedKey: string }
|
|
63
|
-
));
|
|
40
|
+
export interface RuntimeWiseAlgorithmMap {
|
|
41
|
+
node: 'AES-256-GCM' | 'RSA+A256GCM'
|
|
42
|
+
web: 'AES-GCM' | 'RSA+AES-GCM'
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type RuntimeWiseAlgorithm<R extends Runtimes> = RuntimeWiseAlgorithmMap[R];
|
|
64
46
|
|
|
47
|
+
export interface BaseTokenMetaData<R extends Runtimes> {
|
|
48
|
+
runtime: Runtimes;
|
|
49
|
+
algo: RuntimeWiseAlgorithm<R>;
|
|
50
|
+
v: string;
|
|
51
|
+
iv: string;
|
|
65
52
|
}
|
|
66
53
|
|
|
67
|
-
export
|
|
54
|
+
export type TokenMetaData<R extends Runtimes> =
|
|
55
|
+
| (BaseTokenMetaData<R> & {
|
|
56
|
+
runtime: 'node';
|
|
57
|
+
tag: string;
|
|
58
|
+
} & (
|
|
59
|
+
| { type: 'symmetric'; encryptedKey?: never }
|
|
60
|
+
| { type: 'asymmetric'; encryptedKey: string }
|
|
61
|
+
))
|
|
62
|
+
| (BaseTokenMetaData<R> & {
|
|
63
|
+
runtime: 'web';
|
|
64
|
+
tag?: never;
|
|
65
|
+
} & (
|
|
66
|
+
| { type: 'symmetric'; encryptedKey?: never }
|
|
67
|
+
| { type: 'asymmetric'; encryptedKey: string }
|
|
68
|
+
));
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DEFAULT_ALGORITHM = exports.SUPPORTED_ALGORITHM = void 0;
|
|
4
|
-
exports.SUPPORTED_ALGORITHM = {
|
|
5
|
-
node: [
|
|
6
|
-
{ name: 'AES-256-GCM', value: 'aes-256-gcm', type: 'symmetric' },
|
|
7
|
-
{ name: 'RSA+A256GCM', value: 'rsa+a256gcm', type: 'asymmetric' },
|
|
8
|
-
],
|
|
9
|
-
web: [
|
|
10
|
-
{ name: 'AES-GCM', value: 'AES-GCM', type: 'symmetric' },
|
|
11
|
-
{ name: 'RSA+AES-GCM', value: 'RSA+AES-GCM', type: 'asymmetric' }
|
|
12
|
-
],
|
|
13
|
-
edge: [
|
|
14
|
-
{ name: 'AES-GCM', value: 'AES-GCM', type: 'symmetric' },
|
|
15
|
-
{ name: 'RSA+AES-GCM', value: 'RSA+AES-GCM', type: 'asymmetric' }
|
|
16
|
-
]
|
|
17
|
-
};
|
|
18
|
-
exports.DEFAULT_ALGORITHM = {
|
|
19
|
-
node: [
|
|
20
|
-
{ name: 'AES-256-GCM', value: 'aes-256-gcm', type: 'symmetric' },
|
|
21
|
-
],
|
|
22
|
-
web: [
|
|
23
|
-
{ name: 'AES-GCM', value: 'AES-GCM', type: 'symmetric' },
|
|
24
|
-
],
|
|
25
|
-
edge: [
|
|
26
|
-
{ name: 'AES-GCM', value: 'AES-GCM', type: 'symmetric' },
|
|
27
|
-
]
|
|
28
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const RUNTIME: Runtime[];
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function detectRuntime(): Runtime;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.detectRuntime = detectRuntime;
|
|
4
|
-
function detectRuntime() {
|
|
5
|
-
if (typeof process !== 'undefined' && process.versions?.node) {
|
|
6
|
-
return 'node';
|
|
7
|
-
}
|
|
8
|
-
if (typeof window !== 'undefined' && window.crypto?.subtle) {
|
|
9
|
-
return 'web';
|
|
10
|
-
}
|
|
11
|
-
if (typeof globalThis !== 'undefined' && globalThis.crypto?.subtle) {
|
|
12
|
-
return 'edge';
|
|
13
|
-
}
|
|
14
|
-
return 'node';
|
|
15
|
-
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
declare class JATClass<R extends Runtime = Runtime> {
|
|
2
|
-
private runtime;
|
|
3
|
-
private dev;
|
|
4
|
-
private crypto;
|
|
5
|
-
constructor(config?: JATConfig<R>);
|
|
6
|
-
create(header: CreateTokenConfig<R>, payload: any): Promise<string>;
|
|
7
|
-
verify<T>(token: string, key: string): Promise<T>;
|
|
8
|
-
}
|
|
9
|
-
export declare const JAT: <R extends Runtime = Runtime>(config?: JATConfig<R>) => JATClass<R>;
|
|
10
|
-
export declare const getSupportedAlgorithm: () => Record<Runtime, AlgorithmDetails[]>;
|
|
11
|
-
export declare const P2KG: {
|
|
12
|
-
generateKeyPair: (options?: {
|
|
13
|
-
runtime?: Runtime;
|
|
14
|
-
dev?: boolean;
|
|
15
|
-
}) => Promise<GenerateKeyPair>;
|
|
16
|
-
generatePublicKey: (privateKeyPem: string, options?: {
|
|
17
|
-
runtime?: Runtime;
|
|
18
|
-
dev?: boolean;
|
|
19
|
-
}) => Promise<string>;
|
|
20
|
-
};
|
|
21
|
-
declare const jsonauthtoken: {
|
|
22
|
-
JAT: <R extends Runtime = Runtime>(config?: JATConfig<R>) => JATClass<R>;
|
|
23
|
-
P2KG: {
|
|
24
|
-
generateKeyPair: (options?: {
|
|
25
|
-
runtime?: Runtime;
|
|
26
|
-
dev?: boolean;
|
|
27
|
-
}) => Promise<GenerateKeyPair>;
|
|
28
|
-
generatePublicKey: (privateKeyPem: string, options?: {
|
|
29
|
-
runtime?: Runtime;
|
|
30
|
-
dev?: boolean;
|
|
31
|
-
}) => Promise<string>;
|
|
32
|
-
};
|
|
33
|
-
};
|
|
34
|
-
export default jsonauthtoken;
|
package/dist/index.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.P2KG = exports.getSupportedAlgorithm = exports.JAT = void 0;
|
|
4
|
-
const name_config_1 = require("./config/name.config");
|
|
5
|
-
const runtime_config_1 = require("./config/runtime.config");
|
|
6
|
-
const algo_config_1 = require("./config/algo.config");
|
|
7
|
-
const functions_lib_1 = require("./lib/functions.lib");
|
|
8
|
-
const timeformat_1 = require("./lib/timeformat");
|
|
9
|
-
const runtime_1 = require("./runtime/runtime");
|
|
10
|
-
class JATClass {
|
|
11
|
-
runtime;
|
|
12
|
-
dev = false;
|
|
13
|
-
crypto = new runtime_1.RuntimeCrypto();
|
|
14
|
-
constructor(config) {
|
|
15
|
-
try {
|
|
16
|
-
if (config && config.dev == true)
|
|
17
|
-
this.dev = true;
|
|
18
|
-
if (config && config.runtime) {
|
|
19
|
-
if (!name_config_1.RUNTIME.includes(config.runtime)) {
|
|
20
|
-
throw new Error("Unsupported runtime");
|
|
21
|
-
}
|
|
22
|
-
this.runtime = config.runtime;
|
|
23
|
-
}
|
|
24
|
-
else {
|
|
25
|
-
this.runtime = (0, runtime_config_1.detectRuntime)();
|
|
26
|
-
}
|
|
27
|
-
(0, functions_lib_1.print)({ dev: this.dev }, 'Current Runtime: ', this.runtime);
|
|
28
|
-
}
|
|
29
|
-
catch (error) {
|
|
30
|
-
(0, functions_lib_1.print)({ dev: this.dev, color: 'red' }, error);
|
|
31
|
-
throw error;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
async create(header, payload) {
|
|
35
|
-
try {
|
|
36
|
-
if (!header.key) {
|
|
37
|
-
throw new Error('key is required to create token');
|
|
38
|
-
}
|
|
39
|
-
const key = header.key;
|
|
40
|
-
const exp = header.exp ? (0, timeformat_1.jatTimeFormatter)(header.exp) : (0, timeformat_1.jatTimeFormatter)('5MIN');
|
|
41
|
-
const algo = header.algo || (algo_config_1.DEFAULT_ALGORITHM[this.runtime][0].name);
|
|
42
|
-
return await this.crypto.createToken(this.runtime, algo, key, payload, exp);
|
|
43
|
-
}
|
|
44
|
-
catch (error) {
|
|
45
|
-
(0, functions_lib_1.print)({ dev: this.dev, color: 'red' }, error);
|
|
46
|
-
throw error;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
async verify(token, key) {
|
|
50
|
-
try {
|
|
51
|
-
if (!token) {
|
|
52
|
-
throw new Error('Token is required to verify token');
|
|
53
|
-
}
|
|
54
|
-
if (!key) {
|
|
55
|
-
throw new Error('key is required to verify token');
|
|
56
|
-
}
|
|
57
|
-
const unfilterPayload = await this.crypto.verifyToken(this.runtime, token, key);
|
|
58
|
-
if ((0, functions_lib_1.isExpired)(unfilterPayload.exp)) {
|
|
59
|
-
throw new Error('Token expired');
|
|
60
|
-
}
|
|
61
|
-
return unfilterPayload.payload;
|
|
62
|
-
}
|
|
63
|
-
catch (error) {
|
|
64
|
-
(0, functions_lib_1.print)({ dev: this.dev, color: 'red' }, error);
|
|
65
|
-
throw error;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
class PrivatePublicKeyGeneration {
|
|
70
|
-
crypto = new runtime_1.RuntimeCrypto();
|
|
71
|
-
async generateKeyPair(runtime, dev) {
|
|
72
|
-
let finalRuntime = (0, runtime_config_1.detectRuntime)();
|
|
73
|
-
const development = dev === true ? true : false;
|
|
74
|
-
try {
|
|
75
|
-
if (runtime) {
|
|
76
|
-
if (!name_config_1.RUNTIME.includes(runtime)) {
|
|
77
|
-
throw new Error("Unsupported runtime");
|
|
78
|
-
}
|
|
79
|
-
finalRuntime = runtime;
|
|
80
|
-
}
|
|
81
|
-
const { privateKey, publicKey } = await this.crypto.rsaKeyDrivation(finalRuntime, 'keyPair');
|
|
82
|
-
(0, functions_lib_1.print)({ dev: development, color: 'green' }, 'Current Runtime: ', finalRuntime);
|
|
83
|
-
(0, functions_lib_1.print)({ dev: development, color: 'green' }, { privateKey, publicKey });
|
|
84
|
-
return { privateKey, publicKey };
|
|
85
|
-
}
|
|
86
|
-
catch (error) {
|
|
87
|
-
(0, functions_lib_1.print)({ dev: development, color: 'red' }, error);
|
|
88
|
-
throw error;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
async generatePublicKey(privateKeyPem, runtime, dev) {
|
|
92
|
-
let finalRuntime = (0, runtime_config_1.detectRuntime)();
|
|
93
|
-
const development = dev === true ? true : false;
|
|
94
|
-
try {
|
|
95
|
-
if (runtime) {
|
|
96
|
-
if (!name_config_1.RUNTIME.includes(runtime)) {
|
|
97
|
-
throw new Error("Unsupported runtime");
|
|
98
|
-
}
|
|
99
|
-
finalRuntime = runtime;
|
|
100
|
-
}
|
|
101
|
-
const publicKey = await this.crypto.rsaKeyDrivation(finalRuntime, 'publickey', privateKeyPem);
|
|
102
|
-
(0, functions_lib_1.print)({ dev: development, color: 'green' }, 'Current Runtime: ', finalRuntime);
|
|
103
|
-
(0, functions_lib_1.print)({ dev: development, color: 'green' }, publicKey);
|
|
104
|
-
return publicKey;
|
|
105
|
-
}
|
|
106
|
-
catch (error) {
|
|
107
|
-
(0, functions_lib_1.print)({ dev: development, color: 'red' }, error);
|
|
108
|
-
throw error;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
const p2kgObject = new PrivatePublicKeyGeneration();
|
|
113
|
-
const generateKeyPair = (options) => {
|
|
114
|
-
const { runtime, dev } = options || {};
|
|
115
|
-
return p2kgObject.generateKeyPair(runtime, dev);
|
|
116
|
-
};
|
|
117
|
-
const generatePublicKey = (privateKeyPem, options) => {
|
|
118
|
-
const { runtime, dev } = options || {};
|
|
119
|
-
return p2kgObject.generatePublicKey(privateKeyPem, runtime, dev);
|
|
120
|
-
};
|
|
121
|
-
const JAT = (config) => new JATClass(config);
|
|
122
|
-
exports.JAT = JAT;
|
|
123
|
-
const getSupportedAlgorithm = () => algo_config_1.SUPPORTED_ALGORITHM;
|
|
124
|
-
exports.getSupportedAlgorithm = getSupportedAlgorithm;
|
|
125
|
-
exports.P2KG = {
|
|
126
|
-
generateKeyPair: generateKeyPair,
|
|
127
|
-
generatePublicKey: generatePublicKey
|
|
128
|
-
};
|
|
129
|
-
const jsonauthtoken = {
|
|
130
|
-
JAT: exports.JAT,
|
|
131
|
-
P2KG: exports.P2KG
|
|
132
|
-
};
|
|
133
|
-
exports.default = jsonauthtoken;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const decoading = (str) => {
|
|
4
|
-
const base64 = str.replace(/-/g, '+').replace(/_/g, '/');
|
|
5
|
-
const padding = '='.repeat((4 - base64.length % 4) % 4);
|
|
6
|
-
const decoded = Buffer.from(base64 + padding, 'base64').toString('utf8');
|
|
7
|
-
return JSON.parse(decoded);
|
|
8
|
-
};
|
|
9
|
-
exports.default = decoading;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
function encoading(data) {
|
|
4
|
-
const json = JSON.stringify(data);
|
|
5
|
-
const base64 = Buffer.from(json).toString('base64');
|
|
6
|
-
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
7
|
-
}
|
|
8
|
-
exports.default = encoading;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
declare function tokenFormatCreate<R extends Runtime = Runtime>(meta: TokenMetaData<R>, encrypted: string): string;
|
|
2
|
-
declare function tokenFormatVerify<R extends Runtime = Runtime>(token: string): {
|
|
3
|
-
meta: TokenMetaData<R>;
|
|
4
|
-
encrypted: string;
|
|
5
|
-
};
|
|
6
|
-
declare function isExpired(timeStamp: number): boolean;
|
|
7
|
-
type Color = 'red' | 'green' | 'yellow' | 'blue' | 'magenta' | 'cyan' | 'white';
|
|
8
|
-
declare function print({ dev, color }: {
|
|
9
|
-
dev?: boolean;
|
|
10
|
-
color?: Color;
|
|
11
|
-
}, ...args: unknown[]): void;
|
|
12
|
-
export { isExpired, tokenFormatCreate, tokenFormatVerify, print };
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.isExpired = isExpired;
|
|
7
|
-
exports.tokenFormatCreate = tokenFormatCreate;
|
|
8
|
-
exports.tokenFormatVerify = tokenFormatVerify;
|
|
9
|
-
exports.print = print;
|
|
10
|
-
const algo_config_1 = require("../config/algo.config");
|
|
11
|
-
const encoading_lib_1 = __importDefault(require("./encoading.lib"));
|
|
12
|
-
const decoading_lib_1 = __importDefault(require("./decoading.lib"));
|
|
13
|
-
const name_config_1 = require("../config/name.config");
|
|
14
|
-
function tokenFormatCreate(meta, encrypted) {
|
|
15
|
-
return `${(0, encoading_lib_1.default)(meta)}:${encrypted}`;
|
|
16
|
-
}
|
|
17
|
-
function tokenFormatVerify(token) {
|
|
18
|
-
const index = token.indexOf(":");
|
|
19
|
-
if (index === -1) {
|
|
20
|
-
throw new Error("Invalid token format");
|
|
21
|
-
}
|
|
22
|
-
const metaPart = token.substring(0, index);
|
|
23
|
-
const encryptedPart = token.substring(index + 1);
|
|
24
|
-
const meta = (0, decoading_lib_1.default)(metaPart);
|
|
25
|
-
if (!meta) {
|
|
26
|
-
throw new Error("Invalid token format");
|
|
27
|
-
}
|
|
28
|
-
const algorithm = algo_config_1.SUPPORTED_ALGORITHM[meta.runtime]?.find(e => e.name === meta.algo);
|
|
29
|
-
if (!algorithm) {
|
|
30
|
-
throw new Error("Invalid token format");
|
|
31
|
-
}
|
|
32
|
-
if (algorithm.type !== meta.type) {
|
|
33
|
-
throw new Error("Invalid token format");
|
|
34
|
-
}
|
|
35
|
-
if (meta.type === 'asymmetric') {
|
|
36
|
-
if (!meta.encryptedKey) {
|
|
37
|
-
throw new Error("Invalid token format");
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
if (!meta.iv) {
|
|
41
|
-
throw new Error("Invalid token format");
|
|
42
|
-
}
|
|
43
|
-
if (!name_config_1.RUNTIME.includes(meta.runtime)) {
|
|
44
|
-
throw new Error("Invalid token format");
|
|
45
|
-
}
|
|
46
|
-
if (meta.runtime === 'node') {
|
|
47
|
-
if (!meta.tag) {
|
|
48
|
-
throw new Error("Invalid token format");
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
if (!meta.v) {
|
|
52
|
-
throw new Error("Invalid token format");
|
|
53
|
-
}
|
|
54
|
-
return { meta, encrypted: encryptedPart };
|
|
55
|
-
}
|
|
56
|
-
function isExpired(timeStamp) {
|
|
57
|
-
let currentTime = Math.floor(Date.now() / 1000);
|
|
58
|
-
if (timeStamp < currentTime) {
|
|
59
|
-
return true;
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
function print({ dev = false, color = 'green' }, ...args) {
|
|
66
|
-
if (!dev)
|
|
67
|
-
return;
|
|
68
|
-
const colors = {
|
|
69
|
-
red: '\x1b[31m',
|
|
70
|
-
green: '\x1b[32m',
|
|
71
|
-
yellow: '\x1b[33m',
|
|
72
|
-
blue: '\x1b[34m',
|
|
73
|
-
magenta: '\x1b[35m',
|
|
74
|
-
cyan: '\x1b[36m',
|
|
75
|
-
white: '\x1b[37m',
|
|
76
|
-
reset: '\x1b[0m',
|
|
77
|
-
};
|
|
78
|
-
if (typeof window !== 'undefined') {
|
|
79
|
-
console.log(`%c[jsonauthtoken]`, `color:${color}`, ...args);
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
const colorCode = colors[color] ?? colors.green;
|
|
83
|
-
console.log(`${colorCode}[jsonauthtoken]`, ...args, '\x1b[0m');
|
|
84
|
-
}
|
|
85
|
-
}
|
package/dist/lib/timeformat.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function jatTimeFormatter(input: JATTime): number;
|
package/dist/lib/timeformat.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.jatTimeFormatter = jatTimeFormatter;
|
|
4
|
-
function jatTimeFormatter(input) {
|
|
5
|
-
if (typeof input === 'number') {
|
|
6
|
-
if (input <= 0) {
|
|
7
|
-
throw new Error('Expiration time must be greater than zero.');
|
|
8
|
-
}
|
|
9
|
-
return Math.floor(Date.now() / 1000) + input;
|
|
10
|
-
}
|
|
11
|
-
const regex = /^(\d+)(S|MIN|H|D|M|Y)$/i;
|
|
12
|
-
const match = input.match(regex);
|
|
13
|
-
if (!match) {
|
|
14
|
-
throw new Error('Invalid format. Use number or formats like 1S, 1MIN, 1H, 1D, 1M, or 1Y.');
|
|
15
|
-
}
|
|
16
|
-
const amount = parseInt(match[1], 10);
|
|
17
|
-
const unit = match[2].toUpperCase();
|
|
18
|
-
let seconds;
|
|
19
|
-
switch (unit) {
|
|
20
|
-
case 'S':
|
|
21
|
-
seconds = amount;
|
|
22
|
-
break;
|
|
23
|
-
case 'MIN':
|
|
24
|
-
seconds = amount * 60;
|
|
25
|
-
break;
|
|
26
|
-
case 'H':
|
|
27
|
-
seconds = amount * 60 * 60;
|
|
28
|
-
break;
|
|
29
|
-
case 'D':
|
|
30
|
-
seconds = amount * 24 * 60 * 60;
|
|
31
|
-
break;
|
|
32
|
-
case 'M':
|
|
33
|
-
seconds = amount * 30 * 24 * 60 * 60;
|
|
34
|
-
break;
|
|
35
|
-
case 'Y':
|
|
36
|
-
seconds = amount * 365 * 24 * 60 * 60;
|
|
37
|
-
break;
|
|
38
|
-
default:
|
|
39
|
-
throw new Error(`Unsupported time unit: ${unit}`);
|
|
40
|
-
}
|
|
41
|
-
return Math.floor(Date.now() / 1000) + seconds;
|
|
42
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
export declare class NodeCrypto {
|
|
2
|
-
private crypto;
|
|
3
|
-
private __init;
|
|
4
|
-
private _encrypt;
|
|
5
|
-
private _decrypt;
|
|
6
|
-
private _rsaPublicKeyGeneration;
|
|
7
|
-
private _rsaPrivatePublicKeyGeneration;
|
|
8
|
-
encrypt(algo: 'aes-256-gcm', key: string, payload: any, exp: number): Promise<string>;
|
|
9
|
-
decrypt<T>(algo: 'aes-256-gcm', key: string, encryptedData: {
|
|
10
|
-
iv: string;
|
|
11
|
-
encrypted: string;
|
|
12
|
-
tag: string;
|
|
13
|
-
}): Promise<{
|
|
14
|
-
payload: T;
|
|
15
|
-
exp: number;
|
|
16
|
-
}>;
|
|
17
|
-
encryptRSA(payload: any, publicKey: string, exp: number): Promise<string>;
|
|
18
|
-
decryptRSA<T>(privateKey: string, encryptedKey: string, encryptedData: {
|
|
19
|
-
iv: string;
|
|
20
|
-
encrypted: string;
|
|
21
|
-
tag: string;
|
|
22
|
-
}): Promise<{
|
|
23
|
-
payload: T;
|
|
24
|
-
exp: number;
|
|
25
|
-
}>;
|
|
26
|
-
rsaPrivatePublicKeyGeneration(): Promise<{
|
|
27
|
-
privateKey: string;
|
|
28
|
-
publicKey: string;
|
|
29
|
-
}>;
|
|
30
|
-
rsaPublicKeyGeneration(privateKeyPem: string): Promise<string | Buffer<ArrayBufferLike>>;
|
|
31
|
-
}
|