-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathmodifyRule.js
More file actions
65 lines (54 loc) · 1.72 KB
/
modifyRule.js
File metadata and controls
65 lines (54 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const generateTemplate = require("./rules/link");
const RULE_STAGE = "login_success";
const RULE_NAME = "auth0-account-link-extension";
const CLIENT_SECRET_CONFIG_KEY =
"AUTH0_ACCOUNT_LINKING_EXTENSION_CLIENT_SECRET";
const findIn = (rules) => rules.find((rule) => rule.name === RULE_NAME);
// Allowing partial application to make usage with promises nicer
const persistRule =
(api, generatedRule, clientSecret) =>
(rules = []) => {
const existingRule = rules.find((rule) => rule.name === RULE_NAME);
if (existingRule) {
console.log("Updating existing rule");
return api.client.rules
.update({ id: existingRule.id }, generatedRule)
.then(() =>
api.client.rulesConfigs.set(
{ key: CLIENT_SECRET_CONFIG_KEY },
{ value: clientSecret }
)
);
}
return api.client.rules
.create({ stage: RULE_STAGE, ...generatedRule })
.then(() =>
api.client.rulesConfigs.set(
{ key: CLIENT_SECRET_CONFIG_KEY },
{ value: clientSecret }
)
);
};
const destroyRule =
(api) =>
(rules = []) => {
const existingRule = findIn(rules);
if (existingRule) {
api.client.rules.delete({ id: existingRule.id });
api.client.rulesConfigs.delete({ key: CLIENT_SECRET_CONFIG_KEY });
}
};
const install = (api, config) => {
const { clientSecret } = config;
delete config.clientSecret;
const rule = {
name: RULE_NAME,
script: generateTemplate(config),
enabled: true,
};
return api.client.rules.getAll().then(persistRule(api, rule, clientSecret));
};
const uninstall = (api) => {
return api.client.rules.getAll().then(destroyRule(api));
};
module.exports = { install, uninstall };