diff --git a/scaffold/convert/header.template.js b/scaffold/convert/header.template.js index 150a83e..a057f36 100644 --- a/scaffold/convert/header.template.js +++ b/scaffold/convert/header.template.js @@ -1,4 +1,7 @@ -<% if (before && !session && !oauth) { %>const maybeIncludeAuth = (request, z, bundle) => { +<% if (customBasic) { %> +const { replaceVars } = require('./utils'); +<% } %> +<% if (before && !session && !oauth && !customBasic) { %>const maybeIncludeAuth = (request, z, bundle) => { <% Object.keys(mapping).forEach((mapperKey) => { fields.forEach((field) => { @@ -14,6 +17,18 @@ %> return request; }; +<% } else if (customBasic) { %> +const maybeIncludeAuth = (request, z, bundle) => { + const mapping = { + username: '<%= mapping.username %>', + password: '<%= mapping.password %>' + }; + const username = replaceVars(mapping.username, bundle); + const password = replaceVars(mapping.password, bundle); + const encoded = new Buffer(`${username}:${password}`).toString('base64'); + request.headers.Authorization = `Baisc ${encoded}`; + return request; +}; <% } if (before && session) { %>const maybeIncludeAuth = (request, z, bundle) => { diff --git a/scaffold/convert/index.template.js b/scaffold/convert/index.template.js index 61750a9..4a318f4 100644 --- a/scaffold/convert/index.template.js +++ b/scaffold/convert/index.template.js @@ -7,10 +7,8 @@ const App = { version: require('./package.json').version, platformVersion: require('zapier-platform-core').version, -<% if (hasAuth) { %> +<% if (needsAuth) { %> authentication, -<% } else { %> - authentication: {}, <% } %> beforeRequest: [ <%= BEFORE_REQUESTS %> diff --git a/scripts/test-convert.js b/scripts/test-convert.js index 9f57a3c..0ad83b9 100755 --- a/scripts/test-convert.js +++ b/scripts/test-convert.js @@ -15,6 +15,7 @@ const appsToConvert = [ {id: 82250, name: 'search-oauth'}, {id: 82251, name: 'basic-api-header'}, {id: 82460, name: 'custom-fields'}, + {id: 80444, name: 'custom-baisc'}, // TODO: Add more apps that use different scripting methods, as we start to support them ]; diff --git a/src/tests/utils/convert.js b/src/tests/utils/convert.js index ffe54f6..6ade7be 100644 --- a/src/tests/utils/convert.js +++ b/src/tests/utils/convert.js @@ -120,7 +120,7 @@ describe('convert render functions', () => { const wbDef = definitions.noAuth; return convert.renderIndex(wbDef) .then(string => { - string.should.containEql('authentication: {}'); + string.should.not.containEql('authentication:'); convert.hasAuth(wbDef).should.be.false(); }); }); @@ -131,7 +131,7 @@ describe('convert render functions', () => { return convert.renderAuth(wbDef) .then(string => { const auth = loadAuthModuleFromString(string); - auth.type.should.eql('basic'); + auth.type.should.eql('custom'); auth.fields.should.eql([ { key: 'username', @@ -157,7 +157,7 @@ describe('convert render functions', () => { return convert.renderAuth(wbDef) .then(string => { const auth = loadAuthModuleFromString(string); - auth.type.should.eql('basic'); + auth.type.should.eql('custom'); auth.fields.should.eql([ { key: 'username', diff --git a/src/tests/utils/definitions/basic-scripting.json b/src/tests/utils/definitions/basic-scripting.json index 38192f1..1226419 100644 --- a/src/tests/utils/definitions/basic-scripting.json +++ b/src/tests/utils/definitions/basic-scripting.json @@ -72,8 +72,8 @@ "auth_data": {}, "auth_label": "{{username}}", "auth_mapping": { - "Password": "{{password}}", - "Username": "{{username}}" + "password": "{{password}}", + "username": "{{username}}" }, "auth_type": "Basic Auth", "auth_urls": {}, diff --git a/src/tests/utils/definitions/basic.json b/src/tests/utils/definitions/basic.json index cac8115..1069337 100644 --- a/src/tests/utils/definitions/basic.json +++ b/src/tests/utils/definitions/basic.json @@ -72,8 +72,8 @@ "auth_data": {}, "auth_label": "{{username}}", "auth_mapping": { - "Password": "{{password}}", - "Username": "{{username}}" + "password": "{{password}}", + "username": "{{username}}" }, "auth_type": "Basic Auth", "auth_urls": {}, diff --git a/src/utils/convert.js b/src/utils/convert.js index f9807ed..b6bba0f 100644 --- a/src/utils/convert.js +++ b/src/utils/convert.js @@ -112,7 +112,7 @@ const getAuthType = (definition) => { }; const hasAuth = (definition) => { - return getAuthType(definition) !== 'custom' || !_.isEmpty(definition.auth_fields); + return getAuthType(definition) !== 'custom' && !_.isEmpty(definition.auth_fields); }; const renderField = (definition, key, indent = 0) => { @@ -216,6 +216,10 @@ const renderAuthTemplate = (authType, definition) => { const testTriggerKey = getTestTriggerKey(definition); const { hasGetConnectionLabelScripting } = getAuthMetaData(definition); + if (authType === 'basic' && !_.isEmpty(definition.general.auth_mapping)) { + authType = 'custom'; + } + const templateContext = { TEST_TRIGGER_MODULE: `./triggers/${snakeCase(testTriggerKey)}`, TYPE: authType, @@ -463,11 +467,17 @@ const getMetaData = (definition) => { }); }); - const hasBefore = (type === 'api-header' || type === 'api-query' || type === 'session' || type === 'oauth2' || type === 'oauth2-refresh'); - const hasAfter = (type === 'session'); + const needsAuth = hasAuth(definition); + const isCustomBasic = (needsAuth && type === 'basic' && !_.isEmpty(definition.general.auth_mapping)); + const hasBefore = needsAuth && ( + type === 'api-header' || type === 'api-query' || type === 'session' || + type === 'oauth2' || type === 'oauth2-refresh' || isCustomBasic + ); + const hasAfter = (needsAuth && type === 'session'); const fieldsOnQuery = (authPlacement === 'params' || type === 'api-query'); - const isSession = (type === 'session'); - const isOAuth = (type === 'oauth2' || type === 'oauth2-refresh'); + const isSession = (needsAuth && type === 'session'); + const isOAuth = needsAuth && (type === 'oauth2' || type === 'oauth2-refresh'); + const needsLegacyScriptingRunner = isSession || hasAnyScriptingMethods; return { @@ -477,6 +487,7 @@ const getMetaData = (definition) => { fieldsOnQuery, isSession, isOAuth, + isCustomBasic, needsLegacyScriptingRunner, }; }; @@ -489,6 +500,7 @@ const getHeader = (definition) => { hasAfter, isSession, isOAuth, + isCustomBasic, fieldsOnQuery, } = getMetaData(definition); @@ -498,6 +510,7 @@ const getHeader = (definition) => { after: hasAfter, session: isSession, oauth: isOAuth, + customBasic: isCustomBasic, fields: Object.keys(definition.auth_fields), mapping: _.get(definition, ['general', 'auth_mapping'], {}), query: fieldsOnQuery, @@ -602,15 +615,20 @@ const writeStep = (type, definition, key, legacyApp, newAppDir) => { }; // render the authData used in the trigger/search/create test code -const renderAuthData = (authType) => { +const renderAuthData = (definition) => { + const authType = getAuthType(definition); let result; switch (authType) { - case 'basic': + case 'basic': { + let lines = _.map(definition.auth_fields, (field, key) => { + const upperKey = key.toUpperCase(); + return ` ${key}: process.env.${upperKey}`; + }); result = `{ - username: process.env.USERNAME, - password: process.env.PASSWORD +${lines.join(',\n')} }`; break; + } case 'oauth2': result = `{ access_token: process.env.ACCESS_TOKEN @@ -643,9 +661,8 @@ const renderAuthData = (authType) => { }; const renderStepTest = (type, definition, key, legacyApp) => { - const authType = getAuthType(legacyApp); const label = definition.label || _.capitalize(key); - const authData = renderAuthData(authType); + const authData = renderAuthData(legacyApp); const templateContext = { KEY: key, LABEL: label, @@ -678,7 +695,7 @@ const writeUtils = (newAppDir) => { }; const renderIndex = (legacyApp) => { - const _hasAuth = hasAuth(legacyApp); + const needsAuth = hasAuth(legacyApp); const templateContext = { HEADER: '', TRIGGERS: '', @@ -686,7 +703,7 @@ const renderIndex = (legacyApp) => { CREATES: '', BEFORE_REQUESTS: getBeforeRequests(legacyApp), AFTER_RESPONSES: getAfterResponses(legacyApp), - hasAuth: _hasAuth + needsAuth, }; return getHeader(legacyApp) @@ -695,7 +712,7 @@ const renderIndex = (legacyApp) => { const importLines = []; - if (_hasAuth) { + if (needsAuth) { importLines.push("const authentication = require('./authentication');"); }