@naturalcycles/nodejs-lib 15.10.0 → 15.11.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.
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { ErrorData } from '@naturalcycles/js-lib/error';
|
|
2
2
|
import type { AnyObject, JWTString } from '@naturalcycles/js-lib/types';
|
|
3
|
-
import type { AnySchema } from 'joi';
|
|
4
3
|
import type { Algorithm, JwtHeader, SignOptions, VerifyOptions } from 'jsonwebtoken';
|
|
5
4
|
import jsonwebtoken from 'jsonwebtoken';
|
|
5
|
+
import type { AjvSchema } from '../validation/ajv/ajvSchema.js';
|
|
6
6
|
export { jsonwebtoken };
|
|
7
7
|
export type { Algorithm, JwtHeader, SignOptions, VerifyOptions };
|
|
8
8
|
export interface JWTServiceCfg {
|
|
@@ -50,9 +50,9 @@ export interface JWTServiceCfg {
|
|
|
50
50
|
export declare class JWTService {
|
|
51
51
|
cfg: JWTServiceCfg;
|
|
52
52
|
constructor(cfg: JWTServiceCfg);
|
|
53
|
-
sign<T extends AnyObject>(payload: T, schema?:
|
|
54
|
-
verify<T extends AnyObject>(token: JWTString, schema?:
|
|
55
|
-
decode<T extends AnyObject>(token: JWTString, schema?:
|
|
53
|
+
sign<T extends AnyObject>(payload: T, schema?: AjvSchema<T>, opt?: SignOptions): JWTString;
|
|
54
|
+
verify<T extends AnyObject>(token: JWTString, schema?: AjvSchema<T>, opt?: VerifyOptions, publicKey?: string): T;
|
|
55
|
+
decode<T extends AnyObject>(token: JWTString, schema?: AjvSchema<T>): {
|
|
56
56
|
header: JwtHeader;
|
|
57
57
|
payload: T;
|
|
58
58
|
signature: string;
|
package/dist/jwt/jwt.service.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { _assert } from '@naturalcycles/js-lib/error/assert.js';
|
|
2
2
|
import { _errorDataAppend } from '@naturalcycles/js-lib/error/error.util.js';
|
|
3
3
|
import jsonwebtoken from 'jsonwebtoken';
|
|
4
|
-
import { anyObjectSchema } from '../validation/joi/joi.shared.schemas.js';
|
|
5
|
-
import { validate } from '../validation/joi/joi.validation.util.js';
|
|
6
4
|
export { jsonwebtoken };
|
|
7
5
|
// todo: define JWTError and list possible options
|
|
8
6
|
// jwt expired (TokenExpiredError)
|
|
@@ -26,9 +24,9 @@ export class JWTService {
|
|
|
26
24
|
}
|
|
27
25
|
sign(payload, schema, opt = {}) {
|
|
28
26
|
_assert(this.cfg.privateKey, 'JWTService: privateKey is required to be able to verify, but not provided');
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
27
|
+
schema?.validate(payload, {
|
|
28
|
+
mutateInput: true,
|
|
29
|
+
});
|
|
32
30
|
return jsonwebtoken.sign(payload, this.cfg.privateKey, {
|
|
33
31
|
algorithm: this.cfg.algorithm,
|
|
34
32
|
noTimestamp: true,
|
|
@@ -44,9 +42,9 @@ export class JWTService {
|
|
|
44
42
|
...this.cfg.verifyOptions,
|
|
45
43
|
...opt,
|
|
46
44
|
});
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
45
|
+
schema?.validate(data, {
|
|
46
|
+
mutateInput: true,
|
|
47
|
+
});
|
|
50
48
|
return data;
|
|
51
49
|
}
|
|
52
50
|
catch (err) {
|
|
@@ -65,7 +63,9 @@ export class JWTService {
|
|
|
65
63
|
_assert(data?.payload, 'invalid token, decoded value is empty', {
|
|
66
64
|
...this.cfg.errorData,
|
|
67
65
|
});
|
|
68
|
-
validate(data.payload,
|
|
66
|
+
schema?.validate(data.payload, {
|
|
67
|
+
mutateInput: true,
|
|
68
|
+
});
|
|
69
69
|
return data;
|
|
70
70
|
}
|
|
71
71
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { BaseDBEntity, IANATimezone, IsoDateTime, NumberEnum, StringEnum, UnixTimestamp, UnixTimestampMillis } from '@naturalcycles/js-lib/types';
|
|
1
|
+
import type { BaseDBEntity, IANATimezone, IsoDate, IsoDateTime, NumberEnum, StringEnum, UnixTimestamp, UnixTimestampMillis } from '@naturalcycles/js-lib/types';
|
|
2
2
|
import type { AlternativesSchema, AnySchema, ArraySchema, ObjectSchema } from 'joi';
|
|
3
3
|
import type { NumberSchema } from './number.extensions.js';
|
|
4
4
|
import type { StringSchema } from './string.extensions.js';
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/nodejs-lib",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "15.
|
|
4
|
+
"version": "15.11.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@naturalcycles/js-lib": "^15",
|
|
7
7
|
"@types/js-yaml": "^4",
|
|
@@ -80,6 +80,7 @@
|
|
|
80
80
|
"license": "MIT",
|
|
81
81
|
"envByBranch": {
|
|
82
82
|
"master": "master",
|
|
83
|
+
"main": "master",
|
|
83
84
|
"*": "branch"
|
|
84
85
|
},
|
|
85
86
|
"scripts": {
|
package/src/jwt/jwt.service.ts
CHANGED
|
@@ -2,11 +2,9 @@ import type { ErrorData } from '@naturalcycles/js-lib/error'
|
|
|
2
2
|
import { _assert } from '@naturalcycles/js-lib/error/assert.js'
|
|
3
3
|
import { _errorDataAppend } from '@naturalcycles/js-lib/error/error.util.js'
|
|
4
4
|
import type { AnyObject, JWTString } from '@naturalcycles/js-lib/types'
|
|
5
|
-
import type { AnySchema } from 'joi'
|
|
6
5
|
import type { Algorithm, JwtHeader, SignOptions, VerifyOptions } from 'jsonwebtoken'
|
|
7
6
|
import jsonwebtoken from 'jsonwebtoken'
|
|
8
|
-
import {
|
|
9
|
-
import { validate } from '../validation/joi/joi.validation.util.js'
|
|
7
|
+
import type { AjvSchema } from '../validation/ajv/ajvSchema.js'
|
|
10
8
|
export { jsonwebtoken }
|
|
11
9
|
export type { Algorithm, JwtHeader, SignOptions, VerifyOptions }
|
|
12
10
|
|
|
@@ -65,15 +63,15 @@ export interface JWTServiceCfg {
|
|
|
65
63
|
export class JWTService {
|
|
66
64
|
constructor(public cfg: JWTServiceCfg) {}
|
|
67
65
|
|
|
68
|
-
sign<T extends AnyObject>(payload: T, schema?:
|
|
66
|
+
sign<T extends AnyObject>(payload: T, schema?: AjvSchema<T>, opt: SignOptions = {}): JWTString {
|
|
69
67
|
_assert(
|
|
70
68
|
this.cfg.privateKey,
|
|
71
69
|
'JWTService: privateKey is required to be able to verify, but not provided',
|
|
72
70
|
)
|
|
73
71
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
72
|
+
schema?.validate(payload, {
|
|
73
|
+
mutateInput: true,
|
|
74
|
+
})
|
|
77
75
|
|
|
78
76
|
return jsonwebtoken.sign(payload, this.cfg.privateKey, {
|
|
79
77
|
algorithm: this.cfg.algorithm,
|
|
@@ -85,7 +83,7 @@ export class JWTService {
|
|
|
85
83
|
|
|
86
84
|
verify<T extends AnyObject>(
|
|
87
85
|
token: JWTString,
|
|
88
|
-
schema?:
|
|
86
|
+
schema?: AjvSchema<T>,
|
|
89
87
|
opt: VerifyOptions = {},
|
|
90
88
|
publicKey?: string, // allows to override public key
|
|
91
89
|
): T {
|
|
@@ -101,9 +99,9 @@ export class JWTService {
|
|
|
101
99
|
...opt,
|
|
102
100
|
}) as T
|
|
103
101
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
102
|
+
schema?.validate(data, {
|
|
103
|
+
mutateInput: true,
|
|
104
|
+
})
|
|
107
105
|
|
|
108
106
|
return data
|
|
109
107
|
} catch (err) {
|
|
@@ -118,7 +116,7 @@ export class JWTService {
|
|
|
118
116
|
|
|
119
117
|
decode<T extends AnyObject>(
|
|
120
118
|
token: JWTString,
|
|
121
|
-
schema?:
|
|
119
|
+
schema?: AjvSchema<T>,
|
|
122
120
|
): {
|
|
123
121
|
header: JwtHeader
|
|
124
122
|
payload: T
|
|
@@ -136,7 +134,9 @@ export class JWTService {
|
|
|
136
134
|
...this.cfg.errorData,
|
|
137
135
|
})
|
|
138
136
|
|
|
139
|
-
validate(data.payload,
|
|
137
|
+
schema?.validate(data.payload, {
|
|
138
|
+
mutateInput: true,
|
|
139
|
+
})
|
|
140
140
|
|
|
141
141
|
return data
|
|
142
142
|
}
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
import type {
|
|
8
8
|
BaseDBEntity,
|
|
9
9
|
IANATimezone,
|
|
10
|
+
IsoDate,
|
|
10
11
|
IsoDateTime,
|
|
11
12
|
NumberEnum,
|
|
12
13
|
StringEnum,
|
|
@@ -26,7 +27,7 @@ export const numberSchema = Joi.number()
|
|
|
26
27
|
export const numberSchemaTyped = <T>(): NumberSchema<T> => Joi.number<T>()
|
|
27
28
|
export const integerSchema = Joi.number().integer()
|
|
28
29
|
export const percentageSchema = Joi.number().integer().min(0).max(100)
|
|
29
|
-
export const dateStringSchema = stringSchema.dateString()
|
|
30
|
+
export const dateStringSchema: StringSchema<IsoDate> = stringSchema.dateString()
|
|
30
31
|
export const binarySchema = Joi.binary()
|
|
31
32
|
export const dateObjectSchema = Joi.object().instance(Date)
|
|
32
33
|
|