@appweaver/data 2.4.1 → 2.5.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/config.d.ts +2 -0
- package/config.js +2 -0
- package/database/prisma-database.js +7 -1
- package/database/sqlserver-extension.d.ts +18 -0
- package/database/sqlserver-extension.js +269 -0
- package/factory/create-model.js +60 -1
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/module.js +19 -2
- 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.js +4 -0
- package/resource/utils/prune-util.js +3 -1
- 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.js +5 -1
- 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 +158 -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 +19 -0
- package/timeseries/timescale-time-series.js +131 -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
|
@@ -3,6 +3,8 @@ export type * from './models/ConnectedAccount';
|
|
|
3
3
|
export type * from './models/OneTimeToken';
|
|
4
4
|
export type * from './models/Permission';
|
|
5
5
|
export type * from './models/Role';
|
|
6
|
+
export type * from './models/TwoFactorMethod';
|
|
7
|
+
export type * from './models/TwoFactorRecoveryCode';
|
|
6
8
|
export type * from './models/Seeder';
|
|
7
9
|
export type * from './models/File';
|
|
8
10
|
export type * from './commonInputTypes';
|
|
@@ -918,6 +918,7 @@ class ResourceService {
|
|
|
918
918
|
*/
|
|
919
919
|
async cleanupDeletedRecords(deleted) {
|
|
920
920
|
const fileService = (0, core_1.inject)(file_service_1.FileService, false);
|
|
921
|
+
const timeSeriesService = (0, core_1.inject)(core_1.TimeSeriesService, false);
|
|
921
922
|
const groups = [
|
|
922
923
|
[true, deleted.soft],
|
|
923
924
|
[false, deleted.hard]
|
|
@@ -925,6 +926,9 @@ class ResourceService {
|
|
|
925
926
|
for (const [softDeleted, records] of groups) {
|
|
926
927
|
for (const [modelName, ids] of Object.entries(records)) {
|
|
927
928
|
await fileService?.deleteResourcesFiles(modelName, ids, softDeleted);
|
|
929
|
+
if (!softDeleted) {
|
|
930
|
+
await timeSeriesService?.purgeResources(modelName, ids);
|
|
931
|
+
}
|
|
928
932
|
await this._cacheService.invalidateCache(modelName, 'delete');
|
|
929
933
|
}
|
|
930
934
|
}
|
|
@@ -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
|
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;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const create_service_1 = require("../../../factory/create-service");
|
|
4
|
+
const helper_1 = require("../../helper");
|
|
5
|
+
exports.default = (0, helper_1.isTwoFactorEnabled)()
|
|
6
|
+
? (0, create_service_1.createService)({ modelName: 'TwoFactorMethod' })
|
|
7
|
+
: undefined;
|
|
@@ -0,0 +1,49 @@
|
|
|
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: 'TwoFactorRecoveryCode',
|
|
10
|
+
// Cascades from the auth model, so it is soft deleted together with it
|
|
11
|
+
softDelete: !!authModel?.config.softDelete,
|
|
12
|
+
audit: {
|
|
13
|
+
updatedAt: false,
|
|
14
|
+
createdById: false
|
|
15
|
+
},
|
|
16
|
+
scalars: {
|
|
17
|
+
// The SHA-256 of the normalized code, whose randomness makes a slow hash unnecessary
|
|
18
|
+
codeHash: {
|
|
19
|
+
type: 'string',
|
|
20
|
+
maxLength: 64,
|
|
21
|
+
unique: true,
|
|
22
|
+
hidden: true
|
|
23
|
+
},
|
|
24
|
+
usedAt: {
|
|
25
|
+
type: 'dateTime',
|
|
26
|
+
required: false
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
...(authModel
|
|
30
|
+
? {
|
|
31
|
+
relations: {
|
|
32
|
+
[(0, common_1.uncapitalize)(authModel.name)]: {
|
|
33
|
+
model: authModel.name,
|
|
34
|
+
type: 'manyToOne',
|
|
35
|
+
mappedBy: 'twoFactorRecoveryCodes',
|
|
36
|
+
// The codes are meaningless without their user
|
|
37
|
+
onDelete: 'cascade',
|
|
38
|
+
input: {
|
|
39
|
+
type: 'none'
|
|
40
|
+
},
|
|
41
|
+
output: {
|
|
42
|
+
type: 'none'
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
: {})
|
|
48
|
+
})
|
|
49
|
+
: undefined;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const create_service_1 = require("../../../factory/create-service");
|
|
4
|
+
const helper_1 = require("../../helper");
|
|
5
|
+
exports.default = (0, helper_1.isTwoFactorEnabled)()
|
|
6
|
+
? (0, create_service_1.createService)({ modelName: 'TwoFactorRecoveryCode' })
|
|
7
|
+
: undefined;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { SecurityStore,
|
|
1
|
+
import { SecurityStore, TokenValidationResult } from '@appweaver/common';
|
|
2
2
|
export declare class DatabaseSecurityStore extends SecurityStore {
|
|
3
3
|
generateOneTimeToken<T = any>(purpose: string, data: T, ttl: number): Promise<string>;
|
|
4
|
-
useOneTimeToken<T = any>(token: string, purpose: string, validateContent?: (value: T) =>
|
|
4
|
+
useOneTimeToken<T = any>(token: string, purpose: string, validateContent?: (value: T) => TokenValidationResult<T>): Promise<T>;
|
|
5
5
|
}
|
|
@@ -38,7 +38,17 @@ class DatabaseSecurityStore extends common_1.SecurityStore {
|
|
|
38
38
|
if (validateContent) {
|
|
39
39
|
const result = validateContent(data);
|
|
40
40
|
if (!result.valid) {
|
|
41
|
-
|
|
41
|
+
if (result.revoke) {
|
|
42
|
+
await oneTimeTokens.deleteMany({ where: { id: oneTimeToken.id } });
|
|
43
|
+
}
|
|
44
|
+
else if (result.data !== undefined) {
|
|
45
|
+
await oneTimeTokens.updateMany({
|
|
46
|
+
where: { id: oneTimeToken.id },
|
|
47
|
+
data: { data: result.data }
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
throw (result.error ??
|
|
51
|
+
new common_1.AuthError(common_1.ErrorCode.AuthInvalidToken, result.message));
|
|
42
52
|
}
|
|
43
53
|
}
|
|
44
54
|
// Only the one of concurrent uses that removes the token gets its data
|
package/seeder/create-seeder.js
CHANGED
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createSeeder = createSeeder;
|
|
4
4
|
const common_1 = require("@appweaver/common");
|
|
5
|
-
const database_config_1 = require("../database/database-config");
|
|
6
5
|
const core_1 = require("@appweaver/core");
|
|
7
6
|
const internal_1 = require("@appweaver/core/internal");
|
|
8
7
|
const seeder_1 = require("./seeder");
|
|
8
|
+
const resource_loader_1 = require("../resource/resource-loader");
|
|
9
|
+
const database_config_1 = require("../database/database-config");
|
|
9
10
|
/**
|
|
10
11
|
* Creates the database seeder, loading the providers of the project first, and runs the pending seeders unless
|
|
11
12
|
* `autoSeed` is disabled: the seeders of the modules declaring a `seeders` directory first, then the ones of the
|
|
@@ -20,6 +21,9 @@ async function createSeeder(params = {}) {
|
|
|
20
21
|
// Define the providers and the services of the modules
|
|
21
22
|
const modules = await (0, core_1.discoverModules)({ scanPath });
|
|
22
23
|
await (0, internal_1.loadProviders)(modules, scanPath);
|
|
24
|
+
// Load the resources like the application does, i.e. for the JSON fields
|
|
25
|
+
// the database client of SQL Server serializes
|
|
26
|
+
await (0, resource_loader_1.loadResources)(scanPath, undefined, modules.flatMap(({ module }) => module.resources ? [module.resources] : []));
|
|
23
27
|
const seeder = new seeder_1.Seeder(seedersPath, params.continueOnError, params.fixWarnings, modules.flatMap(({ module }) => module.seeders ? [{ name: module.name, path: module.seeders }] : []));
|
|
24
28
|
if (params.autoSeed !== false) {
|
|
25
29
|
await seeder.seed();
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.databaseSeriesConfig = void 0;
|
|
4
|
+
const typebox_1 = require("@sinclair/typebox");
|
|
5
|
+
const common_1 = require("@appweaver/common");
|
|
6
|
+
exports.databaseSeriesConfig = (0, common_1.defineConfig)('timeseries.database', {
|
|
7
|
+
/** How long the `database` provider keeps the points, a duration (`365d`), overridden by the
|
|
8
|
+
* `seriesOptions.retention` of a model, empty to keep every point. Default: `''`. */
|
|
9
|
+
RETENTION: typebox_1.Type.String({ default: '' }),
|
|
10
|
+
/** How often the `database` provider deletes the points older than their retention, a duration. Default: `'1h'`. */
|
|
11
|
+
RETENTION_INTERVAL: typebox_1.Type.String({ default: '1h' }),
|
|
12
|
+
/** Delete the expired points in this instance, `false` for the other instances of an application whose deletes run
|
|
13
|
+
* in one instance (or in a task calling `purge`). Default: `true`. */
|
|
14
|
+
RETENTION_WORKER: typebox_1.Type.Boolean({ default: true })
|
|
15
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { HEALTH_CHECK, HealthCheckResult, SeriesDeleteQuery, SeriesModel, SeriesPoint, SeriesQuery, TimeSeries } from '@appweaver/common';
|
|
2
|
+
/**
|
|
3
|
+
* The `database` driver of `TimeSeries`: the points of a model in the `<Model>Series` table `weaver generate` adds to
|
|
4
|
+
* the Prisma schema, keyed by the resource and the time, written and queried with SQL over the connection of the
|
|
5
|
+
* database, on SQLite, PostgreSQL, MySQL and SQL Server. The points of a resource are deleted with it by the foreign
|
|
6
|
+
* key of the table, and the points older than the retention of their model (`seriesOptions.retention`, or the
|
|
7
|
+
* `timeseries.database.retention` config) once the application starts, then every `timeseries.database.retentionInterval`,
|
|
8
|
+
* in the instances with `timeseries.database.retentionWorker`.
|
|
9
|
+
*/
|
|
10
|
+
export declare class DatabaseTimeSeries extends TimeSeries {
|
|
11
|
+
static [HEALTH_CHECK]: boolean;
|
|
12
|
+
readonly cascadesDeletes: boolean;
|
|
13
|
+
readonly transactional: boolean;
|
|
14
|
+
onInit(): Promise<void>;
|
|
15
|
+
onDestroy(): Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* Starts deleting the points older than the retention of their model, at once and every
|
|
18
|
+
* `timeseries.database.retentionInterval`.
|
|
19
|
+
*
|
|
20
|
+
* @throws {ConfigurationError} `CONFIGURATION_INVALID` for a retention or an interval that is not a duration.
|
|
21
|
+
*/
|
|
22
|
+
register(models: SeriesModel[]): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Deletes the points older than the retention of their model, of the models registered with a retention.
|
|
25
|
+
*
|
|
26
|
+
* @param {Date} [now] - The time the retentions count back from.
|
|
27
|
+
* @return {Promise<number>} The number of points deleted.
|
|
28
|
+
*/
|
|
29
|
+
purge(now?: Date): Promise<number>;
|
|
30
|
+
write(model: SeriesModel, points: SeriesPoint[]): Promise<number>;
|
|
31
|
+
query(model: SeriesModel, query: SeriesQuery): Promise<SeriesPoint[]>;
|
|
32
|
+
delete(model: SeriesModel, query: SeriesDeleteQuery): Promise<number>;
|
|
33
|
+
checkHealth(): Promise<HealthCheckResult>;
|
|
34
|
+
/** The start of the bucket of a time column, overridden by a store with its own bucket function. */
|
|
35
|
+
protected bucket(column: string, width: number): string;
|
|
36
|
+
}
|