@friggframework/core 2.0.0--canary.405.b81908d.0 → 2.0.0--canary.397.c07f148.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 +931 -50
- package/core/create-handler.js +1 -0
- package/credential/credential-repository.js +56 -0
- package/credential/use-cases/get-credential-for-user.js +21 -0
- package/credential/use-cases/update-authentication-status.js +15 -0
- package/encrypt/encrypt.js +27 -4
- package/handlers/app-definition-loader.js +38 -0
- package/handlers/app-handler-helpers.js +0 -3
- package/handlers/backend-utils.js +42 -44
- package/handlers/routers/auth.js +3 -14
- package/handlers/routers/integration-defined-routers.js +8 -5
- package/handlers/routers/user.js +25 -5
- package/handlers/workers/integration-defined-workers.js +6 -3
- package/index.js +1 -16
- package/integrations/index.js +0 -5
- package/integrations/integration-base.js +142 -46
- package/integrations/integration-repository.js +80 -0
- package/integrations/integration-router.js +303 -181
- package/integrations/options.js +1 -1
- package/integrations/tests/doubles/dummy-integration-class.js +90 -0
- package/integrations/tests/doubles/test-integration-repository.js +89 -0
- package/integrations/tests/use-cases/create-integration.test.js +124 -0
- package/integrations/tests/use-cases/delete-integration-for-user.test.js +143 -0
- package/integrations/tests/use-cases/get-integration-for-user.test.js +143 -0
- package/integrations/tests/use-cases/get-integration-instance.test.js +169 -0
- package/integrations/tests/use-cases/get-integrations-for-user.test.js +169 -0
- package/integrations/tests/use-cases/get-possible-integrations.test.js +188 -0
- package/integrations/tests/use-cases/update-integration-messages.test.js +142 -0
- package/integrations/tests/use-cases/update-integration-status.test.js +103 -0
- package/integrations/tests/use-cases/update-integration.test.js +134 -0
- package/integrations/use-cases/create-integration.js +71 -0
- package/integrations/use-cases/delete-integration-for-user.js +72 -0
- package/integrations/use-cases/get-integration-for-user.js +78 -0
- package/integrations/use-cases/get-integration-instance-by-definition.js +67 -0
- package/integrations/use-cases/get-integration-instance.js +82 -0
- package/integrations/use-cases/get-integrations-for-user.js +76 -0
- package/integrations/use-cases/get-possible-integrations.js +27 -0
- package/integrations/use-cases/index.js +11 -0
- package/integrations/use-cases/update-integration-messages.js +31 -0
- package/integrations/use-cases/update-integration-status.js +28 -0
- package/integrations/use-cases/update-integration.js +91 -0
- package/integrations/utils/map-integration-dto.js +36 -0
- package/{module-plugin → modules}/entity.js +1 -0
- package/{module-plugin → modules}/index.js +0 -8
- package/modules/module-factory.js +54 -0
- package/modules/module-repository.js +107 -0
- package/modules/module.js +218 -0
- package/{module-plugin → modules}/test/mock-api/api.js +8 -3
- package/{module-plugin → modules}/test/mock-api/definition.js +12 -8
- package/modules/tests/doubles/test-module-factory.js +16 -0
- package/modules/tests/doubles/test-module-repository.js +19 -0
- package/modules/use-cases/get-entities-for-user.js +32 -0
- package/modules/use-cases/get-entity-options-by-id.js +58 -0
- package/modules/use-cases/get-entity-options-by-type.js +34 -0
- package/modules/use-cases/get-module-instance-from-type.js +31 -0
- package/modules/use-cases/get-module.js +56 -0
- package/modules/use-cases/process-authorization-callback.js +108 -0
- package/modules/use-cases/refresh-entity-options.js +58 -0
- package/modules/use-cases/test-module-auth.js +54 -0
- package/modules/utils/map-module-dto.js +18 -0
- package/package.json +5 -5
- package/syncs/sync.js +0 -1
- package/types/integrations/index.d.ts +2 -6
- package/types/module-plugin/index.d.ts +4 -56
- package/types/syncs/index.d.ts +0 -2
- package/user/tests/doubles/test-user-repository.js +72 -0
- package/user/tests/use-cases/create-individual-user.test.js +24 -0
- package/user/tests/use-cases/create-organization-user.test.js +28 -0
- package/user/tests/use-cases/create-token-for-user-id.test.js +19 -0
- package/user/tests/use-cases/get-user-from-bearer-token.test.js +64 -0
- package/user/tests/use-cases/login-user.test.js +140 -0
- package/user/use-cases/create-individual-user.js +61 -0
- package/user/use-cases/create-organization-user.js +47 -0
- package/user/use-cases/create-token-for-user-id.js +30 -0
- package/user/use-cases/get-user-from-bearer-token.js +77 -0
- package/user/use-cases/login-user.js +122 -0
- package/user/user-repository.js +62 -0
- package/user/user.js +77 -0
- package/handlers/routers/HEALTHCHECK.md +0 -240
- package/handlers/routers/health.js +0 -451
- package/handlers/routers/health.test.js +0 -203
- package/handlers/routers/middleware/loadUser.js +0 -15
- package/handlers/routers/middleware/requireLoggedInUser.js +0 -12
- package/integrations/create-frigg-backend.js +0 -31
- package/integrations/integration-factory.js +0 -251
- package/integrations/integration-user.js +0 -144
- package/integrations/test/integration-base.test.js +0 -144
- package/module-plugin/auther.js +0 -393
- package/module-plugin/entity-manager.js +0 -70
- package/module-plugin/manager.js +0 -169
- package/module-plugin/module-factory.js +0 -61
- /package/{module-plugin → modules}/ModuleConstants.js +0 -0
- /package/{module-plugin → modules}/credential.js +0 -0
- /package/{module-plugin → modules}/requester/api-key.js +0 -0
- /package/{module-plugin → modules}/requester/basic.js +0 -0
- /package/{module-plugin → modules}/requester/oauth-2.js +0 -0
- /package/{module-plugin → modules}/requester/requester.js +0 -0
- /package/{module-plugin → modules}/requester/requester.test.js +0 -0
- /package/{module-plugin → modules}/test/auther.test.js +0 -0
- /package/{module-plugin → modules}/test/mock-api/mocks/hubspot.js +0 -0
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
const _ = require('lodash');
|
|
2
|
-
const { mongoose } = require('../../database/mongoose');
|
|
3
|
-
const { expect } = require('chai');
|
|
4
|
-
const { IntegrationBase } = require("../integration-base");
|
|
5
|
-
const {Credential} = require('../../module-plugin/credential');
|
|
6
|
-
const {Entity} = require('../../module-plugin/entity');
|
|
7
|
-
const { IntegrationMapping } = require('../integration-mapping')
|
|
8
|
-
const {IntegrationModel} = require("../integration-model");
|
|
9
|
-
|
|
10
|
-
describe(`Should fully test the IntegrationBase Class`, () => {
|
|
11
|
-
let integrationRecord;
|
|
12
|
-
let userId;
|
|
13
|
-
const integration = new IntegrationBase;
|
|
14
|
-
|
|
15
|
-
beforeAll(async () => {
|
|
16
|
-
await mongoose.connect(process.env.MONGO_URI);
|
|
17
|
-
userId = new mongoose.Types.ObjectId();
|
|
18
|
-
const credential = await Credential.findOneAndUpdate(
|
|
19
|
-
{
|
|
20
|
-
user: this.userId,
|
|
21
|
-
},
|
|
22
|
-
{ $set: { user: this.userId } },
|
|
23
|
-
{
|
|
24
|
-
new: true,
|
|
25
|
-
upsert: true,
|
|
26
|
-
setDefaultsOnInsert: true,
|
|
27
|
-
}
|
|
28
|
-
);
|
|
29
|
-
const entity1 = await Entity.findOneAndUpdate(
|
|
30
|
-
{
|
|
31
|
-
user: this.userId,
|
|
32
|
-
},
|
|
33
|
-
{
|
|
34
|
-
$set: {
|
|
35
|
-
credential: credential.id,
|
|
36
|
-
user: userId,
|
|
37
|
-
},
|
|
38
|
-
},
|
|
39
|
-
{
|
|
40
|
-
new: true,
|
|
41
|
-
upsert: true,
|
|
42
|
-
setDefaultsOnInsert: true,
|
|
43
|
-
}
|
|
44
|
-
);
|
|
45
|
-
const entity2 = await Entity.findOneAndUpdate(
|
|
46
|
-
{
|
|
47
|
-
user: userId,
|
|
48
|
-
},
|
|
49
|
-
{
|
|
50
|
-
$set: {
|
|
51
|
-
credential: credential.id,
|
|
52
|
-
user: userId,
|
|
53
|
-
},
|
|
54
|
-
},
|
|
55
|
-
{
|
|
56
|
-
new: true,
|
|
57
|
-
upsert: true,
|
|
58
|
-
setDefaultsOnInsert: true,
|
|
59
|
-
}
|
|
60
|
-
);
|
|
61
|
-
integrationRecord = await IntegrationModel.create({
|
|
62
|
-
entities: [entity1, entity2],
|
|
63
|
-
user: userId
|
|
64
|
-
});
|
|
65
|
-
integration.record = integrationRecord;
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
afterAll(async () => {
|
|
69
|
-
await Entity.deleteMany();
|
|
70
|
-
await Credential.deleteMany();
|
|
71
|
-
await IntegrationMapping.deleteMany();
|
|
72
|
-
await IntegrationModel.deleteMany();
|
|
73
|
-
await mongoose.disconnect();
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
beforeEach(() => {
|
|
77
|
-
integration.record = integrationRecord;
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
describe('getIntegrationMapping()', () => {
|
|
81
|
-
it('should return null if not found', async () => {
|
|
82
|
-
const mappings = await integration.getMapping('badId');
|
|
83
|
-
expect(mappings).to.be.null;
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it('should return if valid ids', async () => {
|
|
87
|
-
await integration.upsertMapping('validId', {});
|
|
88
|
-
const mapping = await integration.getMapping('validId');
|
|
89
|
-
expect(mapping).to.eql({})
|
|
90
|
-
});
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
describe('upsertIntegrationMapping()', () => {
|
|
94
|
-
it('should throw error if sourceId is null', async () => {
|
|
95
|
-
try {
|
|
96
|
-
await integration.upsertMapping( null, {});
|
|
97
|
-
fail('should have thrown error')
|
|
98
|
-
} catch(err) {
|
|
99
|
-
expect(err.message).to.contain('sourceId must be set');
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
it('should return for empty mapping', async () => {
|
|
104
|
-
const mapping = await integration.upsertMapping( 'validId2', {});
|
|
105
|
-
expect(_.pick(mapping, ['integration', 'sourceId', 'mapping'])).to.eql({
|
|
106
|
-
integration: integrationRecord._id,
|
|
107
|
-
sourceId: 'validId2',
|
|
108
|
-
mapping: {}
|
|
109
|
-
})
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
it('should return for filled mapping', async () => {
|
|
113
|
-
const mapping = await integration.upsertMapping('validId3', {
|
|
114
|
-
name: 'someName',
|
|
115
|
-
value: 5
|
|
116
|
-
});
|
|
117
|
-
expect(_.pick(mapping, ['integration', 'sourceId', 'mapping'])).to.eql({
|
|
118
|
-
integration: integrationRecord._id,
|
|
119
|
-
sourceId: 'validId3',
|
|
120
|
-
mapping: {
|
|
121
|
-
name: 'someName',
|
|
122
|
-
value: 5
|
|
123
|
-
}
|
|
124
|
-
})
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
it('should allow upserting to same id', async () => {
|
|
128
|
-
await integration.upsertMapping('validId4', {});
|
|
129
|
-
const mapping = await integration.upsertMapping('validId4', {
|
|
130
|
-
name: 'trustMe',
|
|
131
|
-
thisWorks: true,
|
|
132
|
-
});
|
|
133
|
-
expect(_.pick(mapping, ['integration', 'sourceId', 'mapping'])).to.eql({
|
|
134
|
-
integration: integrationRecord._id,
|
|
135
|
-
sourceId: 'validId4',
|
|
136
|
-
mapping: {
|
|
137
|
-
name: 'trustMe',
|
|
138
|
-
thisWorks: true,
|
|
139
|
-
}
|
|
140
|
-
})
|
|
141
|
-
});
|
|
142
|
-
})
|
|
143
|
-
|
|
144
|
-
});
|
package/module-plugin/auther.js
DELETED
|
@@ -1,393 +0,0 @@
|
|
|
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
|
-
const { Delegate } = require('../core');
|
|
29
|
-
const { get } = require('../assertions');
|
|
30
|
-
const _ = require('lodash');
|
|
31
|
-
const { flushDebugLog } = require('../logs');
|
|
32
|
-
const { Credential } = require('./credential');
|
|
33
|
-
const { Entity } = require('./entity');
|
|
34
|
-
const { mongoose } = require('../database/mongoose');
|
|
35
|
-
const { ModuleConstants } = require('./ModuleConstants');
|
|
36
|
-
|
|
37
|
-
class Auther extends Delegate {
|
|
38
|
-
static validateDefinition(definition) {
|
|
39
|
-
if (!definition) {
|
|
40
|
-
throw new Error('Auther definition is required');
|
|
41
|
-
}
|
|
42
|
-
if (!definition.moduleName) {
|
|
43
|
-
throw new Error('Auther definition requires moduleName');
|
|
44
|
-
}
|
|
45
|
-
if (!definition.API) {
|
|
46
|
-
throw new Error('Auther definition requires API class');
|
|
47
|
-
}
|
|
48
|
-
// if (!definition.Credential) {
|
|
49
|
-
// throw new Error('Auther definition requires Credential class');
|
|
50
|
-
// }
|
|
51
|
-
// if (!definition.Entity) {
|
|
52
|
-
// throw new Error('Auther definition requires Entity class');
|
|
53
|
-
// }
|
|
54
|
-
if (!definition.requiredAuthMethods) {
|
|
55
|
-
throw new Error('Auther definition requires requiredAuthMethods');
|
|
56
|
-
} else {
|
|
57
|
-
if (
|
|
58
|
-
definition.API.requesterType ===
|
|
59
|
-
ModuleConstants.authType.oauth2 &&
|
|
60
|
-
!definition.requiredAuthMethods.getToken
|
|
61
|
-
) {
|
|
62
|
-
throw new Error(
|
|
63
|
-
'Auther definition requires requiredAuthMethods.getToken'
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
if (!definition.requiredAuthMethods.getEntityDetails) {
|
|
67
|
-
throw new Error(
|
|
68
|
-
'Auther definition requires requiredAuthMethods.getEntityDetails'
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
if (!definition.requiredAuthMethods.getCredentialDetails) {
|
|
72
|
-
throw new Error(
|
|
73
|
-
'Auther definition requires requiredAuthMethods.getCredentialDetails'
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
if (!definition.requiredAuthMethods.apiPropertiesToPersist) {
|
|
77
|
-
throw new Error(
|
|
78
|
-
'Auther definition requires requiredAuthMethods.apiPropertiesToPersist'
|
|
79
|
-
);
|
|
80
|
-
} else if (definition.Credential) {
|
|
81
|
-
for (const prop of definition.requiredAuthMethods
|
|
82
|
-
.apiPropertiesToPersist?.credential) {
|
|
83
|
-
if (
|
|
84
|
-
!definition.Credential.schema.paths.hasOwnProperty(prop)
|
|
85
|
-
) {
|
|
86
|
-
throw new Error(
|
|
87
|
-
`Auther definition requires Credential schema to have property ${prop}`
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
if (!definition.requiredAuthMethods.testAuthRequest) {
|
|
93
|
-
throw new Error(
|
|
94
|
-
'Auther definition requires requiredAuthMethods.testAuth'
|
|
95
|
-
);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
constructor(params) {
|
|
101
|
-
super(params);
|
|
102
|
-
this.userId = get(params, 'userId', null); // Making this non-required
|
|
103
|
-
const definition = get(params, 'definition');
|
|
104
|
-
Auther.validateDefinition(definition);
|
|
105
|
-
Object.assign(this, definition.requiredAuthMethods);
|
|
106
|
-
if (definition.getEntityOptions) {
|
|
107
|
-
this.getEntityOptions = definition.getEntityOptions;
|
|
108
|
-
}
|
|
109
|
-
if (definition.refreshEntityOptions) {
|
|
110
|
-
this.refreshEntityOptions = definition.refreshEntityOptions;
|
|
111
|
-
}
|
|
112
|
-
this.name = definition.moduleName;
|
|
113
|
-
this.modelName = definition.modelName;
|
|
114
|
-
this.apiClass = definition.API;
|
|
115
|
-
this.CredentialModel =
|
|
116
|
-
definition.Credential || this.getCredentialModel();
|
|
117
|
-
this.EntityModel = definition.Entity || this.getEntityModel();
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
static async getInstance(params) {
|
|
121
|
-
const instance = new this(params);
|
|
122
|
-
if (params.entityId) {
|
|
123
|
-
instance.entity = await instance.EntityModel.findById(
|
|
124
|
-
params.entityId
|
|
125
|
-
);
|
|
126
|
-
instance.credential = await instance.CredentialModel.findById(
|
|
127
|
-
instance.entity.credential
|
|
128
|
-
);
|
|
129
|
-
} else if (params.credentialId) {
|
|
130
|
-
instance.credential = await instance.CredentialModel.findById(
|
|
131
|
-
params.credentialId
|
|
132
|
-
);
|
|
133
|
-
}
|
|
134
|
-
let credential = {};
|
|
135
|
-
let entity = {};
|
|
136
|
-
if (instance.credential) {
|
|
137
|
-
credential = instance.credential.toObject();
|
|
138
|
-
}
|
|
139
|
-
if (instance.entity) {
|
|
140
|
-
entity = instance.entity.toObject();
|
|
141
|
-
}
|
|
142
|
-
const apiParams = {
|
|
143
|
-
...params.definition.env,
|
|
144
|
-
delegate: instance,
|
|
145
|
-
...instance.apiParamsFromCredential(credential),
|
|
146
|
-
...instance.apiParamsFromEntity(entity),
|
|
147
|
-
};
|
|
148
|
-
instance.api = new instance.apiClass(apiParams);
|
|
149
|
-
return instance;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
static getEntityModelFromDefinition(definition) {
|
|
153
|
-
const partialModule = new this({ definition });
|
|
154
|
-
return partialModule.getEntityModel();
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
getName() {
|
|
158
|
-
return this.name;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
apiParamsFromCredential(credential) {
|
|
162
|
-
return _.pick(credential, ...this.apiPropertiesToPersist?.credential);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
apiParamsFromEntity(entity) {
|
|
166
|
-
return _.pick(entity, ...this.apiPropertiesToPersist?.entity);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
getEntityModel() {
|
|
170
|
-
if (!this.EntityModel) {
|
|
171
|
-
const prefix = this.modelName ?? _.upperFirst(this.getName());
|
|
172
|
-
const arrayToDefaultObject = (array, defaultValue) =>
|
|
173
|
-
_.mapValues(_.keyBy(array), () => defaultValue);
|
|
174
|
-
const schema = new mongoose.Schema(
|
|
175
|
-
arrayToDefaultObject(this.apiPropertiesToPersist.entity, {
|
|
176
|
-
type: mongoose.Schema.Types.Mixed,
|
|
177
|
-
trim: true,
|
|
178
|
-
})
|
|
179
|
-
);
|
|
180
|
-
const name = `${prefix}Entity`;
|
|
181
|
-
this.EntityModel =
|
|
182
|
-
Entity.discriminators?.[name] ||
|
|
183
|
-
Entity.discriminator(name, schema);
|
|
184
|
-
}
|
|
185
|
-
return this.EntityModel;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
getCredentialModel() {
|
|
189
|
-
if (!this.CredentialModel) {
|
|
190
|
-
const arrayToDefaultObject = (array, defaultValue) =>
|
|
191
|
-
_.mapValues(_.keyBy(array), () => defaultValue);
|
|
192
|
-
const schema = new mongoose.Schema(
|
|
193
|
-
arrayToDefaultObject(this.apiPropertiesToPersist.credential, {
|
|
194
|
-
type: mongoose.Schema.Types.Mixed,
|
|
195
|
-
trim: true,
|
|
196
|
-
lhEncrypt: true,
|
|
197
|
-
})
|
|
198
|
-
);
|
|
199
|
-
const prefix = this.modelName ?? _.upperFirst(this.getName());
|
|
200
|
-
const name = `${prefix}Credential`;
|
|
201
|
-
this.CredentialModel =
|
|
202
|
-
Credential.discriminators?.[name] ||
|
|
203
|
-
Credential.discriminator(name, schema);
|
|
204
|
-
}
|
|
205
|
-
return this.CredentialModel;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
async getEntitiesForUserId(userId) {
|
|
209
|
-
// Only return non-internal fields. Leverages "select" and "options" to non-excepted fields and a pure object.
|
|
210
|
-
const list = await this.EntityModel.find(
|
|
211
|
-
{ user: userId },
|
|
212
|
-
'-dateCreated -dateUpdated -user -credentials -credential -__t -__v',
|
|
213
|
-
{ lean: true }
|
|
214
|
-
);
|
|
215
|
-
console.log('getEntitiesForUserId list', list, userId);
|
|
216
|
-
return list.map((entity) => ({
|
|
217
|
-
id: entity._id,
|
|
218
|
-
type: this.getName(),
|
|
219
|
-
...entity,
|
|
220
|
-
}));
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
async validateAuthorizationRequirements() {
|
|
224
|
-
const requirements = await this.getAuthorizationRequirements();
|
|
225
|
-
let valid = true;
|
|
226
|
-
if (
|
|
227
|
-
['oauth1', 'oauth2'].includes(requirements.type) &&
|
|
228
|
-
!requirements.url
|
|
229
|
-
) {
|
|
230
|
-
valid = false;
|
|
231
|
-
}
|
|
232
|
-
return valid;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
async getAuthorizationRequirements(params) {
|
|
236
|
-
// TODO: How can this be more helpful both to implement and consume
|
|
237
|
-
// this function must return a dictionary with the following format
|
|
238
|
-
// node only url key is required. Data would be used for Base Authentication
|
|
239
|
-
// let returnData = {
|
|
240
|
-
// url: "callback url for the data or teh redirect url for login",
|
|
241
|
-
// type: one of the types defined in modules/Constants.js
|
|
242
|
-
// data: ["required", "fields", "we", "may", "need"]
|
|
243
|
-
// }
|
|
244
|
-
return this.api.getAuthorizationRequirements();
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
async testAuth(params) {
|
|
248
|
-
let validAuth = false;
|
|
249
|
-
try {
|
|
250
|
-
if (await this.testAuthRequest(this.api)) validAuth = true;
|
|
251
|
-
} catch (e) {
|
|
252
|
-
flushDebugLog(e);
|
|
253
|
-
}
|
|
254
|
-
return validAuth;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
async processAuthorizationCallback(params) {
|
|
258
|
-
let tokenResponse;
|
|
259
|
-
if (this.apiClass.requesterType === ModuleConstants.authType.oauth2) {
|
|
260
|
-
tokenResponse = await this.getToken(this.api, params);
|
|
261
|
-
} else {
|
|
262
|
-
tokenResponse = await this.setAuthParams(this.api, params);
|
|
263
|
-
await this.onTokenUpdate();
|
|
264
|
-
}
|
|
265
|
-
const authRes = await this.testAuth();
|
|
266
|
-
if (!authRes) {
|
|
267
|
-
throw new Error('Authorization failed');
|
|
268
|
-
}
|
|
269
|
-
const entityDetails = await this.getEntityDetails(
|
|
270
|
-
this.api,
|
|
271
|
-
params,
|
|
272
|
-
tokenResponse,
|
|
273
|
-
this.userId
|
|
274
|
-
);
|
|
275
|
-
Object.assign(
|
|
276
|
-
entityDetails.details,
|
|
277
|
-
this.apiParamsFromEntity(this.api)
|
|
278
|
-
);
|
|
279
|
-
await this.findOrCreateEntity(entityDetails);
|
|
280
|
-
return {
|
|
281
|
-
credential_id: this.credential.id,
|
|
282
|
-
entity_id: this.entity.id,
|
|
283
|
-
type: this.getName(),
|
|
284
|
-
};
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
async onTokenUpdate() {
|
|
288
|
-
const credentialDetails = await this.getCredentialDetails(
|
|
289
|
-
this.api,
|
|
290
|
-
this.userId
|
|
291
|
-
);
|
|
292
|
-
Object.assign(
|
|
293
|
-
credentialDetails.details,
|
|
294
|
-
this.apiParamsFromCredential(this.api)
|
|
295
|
-
);
|
|
296
|
-
credentialDetails.details.auth_is_valid = true;
|
|
297
|
-
await this.updateOrCreateCredential(credentialDetails);
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
async receiveNotification(notifier, delegateString, object = null) {
|
|
301
|
-
if (delegateString === this.api.DLGT_TOKEN_UPDATE) {
|
|
302
|
-
await this.onTokenUpdate();
|
|
303
|
-
} else if (delegateString === this.api.DLGT_TOKEN_DEAUTHORIZED) {
|
|
304
|
-
await this.deauthorize();
|
|
305
|
-
} else if (delegateString === this.api.DLGT_INVALID_AUTH) {
|
|
306
|
-
await this.markCredentialsInvalid();
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
async getEntityOptions() {
|
|
311
|
-
throw new Error(
|
|
312
|
-
'Method getEntityOptions() is not defined in the class'
|
|
313
|
-
);
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
async refreshEntityOptions() {
|
|
317
|
-
throw new Error(
|
|
318
|
-
'Method refreshEntityOptions() is not defined in the class'
|
|
319
|
-
);
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
async findOrCreateEntity(entityDetails) {
|
|
323
|
-
const identifiers = get(entityDetails, 'identifiers');
|
|
324
|
-
const details = get(entityDetails, 'details');
|
|
325
|
-
const search = await this.EntityModel.find(identifiers);
|
|
326
|
-
if (search.length > 1) {
|
|
327
|
-
throw new Error(
|
|
328
|
-
'Multiple entities found with the same identifiers: ' +
|
|
329
|
-
JSON.stringify(identifiers)
|
|
330
|
-
);
|
|
331
|
-
} else if (search.length === 0) {
|
|
332
|
-
this.entity = await this.EntityModel.create({
|
|
333
|
-
credential: this.credential.id,
|
|
334
|
-
...details,
|
|
335
|
-
...identifiers,
|
|
336
|
-
});
|
|
337
|
-
} else if (search.length === 1) {
|
|
338
|
-
this.entity = search[0];
|
|
339
|
-
}
|
|
340
|
-
if (this.entity.credential === undefined) {
|
|
341
|
-
this.entity.credential = this.credential.id;
|
|
342
|
-
await this.entity.save();
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
async updateOrCreateCredential(credentialDetails) {
|
|
347
|
-
const identifiers = get(credentialDetails, 'identifiers');
|
|
348
|
-
const details = get(credentialDetails, 'details');
|
|
349
|
-
|
|
350
|
-
if (!this.credential) {
|
|
351
|
-
const credentialSearch = await this.CredentialModel.find(
|
|
352
|
-
identifiers
|
|
353
|
-
);
|
|
354
|
-
if (credentialSearch.length > 1) {
|
|
355
|
-
throw new Error(
|
|
356
|
-
`Multiple credentials found with same identifiers: ${identifiers}`
|
|
357
|
-
);
|
|
358
|
-
} else if (credentialSearch.length === 1) {
|
|
359
|
-
// found exactly one credential with these identifiers
|
|
360
|
-
this.credential = credentialSearch[0];
|
|
361
|
-
} else {
|
|
362
|
-
// found no credential with these identifiers (match none for insert)
|
|
363
|
-
this.credential = { $exists: false };
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
// update credential or create if none was found
|
|
367
|
-
this.credential = await this.CredentialModel.findOneAndUpdate(
|
|
368
|
-
{ _id: this.credential },
|
|
369
|
-
{ $set: { ...identifiers, ...details } },
|
|
370
|
-
{ useFindAndModify: true, new: true, upsert: true }
|
|
371
|
-
);
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
async markCredentialsInvalid() {
|
|
375
|
-
if (this.credential) {
|
|
376
|
-
this.credential.auth_is_valid = false;
|
|
377
|
-
await this.credential.save();
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
async deauthorize() {
|
|
382
|
-
this.api = new this.apiClass();
|
|
383
|
-
if (this.entity?.credential) {
|
|
384
|
-
await this.CredentialModel.deleteOne({
|
|
385
|
-
_id: this.entity.credential,
|
|
386
|
-
});
|
|
387
|
-
this.entity.credential = undefined;
|
|
388
|
-
await this.entity.save();
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
module.exports = { Auther };
|
|
@@ -1,70 +0,0 @@
|
|
|
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 };
|