Skip to content

Commit

Permalink
APIKey identication resolved #1
Browse files Browse the repository at this point in the history
  • Loading branch information
yushkevich committed Aug 12, 2019
1 parent e5b0590 commit 5e3c751
Show file tree
Hide file tree
Showing 19 changed files with 141 additions and 47 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.0

* (New) Early Access version of NetLicensing / Zapier integration

## 0.9.1

* API Key authentication added
53 changes: 53 additions & 0 deletions authentication/Custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const constants = require('../config/Constants');

const test = (z) => {
// Normally you want to make a request to an endpoint that is either specifically designed to test auth, or one that
// every user will have access to, such as an account or profile endpoint like /me.
// In this example, we'll hit httpbin, which validates the Authorization Header against the arguments passed in the
// URL path

// This method can return any truthy value to indicate the credentials are valid.
// Raise an error to show
return z.request({
url: `${constants.BASE_HOST + constants.BASE_PATH}/utility/licensingModels`,
method: 'GET',
headers: {
Accept: 'application/json',
},
params: {},
}).then((response) => {
if (response.status === 401) {
throw new Error(constants.authentication.LOGIN_FAILED_TEXT);
} else if (response.status >= 500) {
throw new Error(constants.authentication.SERVICE_UNAVAILABLE_TEXT);
}
return response;
});
};

module.exports = {
type: 'custom',
// The test method allows Zapier to verify that the credentials a user provides are valid. We'll execute this
// method whenver a user connects their account for the first time.
test,
// assuming "username" is a key returned from the test
// connectionLabel: '{{username}}',
fields: [
{
key: 'username',
type: 'string',
required: false,
},
{
key: 'password',
type: 'password',
required: false,
},
{
key: 'apiKey',
type: 'string',
required: false,
helpText: 'You could authenticate by Username and Password or API Key',
},
],
};
9 changes: 3 additions & 6 deletions creates/CreateLicensee.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
const { LicenseeService, Licensee, Context, Constants } = require('netlicensing-client/dist/netlicensing-client.node');
const { LicenseeService, Licensee } = require('netlicensing-client/dist/netlicensing-client.node');
const constants = require('../config/Constants');
const getContext = require('../utils/getContext');

