-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateAuth0Callbacks.ts
More file actions
115 lines (100 loc) · 3.05 KB
/
Copy pathupdateAuth0Callbacks.ts
File metadata and controls
115 lines (100 loc) · 3.05 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// DENO
const checkEnvVar = (envVar: string) => {
if (!Deno.env.get(envVar)) throw new Error(`${envVar} not set`);
};
const fetchAuth0ApiToken = async () => {
const reqInit: RequestInit = {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
client_id: Deno.env.get("AUTH0_MGMT_APP_CLIENT_ID"),
client_secret: Deno.env.get("AUTH0_MGMT_APP_CLIENT_SECRET"),
audience: `https://${AUTH0_HOST}/api/v2/`,
grant_type: `client_credentials`,
}),
};
try {
return await (await fetch(`https://${AUTH0_HOST}/oauth/token`, reqInit))
.json();
} catch (error) {
throw error;
}
};
const fetchClientCallbacks = async (token: string) => {
const reqInit: RequestInit = {
headers: {
"Content-Type": `application/json`,
"Authorization": `Bearer ${token}`,
},
};
try {
return await (await fetch(
`https://${AUTH0_HOST}/api/v2/clients/${AUTH0_TARGET_CLIENT_ID}?fields=callbacks`,
reqInit,
)).json();
} catch (error) {
throw error;
}
};
const updateAuthOClientCallbacks = async (
token: string,
callbacks: string[],
) => {
const reqInit: RequestInit = {
method: "PATCH",
headers: {
"Content-Type": `application/json`,
"Authorization": `Bearer ${token}`,
},
body: JSON.stringify({ callbacks }),
};
try {
return await (await fetch(
`https://${AUTH0_HOST}/api/v2/clients/${AUTH0_TARGET_CLIENT_ID}`,
reqInit,
)).json();
} catch (error) {
throw error;
}
};
type Operation = "add" | "remove";
const updateAuth0Callbacks = async (
operation: Operation,
callbackUrls: string,
) => {
try {
const { access_token } = await fetchAuth0ApiToken();
let { callbacks } = await fetchClientCallbacks(access_token);
const urls: string[] = callbackUrls.split(",");
if (operation === "add") {
urls.map((url) => {
if (!callbacks.includes(url)) callbacks.push(url);
});
}
if (operation === "remove") {
urls.map((url) => {
callbacks = callbacks.filter((cb: string) => cb !== url);
});
}
const res = await updateAuthOClientCallbacks(access_token, callbacks);
console.log(`updated callbacks ${res.callbacks}`);
} catch (error) {
throw error;
}
};
const args = Deno.args;
console.log(args.length);
if (args.length < 2) {
console.error(
`You need to provide a list of callbackUrl and whether to remove them
deno run --allow-env --allow-net updateAuth0Callbacks.ts <add|remove> <comma,separated,callbackUrls>
deno run --allow-env --allow-net updateAuth0Callbacks.ts add http://localhost:4001,http://trusted.api.com`,
);
throw new Error("insufficient arguments error");
}
checkEnvVar("AUTH0_MGMT_APP_CLIENT_ID");
checkEnvVar("AUTH0_MGMT_APP_CLIENT_SECRET");
// checkEnvVar('AUTH0_TARGET_CLIENT_ID');
const AUTH0_HOST = Deno.env.get("AUTH0_HOST") || `dev-ywte7knl.eu.auth0.com`;
const AUTH0_TARGET_CLIENT_ID = Deno.env.get("AUTH0_TARGET_CLIENT_ID") || "5nWwLM7ZqQOu6UxDvlj2yz9HPN5wAiG3";
updateAuth0Callbacks(args[0] as Operation, args[1]);