@appweaver/data 2.4.1 → 2.6.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/README.md +4 -2
- package/config.d.ts +3 -0
- package/config.js +3 -0
- package/database/database-config.d.ts +9 -0
- package/database/database-config.js +56 -1
- package/database/database-error.d.ts +8 -0
- package/database/database-error.js +18 -0
- package/database/prisma-database.js +18 -1
- package/database/setup-database.d.ts +23 -0
- package/database/setup-database.js +59 -0
- package/database/sqlserver-extension.d.ts +18 -0
- package/database/sqlserver-extension.js +183 -0
- package/database/telemetry-extension.d.ts +10 -0
- package/database/telemetry-extension.js +52 -0
- package/export/export-service.js +4 -0
- package/factory/create-model.d.ts +1 -1
- package/factory/create-model.js +135 -5
- package/factory/create-service.js +8 -0
- package/index.d.ts +3 -0
- package/index.js +6 -0
- package/internal.d.ts +2 -0
- package/internal.js +3 -1
- package/module.js +23 -3
- package/package.json +3 -3
- package/prisma/client/browser.d.ts +10 -0
- package/prisma/client/client.d.ts +10 -0
- package/prisma/client/commonInputTypes.d.ts +81 -0
- package/prisma/client/enums.d.ts +6 -1
- package/prisma/client/enums.js +6 -0
- package/prisma/client/internal/class.d.ts +22 -0
- package/prisma/client/internal/class.js +4 -4
- package/prisma/client/internal/prismaNamespace.d.ts +175 -1
- package/prisma/client/internal/prismaNamespace.js +19 -1
- package/prisma/client/internal/prismaNamespaceBrowser.d.ts +20 -0
- package/prisma/client/internal/prismaNamespaceBrowser.js +19 -1
- package/prisma/client/models/TwoFactorMethod.d.ts +1140 -0
- package/prisma/client/models/TwoFactorMethod.js +2 -0
- package/prisma/client/models/TwoFactorRecoveryCode.d.ts +1005 -0
- package/prisma/client/models/TwoFactorRecoveryCode.js +2 -0
- package/prisma/client/models.d.ts +2 -0
- package/resource/resource-service.d.ts +32 -7
- package/resource/resource-service.js +248 -35
- package/resource/schemas/resource-filter-schema.d.ts +8 -0
- package/resource/schemas/resource-filter-schema.js +40 -1
- package/resource/schemas/resource-sort-schema.js +10 -0
- package/resource/utils/cursor-util.d.ts +13 -0
- package/resource/utils/cursor-util.js +21 -0
- package/resource/utils/filter-util.d.ts +9 -0
- package/resource/utils/filter-util.js +17 -0
- package/resource/utils/prune-util.js +3 -1
- package/resource/utils/sort-util.d.ts +14 -0
- package/resource/utils/sort-util.js +1 -0
- package/resource/utils/where-util.d.ts +20 -0
- package/resource/utils/where-util.js +210 -0
- package/resources.d.ts +4 -0
- package/resources.js +17 -9
- package/security/auth-config.d.ts +3 -0
- package/security/auth-config.js +6 -1
- package/security/create-auth-resources.d.ts +3 -3
- package/security/create-auth-resources.js +44 -7
- package/security/helper.d.ts +6 -0
- package/security/helper.js +11 -0
- package/security/resources/two-factor-method/model.d.ts +2 -0
- package/security/resources/two-factor-method/model.js +64 -0
- package/security/resources/two-factor-method/service.d.ts +2 -0
- package/security/resources/two-factor-method/service.js +7 -0
- package/security/resources/two-factor-recovery-code/model.d.ts +2 -0
- package/security/resources/two-factor-recovery-code/model.js +49 -0
- package/security/resources/two-factor-recovery-code/service.d.ts +2 -0
- package/security/resources/two-factor-recovery-code/service.js +7 -0
- package/security/store/database-security-store.d.ts +2 -2
- package/security/store/database-security-store.js +11 -1
- package/seeder/create-seeder.d.ts +3 -0
- package/seeder/create-seeder.js +6 -2
- package/seeder/seeder.d.ts +4 -2
- package/seeder/seeder.js +56 -23
- package/spatial/geo-schema.d.ts +22 -0
- package/spatial/geo-schema.js +60 -0
- package/spatial/spatial-config.d.ts +21 -0
- package/spatial/spatial-config.js +52 -0
- package/spatial/spatial-engine.d.ts +78 -0
- package/spatial/spatial-engine.js +232 -0
- package/spatial/spatial-extension.d.ts +10 -0
- package/spatial/spatial-extension.js +100 -0
- package/spatial/spatial-service.d.ts +54 -0
- package/spatial/spatial-service.js +213 -0
- package/spatial/utils/box-util.d.ts +31 -0
- package/spatial/utils/box-util.js +62 -0
- package/timeseries/database-series-config.d.ts +5 -0
- package/timeseries/database-series-config.js +15 -0
- package/timeseries/database-time-series.d.ts +36 -0
- package/timeseries/database-time-series.js +147 -0
- package/timeseries/index.d.ts +2 -0
- package/timeseries/index.js +18 -0
- package/timeseries/timescale-config.d.ts +6 -0
- package/timeseries/timescale-config.js +19 -0
- package/timeseries/timescale-time-series.d.ts +21 -0
- package/timeseries/timescale-time-series.js +158 -0
- package/timeseries/utils/series-sql-util.d.ts +90 -0
- package/timeseries/utils/series-sql-util.js +313 -0
- package/types/generated.d.ts +92 -0
- package/utils/schema-util.d.ts +8 -0
- package/utils/schema-util.js +22 -0
- package/utils/write-data-util.d.ts +22 -0
- package/utils/write-data-util.js +144 -0
|
@@ -1,10 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FINGERPRINT = void 0;
|
|
4
|
+
exports.fingerprintAs = fingerprintAs;
|
|
3
5
|
exports.queryFingerprint = queryFingerprint;
|
|
4
6
|
exports.encodeCursor = encodeCursor;
|
|
5
7
|
exports.pageCursors = pageCursors;
|
|
6
8
|
exports.decodeCursor = decodeCursor;
|
|
7
9
|
const common_1 = require("@appweaver/common");
|
|
10
|
+
/**
|
|
11
|
+
* The key of the value a query condition is fingerprinted as instead of itself, i.e. the spatial filter a list of
|
|
12
|
+
* matching ids was resolved from, so a cursor stays valid while the records it matches change.
|
|
13
|
+
*/
|
|
14
|
+
exports.FINGERPRINT = Symbol('appweaver.fingerprint');
|
|
15
|
+
/**
|
|
16
|
+
* Fingerprints a value as another one, see {@link FINGERPRINT}.
|
|
17
|
+
*
|
|
18
|
+
* @param {T} value - The value, an object or a list.
|
|
19
|
+
* @param {unknown} as - The value fingerprinted in its place.
|
|
20
|
+
* @return {T} The value.
|
|
21
|
+
*/
|
|
22
|
+
function fingerprintAs(value, as) {
|
|
23
|
+
Object.defineProperty(value, exports.FINGERPRINT, { value: as, enumerable: false });
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
8
26
|
/**
|
|
9
27
|
* Builds the fingerprint identifying the query a cursor belongs to. A cursor
|
|
10
28
|
* only yields the intended records while the resource, the query, and the order
|
|
@@ -103,6 +121,9 @@ function decodeCursor(cursor, fingerprint) {
|
|
|
103
121
|
* @return {string} The serialized value.
|
|
104
122
|
*/
|
|
105
123
|
function stableStringify(value) {
|
|
124
|
+
if (value?.[exports.FINGERPRINT] !== undefined) {
|
|
125
|
+
return stableStringify(value[exports.FINGERPRINT]);
|
|
126
|
+
}
|
|
106
127
|
if ((0, common_1.isArray)(value)) {
|
|
107
128
|
return `[${value.map(stableStringify).join(',')}]`;
|
|
108
129
|
}
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The database conditions a filter field was resolved into ahead of the
|
|
3
|
+
* mapping, i.e. the ids matching the spatial operators of a geo field, added
|
|
4
|
+
* to the conditions of the level of the filter holding the field.
|
|
5
|
+
*/
|
|
6
|
+
export declare class ResolvedFilterConditions {
|
|
7
|
+
readonly conditions: any[];
|
|
8
|
+
constructor(conditions: any[]);
|
|
9
|
+
}
|
|
1
10
|
/**
|
|
2
11
|
* Maps a request filter to a Prisma `where` clause based on the schema of the
|
|
3
12
|
* given resource model. The logical operators (`_and`, `_or`, `_not`, `_nor`)
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ResolvedFilterConditions = void 0;
|
|
3
4
|
exports.mapQueryFilter = mapQueryFilter;
|
|
4
5
|
const common_1 = require("@appweaver/common");
|
|
5
6
|
const core_1 = require("@appweaver/core");
|
|
@@ -44,6 +45,18 @@ const likePattern = (pattern) => {
|
|
|
44
45
|
}
|
|
45
46
|
return { equals: value };
|
|
46
47
|
};
|
|
48
|
+
/**
|
|
49
|
+
* The database conditions a filter field was resolved into ahead of the
|
|
50
|
+
* mapping, i.e. the ids matching the spatial operators of a geo field, added
|
|
51
|
+
* to the conditions of the level of the filter holding the field.
|
|
52
|
+
*/
|
|
53
|
+
class ResolvedFilterConditions {
|
|
54
|
+
conditions;
|
|
55
|
+
constructor(conditions) {
|
|
56
|
+
this.conditions = conditions;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.ResolvedFilterConditions = ResolvedFilterConditions;
|
|
47
60
|
/**
|
|
48
61
|
* Field comparison operators mapped to their database query conditions.
|
|
49
62
|
* Operators combined in the same object are merged into a single condition.
|
|
@@ -100,6 +113,10 @@ function mapQueryFilter(filter, resourceName) {
|
|
|
100
113
|
const filesModel = resourceModel?.filesModel;
|
|
101
114
|
for (const key in filter) {
|
|
102
115
|
const value = filter[key];
|
|
116
|
+
if (value instanceof ResolvedFilterConditions) {
|
|
117
|
+
queryFilter['AND'] = [...(queryFilter['AND'] ?? []), ...value.conditions];
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
103
120
|
// Logical operators combine their conditions, given either as a filter
|
|
104
121
|
// object whose entries become separate conditions or as a list of
|
|
105
122
|
// filters, with the matching database connective
|
|
@@ -14,7 +14,9 @@ const authModelNames = [
|
|
|
14
14
|
'Permission',
|
|
15
15
|
'ApiKey',
|
|
16
16
|
'OneTimeToken',
|
|
17
|
-
'ConnectedAccount'
|
|
17
|
+
'ConnectedAccount',
|
|
18
|
+
'TwoFactorMethod',
|
|
19
|
+
'TwoFactorRecoveryCode'
|
|
18
20
|
];
|
|
19
21
|
/**
|
|
20
22
|
* Removes the models of the framework an application does not use, so its database gets none of their tables: the
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { ActionType, QuerySort } from '@appweaver/common';
|
|
2
|
+
/** A single sort entry, holding a dot notation field path and its direction. */
|
|
3
|
+
type SortEntry = [path: string, direction: 'asc' | 'desc'];
|
|
2
4
|
/**
|
|
3
5
|
* Maps a sort input to the ordered list of Prisma `orderBy` entries. The input is accepted either as a comma-separated
|
|
4
6
|
* field list, where a field prefixed with `-` is sorted in descending order, or as a (possibly nested) object of `asc`
|
|
@@ -29,3 +31,15 @@ export declare function mapSortValues(sort: QuerySort, resourceName: string, act
|
|
|
29
31
|
* @throws {ResourceError} `RESOURCE_INVALID_SORT` under the same conditions as {@link mapSortValues}.
|
|
30
32
|
*/
|
|
31
33
|
export declare function mapStableSortValues(sort: QuerySort, resourceName: string, action?: ActionType): any[];
|
|
34
|
+
/**
|
|
35
|
+
* Flattens a sort input into the list of its field paths and directions, keeping the order the fields were declared
|
|
36
|
+
* in. String inputs are split on commas, with a `-` or `+` prefix selecting the direction, and object inputs are
|
|
37
|
+
* walked recursively, joining the nested keys with a dot.
|
|
38
|
+
*
|
|
39
|
+
* @param {QuerySort} sort - The sort input to flatten.
|
|
40
|
+
* @param {string} [parentPath] - The dot notation path of the object the walk descended from.
|
|
41
|
+
* @return {SortEntry[]} The flattened sort entries, each holding a dot notation field path and its direction.
|
|
42
|
+
* @throws {ResourceError} `RESOURCE_INVALID_SORT` if a sort value is neither a direction nor a nested object of directions.
|
|
43
|
+
*/
|
|
44
|
+
export declare function sortEntries(sort: QuerySort, parentPath?: string): SortEntry[];
|
|
45
|
+
export {};
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.mapSortValues = mapSortValues;
|
|
4
4
|
exports.mapStableSortValues = mapStableSortValues;
|
|
5
|
+
exports.sortEntries = sortEntries;
|
|
5
6
|
const common_1 = require("@appweaver/common");
|
|
6
7
|
const core_1 = require("@appweaver/core");
|
|
7
8
|
const relation_util_1 = require("./relation-util");
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Matches a record against a `where` filter of Prisma without the database, i.e. the read restrictions of a policy
|
|
3
|
+
* against the resource of an event. The filter may combine `AND`, `OR` and `NOT` with the scalar conditions (`equals`,
|
|
4
|
+
* `not`, `in`, `notIn`, `lt`, `lte`, `gt`, `gte`, `contains`, `startsWith`, `endsWith`, with `mode: 'insensitive'`).
|
|
5
|
+
*
|
|
6
|
+
* @param {Object} record - The record to match, its dates as `Date` objects or ISO strings.
|
|
7
|
+
* @param {Object} where - The filter, in the native Prisma filter syntax.
|
|
8
|
+
* @return {boolean | undefined} Whether the record matches, or `undefined` for a filter it cannot evaluate, i.e. a
|
|
9
|
+
* relation filter or a field the record lacks, which only the database can match.
|
|
10
|
+
*/
|
|
11
|
+
export declare function matchesWhere(record: Record<string, any>, where: Record<string, any> | null | undefined): boolean | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* Maps a query filter of the resource routes, i.e. `{ status: 'open', total: { _gt: 100 } }`, to the `where` filter of
|
|
14
|
+
* Prisma {@link matchesWhere} matches, i.e. the filter of a realtime subscription.
|
|
15
|
+
*
|
|
16
|
+
* @param {Object} filter - The query filter.
|
|
17
|
+
* @param {string} modelName - The name of the model the filter is matched against.
|
|
18
|
+
* @return {Object} The `where` filter.
|
|
19
|
+
*/
|
|
20
|
+
export declare function queryFilterWhere(filter: Record<string, unknown>, modelName: string): Record<string, any>;
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.matchesWhere = matchesWhere;
|
|
4
|
+
exports.queryFilterWhere = queryFilterWhere;
|
|
5
|
+
const common_1 = require("@appweaver/common");
|
|
6
|
+
const filter_util_1 = require("./filter-util");
|
|
7
|
+
const OPERATORS = new Set([
|
|
8
|
+
'equals',
|
|
9
|
+
'not',
|
|
10
|
+
'in',
|
|
11
|
+
'notIn',
|
|
12
|
+
'lt',
|
|
13
|
+
'lte',
|
|
14
|
+
'gt',
|
|
15
|
+
'gte',
|
|
16
|
+
'contains',
|
|
17
|
+
'startsWith',
|
|
18
|
+
'endsWith',
|
|
19
|
+
'mode'
|
|
20
|
+
]);
|
|
21
|
+
/**
|
|
22
|
+
* Matches a record against a `where` filter of Prisma without the database, i.e. the read restrictions of a policy
|
|
23
|
+
* against the resource of an event. The filter may combine `AND`, `OR` and `NOT` with the scalar conditions (`equals`,
|
|
24
|
+
* `not`, `in`, `notIn`, `lt`, `lte`, `gt`, `gte`, `contains`, `startsWith`, `endsWith`, with `mode: 'insensitive'`).
|
|
25
|
+
*
|
|
26
|
+
* @param {Object} record - The record to match, its dates as `Date` objects or ISO strings.
|
|
27
|
+
* @param {Object} where - The filter, in the native Prisma filter syntax.
|
|
28
|
+
* @return {boolean | undefined} Whether the record matches, or `undefined` for a filter it cannot evaluate, i.e. a
|
|
29
|
+
* relation filter or a field the record lacks, which only the database can match.
|
|
30
|
+
*/
|
|
31
|
+
function matchesWhere(record, where) {
|
|
32
|
+
if (!where) {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
let result = true;
|
|
36
|
+
for (const [key, condition] of Object.entries(where)) {
|
|
37
|
+
if (condition === undefined) {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
let matched;
|
|
41
|
+
if (key === 'AND') {
|
|
42
|
+
matched = every(((0, common_1.isArray)(condition) ? condition : [condition]).map((c) => matchesWhere(record, c)));
|
|
43
|
+
}
|
|
44
|
+
else if (key === 'OR') {
|
|
45
|
+
matched = some(condition.map((c) => matchesWhere(record, c)));
|
|
46
|
+
}
|
|
47
|
+
else if (key === 'NOT') {
|
|
48
|
+
matched = negate(some(((0, common_1.isArray)(condition) ? condition : [condition]).map((c) => matchesWhere(record, c))));
|
|
49
|
+
}
|
|
50
|
+
else if (!(key in record)) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
matched = matchesField(record[key], condition);
|
|
55
|
+
}
|
|
56
|
+
result = every([result, matched]);
|
|
57
|
+
if (result === false) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Maps a query filter of the resource routes, i.e. `{ status: 'open', total: { _gt: 100 } }`, to the `where` filter of
|
|
65
|
+
* Prisma {@link matchesWhere} matches, i.e. the filter of a realtime subscription.
|
|
66
|
+
*
|
|
67
|
+
* @param {Object} filter - The query filter.
|
|
68
|
+
* @param {string} modelName - The name of the model the filter is matched against.
|
|
69
|
+
* @return {Object} The `where` filter.
|
|
70
|
+
*/
|
|
71
|
+
function queryFilterWhere(filter, modelName) {
|
|
72
|
+
return (0, filter_util_1.mapQueryFilter)(filter, modelName);
|
|
73
|
+
}
|
|
74
|
+
function matchesField(value, condition) {
|
|
75
|
+
if (!isConditionObject(condition)) {
|
|
76
|
+
return isScalar(value) ? equals(value, condition) : undefined;
|
|
77
|
+
}
|
|
78
|
+
// A nested filter of a relation or a JSON field, matched by the database
|
|
79
|
+
if (!Object.keys(condition).every((key) => OPERATORS.has(key))) {
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
if (!isScalar(value)) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
const insensitive = condition.mode === 'insensitive';
|
|
86
|
+
const results = [];
|
|
87
|
+
for (const [operator, operand] of Object.entries(condition)) {
|
|
88
|
+
if (operand === undefined || operator === 'mode') {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
results.push(compare(value, operator, operand, insensitive));
|
|
92
|
+
}
|
|
93
|
+
return every(results);
|
|
94
|
+
}
|
|
95
|
+
function compare(value, operator, operand, insensitive) {
|
|
96
|
+
switch (operator) {
|
|
97
|
+
case 'equals':
|
|
98
|
+
return equals(value, operand, insensitive);
|
|
99
|
+
case 'not':
|
|
100
|
+
return isConditionObject(operand)
|
|
101
|
+
? negate(matchesField(value, operand))
|
|
102
|
+
: !equals(value, operand, insensitive);
|
|
103
|
+
case 'in':
|
|
104
|
+
return (0, common_1.isArray)(operand)
|
|
105
|
+
? operand.some((o) => equals(value, o, insensitive))
|
|
106
|
+
: undefined;
|
|
107
|
+
case 'notIn':
|
|
108
|
+
return (0, common_1.isArray)(operand)
|
|
109
|
+
? !operand.some((o) => equals(value, o, insensitive))
|
|
110
|
+
: undefined;
|
|
111
|
+
case 'lt':
|
|
112
|
+
case 'lte':
|
|
113
|
+
case 'gt':
|
|
114
|
+
case 'gte':
|
|
115
|
+
return order(value, operator, operand);
|
|
116
|
+
default:
|
|
117
|
+
return matchesText(value, operator, operand, insensitive);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function matchesText(value, operator, operand, insensitive) {
|
|
121
|
+
if (typeof value !== 'string' || typeof operand !== 'string') {
|
|
122
|
+
return value === null ? false : undefined;
|
|
123
|
+
}
|
|
124
|
+
const text = insensitive ? value.toLowerCase() : value;
|
|
125
|
+
const search = insensitive ? operand.toLowerCase() : operand;
|
|
126
|
+
if (operator === 'contains') {
|
|
127
|
+
return text.includes(search);
|
|
128
|
+
}
|
|
129
|
+
return operator === 'startsWith'
|
|
130
|
+
? text.startsWith(search)
|
|
131
|
+
: text.endsWith(search);
|
|
132
|
+
}
|
|
133
|
+
function order(value, operator, operand) {
|
|
134
|
+
if (value === null || operand === null) {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
const left = comparable(value, operand);
|
|
138
|
+
const right = comparable(operand, value);
|
|
139
|
+
if (left === undefined || right === undefined) {
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
switch (operator) {
|
|
143
|
+
case 'lt':
|
|
144
|
+
return left < right;
|
|
145
|
+
case 'lte':
|
|
146
|
+
return left <= right;
|
|
147
|
+
case 'gt':
|
|
148
|
+
return left > right;
|
|
149
|
+
default:
|
|
150
|
+
return left >= right;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function equals(value, operand, insensitive = false) {
|
|
154
|
+
if (value === null || operand === null) {
|
|
155
|
+
return value === operand;
|
|
156
|
+
}
|
|
157
|
+
if (insensitive && typeof value === 'string' && typeof operand === 'string') {
|
|
158
|
+
return value.toLowerCase() === operand.toLowerCase();
|
|
159
|
+
}
|
|
160
|
+
const left = comparable(value, operand);
|
|
161
|
+
const right = comparable(operand, value);
|
|
162
|
+
return left !== undefined && left === right;
|
|
163
|
+
}
|
|
164
|
+
/** The value to compare, a date as its time when either side is a date, and a big integer as a number. */
|
|
165
|
+
function comparable(value, other) {
|
|
166
|
+
if (value instanceof Date) {
|
|
167
|
+
return value.getTime();
|
|
168
|
+
}
|
|
169
|
+
if (other instanceof Date && typeof value === 'string') {
|
|
170
|
+
const time = Date.parse(value);
|
|
171
|
+
return Number.isNaN(time) ? undefined : time;
|
|
172
|
+
}
|
|
173
|
+
if (typeof value === 'bigint') {
|
|
174
|
+
return Number(value);
|
|
175
|
+
}
|
|
176
|
+
if (typeof value === 'string' ||
|
|
177
|
+
typeof value === 'number' ||
|
|
178
|
+
typeof value === 'boolean') {
|
|
179
|
+
return value;
|
|
180
|
+
}
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
183
|
+
function isScalar(value) {
|
|
184
|
+
return (value === null ||
|
|
185
|
+
value instanceof Date ||
|
|
186
|
+
['string', 'number', 'boolean', 'bigint'].includes(typeof value));
|
|
187
|
+
}
|
|
188
|
+
function isConditionObject(value) {
|
|
189
|
+
return (typeof value === 'object' &&
|
|
190
|
+
value !== null &&
|
|
191
|
+
!(0, common_1.isArray)(value) &&
|
|
192
|
+
!(value instanceof Date));
|
|
193
|
+
}
|
|
194
|
+
/** `false` when any is false, `undefined` when any is unknown, `true` otherwise. */
|
|
195
|
+
function every(results) {
|
|
196
|
+
if (results.includes(false)) {
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
return results.includes(undefined) ? undefined : true;
|
|
200
|
+
}
|
|
201
|
+
/** `true` when any is true, `undefined` when any is unknown, `false` otherwise. */
|
|
202
|
+
function some(results) {
|
|
203
|
+
if (results.includes(true)) {
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
206
|
+
return results.includes(undefined) ? undefined : false;
|
|
207
|
+
}
|
|
208
|
+
function negate(result) {
|
|
209
|
+
return result === undefined ? undefined : !result;
|
|
210
|
+
}
|
package/resources.d.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
export { default as apiKey } from './security/resources/api-key/model';
|
|
2
2
|
export { default as connectedAccount } from './security/resources/connected-account/model';
|
|
3
3
|
export { default as oneTimeToken } from './security/resources/one-time-token/model';
|
|
4
|
+
export { default as twoFactorMethod } from './security/resources/two-factor-method/model';
|
|
5
|
+
export { default as twoFactorRecoveryCode } from './security/resources/two-factor-recovery-code/model';
|
|
4
6
|
export { default as role } from './security/resources/role/model';
|
|
5
7
|
export { default as permission } from './security/resources/permission/model';
|
|
6
8
|
export { default as seeder } from './seeder/resources/seeder/model';
|
|
7
9
|
export { default as file } from './storage/resources/file/model';
|
|
8
10
|
export { default as apiKeyService } from './security/resources/api-key/service';
|
|
9
11
|
export { default as connectedAccountService } from './security/resources/connected-account/service';
|
|
12
|
+
export { default as twoFactorMethodService } from './security/resources/two-factor-method/service';
|
|
13
|
+
export { default as twoFactorRecoveryCodeService } from './security/resources/two-factor-recovery-code/service';
|
|
10
14
|
export { default as apiKeyPolicy } from './security/resources/api-key/policy';
|
package/resources.js
CHANGED
|
@@ -3,24 +3,32 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.apiKeyPolicy = exports.connectedAccountService = exports.apiKeyService = exports.file = exports.seeder = exports.permission = exports.role = exports.oneTimeToken = exports.connectedAccount = exports.apiKey = void 0;
|
|
6
|
+
exports.apiKeyPolicy = exports.twoFactorRecoveryCodeService = exports.twoFactorMethodService = exports.connectedAccountService = exports.apiKeyService = exports.file = exports.seeder = exports.permission = exports.role = exports.twoFactorRecoveryCode = exports.twoFactorMethod = exports.oneTimeToken = exports.connectedAccount = exports.apiKey = void 0;
|
|
7
7
|
var model_1 = require("./security/resources/api-key/model");
|
|
8
8
|
Object.defineProperty(exports, "apiKey", { enumerable: true, get: function () { return __importDefault(model_1).default; } });
|
|
9
9
|
var model_2 = require("./security/resources/connected-account/model");
|
|
10
10
|
Object.defineProperty(exports, "connectedAccount", { enumerable: true, get: function () { return __importDefault(model_2).default; } });
|
|
11
11
|
var model_3 = require("./security/resources/one-time-token/model");
|
|
12
12
|
Object.defineProperty(exports, "oneTimeToken", { enumerable: true, get: function () { return __importDefault(model_3).default; } });
|
|
13
|
-
var model_4 = require("./security/resources/
|
|
14
|
-
Object.defineProperty(exports, "
|
|
15
|
-
var model_5 = require("./security/resources/
|
|
16
|
-
Object.defineProperty(exports, "
|
|
17
|
-
var model_6 = require("./
|
|
18
|
-
Object.defineProperty(exports, "
|
|
19
|
-
var model_7 = require("./
|
|
20
|
-
Object.defineProperty(exports, "
|
|
13
|
+
var model_4 = require("./security/resources/two-factor-method/model");
|
|
14
|
+
Object.defineProperty(exports, "twoFactorMethod", { enumerable: true, get: function () { return __importDefault(model_4).default; } });
|
|
15
|
+
var model_5 = require("./security/resources/two-factor-recovery-code/model");
|
|
16
|
+
Object.defineProperty(exports, "twoFactorRecoveryCode", { enumerable: true, get: function () { return __importDefault(model_5).default; } });
|
|
17
|
+
var model_6 = require("./security/resources/role/model");
|
|
18
|
+
Object.defineProperty(exports, "role", { enumerable: true, get: function () { return __importDefault(model_6).default; } });
|
|
19
|
+
var model_7 = require("./security/resources/permission/model");
|
|
20
|
+
Object.defineProperty(exports, "permission", { enumerable: true, get: function () { return __importDefault(model_7).default; } });
|
|
21
|
+
var model_8 = require("./seeder/resources/seeder/model");
|
|
22
|
+
Object.defineProperty(exports, "seeder", { enumerable: true, get: function () { return __importDefault(model_8).default; } });
|
|
23
|
+
var model_9 = require("./storage/resources/file/model");
|
|
24
|
+
Object.defineProperty(exports, "file", { enumerable: true, get: function () { return __importDefault(model_9).default; } });
|
|
21
25
|
var service_1 = require("./security/resources/api-key/service");
|
|
22
26
|
Object.defineProperty(exports, "apiKeyService", { enumerable: true, get: function () { return __importDefault(service_1).default; } });
|
|
23
27
|
var service_2 = require("./security/resources/connected-account/service");
|
|
24
28
|
Object.defineProperty(exports, "connectedAccountService", { enumerable: true, get: function () { return __importDefault(service_2).default; } });
|
|
29
|
+
var service_3 = require("./security/resources/two-factor-method/service");
|
|
30
|
+
Object.defineProperty(exports, "twoFactorMethodService", { enumerable: true, get: function () { return __importDefault(service_3).default; } });
|
|
31
|
+
var service_4 = require("./security/resources/two-factor-recovery-code/service");
|
|
32
|
+
Object.defineProperty(exports, "twoFactorRecoveryCodeService", { enumerable: true, get: function () { return __importDefault(service_4).default; } });
|
|
25
33
|
var policy_1 = require("./security/resources/api-key/policy");
|
|
26
34
|
Object.defineProperty(exports, "apiKeyPolicy", { enumerable: true, get: function () { return __importDefault(policy_1).default; } });
|
package/security/auth-config.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.connectedAccountConfig = exports.systemConfig = exports.apiKeyConfig = exports.passwordConfig = void 0;
|
|
3
|
+
exports.twoFactorModelConfig = exports.connectedAccountConfig = exports.systemConfig = exports.apiKeyConfig = exports.passwordConfig = void 0;
|
|
4
4
|
const typebox_1 = require("@sinclair/typebox");
|
|
5
5
|
const common_1 = require("@appweaver/common");
|
|
6
6
|
exports.passwordConfig = (0, common_1.defineConfig)('security.password', {
|
|
@@ -41,3 +41,8 @@ exports.connectedAccountConfig = (0, common_1.defineConfig)('security.oauth2', {
|
|
|
41
41
|
/** Keep the `ConnectedAccount` database table even when every OAuth2 provider is disabled. Default: `false`. */
|
|
42
42
|
CONNECTED_ACCOUNTS_KEEP_DATABASE_TABLE: typebox_1.Type.Boolean({ default: false })
|
|
43
43
|
});
|
|
44
|
+
exports.twoFactorModelConfig = (0, common_1.defineConfig)('security.account.2fa', {
|
|
45
|
+
/** Keep the `TwoFactorMethod` database table even when the two-factor authentication is disabled. Default:
|
|
46
|
+
* `false`. */
|
|
47
|
+
KEEP_DATABASE_TABLE: typebox_1.Type.Boolean({ default: false })
|
|
48
|
+
});
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { Ctor, IResourceService, ModelName, RegistryType, ResourceModel, ResourceModelConfig, ResourceServiceConfig } from '@appweaver/common';
|
|
2
2
|
import { CheckOAuth2UserFn, RegistrationDataFn, RegistrationFilesFn } from '../types/auth';
|
|
3
3
|
/** The fields an auth model adds to the configured ones. */
|
|
4
|
-
type AuthFieldName = 'email' | 'passwordHash' | 'verifiedEmail' | '
|
|
4
|
+
type AuthFieldName = 'email' | 'passwordHash' | 'verifiedEmail' | 'phoneNumber' | 'verifiedPhone' | 'enabled' | 'logoutAt' | 'password' | 'roles' | 'apiKeys' | 'twoFactorMethods' | 'twoFactorRecoveryCodes';
|
|
5
5
|
/**
|
|
6
|
-
* Creates the resource model of an authenticatable user, adding the email, password, verification, two-factor
|
|
7
|
-
* role fields to the configured ones.
|
|
6
|
+
* Creates the resource model of an authenticatable user, adding the email, password, verification, phone, two-factor
|
|
7
|
+
* method, and role fields to the configured ones.
|
|
8
8
|
*
|
|
9
9
|
* @param {ResourceModelConfig} modelConfig - The model configuration.
|
|
10
10
|
* @return {ResourceModel} The created auth resource model.
|
|
@@ -8,8 +8,8 @@ const helper_1 = require("./helper");
|
|
|
8
8
|
const create_model_1 = require("../factory/create-model");
|
|
9
9
|
const create_service_1 = require("../factory/create-service");
|
|
10
10
|
/**
|
|
11
|
-
* Creates the resource model of an authenticatable user, adding the email, password, verification, two-factor
|
|
12
|
-
* role fields to the configured ones.
|
|
11
|
+
* Creates the resource model of an authenticatable user, adding the email, password, verification, phone, two-factor
|
|
12
|
+
* method, and role fields to the configured ones.
|
|
13
13
|
*
|
|
14
14
|
* @param {ResourceModelConfig} modelConfig - The model configuration.
|
|
15
15
|
* @return {ResourceModel} The created auth resource model.
|
|
@@ -32,10 +32,16 @@ function createAuthModel(modelConfig) {
|
|
|
32
32
|
type: 'boolean',
|
|
33
33
|
default: false
|
|
34
34
|
},
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
// Set by the phone verification or the app code, never by the resource routes, in the E.164 format
|
|
36
|
+
phoneNumber: {
|
|
37
|
+
type: 'string',
|
|
38
|
+
required: false,
|
|
39
|
+
maxLength: 16,
|
|
40
|
+
pattern: '^\\+[1-9]\\d{6,14}$'
|
|
41
|
+
},
|
|
42
|
+
verifiedPhone: {
|
|
43
|
+
type: 'boolean',
|
|
44
|
+
default: false
|
|
39
45
|
},
|
|
40
46
|
enabled: {
|
|
41
47
|
type: 'boolean',
|
|
@@ -96,6 +102,32 @@ function createAuthModel(modelConfig) {
|
|
|
96
102
|
}
|
|
97
103
|
}
|
|
98
104
|
: {}),
|
|
105
|
+
...((0, helper_1.isTwoFactorEnabled)()
|
|
106
|
+
? {
|
|
107
|
+
twoFactorMethods: {
|
|
108
|
+
model: 'TwoFactorMethod',
|
|
109
|
+
type: 'oneToMany',
|
|
110
|
+
mappedBy: (0, common_1.uncapitalize)(config.name),
|
|
111
|
+
input: {
|
|
112
|
+
type: 'none'
|
|
113
|
+
},
|
|
114
|
+
output: {
|
|
115
|
+
type: 'none'
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
twoFactorRecoveryCodes: {
|
|
119
|
+
model: 'TwoFactorRecoveryCode',
|
|
120
|
+
type: 'oneToMany',
|
|
121
|
+
mappedBy: (0, common_1.uncapitalize)(config.name),
|
|
122
|
+
input: {
|
|
123
|
+
type: 'none'
|
|
124
|
+
},
|
|
125
|
+
output: {
|
|
126
|
+
type: 'none'
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
: {}),
|
|
99
131
|
...((0, helper_1.isOAuth2Enabled)()
|
|
100
132
|
? {
|
|
101
133
|
connectedAccounts: {
|
|
@@ -112,7 +144,12 @@ function createAuthModel(modelConfig) {
|
|
|
112
144
|
}
|
|
113
145
|
: {})
|
|
114
146
|
};
|
|
115
|
-
const authModelInputOmit = [
|
|
147
|
+
const authModelInputOmit = [
|
|
148
|
+
'verifiedEmail',
|
|
149
|
+
'phoneNumber',
|
|
150
|
+
'verifiedPhone',
|
|
151
|
+
'logoutAt'
|
|
152
|
+
];
|
|
116
153
|
config.scalars = { ...config.scalars, ...authModelScalars };
|
|
117
154
|
config.virtual = { ...config.virtual, ...authModelVirtual };
|
|
118
155
|
config.relations = { ...config.relations, ...authModelRelations };
|
package/security/helper.d.ts
CHANGED
|
@@ -68,6 +68,12 @@ export declare function validatePasswordComplexity(password: string): Validation
|
|
|
68
68
|
* @return {boolean} True when any OAuth2 provider is enabled.
|
|
69
69
|
*/
|
|
70
70
|
export declare function isOAuth2Enabled(): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Checks if the two-factor authentication is enabled, so the auth model has the `TwoFactorMethod` relation.
|
|
73
|
+
*
|
|
74
|
+
* @return {boolean} True if 2FA is enabled or its database table is kept.
|
|
75
|
+
*/
|
|
76
|
+
export declare function isTwoFactorEnabled(): boolean;
|
|
71
77
|
/**
|
|
72
78
|
* Checks if the given user has a specific role.
|
|
73
79
|
*
|
package/security/helper.js
CHANGED
|
@@ -8,6 +8,7 @@ exports.needsRehash = needsRehash;
|
|
|
8
8
|
exports.updatePasswordHash = updatePasswordHash;
|
|
9
9
|
exports.validatePasswordComplexity = validatePasswordComplexity;
|
|
10
10
|
exports.isOAuth2Enabled = isOAuth2Enabled;
|
|
11
|
+
exports.isTwoFactorEnabled = isTwoFactorEnabled;
|
|
11
12
|
exports.hasRole = hasRole;
|
|
12
13
|
exports.hasRoles = hasRoles;
|
|
13
14
|
exports.hasPermission = hasPermission;
|
|
@@ -191,6 +192,16 @@ function isOAuth2Enabled() {
|
|
|
191
192
|
.filter((source) => source.startsWith('oauth2'))
|
|
192
193
|
.some((source) => common_1.config.bool(`SECURITY_OAUTH2_${source.replace('oauth2', '').toUpperCase()}_ENABLED`, false));
|
|
193
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* Checks if the two-factor authentication is enabled, so the auth model has the `TwoFactorMethod` relation.
|
|
197
|
+
*
|
|
198
|
+
* @return {boolean} True if 2FA is enabled or its database table is kept.
|
|
199
|
+
*/
|
|
200
|
+
function isTwoFactorEnabled() {
|
|
201
|
+
// Read by the flattened key, since the 2FA config belongs to the web module
|
|
202
|
+
return (common_1.config.bool('SECURITY_ACCOUNT_2FA_ENABLED', false) ||
|
|
203
|
+
auth_config_1.twoFactorModelConfig.KEEP_DATABASE_TABLE);
|
|
204
|
+
}
|
|
194
205
|
/**
|
|
195
206
|
* Checks if the given user has a specific role.
|
|
196
207
|
*
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const common_1 = require("@appweaver/common");
|
|
4
|
+
const create_model_1 = require("../../../factory/create-model");
|
|
5
|
+
const helper_1 = require("../../helper");
|
|
6
|
+
const authModel = (0, helper_1.resourceAuthModel)();
|
|
7
|
+
exports.default = (0, helper_1.isTwoFactorEnabled)()
|
|
8
|
+
? (0, create_model_1.createModel)({
|
|
9
|
+
name: 'TwoFactorMethod',
|
|
10
|
+
// Cascades from the auth model, so it is soft deleted together with it
|
|
11
|
+
softDelete: !!authModel?.config.softDelete,
|
|
12
|
+
audit: {
|
|
13
|
+
createdById: false
|
|
14
|
+
},
|
|
15
|
+
scalars: {
|
|
16
|
+
type: {
|
|
17
|
+
type: 'enum',
|
|
18
|
+
values: Object.values(common_1.TwoFactorMethodType)
|
|
19
|
+
},
|
|
20
|
+
// The order of the methods of a user, the lowest is the default
|
|
21
|
+
priority: {
|
|
22
|
+
type: 'int',
|
|
23
|
+
default: 0
|
|
24
|
+
},
|
|
25
|
+
// The encrypted TOTP secret
|
|
26
|
+
secret: {
|
|
27
|
+
type: 'string',
|
|
28
|
+
required: false,
|
|
29
|
+
hidden: true
|
|
30
|
+
},
|
|
31
|
+
// Unset while a TOTP method waits for the confirmation of its first code
|
|
32
|
+
confirmedAt: {
|
|
33
|
+
type: 'dateTime',
|
|
34
|
+
required: false
|
|
35
|
+
},
|
|
36
|
+
// The last accepted TOTP time step, so a code is never accepted twice
|
|
37
|
+
lastUsedStep: {
|
|
38
|
+
type: 'int',
|
|
39
|
+
required: false,
|
|
40
|
+
hidden: true
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
...(authModel
|
|
44
|
+
? {
|
|
45
|
+
relations: {
|
|
46
|
+
[(0, common_1.uncapitalize)(authModel.name)]: {
|
|
47
|
+
model: authModel.name,
|
|
48
|
+
type: 'manyToOne',
|
|
49
|
+
mappedBy: 'twoFactorMethods',
|
|
50
|
+
// The methods are meaningless without their user
|
|
51
|
+
onDelete: 'cascade',
|
|
52
|
+
input: {
|
|
53
|
+
type: 'none'
|
|
54
|
+
},
|
|
55
|
+
output: {
|
|
56
|
+
type: 'none'
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
unique: [[`${(0, common_1.uncapitalize)(authModel.name)}Id`, 'type']]
|
|
61
|
+
}
|
|
62
|
+
: {})
|
|
63
|
+
})
|
|
64
|
+
: undefined;
|