@friggframework/core 0.2.31-v1-alpha-package-update.0 → 1.0.1-v1-alpha-package-update.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/assertions/CHANGELOG.md +87 -0
- package/assertions/LICENSE.md +9 -0
- package/assertions/README.md +3 -0
- package/assertions/bump.txt +1 -0
- package/assertions/get.js +139 -0
- package/assertions/index.js +19 -0
- package/assertions/is-equal.js +17 -0
- package/associations/LICENSE.md +9 -0
- package/associations/README.md +3 -0
- package/associations/association.js +78 -0
- package/associations/bump3.txt +0 -0
- package/associations/jest.config.js +5 -0
- package/associations/model.js +54 -0
- package/bump.txt +1 -0
- package/core/.eslintrc.json +3 -0
- package/{Delegate.js → core/Delegate.js} +1 -1
- package/core/LICENSE.md +9 -0
- package/{Worker.js → core/Worker.js} +2 -2
- package/core/bump3.txt +0 -0
- package/{create-handler.js → core/create-handler.js} +2 -2
- package/core/index.js +6 -0
- package/core/jest.config.js +5 -0
- package/database/.eslintrc.json +3 -0
- package/database/CHANGELOG.md +97 -0
- package/database/LICENSE.md +9 -0
- package/database/README.md +3 -0
- package/database/bump3.txt +0 -0
- package/database/index.js +17 -0
- package/database/jest.config.js +5 -0
- package/database/models/IndividualUser.js +76 -0
- package/database/models/OrganizationUser.js +29 -0
- package/database/models/State.js +9 -0
- package/database/models/Token.js +70 -0
- package/database/models/UserModel.js +7 -0
- package/database/mongo.js +45 -0
- package/database/mongoose.js +5 -0
- package/encrypt/.eslintrc.json +3 -0
- package/encrypt/CHANGELOG.md +65 -0
- package/encrypt/Cryptor.js +236 -0
- package/encrypt/Cryptor.test.js +32 -0
- package/encrypt/LICENSE.md +9 -0
- package/encrypt/README.md +3 -0
- package/encrypt/aes.js +27 -0
- package/encrypt/bump3.txt +0 -0
- package/encrypt/encrypt.js +124 -0
- package/encrypt/encrypt.test.js +1068 -0
- package/encrypt/index.js +3 -0
- package/encrypt/jest.config.js +5 -0
- package/encrypt/test-encrypt.js +107 -0
- package/errors/.eslintrc.json +3 -0
- package/errors/CHANGELOG.md +44 -0
- package/errors/LICENSE.md +9 -0
- package/errors/README.md +3 -0
- package/errors/base-error.js +23 -0
- package/errors/base-error.test.js +32 -0
- package/errors/bump.txt +1 -0
- package/errors/bump3.txt +0 -0
- package/errors/fetch-error.js +72 -0
- package/errors/fetch-error.test.js +79 -0
- package/errors/halt-error.js +10 -0
- package/errors/halt-error.test.js +11 -0
- package/errors/index.js +15 -0
- package/errors/jest.config.js +5 -0
- package/errors/validation-errors.js +23 -0
- package/errors/validation-errors.test.js +120 -0
- package/eslint-config/.eslintrc.json +3 -0
- package/eslint-config/CHANGELOG.md +17 -0
- package/eslint-config/LICENSE.md +9 -0
- package/eslint-config/README.md +3 -0
- package/eslint-config/bump3.txt +0 -0
- package/eslint-config/index.js +38 -0
- package/index.js +29 -5
- package/integrations/.eslintrc.json +3 -0
- package/integrations/CHANGELOG.md +244 -0
- package/integrations/LICENSE.md +9 -0
- package/integrations/README.md +3 -0
- package/integrations/bump3.txt +0 -0
- package/integrations/create-frigg-backend.js +31 -0
- package/integrations/index.js +19 -0
- package/integrations/integration-base.js +162 -0
- package/integrations/integration-factory.js +166 -0
- package/integrations/integration-mapping.js +43 -0
- package/integrations/integration-model.js +42 -0
- package/integrations/integration-router.js +367 -0
- package/integrations/integration-user.js +144 -0
- package/integrations/jest-setup.js +2 -0
- package/integrations/jest-teardown.js +2 -0
- package/integrations/jest.config.js +12 -0
- package/integrations/options.js +54 -0
- package/integrations/test/integration-base.test.js +143 -0
- package/lambda/README.md +3 -0
- package/lambda/TimeoutCatcher.js +43 -0
- package/lambda/TimeoutCatcher.test.js +68 -0
- package/lambda/bump3.txt +0 -0
- package/lambda/index.js +3 -0
- package/lambda/jest.config.js +3 -0
- package/logs/.eslintrc.json +3 -0
- package/logs/CHANGELOG.md +57 -0
- package/logs/LICENSE.md +9 -0
- package/logs/README.md +3 -0
- package/logs/bump3.txt +0 -0
- package/logs/index.js +7 -0
- package/logs/jest.config.js +5 -0
- package/logs/logger.js +69 -0
- package/logs/logger.test.js +76 -0
- package/migrations/README.md +3 -0
- package/migrations/bump3.txt +0 -0
- package/migrations/index.js +9 -0
- package/migrations/jest.config.js +3 -0
- package/migrations/manager.js +33 -0
- package/migrations/migrator.js +170 -0
- package/migrations/options.js +28 -0
- package/module-plugin/.eslintrc.json +3 -0
- package/module-plugin/CHANGELOG.md +224 -0
- package/module-plugin/LICENSE.md +9 -0
- package/module-plugin/ModuleConstants.js +10 -0
- package/module-plugin/README.md +3 -0
- package/module-plugin/auther.js +342 -0
- package/module-plugin/bump3.txt +0 -0
- package/module-plugin/credential.js +22 -0
- package/module-plugin/entity-manager.js +70 -0
- package/module-plugin/entity.js +46 -0
- package/module-plugin/index.js +25 -0
- package/module-plugin/jest-setup.js +3 -0
- package/module-plugin/jest-teardown.js +2 -0
- package/module-plugin/jest.config.js +20 -0
- package/module-plugin/manager.js +169 -0
- package/module-plugin/module-factory.js +60 -0
- package/module-plugin/requester/api-key.js +36 -0
- package/module-plugin/requester/basic.js +43 -0
- package/module-plugin/requester/oauth-2.js +219 -0
- package/module-plugin/requester/requester.js +150 -0
- package/module-plugin/requester/requester.test.js +28 -0
- package/module-plugin/test/auther.test.js +97 -0
- package/module-plugin/test/mock-api/api.js +29 -0
- package/module-plugin/test/mock-api/definition.js +48 -0
- package/module-plugin/test/mock-api/mocks/hubspot.js +43 -0
- package/package.json +37 -12
- package/prettier-config/.eslintrc.json +3 -0
- package/prettier-config/CHANGELOG.md +17 -0
- package/prettier-config/LICENSE.md +9 -0
- package/prettier-config/README.md +3 -0
- package/prettier-config/bump3.txt +0 -0
- package/prettier-config/index.js +6 -0
- package/syncs/README.md +3 -0
- package/syncs/bump3.txt +0 -0
- package/syncs/jest.config.js +5 -0
- package/syncs/manager.js +466 -0
- package/syncs/model.js +62 -0
- package/syncs/sync.js +114 -0
- package/test-environment/.eslintrc.json +3 -0
- package/test-environment/Authenticator.js +73 -0
- package/test-environment/CHANGELOG.md +46 -0
- package/test-environment/LICENSE.md +9 -0
- package/test-environment/README.md +3 -0
- package/test-environment/auther-definition-method-tester.js +45 -0
- package/test-environment/auther-definition-tester.js +104 -0
- package/test-environment/bump3.txt +0 -0
- package/test-environment/index.js +24 -0
- package/test-environment/integration-validator.js +2 -0
- package/test-environment/jest-global-setup.js +6 -0
- package/test-environment/jest-global-teardown.js +3 -0
- package/test-environment/jest-preset.js +14 -0
- package/test-environment/mock-api-readme.md +102 -0
- package/test-environment/mock-api.js +284 -0
- package/test-environment/mock-integration.js +82 -0
- package/test-environment/mongodb.js +22 -0
- package/test-environment/override-environment.js +11 -0
- package/types/CHANGELOG.md +49 -0
- package/types/README.md +24 -0
- package/types/assertions/index.d.ts +83 -0
- package/types/associations/index.d.ts +74 -0
- package/types/bump3.txt +0 -0
- package/types/core/index.d.ts +54 -0
- package/types/database/index.d.ts +3 -0
- package/types/encrypt/index.d.ts +5 -0
- package/types/errors/index.d.ts +66 -0
- package/types/eslint-config/index.d.ts +41 -0
- package/types/index.d.ts +14 -0
- package/types/integrations/index.d.ts +191 -0
- package/types/lambda/index.d.ts +31 -0
- package/types/logs/index.d.ts +5 -0
- package/types/module-plugin/index.d.ts +293 -0
- package/types/prettier-config/index.d.ts +6 -0
- package/types/syncs/index.d.ts +128 -0
- package/types/test-environment/index.d.ts +17 -0
- package/types/tsconfig.json +103 -0
- /package/{.eslintrc.json → assertions/.eslintrc.json} +0 -0
- /package/{bump3.txt → assertions/bump3.txt} +0 -0
- /package/{jest.config.js → assertions/jest.config.js} +0 -0
- /package/{CHANGELOG.md → core/CHANGELOG.md} +0 -0
- /package/{README.md → core/README.md} +0 -0
- /package/{load-installed-modules.js → core/load-installed-modules.js} +0 -0
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
// Manages authorization and credential persistence
|
|
2
|
+
// Instantiation of an API Class
|
|
3
|
+
// Expects input object like this:
|
|
4
|
+
// const authDef = {
|
|
5
|
+
// API: class anAPI{},
|
|
6
|
+
// moduleName: 'anAPI', //maybe not required
|
|
7
|
+
// requiredAuthMethods: {
|
|
8
|
+
// // oauth methods, how to handle these being required/not?
|
|
9
|
+
// getToken: async function(params, callbackParams, tokenResponse) {},
|
|
10
|
+
// // required for all Auth methods
|
|
11
|
+
// getEntityDetails: async function(params) {}, //probably calls api method
|
|
12
|
+
// getCredentialDetails: async function(params) {}, // might be same as above
|
|
13
|
+
// apiParamsFromCredential: function(params) {},
|
|
14
|
+
// testAuth: async function() {}, // basic request to testAuth
|
|
15
|
+
// },
|
|
16
|
+
// env: {
|
|
17
|
+
// client_id: process.env.HUBSPOT_CLIENT_ID,
|
|
18
|
+
// client_secret: process.env.HUBSPOT_CLIENT_SECRET,
|
|
19
|
+
// scope: process.env.HUBSPOT_SCOPE,
|
|
20
|
+
// redirect_uri: `${process.env.REDIRECT_URI}/an-api`,
|
|
21
|
+
// }
|
|
22
|
+
// };
|
|
23
|
+
|
|
24
|
+
//TODO:
|
|
25
|
+
// 1. Add definition of expected params to API Class (or could just be credential?)
|
|
26
|
+
// 2.
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
const { Delegate } = require('../core');
|
|
30
|
+
const { get } = require('../assertions');
|
|
31
|
+
const _ = require('lodash');
|
|
32
|
+
const {flushDebugLog} = require('../logs');
|
|
33
|
+
const { Credential } = require('./credential');
|
|
34
|
+
const { Entity } = require('./entity');
|
|
35
|
+
const { mongoose } = require('../database/mongoose');
|
|
36
|
+
const {ModuleConstants} = require("./ModuleConstants");
|
|
37
|
+
|
|
38
|
+
class Auther extends Delegate {
|
|
39
|
+
static validateDefinition(definition) {
|
|
40
|
+
if (!definition) {
|
|
41
|
+
throw new Error('Auther definition is required');
|
|
42
|
+
}
|
|
43
|
+
if (!definition.moduleName) {
|
|
44
|
+
throw new Error('Auther definition requires moduleName');
|
|
45
|
+
}
|
|
46
|
+
if (!definition.API) {
|
|
47
|
+
throw new Error('Auther definition requires API class');
|
|
48
|
+
}
|
|
49
|
+
// if (!definition.Credential) {
|
|
50
|
+
// throw new Error('Auther definition requires Credential class');
|
|
51
|
+
// }
|
|
52
|
+
// if (!definition.Entity) {
|
|
53
|
+
// throw new Error('Auther definition requires Entity class');
|
|
54
|
+
// }
|
|
55
|
+
if (!definition.requiredAuthMethods) {
|
|
56
|
+
throw new Error('Auther definition requires requiredAuthMethods');
|
|
57
|
+
} else {
|
|
58
|
+
if (definition.API.requesterType === ModuleConstants.authType.oauth2 &&
|
|
59
|
+
!definition.requiredAuthMethods.getToken) {
|
|
60
|
+
throw new Error('Auther definition requires requiredAuthMethods.getToken');
|
|
61
|
+
}
|
|
62
|
+
if (!definition.requiredAuthMethods.getEntityDetails) {
|
|
63
|
+
throw new Error('Auther definition requires requiredAuthMethods.getEntityDetails');
|
|
64
|
+
}
|
|
65
|
+
if (!definition.requiredAuthMethods.getCredentialDetails) {
|
|
66
|
+
throw new Error('Auther definition requires requiredAuthMethods.getCredentialDetails');
|
|
67
|
+
}
|
|
68
|
+
if (!definition.requiredAuthMethods.apiPropertiesToPersist) {
|
|
69
|
+
throw new Error('Auther definition requires requiredAuthMethods.apiPropertiesToPersist');
|
|
70
|
+
} else if (definition.Credential){
|
|
71
|
+
for (const prop of definition.requiredAuthMethods.apiPropertiesToPersist?.credential) {
|
|
72
|
+
if (!definition.Credential.schema.paths.hasOwnProperty(prop)) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
`Auther definition requires Credential schema to have property ${prop}`
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (!definition.requiredAuthMethods.testAuthRequest) {
|
|
80
|
+
throw new Error('Auther definition requires requiredAuthMethods.testAuth');
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
constructor(params) {
|
|
86
|
+
super(params);
|
|
87
|
+
this.userId = get(params, 'userId', null); // Making this non-required
|
|
88
|
+
const definition = get(params, 'definition');
|
|
89
|
+
Auther.validateDefinition(definition);
|
|
90
|
+
Object.assign(this, definition.requiredAuthMethods);
|
|
91
|
+
this.name = definition.moduleName;
|
|
92
|
+
this.modelName = definition.modelName;
|
|
93
|
+
this.apiClass = definition.API;
|
|
94
|
+
this.CredentialModel = definition.Credential || this.getCredentialModel();
|
|
95
|
+
this.EntityModel = definition.Entity || this.getEntityModel();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
static async getInstance(params) {
|
|
99
|
+
const instance = new this(params);
|
|
100
|
+
if (params.entityId) {
|
|
101
|
+
instance.entity = await instance.EntityModel.findById(params.entityId);
|
|
102
|
+
instance.credential = await instance.CredentialModel.findById(
|
|
103
|
+
instance.entity.credential
|
|
104
|
+
);
|
|
105
|
+
} else if (params.credentialId) {
|
|
106
|
+
instance.credential = await instance.CredentialModel.findById(
|
|
107
|
+
params.credentialId
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
let credential = {};
|
|
111
|
+
let entity = {};
|
|
112
|
+
if (instance.credential) {
|
|
113
|
+
credential = instance.credential.toObject();
|
|
114
|
+
}
|
|
115
|
+
if (instance.entity) {
|
|
116
|
+
entity = instance.entity.toObject();
|
|
117
|
+
}
|
|
118
|
+
const apiParams = {
|
|
119
|
+
...params.definition.env,
|
|
120
|
+
delegate: instance,
|
|
121
|
+
...instance.apiParamsFromCredential(credential),
|
|
122
|
+
...instance.apiParamsFromEntity(entity),
|
|
123
|
+
};
|
|
124
|
+
instance.api = new instance.apiClass(apiParams);
|
|
125
|
+
return instance;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
static getEntityModelFromDefinition(definition) {
|
|
129
|
+
const partialModule = new this({definition});
|
|
130
|
+
return partialModule.getEntityModel();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
getName() {
|
|
134
|
+
return this.name;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
apiParamsFromCredential(credential) {
|
|
138
|
+
return _.pick(credential, ...this.apiPropertiesToPersist?.credential);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
apiParamsFromEntity(entity) {
|
|
142
|
+
return _.pick(entity, ...this.apiPropertiesToPersist?.entity);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
getEntityModel() {
|
|
146
|
+
if (!this.EntityModel) {
|
|
147
|
+
const prefix = this.modelName ?? _.upperFirst(this.getName());
|
|
148
|
+
const arrayToDefaultObject = (array, defaultValue) => _.mapValues(_.keyBy(array), () => defaultValue);
|
|
149
|
+
const schema = new mongoose.Schema(arrayToDefaultObject(this.apiPropertiesToPersist.entity, {
|
|
150
|
+
type: mongoose.Schema.Types.Mixed,
|
|
151
|
+
trim: true,
|
|
152
|
+
}));
|
|
153
|
+
const name = `${prefix}Entity`;
|
|
154
|
+
this.EntityModel =
|
|
155
|
+
Entity.discriminators?.[name] || Entity.discriminator(name, schema);
|
|
156
|
+
}
|
|
157
|
+
return this.EntityModel;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
getCredentialModel() {
|
|
161
|
+
if (!this.CredentialModel) {
|
|
162
|
+
const arrayToDefaultObject = (array, defaultValue) => _.mapValues(_.keyBy(array), () => defaultValue);
|
|
163
|
+
const schema = new mongoose.Schema(arrayToDefaultObject(this.apiPropertiesToPersist.credential, {
|
|
164
|
+
type: mongoose.Schema.Types.Mixed,
|
|
165
|
+
trim: true,
|
|
166
|
+
lhEncrypt: true
|
|
167
|
+
}));
|
|
168
|
+
const prefix = this.modelName ?? _.upperFirst(this.getName());
|
|
169
|
+
const name = `${prefix}Credential`;
|
|
170
|
+
this.CredentialModel =
|
|
171
|
+
Credential.discriminators?.[name] || Credential.discriminator(name, schema);
|
|
172
|
+
}
|
|
173
|
+
return this.CredentialModel;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async getEntitiesForUserId(userId) {
|
|
177
|
+
// Only return non-internal fields. Leverages "select" and "options" to non-excepted fields and a pure object.
|
|
178
|
+
const list = await this.EntityModel.find(
|
|
179
|
+
{ user: userId },
|
|
180
|
+
'-dateCreated -dateUpdated -user -credentials -credential -__t -__v',
|
|
181
|
+
{ lean: true }
|
|
182
|
+
);
|
|
183
|
+
console.log('getEntitiesForUserId list', list, userId);
|
|
184
|
+
return list.map((entity) => ({
|
|
185
|
+
id: entity._id,
|
|
186
|
+
type: this.getName(),
|
|
187
|
+
...entity,
|
|
188
|
+
}));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
async validateAuthorizationRequirements() {
|
|
192
|
+
const requirements = await this.getAuthorizationRequirements();
|
|
193
|
+
let valid = true;
|
|
194
|
+
if (['oauth1', 'oauth2'].includes(requirements.type) && !requirements.url) {
|
|
195
|
+
valid = false;
|
|
196
|
+
}
|
|
197
|
+
return valid;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
getAuthorizationRequirements(params) {
|
|
201
|
+
// TODO: How can this be more helpful both to implement and consume
|
|
202
|
+
// this function must return a dictionary with the following format
|
|
203
|
+
// node only url key is required. Data would be used for Base Authentication
|
|
204
|
+
// let returnData = {
|
|
205
|
+
// url: "callback url for the data or teh redirect url for login",
|
|
206
|
+
// type: one of the types defined in modules/Constants.js
|
|
207
|
+
// data: ["required", "fields", "we", "may", "need"]
|
|
208
|
+
// }
|
|
209
|
+
return this.api.getAuthorizationRequirements();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async testAuth(params) {
|
|
213
|
+
let validAuth = false;
|
|
214
|
+
try {
|
|
215
|
+
if (await this.testAuthRequest(this.api)) validAuth = true;
|
|
216
|
+
} catch (e) {
|
|
217
|
+
flushDebugLog(e);
|
|
218
|
+
}
|
|
219
|
+
return validAuth;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async processAuthorizationCallback(params) {
|
|
223
|
+
let tokenResponse;
|
|
224
|
+
if (this.apiClass.requesterType === ModuleConstants.authType.oauth2) {
|
|
225
|
+
tokenResponse = await this.getToken(this.api, params);
|
|
226
|
+
} else {
|
|
227
|
+
tokenResponse = await this.setAuthParams(this.api, params);
|
|
228
|
+
await this.onTokenUpdate();
|
|
229
|
+
}
|
|
230
|
+
const authRes = await this.testAuth();
|
|
231
|
+
if (!authRes) {
|
|
232
|
+
throw new Error('Authorization failed');
|
|
233
|
+
}
|
|
234
|
+
const entityDetails = await this.getEntityDetails(
|
|
235
|
+
this.api, params, tokenResponse, this.userId
|
|
236
|
+
);
|
|
237
|
+
Object.assign(entityDetails.details, this.apiParamsFromEntity(this.api));
|
|
238
|
+
await this.findOrCreateEntity(entityDetails);
|
|
239
|
+
return {
|
|
240
|
+
credential_id: this.credential.id,
|
|
241
|
+
entity_id: this.entity.id,
|
|
242
|
+
type: this.getName(),
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
async onTokenUpdate() {
|
|
247
|
+
const credentialDetails = await this.getCredentialDetails(this.api, this.userId);
|
|
248
|
+
Object.assign(credentialDetails.details, this.apiParamsFromCredential(this.api));
|
|
249
|
+
credentialDetails.details.auth_is_valid = true;
|
|
250
|
+
await this.updateOrCreateCredential(credentialDetails);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
async receiveNotification(notifier, delegateString, object = null) {
|
|
254
|
+
if (delegateString === this.api.DLGT_TOKEN_UPDATE) {
|
|
255
|
+
await this.onTokenUpdate();
|
|
256
|
+
}
|
|
257
|
+
else if (delegateString === this.api.DLGT_TOKEN_DEAUTHORIZED) {
|
|
258
|
+
await this.deauthorize();
|
|
259
|
+
}
|
|
260
|
+
else if (delegateString === this.api.DLGT_INVALID_AUTH) {
|
|
261
|
+
await this.markCredentialsInvalid();
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
async getEntityOptions() {
|
|
266
|
+
// May not be needed if the callback already creates the entity, such as in situations
|
|
267
|
+
// like HubSpot where the account is determined in the authorization flow.
|
|
268
|
+
// This should only be used in situations such as FreshBooks where the user needs to make
|
|
269
|
+
// an account decision on the front end.
|
|
270
|
+
throw new Error(
|
|
271
|
+
'Entity requirement method getEntityOptions() is not defined in the class'
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
async findOrCreateEntity(entityDetails) {
|
|
276
|
+
const identifiers = get(entityDetails, 'identifiers');
|
|
277
|
+
const details = get(entityDetails, 'details');
|
|
278
|
+
const search = await this.EntityModel.find(identifiers);
|
|
279
|
+
if (search.length > 1) {
|
|
280
|
+
throw new Error(
|
|
281
|
+
'Multiple entities found with the same identifiers: ' + JSON.stringify(identifiers)
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
else if (search.length === 0) {
|
|
285
|
+
this.entity = await this.EntityModel.create({
|
|
286
|
+
credential: this.credential.id,
|
|
287
|
+
...details,
|
|
288
|
+
...identifiers,
|
|
289
|
+
});
|
|
290
|
+
} else if (search.length === 1) {
|
|
291
|
+
this.entity = search[0];
|
|
292
|
+
}
|
|
293
|
+
if (this.entity.credential === undefined) {
|
|
294
|
+
this.entity.credential = this.credential.id;
|
|
295
|
+
await this.entity.save();
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
async updateOrCreateCredential(credentialDetails) {
|
|
300
|
+
const identifiers = get(credentialDetails, 'identifiers');
|
|
301
|
+
const details = get(credentialDetails, 'details');
|
|
302
|
+
|
|
303
|
+
if (!this.credential){
|
|
304
|
+
const credentialSearch = await this.CredentialModel.find(identifiers);
|
|
305
|
+
if (credentialSearch.length > 1) {
|
|
306
|
+
throw new Error(`Multiple credentials found with same identifiers: ${identifiers}`);
|
|
307
|
+
}
|
|
308
|
+
else if (credentialSearch.length === 1) {
|
|
309
|
+
// found exactly one credential with these identifiers
|
|
310
|
+
this.credential = credentialSearch[0];
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
// found no credential with these identifiers (match none for insert)
|
|
314
|
+
this.credential = {$exists: false};
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
// update credential or create if none was found
|
|
318
|
+
this.credential = await this.CredentialModel.findOneAndUpdate(
|
|
319
|
+
{_id: this.credential},
|
|
320
|
+
{$set: {...identifiers, ...details}},
|
|
321
|
+
{useFindAndModify: true, new: true, upsert: true}
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
async markCredentialsInvalid() {
|
|
326
|
+
if (this.credential) {
|
|
327
|
+
this.credential.auth_is_valid = false;
|
|
328
|
+
await this.credential.save();
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
async deauthorize() {
|
|
333
|
+
this.api = new this.apiClass();
|
|
334
|
+
if (this.entity?.credential) {
|
|
335
|
+
await this.CredentialModel.deleteOne({ _id: this.entity.credential });
|
|
336
|
+
this.entity.credential = undefined;
|
|
337
|
+
await this.entity.save();
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
module.exports = { Auther };
|
|
File without changes
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const { mongoose } = require('../database/mongoose');
|
|
2
|
+
const { Encrypt } = require('../encrypt');
|
|
3
|
+
|
|
4
|
+
const schema = new mongoose.Schema(
|
|
5
|
+
{
|
|
6
|
+
user: {
|
|
7
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
8
|
+
ref: 'User',
|
|
9
|
+
required: false,
|
|
10
|
+
},
|
|
11
|
+
subType: { type: String },
|
|
12
|
+
auth_is_valid: { type: Boolean },
|
|
13
|
+
externalId: { type: String }, // Used for lookups, identifying the owner of the credential
|
|
14
|
+
},
|
|
15
|
+
{ timestamps: true }
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
schema.plugin(Encrypt);
|
|
19
|
+
|
|
20
|
+
const Credential =
|
|
21
|
+
mongoose.models.Credential || mongoose.model('Credential', schema);
|
|
22
|
+
module.exports = { Credential };
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
const { loadInstalledModules, Delegate } = require('../core');
|
|
2
|
+
|
|
3
|
+
const { Entity } = require('./entity');
|
|
4
|
+
const { ModuleManager } = require('./manager');
|
|
5
|
+
|
|
6
|
+
class EntityManager {
|
|
7
|
+
static primaryEntityClass = null; //primaryEntity;
|
|
8
|
+
|
|
9
|
+
static entityManagerClasses = loadInstalledModules().map(
|
|
10
|
+
(m) => m.EntityManager
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
static entityTypes = EntityManager.entityManagerClasses.map(
|
|
14
|
+
(ManagerClass) => ManagerClass.getName()
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
static async getEntitiesForUser(userId) {
|
|
18
|
+
const results = [];
|
|
19
|
+
for (const Manager of this.entityManagerClasses) {
|
|
20
|
+
results.push(...(await Manager.getEntitiesForUserId(userId)));
|
|
21
|
+
}
|
|
22
|
+
return results;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static checkIsValidType(entityType) {
|
|
26
|
+
const indexOfEntity = EntityManager.entityTypes.indexOf(entityType);
|
|
27
|
+
return indexOfEntity >= 0;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
static getEntityManagerClass(entityType = '') {
|
|
31
|
+
const normalizedType = entityType.toLowerCase();
|
|
32
|
+
|
|
33
|
+
const indexOfEntityType =
|
|
34
|
+
EntityManager.entityTypes.indexOf(normalizedType);
|
|
35
|
+
if (!EntityManager.checkIsValidType(normalizedType)) {
|
|
36
|
+
throw new Error(
|
|
37
|
+
`Error: Invalid entity type of ${normalizedType}, options are ${EntityManager.entityTypes.join(
|
|
38
|
+
', '
|
|
39
|
+
)}`
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const managerClass =
|
|
44
|
+
EntityManager.entityManagerClasses[indexOfEntityType];
|
|
45
|
+
|
|
46
|
+
if (!(managerClass.prototype instanceof ModuleManager)) {
|
|
47
|
+
throw new Error('The Entity is not an instance of ModuleManager');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return managerClass;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
static async getEntityManagerInstanceFromEntityId(entityId, userId) {
|
|
54
|
+
const entityMO = new Entity();
|
|
55
|
+
const entity = await entityMO.get(entityId);
|
|
56
|
+
let entityManagerClass;
|
|
57
|
+
for (const Manager of this.entityManagerClasses) {
|
|
58
|
+
if (entity instanceof Manager.Entity.Model) {
|
|
59
|
+
entityManagerClass = Manager;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const instance = await entityManagerClass.getInstance({
|
|
63
|
+
userId,
|
|
64
|
+
entityId,
|
|
65
|
+
});
|
|
66
|
+
return instance;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
module.exports = { EntityManager };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
const { mongoose } = require('../database/mongoose');
|
|
2
|
+
const schema = new mongoose.Schema(
|
|
3
|
+
{
|
|
4
|
+
credential: {
|
|
5
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
6
|
+
ref: 'Credential',
|
|
7
|
+
required: false,
|
|
8
|
+
},
|
|
9
|
+
subType: { type: String },
|
|
10
|
+
user: {
|
|
11
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
12
|
+
ref: 'User',
|
|
13
|
+
required: false,
|
|
14
|
+
},
|
|
15
|
+
name: { type: String },
|
|
16
|
+
externalId: { type: String },
|
|
17
|
+
},
|
|
18
|
+
{ timestamps: true }
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
schema.static({
|
|
22
|
+
findByUserId: async function (userId) {
|
|
23
|
+
const entities = await this.find({ user: userId });
|
|
24
|
+
if (entities.length === 0) {
|
|
25
|
+
return null;
|
|
26
|
+
} else if (entities.length === 1) {
|
|
27
|
+
return entities[0];
|
|
28
|
+
} else {
|
|
29
|
+
throw new Error('multiple entities with same userId');
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
findAllByUserId(userId) {
|
|
33
|
+
return this.find({ user: userId });
|
|
34
|
+
},
|
|
35
|
+
upsert: async function (filter, obj) {
|
|
36
|
+
return this.findOneAndUpdate(filter, obj, {
|
|
37
|
+
new: true,
|
|
38
|
+
upsert: true,
|
|
39
|
+
setDefaultsOnInsert: true,
|
|
40
|
+
});
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const Entity = mongoose.models.Entity || mongoose.model('Entity', schema);
|
|
45
|
+
|
|
46
|
+
module.exports = { Entity };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const { Credential } = require('./credential');
|
|
2
|
+
const { EntityManager } = require('./entity-manager');
|
|
3
|
+
const { Entity } = require('./entity');
|
|
4
|
+
const { ModuleManager } = require('./manager');
|
|
5
|
+
const { ApiKeyRequester } = require('./requester/api-key');
|
|
6
|
+
const { BasicAuthRequester } = require('./requester/basic');
|
|
7
|
+
const { OAuth2Requester } = require('./requester/oauth-2');
|
|
8
|
+
const { Requester } = require('./requester/requester');
|
|
9
|
+
const { ModuleConstants } = require('./ModuleConstants');
|
|
10
|
+
const { ModuleFactory } = require('./module-factory');
|
|
11
|
+
const { Auther } = require('./auther');
|
|
12
|
+
|
|
13
|
+
module.exports = {
|
|
14
|
+
Credential,
|
|
15
|
+
EntityManager,
|
|
16
|
+
Entity,
|
|
17
|
+
ModuleManager,
|
|
18
|
+
ApiKeyRequester,
|
|
19
|
+
BasicAuthRequester,
|
|
20
|
+
OAuth2Requester,
|
|
21
|
+
Requester,
|
|
22
|
+
ModuleConstants,
|
|
23
|
+
ModuleFactory,
|
|
24
|
+
Auther
|
|
25
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* For a detailed explanation regarding each configuration property, visit:
|
|
3
|
+
* https://jestjs.io/docs/configuration
|
|
4
|
+
*/
|
|
5
|
+
module.exports = {
|
|
6
|
+
// preset: '@friggframework/test-environment',
|
|
7
|
+
coverageThreshold: {
|
|
8
|
+
global: {
|
|
9
|
+
statements: 13,
|
|
10
|
+
branches: 0,
|
|
11
|
+
functions: 1,
|
|
12
|
+
lines: 13,
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
// A path to a module which exports an async function that is triggered once before all test suites
|
|
16
|
+
globalSetup: './jest-setup.js',
|
|
17
|
+
|
|
18
|
+
// A path to a module which exports an async function that is triggered once after all test suites
|
|
19
|
+
globalTeardown: './jest-teardown.js',
|
|
20
|
+
};
|