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

Commit

Permalink
Merge pull request #185 from zapier/convert-auth-scripting
Browse files Browse the repository at this point in the history
zapier convert: Generate different auth code based on scripting
  • Loading branch information
eliangcs committed Nov 28, 2017
2 parents 6bf0d8d + 7b4f7e6 commit 303cb06
Show file tree
Hide file tree
Showing 14 changed files with 1,037 additions and 310 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"readable-stream": "2.2.2",
"semver": "5.3.0",
"string-length": "1.0.1",
"strip-comments": "0.4.4",
"tar-stream": "1.5.2",
"through2": "2.0.3",
"tmp": "0.0.31",
Expand All @@ -67,6 +68,7 @@
"mocha": "3.4.2",
"ngrok": "2.2.10",
"node-watch": "0.5.4",
"require-from-string": "2.0.1",
"should": "11.2.1",
"yamljs": "0.3.0"
},
Expand Down
11 changes: 5 additions & 6 deletions scaffold/convert/header.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
});
%>
return request;
}
};
<% }

if (before && session) { %>const maybeIncludeAuth = (request, z, bundle) => {
Expand All @@ -24,15 +24,15 @@ if (before && session) { %>const maybeIncludeAuth = (request, z, bundle) => {
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) { %>
Expand All @@ -42,8 +42,7 @@ const maybeRefresh = (response, z, bundle) => {
}

return response;
}

};
<% }

if (session) { %>
Expand Down Expand Up @@ -71,5 +70,5 @@ const getSessionKey = (z, bundle) => {
sessionKey: firstKeyValue
};
});
}
};
<% } %>
8 changes: 5 additions & 3 deletions scaffold/convert/index.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
const App = {
version: require('./package.json').version,
platformVersion: require('zapier-platform-core').version,

authentication: <%= AUTHENTICATION %>,

<% if (hasAuth) { %>
authentication,
<% } else { %>
authentication: {},
<% } %>
beforeRequest: [
<%= BEFORE_REQUESTS %>
],
Expand Down
47 changes: 0 additions & 47 deletions scaffold/convert/oauth2-refresh.template.js

This file was deleted.

152 changes: 148 additions & 4 deletions scaffold/convert/oauth2.template.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,120 @@
{
const testTrigger = require('<%= TEST_TRIGGER_MODULE %>');
<% if (hasPreOAuthTokenScripting && hasPostOAuthTokenScripting) { %>
const getAccessToken = (z, bundle) => {
const scripting = require('./scripting');
const legacyScriptingRunner = require('zapier-platform-legacy-scripting-runner')(scripting);

bundle._legacyUrl = '<%= ACCESS_TOKEN_URL %>';

// Do a pre_oauthv2_token() from scripting.
const preOAuth2TokenEvent = {
name: 'auth.oauth2.token.pre',
};
return legacyScriptingRunner.runEvent(preOAuth2TokenEvent, z, bundle)
.then((preOAuth2TokenResult) => z.request(preOAuth2TokenResult))
.then((response) => {
if (response.status !== 200) {
throw new Error(`Unable to fetch access token: ${response.content}`);
}

// Do a post_oauthv2_token() from scripting.
const postOAuth2TokenEvent = {
name: 'auth.oauth2.token.post',
response,
};
return legacyScriptingRunner.runEvent(postOAuth2TokenEvent, z, bundle);
});
};
<% } else if (hasPreOAuthTokenScripting && !hasPostOAuthTokenScripting) { %>
const getAccessToken = (z, bundle) => {
const scripting = require('./scripting');
const legacyScriptingRunner = require('zapier-platform-legacy-scripting-runner')(scripting);

bundle._legacyUrl = '<%= ACCESS_TOKEN_URL %>';

// Do a pre_oauthv2_token() from scripting.
const preOAuth2TokenEvent = {
name: 'auth.oauth2.token.pre',
};
return legacyScriptingRunner.runEvent(preOAuth2TokenEvent, z, bundle)
.then((preOAuth2TokenResult) => z.request(preOAuth2TokenResult))
.then((response) => {
if (response.status !== 200) {
throw new Error(`Unable to fetch access token: ${response.content}`);
}
return z.JSON.parse(response.content);
});
};
<% } else if (!hasPreOAuthTokenScripting && hasPostOAuthTokenScripting) { %>
const getAccessToken = (z, bundle) => {
const scripting = require('./scripting');
const legacyScriptingRunner = require('zapier-platform-legacy-scripting-runner')(scripting);

bundle._legacyUrl = '<%= ACCESS_TOKEN_URL %>';

const responsePromise = z.request({
method: 'POST',
url: bundle._legacyUrl,
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',
}
});
return responsePromise.then((response) => {
// Do a post_oauthv2_token() from scripting.
const postOAuth2TokenEvent = {
name: 'auth.oauth2.token.post',
response,
};
return legacyScriptingRunner.runEvent(postOAuth2TokenEvent, z, bundle);
});
<% }

if (withRefresh && hasPreOAuthRefreshScripting) { %>
const refreshAccessToken = (z, bundle) => {
const scripting = require('./scripting');
const legacyScriptingRunner = require('zapier-platform-legacy-scripting-runner')(scripting);

bundle._legacyUrl = '<%= REFRESH_TOKEN_URL %>';

// Do a pre_oauthv2_refresh() from scripting.
const preOAuth2RefreshEvent = {
name: 'auth.oauth2.refresh.pre',
};
return legacyScriptingRunner.runEvent(preOAuth2RefreshEvent, z, bundle)
.then((preOAuth2RefreshResult) => z.request(preOAuth2RefreshResult))
.then((response) => {
if (response.status !== 200) {
throw new Error(`Unable to fetch access token: ${response.content}`);
}

return z.JSON.parse(response.content);
});
};
<% }

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

// Do a get_connection_label() from scripting.
const connectionLabelEvent = {
name: 'auth.connectionLabel',
};
return legacyScriptingRunner.runEvent(connectionLabelEvent, z, bundle);
};
<% } %>
const authentication = {
// TODO: just an example stub - you'll need to complete
type: 'oauth2',
test: AuthTest.operation.perform,
test: testTrigger.operation.perform,
oauth2Config: {
authorizeUrl: {
method: 'GET',
Expand All @@ -13,6 +126,9 @@
response_type: 'code'
}
},
<% if (hasPreOAuthTokenScripting || hasPostOAuthTokenScripting) { %>
getAccessToken: getAccessToken,
<% } else { %>
getAccessToken: {
method: 'POST',
url: '<%= ACCESS_TOKEN_URL %>',
Expand All @@ -27,7 +143,35 @@
'Content-Type': 'application/x-www-form-urlencoded'
}
},
scope: '<%= SCOPE %>'
<% }

