-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
93 lines (79 loc) · 2.71 KB
/
main.ts
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
import 'isomorphic-fetch';
import { DefaultAzureCredential } from '@azure/identity';
import { Client, GraphError } from '@microsoft/microsoft-graph-client';
import { TokenCredentialAuthenticationProvider } from '@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials';
import commander, { Argument } from 'commander';
import { version, description } from './package.json';
const program = new commander.Command();
program
.name('npx @intility/azure-app-redirect-uris')
.description(description)
.version(version)
.argument('<appObjectId>', 'The object ID of the app registration')
.addArgument(
new Argument('<platform>', 'Redirect URI platform').choices([
'publicClient',
'web',
'spa',
])
)
.addArgument(
new Argument('<action>', 'The action to perform').choices(['add', 'remove'])
)
.argument('<redirectUri>', 'The redirect URI')
.action(
async (
appObjectId: string,
platform: 'publicClient' | 'web' | 'spa',
action: 'add' | 'remove',
redirectUri: 'string'
) => {
try {
const credential = new DefaultAzureCredential();
const authProvider = new TokenCredentialAuthenticationProvider(
credential,
{
scopes: ['https://graph.microsoft.com/.default'],
}
);
const client = Client.initWithMiddleware({ authProvider });
const app = await client.api(`/applications/${appObjectId}`).get();
const redirectUris = new Set<string>(app[platform].redirectUris);
// store message in a variable to be able to print it after success
let message: string | undefined;
if (action === 'add') {
if (redirectUris.has(redirectUri)) {
console.log(
`Redirect URI ${redirectUri} is already registered, doing nothing.`
);
return;
}
message = `Redirect URI ${redirectUri} successfully added.`;
redirectUris.add(redirectUri);
}
if (action === 'remove') {
if (!redirectUris.has(redirectUri)) {
console.log(
`Redirect URI ${redirectUri} not registered, doing nothing.`
);
return;
}
message = `Redirect URI ${redirectUri} successfully removed.`;
redirectUris.delete(redirectUri);
}
await client.api(`/applications/${appObjectId}`).patch({
[platform]: { ...app[platform], redirectUris: [...redirectUris] },
});
if (message) {
console.log(message);
}
} catch (e) {
if (e instanceof GraphError) {
console.error(e.message);
return;
}
throw e;
}
}
);
program.parse();