Skip to content

Commit

Permalink
use rules config
Browse files Browse the repository at this point in the history
  • Loading branch information
gagalago committed Jul 28, 2020
1 parent 710fb26 commit 6c8174a
Show file tree
Hide file tree
Showing 10 changed files with 22,006 additions and 182 deletions.
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
3 changes: 2 additions & 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',
});
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.

29 changes: 22 additions & 7 deletions modifyRule.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const generateTemplate = require('./rules/link');
const fs = require('fs');
const path = require('path');

const RULE_STAGE = 'login_success';
const RULE_NAME = 'auth0-account-link-extension';
Expand All @@ -10,24 +11,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: fs.readFileSync(path.join(__dirname, 'rules/link.js')),
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
Loading

0 comments on commit 6c8174a

Please sign in to comment.