Skip to content
This repository has been archived by the owner on Jun 27, 2019. It is now read-only.

Adding more auth conversion templates #167

Merged
merged 9 commits into from
Oct 31, 2017
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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"version": "npm run docs && npm run gen-completions && git add docs/* goodies/* README.md",
"postversion": "git push && git push --tags && npm publish",
"prepublish": "npm run build",
"build": "rm -rf lib && node_modules/babel-cli/bin/babel.js src -d lib",
"watch": "rm -rf lib && node_modules/babel-cli/bin/babel.js --watch src -d lib",
"lint": "node_modules/.bin/eslint zapier.js src test",
"build": "rm -rf lib && node_modules/babel-cli/bin/babel.js src -d lib --copy-files",
"watch": "rm -rf lib && node_modules/babel-cli/bin/babel.js --watch src -d lib --copy-files",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just want to clarify. What does --copy-files option do? I compared the results with or without --copy-files. They look the same to me.

Copy link
Contributor Author

@BrunoBernardino BrunoBernardino Oct 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It copies the definitions/*.json files inside src/tests/utils, otherwise only the .js files are passed along, at least for me.

"lint": "node_modules/.bin/eslint zapier.js src",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reasons why we don't want to lint test code?

Copy link
Contributor Author

@BrunoBernardino BrunoBernardino Oct 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're still linting the test code, since it's inside src/tests/*, this wasn't doing anything as there's no /test directory.

"lint-snippets": "node_modules/.bin/eslint snippets --rule 'no-unused-vars: 0'",
"test": "npm run build && node_modules/mocha/bin/mocha -t 15000 --recursive lib/tests && npm run lint && npm run lint-snippets",
"zapier": "zapier.js",
Expand Down
75 changes: 75 additions & 0 deletions scaffold/convert/header.template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<% if (before && !session && !oauth) { %>const maybeIncludeAuth = (request, z, bundle) => {
<%
Object.keys(mapping).forEach((mapperKey) => {
fields.forEach((field) => {
if (mapping[mapperKey].indexOf(`{{${field}}}`) !== -1) {
if (query) { %>
request.params['<%= mapperKey %>'] = bundle.authData['<%= field %>'];
<% } else { %>
request.headers['<%= mapperKey %>'] = bundle.authData['<%= field %>'];
<% }
}
});
});
%>
return request;
}
<% }

if (before && session) { %>const maybeIncludeAuth = (request, z, bundle) => {
<%
if (query) { %>
request.params['<%= Object.keys(mapping)[0] %>'] = bundle.authData.sessionKey;;
<% } else { %>
request.headers['<%= Object.keys(mapping)[0] %>'] = bundle.authData.sessionKey;
<% } %>
return request;
}
<% }

if (before && oauth) { %>const maybeIncludeAuth = (request, z, bundle) => {

request.headers.Authorization = `Bearer ${bundle.authData.access_token}`;

return request;
}
<% }

if (after) { %>
const maybeRefresh = (response, z, bundle) => {
if (response.status === 401 || response.status === 403) {
throw new z.errors.RefreshAuthError('Session key needs refreshing.');
}

return response;
}

<% }

if (session) { %>
const getSessionKey = (z, bundle) => {
const scripting = require('../scripting');
const legacyScriptingRunner = require('zapier-platform-legacy-scripting-runner')(scripting);

// Do a get_session_info() from scripting.
const getSessionEvent = {
name: 'auth.session'
};
return legacyScriptingRunner.runEvent(getSessionEvent, z, bundle)
.then((getSessionResult) => {
// IMPORTANT NOTE:
// WB apps in scripting's get_session_info() allowed to return any object and that would be
// added to the authData, but CLI apps require you to specifically define those.
// That means that if you return more than one key from your scripting's get_session_info(),
// you might need to manually tweak this method to return that value at the end of this method,
// and also add more fields to the authentication definition.

const resultKeys = Object.keys(getSessionResult);
const firstKeyValue = (getSessionResult && resultKeys.length > 0) ? getSessionResult[resultKeys[0]] : getSessionResult;

return {
sessionKey: firstKeyValue
};
});
}
<% } %>
10 changes: 10 additions & 0 deletions scaffold/convert/index.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@

<%= REQUIRES %>

<%= HEADER %>

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

authentication: <%= AUTHENTICATION %>,

beforeRequest: [
<%= BEFORE_REQUESTS %>
],

afterResponse: [
<%= AFTER_RESPONSES %>
],

resources: {
},

Expand Down
47 changes: 47 additions & 0 deletions scaffold/convert/oauth2-refresh.template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
// TODO: just an example stub - you'll need to complete
type: 'oauth2',
test: AuthTest.operation.perform,
oauth2Config: {
authorizeUrl: {
method: 'GET',
url: '<%= AUTHORIZE_URL %>',
params: {
client_id: '{{process.env.CLIENT_ID}}',
state: '{{bundle.inputData.state}}',
redirect_uri: '{{bundle.inputData.redirect_uri}}',
response_type: 'code'
}
},
getAccessToken: {
method: 'POST',
url: '<%= ACCESS_TOKEN_URL %>',
body: {
code: '{{bundle.inputData.code}}',
client_id: '{{process.env.CLIENT_ID}}',
client_secret: '{{process.env.CLIENT_SECRET}}',
redirect_uri: '{{bundle.inputData.redirect_uri}}',
grant_type: 'authorization_code'
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
},
refreshAccessToken: {
method: 'POST',
url: '<%= REFRESH_TOKEN_URL %>',
body: {
refresh_token: '{{bundle.authData.refresh_token}}',
client_id: '{{process.env.CLIENT_ID}}',
client_secret: '{{process.env.CLIENT_SECRET}}',
grant_type: 'refresh_token'
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
},
scope: '<%= SCOPE %>',
autoRefresh: true
},
connectionLabel: '<%= CONNECTION_LABEL %>'
}
10 changes: 5 additions & 5 deletions scaffold/convert/oauth2.template.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
// TODO: just an example stub - you'll need to complete
type: 'oauth2',
test: {
url: 'http://www.EXAMPLE.com/auth'
},
test: AuthTest.operation.perform,
oauth2Config: {
authorizeUrl: {
method: 'GET',
Expand All @@ -28,6 +26,8 @@
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
}
},
scope: '<%= SCOPE %>'
},
connectionLabel: '<%= CONNECTION_LABEL %>'
}
17 changes: 8 additions & 9 deletions scaffold/convert/package.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
"test": "node node_modules/mocha/bin/mocha --recursive"
},
"engines": {
"node": ">=4.3.0",
"npm": ">=2.0.0"
"node": ">=6.10.0",
"npm": ">=3.0.0"
},
"dependencies": {
"lodash": "4.15.0",
"zapier-platform-core": "<%= ZAPIER_CORE_VERSION %>"
"lodash": "4.17.4",
<%= DEPENDENCIES %>
},
"devDependencies": {
"babel": "6.5.2",
"babel-eslint": "6.1.2",
"mocha": "2.5.3",
"should": "9.0.2"
}
"mocha": "4.0.1",
"should": "13.1.2"
},
"private": true
}
29 changes: 29 additions & 0 deletions scaffold/convert/scripting.template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

// START: HEADER -- AUTOMATICALLY ADDED FOR COMPATIBILITY - v<%= VERSION %>
const _ = require('lodash');
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
const crypto = require('crypto');
const async = require('async');
const moment = require('moment-timezone');
const DOMParser = require('zapier-platform-legacy-scripting-runner/dom-parser');
const XMLSerializer = require('zapier-platform-legacy-scripting-runner/xml-serializer');
const atob = require('zapier-platform-legacy-scripting-runner/atob');
const btoa = require('zapier-platform-legacy-scripting-runner/btoa');
const z = require('zapier-platform-legacy-scripting-runner/z');
const $ = require('zapier-platform-legacy-scripting-runner/$');
const {
ErrorException,
HaltedException,
StopRequestException,
ExpiredAuthException,
RefreshTokenException,
InvalidSessionException,
} = require('zapier-platform-legacy-scripting-runner/exceptions');
// END: HEADER -- AUTOMATICALLY ADDED FOR COMPATIBILITY - v<%= VERSION %>

<%= CODE %>

// START: FOOTER -- AUTOMATICALLY ADDED FOR COMPATIBILITY - v<%= VERSION %>
module.exports = Zap;
// END: FOOTER -- AUTOMATICALLY ADDED FOR COMPATIBILITY - v<%= VERSION %>
12 changes: 12 additions & 0 deletions scaffold/convert/session.template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
// TODO: just an example stub - you'll need to complete
type: 'session',
test: AuthTest.operation.perform,
fields: [
<%= FIELDS %>
],
sessionConfig: {
perform: getSessionKey
},
connectionLabel: '<%= CONNECTION_LABEL %>'
}
1 change: 1 addition & 0 deletions src/tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('utils', () => {
global._babelPolyfill.should.eql(true);
});

// For some reason, Travis dies on this one
it.skip('should print a nice little table', () => {
const table = utils.makeTable(
[{id: 123, title: 'hello'}, {id: 456, title: 'world'}],
Expand Down
Loading