Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ironclad updates and sub type #91

Merged
merged 3 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions api-module-library/ironclad/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
} = require('@friggframework/module-plugin');
const AuthFields = require('./authFields');
const Config = require('./defaultConfig.json');
const { flushDebugLog } = require('@friggframework/logs');

class Manager extends ModuleManager {
static Entity = Entity;
Expand All @@ -32,11 +33,8 @@ class Manager extends ModuleManager {
instance.credential = await Credential.findById(
instance.entity.credential
);
managerParams.apiKey = instance.credential.apiKey;
} else if (params.credentialId) {
instance.credential = await Credential.findById(
params.credentialId
);
if (instance.credential.subdomain)
managerParams.subdomain = instance.credential.subdomain;
managerParams.apiKey = instance.credential.apiKey;
}
instance.api = await new Api(managerParams);
Expand All @@ -58,13 +56,24 @@ class Manager extends ModuleManager {
async processAuthorizationCallback(params) {
const apiKey = get(params.data, 'apiKey', null);
const subdomain = get(params.data, 'subdomain', null);
const subType = get(params.data, 'subType', null);
this.userId = this.userId || get(params, 'userId');
this.api = new Api({ apiKey, subdomain });
const authRes = await this.testAuth();
if (!authRes) throw new Error('Auth Error');

// Grab identifying information if available.
// Currently not available in the Ironclad API

await this.findOrCreateCredential({
apiKey,
subType,
subdomain,
});
await this.findOrCreateEntity({
apiKey,
subType,
subdomain,
});
return {
credential_id: this.credential.id,
Expand All @@ -74,17 +83,23 @@ class Manager extends ModuleManager {
}

async findOrCreateCredential(params) {
const apiKey = get(params.data, 'apiKey', null);
const apiKey = get(params, 'apiKey', null);
const subdomain = get(params, 'subdomain', null);
const subType = get(params, 'subType', null);

const search = await Entity.find({
user: this.userId,
apiKey,
subType,
subdomain,
});

if (search.length === 0) {
const createObj = {
user: this.userId,
apiKey,
subType,
subdomain,
};
this.credential = await Credential.create(createObj);
} else if (search.length === 1) {
Expand All @@ -100,16 +115,19 @@ class Manager extends ModuleManager {
async findOrCreateEntity(params) {
const apiKey = get(params.data, 'apiKey', null);
const name = get(params, 'name', null);
const subType = get(params, 'subType', null);

const search = await Entity.find({
user: this.userId,
externalId: apiKey,
subType,
});
if (search.length === 0) {
const createObj = {
credential: this.credential.id,
user: this.userId,
name,
subType,
externalId: apiKey,
};
this.entity = await Entity.create(createObj);
Expand All @@ -121,6 +139,15 @@ class Manager extends ModuleManager {
}
}

async testAuth() {
let validAuth = false;
try {
if (await this.api.listWebhooks()) validAuth = true;
} catch (e) {
flushDebugLog(e);
}
return validAuth;
}
async deauthorize() {
this.api = new Api();

Expand Down
70 changes: 68 additions & 2 deletions api-module-library/ironclad/test/manager.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Manager = require('../manager');
const mongoose = require('mongoose');
const config = require('../defaultConfig.json');
const { expect } = require('chai');
require('dotenv').config();

describe(`Should fully test the ${config.label} Manager`, () => {
Expand All @@ -19,9 +20,74 @@ describe(`Should fully test the ${config.label} Manager`, () => {
await mongoose.disconnect();
});

it('should return auth requirements', async () => {
it('getAuthorizationRequirements() should return auth requirements', async () => {
const requirements = await manager.getAuthorizationRequirements();
expect(requirements).exists;
expect(requirements.type).toEqual('apiKey');
expect(requirements.type).to.equal('apiKey');
});
describe('processAuthorizationCallback()', () => {
it('should return client', async () => {
const authRes = await manager.processAuthorizationCallback({
data: {
apiKey: process.env.IRONCLAD_API_KEY,
subdomain: process.env.IRONCLAD_SUBDOMAIN,
},
});
expect(authRes).exists;
expect(authRes).to.have.property('entity_id');
expect(authRes).to.have.property('credential_id');
expect(authRes).to.have.property('type');
});
it('should error if incorrect auth data', async () => {
try {
const authRes = await manager.processAuthorizationCallback({
data: {
apiKey: 'bad',
subdomain: process.env.IRONCLAD_SUBDOMAIN,
},
});
expect(authRes).to.not.exist;
} catch (e) {
expect(e.message).to.contain('Auth Error');
}
});
it('should return store subType', async () => {
const authRes = await manager.processAuthorizationCallback({
data: {
apiKey: process.env.IRONCLAD_API_KEY,
subdomain: process.env.IRONCLAD_SUBDOMAIN,
subType: process.env.IRONCLAD_SUBTYPE,
},
});
expect(authRes).exists;
expect(authRes).to.have.property('entity_id');
expect(authRes).to.have.property('credential_id');
expect(authRes).to.have.property('type');
});
it('should return distinguish between subTypes', async () => {
const authRes = await manager.processAuthorizationCallback({
data: {
apiKey: process.env.IRONCLAD_API_KEY,
subdomain: process.env.IRONCLAD_SUBDOMAIN,
subType: process.env.IRONCLAD_SUBTYPE,
},
});
expect(authRes).exists;
expect(authRes).to.have.property('entity_id');
expect(authRes).to.have.property('credential_id');
expect(authRes).to.have.property('type');
const secondAuthRes = await manager.processAuthorizationCallback({
data: {
apiKey: process.env.IRONCLAD_API_KEY,
subdomain: process.env.IRONCLAD_SUBDOMAIN,
subType: 'fresh',
},
});
expect(secondAuthRes).exists;
expect(secondAuthRes).to.have.property('entity_id');
expect(secondAuthRes).to.have.property('credential_id');
expect(secondAuthRes).to.have.property('type');
expect(secondAuthRes.entity_id).to.not.equal(authRes.entity_id);
});
});
});
1 change: 1 addition & 0 deletions packages/module-plugin/credential.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const schema = new mongoose.Schema(
ref: 'User',
required: true,
},
subType: { type: String },
auth_is_valid: { type: Boolean },
externalId: { type: String }, // Used for lookups, identifying the owner of the credential
},
Expand Down
1 change: 1 addition & 0 deletions packages/module-plugin/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const schema = new mongoose.Schema(
ref: 'Credential',
required: false,
},
subType: { type: String },
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
Expand Down