const createLicensee = async (z, bundle) => {
const context = new Context();
context.setUsername(bundle.authData.username);
context.setPassword(bundle.authData.password);
context.setSecurityMode(Constants.BASIC_AUTHENTICATION);
const context = getContext(bundle);

let licensee = new Licensee();

if (bundle.inputData.name !== undefined) {
licensee.setName(bundle.inputData.name);
}
Expand Down
8 changes: 3 additions & 5 deletions creates/CreateProduct.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const { ProductService, Product, Context, Constants } = require('netlicensing-client/dist/netlicensing-client.node');
const { ProductService, Product } = require('netlicensing-client/dist/netlicensing-client.node');
const constants = require('../config/Constants');
const getContext = require('../utils/getContext');

const createProduct = async (z, bundle) => {
const context = new Context();
context.setUsername(bundle.authData.username);
context.setPassword(bundle.authData.password);
context.setSecurityMode(Constants.BASIC_AUTHENTICATION);
const context = getContext(bundle);

let product = new Product()
.setName(bundle.inputData.name);
Expand Down
22 changes: 19 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const { Token, TokenService, Constants, Context } = require('netlicensing-client/dist/netlicensing-client.node');

const { version: platformVersion } = require('zapier-platform-core');
const { version } = require('./package.json');

// auth (basic)
const authentication = require('./authentication/BasicAuth');
// auth
// const authentication = require('./authentication/BasicAuth');
const authentication = require('./authentication/Custom');

// triggers
const newProduct = require('./triggers/NewProduct');
const newLicensee = require('./triggers/NewLicensee');
Expand All @@ -16,6 +20,18 @@ const createLicensee = require('./creates/CreateLicensee');
const searchOrCreateProduct = require('./search_or_creates/SearchOrCreateProduct');
const searchOrCreateLicensee = require('./search_or_creates/SearchOrCreateLicensee');

const addAuthToHeader = (request, z, bundle) => {
if (bundle.authData.username.trim() && bundle.authData.password.trim()) {
request.headers.Authorization = `Basic ${Buffer.from(`${bundle.authData.username}:${bundle.authData.password}`)
.toString('base64')}`;
} else if (bundle.authData.apiKey.trim()) {
request.headers.Authorization = `Basic ${Buffer.from(`apiKey:${bundle.authData.apiKey}`)
.toString('base64')}`;
}

return request;
};

// We can roll up all our behaviors in an App.
const App = {
// This is just shorthand to reference the installed dependencies you have. Zapier will
Expand All @@ -28,7 +44,7 @@ const App = {
authentication,

// beforeRequest & afterResponse are optional hooks into the provided HTTP client
beforeRequest: [],
beforeRequest: [addAuthToHeader],

afterResponse: [],

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "netlicensing",
"version": "0.9.0",
"version": "0.9.1",
"description": "NetLicensing / Zapier integration",
"repository": "github:Labs64/NetLicensing-Zapier",
"homepage": "https://netlicensing.io",
Expand Down
10 changes: 3 additions & 7 deletions searches/FindLicensee.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
const { LicenseeService, Context, Constants } = require('netlicensing-client/dist/netlicensing-client.node');
const { LicenseeService } = require('netlicensing-client/dist/netlicensing-client.node');
const constants = require('../config/Constants');
const getContext = require('../utils/getContext');

const getLicensee = async (z, bundle) => {
const context = new Context();
context.setUsername(bundle.authData.username);
context.setPassword(bundle.authData.password);
context.setSecurityMode(Constants.BASIC_AUTHENTICATION);

const context = getContext(bundle);
const licensee = await LicenseeService.get(context, bundle.inputData.number);

return [licensee.getProperties()];
};

Expand Down
10 changes: 3 additions & 7 deletions searches/FindProduct.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
const { ProductService, Context, Constants } = require('netlicensing-client/dist/netlicensing-client.node');
const { ProductService } = require('netlicensing-client/dist/netlicensing-client.node');
const constants = require('../config/Constants');
const getContext = require('../utils/getContext');

const getProduct = async (z, bundle) => {
const context = new Context();
context.setUsername(bundle.authData.username);
context.setPassword(bundle.authData.password);
context.setSecurityMode(Constants.BASIC_AUTHENTICATION);

const context = getContext(bundle);
const product = await ProductService.get(context, bundle.inputData.number);

return [product.getProperties()];
};

Expand Down
37 changes: 37 additions & 0 deletions test/mock/authentication/MockApiKeyAuth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const nock = require('nock');

const zapier = require('zapier-platform-core');

const constants = require('../../../config/Constants');

// Use this to make test calls into your app:
const App = require('../../../index');

const appTester = zapier.createAppTester(App);

describe('Authentication', () => {
const apiMock = nock(constants.BASE_HOST);
const authData = {
username: '',
password: '',
apiKey: 'testApiKey',
};

afterEach(() => {
nock.cleanAll();
});

it('Success auth', (done) => {
const bundle = Object.assign({}, { authData });

apiMock.get(`${constants.BASE_PATH}/utility/licensingModels`)
.reply(200);

appTester(App.authentication.test, bundle)
.then((response) => {
response.status.should.eql(200);
done();
})
.catch(done);
});
});
3 changes: 1 addition & 2 deletions test/mock/authentication/MockBasicAuth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const nock = require('nock');
const should = require('should');

const zapier = require('zapier-platform-core');

Expand All @@ -13,7 +12,7 @@ const appTester = zapier.createAppTester(App);
describe('Authentication', () => {
const apiMock = nock(constants.BASE_HOST);
const authData = {
email: constants.NLIC_USERNAME,
username: constants.NLIC_USERNAME,
password: constants.NLIC_PASSWORD,
};

Expand Down
2 changes: 0 additions & 2 deletions test/mock/triggers/MockNewLecensee.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ const zapier = require('zapier-platform-core/index');

const nock = require('nock');

const should = require('should');

const constants = require('../../../config/Constants');

const App = require('../../../index');
Expand Down
2 changes: 0 additions & 2 deletions test/mock/triggers/MockNewProduct.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ const zapier = require('zapier-platform-core/index');

const nock = require('nock');

const should = require('should');

const constants = require('../../../config/Constants');

const App = require('../../../index');
Expand Down
2 changes: 0 additions & 2 deletions test/production/creates/CreateLicensee.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const should = require('should/should');

const zapier = require('zapier-platform-core/index');

const constants = require('../../../config/Constants');
Expand Down
2 changes: 0 additions & 2 deletions test/production/creates/CreateProduct.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const should = require('should/should');

const zapier = require('zapier-platform-core/index');

const constants = require('../../../config/Constants');
Expand Down
2 changes: 0 additions & 2 deletions test/production/searches/FindLicensee.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const should = require('should/should');

const zapier = require('zapier-platform-core/index');

const App = require('../../../index');
Expand Down
2 changes: 0 additions & 2 deletions test/production/searches/FindProduct.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const should = require('should/should');

const zapier = require('zapier-platform-core/index');

const App = require('../../../index');
Expand Down
2 changes: 0 additions & 2 deletions test/production/triggers/NewLecensee.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const should = require('should/should');

const zapier = require('zapier-platform-core/index');

const App = require('../../../index');
Expand Down
2 changes: 0 additions & 2 deletions test/production/triggers/NewProduct.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const should = require('should/should');

const zapier = require('zapier-platform-core/index');

const App = require('../../../index');
Expand Down
14 changes: 14 additions & 0 deletions utils/getContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { Context, Constants } = require('netlicensing-client/dist/netlicensing-client.node');

module.exports = (bundle) => {
const context = new Context();
if (bundle.authData.username.trim() && bundle.authData.password.trim()) {
context.setUsername(bundle.authData.username);
context.setPassword(bundle.authData.password);
context.setSecurityMode(Constants.BASIC_AUTHENTICATION);
} else {
context.setApiKey(bundle.authData.apiKey);
context.setSecurityMode(Constants.APIKEY_IDENTIFICATION);
}
return context;
};

0 comments on commit 5e3c751

Please sign in to comment.