if (withRefresh && hasPreOAuthRefreshScripting) { %>
refreshAccessToken: refreshAccessToken,
<% } else if (withRefresh) { %>
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 %>',
<% if (withRefresh) { %>
autoRefresh: true
<% } %>
},
<% if (hasGetConnectionLabelScripting) { %>
connectionLabel: getConnectionLabel
<% } else { %>
connectionLabel: '<%= CONNECTION_LABEL %>'
}
<% } %>
};

module.exports = authentication;
53 changes: 49 additions & 4 deletions scaffold/convert/session.template.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,57 @@
{
const testTrigger = require('<%= TEST_TRIGGER_MODULE %>');

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,
};
});
};
<% if (hasGetConnectionLabelScripting) { %>
const getConnectionLabel = (z, bundle) => {
const scripting = require('./scripting');
const legacyScriptingRunner = require('zapier-platform-legacy-scripting-runner')(scripting);

// Do a get_connection_label() from scripting.
const connectionLabelEvent = {
name: 'auth.connectionLabel',
};
return legacyScriptingRunner.runEvent(connectionLabelEvent, z, bundle);
};
<% } %>
const authentication = {
// TODO: just an example stub - you'll need to complete
type: 'session',
test: AuthTest.operation.perform,
test: testTrigger.operation.perform,
fields: [
<%= FIELDS %>
<%= FIELDS %>
],
sessionConfig: {
perform: getSessionKey
},
<% if (hasGetConnectionLabelScripting) { %>
connectionLabel: getConnectionLabel
<% } else { %>
connectionLabel: '<%= CONNECTION_LABEL %>'
}
<% } %>
};

module.exports = authentication;
28 changes: 28 additions & 0 deletions scaffold/convert/simple-auth.template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const testTrigger = require('<%= TEST_TRIGGER_MODULE %>');
<% if (hasGetConnectionLabelScripting) { %>
const getConnectionLabel = (z, bundle) => {
const scripting = require('./scripting');
const legacyScriptingRunner = require('zapier-platform-legacy-scripting-runner')(scripting);

// Do a get_connection_label() from scripting.
const connectionLabelEvent = {
name: 'auth.connectionLabel',
};
return legacyScriptingRunner.runEvent(connectionLabelEvent, z, bundle);
};
<% } %>
const authentication = {
// TODO: just an example stub - you'll need to complete
type: '<%= TYPE %>',
test: testTrigger.operation.perform,
fields: [
<%= FIELDS %>
],
<% if (hasGetConnectionLabelScripting) { %>
connectionLabel: getConnectionLabel
<% } else { %>
connectionLabel: '<%= CONNECTION_LABEL %>'
<% } %>
};

module.exports = authentication;
3 changes: 2 additions & 1 deletion scripts/test-convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const childProcess = utils.promisifyAll(require('child_process'));
const appsToConvert = [
{id: 80082, name: 'simple-basic-auth'},
{id: 80182, name: 'trigger-session-auth'},
// TODO: Add more apps that require scripting and different auths, once zapier-platform-legacy-scripting-runner is live
{id: 82052, name: 'simple-oauth'},
// TODO: Add more apps that require different scripting methods, as we start to support them
];

const testConvertedApp = (appToConvert, rootTmpDir) => {
Expand Down
Loading

0 comments on commit 303cb06

Please sign in to comment.