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

use rules config #78

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.9
8
8 changes: 4 additions & 4 deletions api/hooks/post_install.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ module.exports = server => ({
handler: (req, reply) => {
logger.info('Starting rule installation...');

install(req.pre.auth0.rules, {
extensionURL: config('PUBLIC_WT_URL'),
clientID: config('AUTH0_CLIENT_ID'),
clientSecret: config('AUTH0_CLIENT_SECRET')
install(req.pre.auth0, {
accountLinkExtentionUrl: config('PUBLIC_WT_URL'),
accountLinkClientId: config('AUTH0_CLIENT_ID'),
accountLinkSecretId: config('AUTH0_CLIENT_SECRET')
})
.then(() => reply().code(204))
.then(() => {
Expand Down
2 changes: 1 addition & 1 deletion build/bundle.js

Large diffs are not rendered by default.

26 changes: 18 additions & 8 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ const util = require('gulp-util');
const ngrok = require('ngrok');
const nodemon = require('gulp-nodemon');
const { install } = require('./modifyRule');
const managementAdapter = require('./lib/managementAdapter');
const path = require('path');
const { readFile } = require('fs');
const { promisify } = require('bluebird');
const { ManagementClient } = require('auth0');

const { ManagementClientAdapter, getCurrentConfig } = managementAdapter;
async function getCurrentConfig() {
const configFilePath = path.join(__dirname, './server/config.json');
return JSON.parse(await promisify(readFile)(configFilePath));
}

gulp.task('run', () => {
ngrok.connect(3000, (ngrokError, url) => {
Expand Down Expand Up @@ -39,12 +45,16 @@ gulp.task('run', () => {

util.log('Patching rule on tenant.');
getCurrentConfig().then((config) => {
const adapter = new ManagementClientAdapter(config);
install(adapter, {
extensionURL: publicUrl,
username: 'Development',
clientID: config.AUTH0_CLIENT_ID,
clientSecret: config.AUTH0_CLIENT_SECRET
const auth0 = new ManagementClient({
domain: config.AUTH0_DOMAIN,
clientId: config.AUTH0_CLIENT_ID,
clientSecret: config.AUTH0_CLIENT_SECRET,
scope: 'read:rules update:rules delete:rules create:rules read:rules_configs delete:rules_configs update:rules_configs'
});
install(auth0, {
accountLinkExtentionUrl: publicUrl,
accountLinkClientId: config.AUTH0_CLIENT_ID,
accountLinkSecretId: config.AUTH0_CLIENT_SECRET
})
.then(() => {
util.log('Rule patched on tenant.');
Expand Down
60 changes: 0 additions & 60 deletions lib/managementAdapter.js

This file was deleted.

26 changes: 20 additions & 6 deletions modifyRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,38 @@ const persistRule = (api, generatedRule) => (rules = []) => {
const existingRule = rules.find(rule => rule.name === RULE_NAME);

if (existingRule) {
return api.update({ id: existingRule.id }, generatedRule);
return api.rules.update({ id: existingRule.id }, generatedRule);
}

return api.create({ stage: RULE_STAGE, ...generatedRule });
return api.rules.create({ stage: RULE_STAGE, ...generatedRule });
};

const persistConfigRule = (api, config) => (configs = []) =>
Promise.all(
Object.keys(config)
.filter(key => !configs.some(c => c.key === key))
.map(key => api.rulesConfigs.set({ key }, { value: config[key] }))
);

const destroyRule = api => (rules = []) => {
const existingRule = findIn(rules);

if (existingRule) {
api.delete({ id: existingRule.id });
api.rules.delete({ id: existingRule.id });
}
};

const install = (api, config) => {
const rule = { name: RULE_NAME, script: generateTemplate(config), enabled: true };

return api.getAll().then(persistRule(api, rule));
const rule = {
name: RULE_NAME,
script: generateTemplate(config),
enabled: true
};

return Promise.all([
api.rules.getAll().then(persistRule(api, rule)),
api.rulesConfigs.getAll().then(persistConfigRule(api, config))
]);
};
const uninstall = api => api.getAll().then(destroyRule(api));

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "auth0-account-link-extension",
"version": "2.6.0",
"version": "2.7.0",
"description": "Auth0 Account Link Extension",
"main": "index.js",
"engines": {
Expand All @@ -13,7 +13,7 @@
"lint": "eslint .",
"serve:dev": "gulp run",
"client:build": "minify --clean --output dist/assets/link.$npm_package_version.min.css public/css/link.css && minify --clean --output dist/assets/admin.$npm_package_version.min.css public/css/admin.css",
"extension:build": "a0-ext build:server ./webtask.js ./dist && cp ./dist/auth0-account-link.extension.$npm_package_version.js ./build/bundle.js",
"extension:build": "a0-ext build:server ./webtask.js ./dist && cp ./dist/auth0-account-link-sortlist.extension.$npm_package_version.js ./build/bundle.js",
"build": "yarn run client:build && yarn run extension:build"
},
"author": "Auth0",
Expand Down
12 changes: 6 additions & 6 deletions rules/link.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* This file is meant to be included as a string template
*/
module.exports = ({ extensionURL = '', username = 'Unknown', clientID = '', clientSecret = '' }) => {
module.exports = ({ username = 'Auth0 Account Link Extension' }) => {
const template = `function (user, context, callback) {
/**
* This rule has been automatically generated by
Expand All @@ -21,14 +21,14 @@ module.exports = ({ extensionURL = '', username = 'Unknown', clientID = '', clie

var config = {
endpoints: {
linking: '${extensionURL.replace(/\/$/g, '')}',
linking: configuration.accountLinkExtentionUrl.replace(/\\/$/g, ''),
userApi: auth0.baseUrl + '/users',
usersByEmailApi: auth0.baseUrl + '/users-by-email'
},
token: {
clientId: '${clientID}',
clientSecret: '${clientSecret}',
issuer: auth0.domain
clientId: configuration.accountLinkClientId,
clientSecret: configuration.accountLinkSecretId,
issuer: configuration.customDomain || auth0.domain
}
};

Expand Down Expand Up @@ -257,4 +257,4 @@ module.exports = ({ extensionURL = '', username = 'Unknown', clientID = '', clie
}`;

return template;
};
};
8 changes: 1 addition & 7 deletions test/unit/linkRule_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ const generateTemplate = require('../../rules/link');

describe('Generating Link Rule', function() {
const args = {
username: 'foobarbaz',
extensionURL: 'http://someurl.com',
clientID: 'myClientID',
clientSecret: 'myClientSecret'
username: 'foobarbaz'
};

let template;
Expand All @@ -23,8 +20,5 @@ describe('Generating Link Rule', function() {

it('contains replaced variables', function() {
expect(template).to.match(/foobarbaz/);
expect(template).to.match(/someurl\.com/);
expect(template).to.match(/myClientID/);
expect(template).to.match(/myClientSecret/);
});
});
36 changes: 0 additions & 36 deletions test/unit/managementAdapter_test.js

This file was deleted.

61 changes: 40 additions & 21 deletions test/unit/modifyRule_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('Modifying Rules', function() {
let api = getStubApi();

return install(api, {})
.then(api.getAll)
.then(api.rules.getAll)
.then(function(rules) {
expect(rules.map(r => r.id)).to.contain(1);
});
Expand All @@ -18,7 +18,7 @@ describe('Modifying Rules', function() {

return install(api, {})
.then(_ => install(api, {}))
.then(x => api.getAll())
.then(x => api.rules.getAll())
.then(function(rules) {
expect(rules.map(r => r.id)).to.contain(1);
});
Expand All @@ -39,32 +39,51 @@ describe('Modifying Rules', function() {
function getStubApi(fail = false) {
const result = (value) => fail ? Promise.reject(new Error('nope')) : Promise.resolve(value);
let existingRules = [];
let existingRulesConfigs = [];
let currentId = 1;

return {
getAll() {
return Promise.resolve(existingRules);
},
create(rule) {
existingRules.push({ id: currentId, ...rule });
currentId = currentId + 1;
rules: {
getAll() {
return Promise.resolve(existingRules);
},
create(rule) {
existingRules.push({ id: currentId, ...rule });
currentId = currentId + 1;

return result(existingRules);
},
update(existingRule, updatedRule) {
const index = existingRules.findIndex(r => r.id === existingRule.id);
return result(existingRules);
},
update(existingRule, updatedRule) {
const index = existingRules.findIndex(
(r) => r.id === existingRule.id
);

if (index !== -1) {
existingRules[index] = Object.assign({}, existingRules[index], updatedRule);
}
if (index !== -1) {
existingRules[index] = Object.assign(
{},
existingRules[index],
updatedRule
);
}

return result(existingRules);
},
delete(rule) {
existingRules = existingRules.filter(r => r.id !== rule.id);
return result(existingRules);
},
delete(rule) {
existingRules = existingRules.filter((r) => r.id !== rule.id);

return result(existingRules);
}
return result(existingRules);
},
},
rulesConfigs: {
getAll() {
return Promise.resolve(existingRulesConfigs);
},
set({ key }, { value }) {
const config = existingRulesConfigs.any((config) => config.key === key)
if (!config) existingRulesConfigs.push({ key });
return { key, value };
}
},
};
};
});
6 changes: 3 additions & 3 deletions webtask.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"title": "Auth0 Account Link",
"name": "auth0-account-link",
"version": "2.6.0",
"preVersion": "2.5.0",
"version": "2.7.0",
"preVersion": "2.6.0",
"author": "auth0",
"description":
"This extension gives Auth0 customers the ability to allow their users to link their accounts",
Expand All @@ -25,6 +25,6 @@
"onInstallPath": "/.extensions/on-install",
"onUpdatePath": "/.extensions/on-install",
"scopes":
"read:connections read:users read:rules create:rules update:rules delete:rules delete:clients"
"read:connections read:users read:rules create:rules update:rules delete:rules delete:clients read:rules_configs delete:rules_configs update:rules_configs"
}
}
Loading