Reorganized the module structure.
This commit is contained in:
32
src/graph/app.js
Normal file
32
src/graph/app.js
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Get an Azure application by its display name.
|
||||
*
|
||||
* @param { Object } client
|
||||
* @param { string } appName
|
||||
* @returns
|
||||
*/
|
||||
export async function getApp(client, appName) {
|
||||
const result = await client
|
||||
.api("/applications")
|
||||
.filter(`displayName eq '${appName}'`)
|
||||
.get();
|
||||
|
||||
// Return the first application found or null if none exists
|
||||
return result.value.length > 0 ? result.value[0] : null;
|
||||
}
|
||||
|
||||
export async function createApp(client, appName) {
|
||||
const app = await client.api("/applications").post({
|
||||
displayName: appName,
|
||||
});
|
||||
|
||||
if (!app || !app.appId) {
|
||||
throw new Error("Failed to create application");
|
||||
}
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
export async function deleteApp(client, appObjectId) {
|
||||
await client.api(`/applications/${appObjectId}`).delete();
|
||||
}
|
||||
27
src/graph/auth.js
Normal file
27
src/graph/auth.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import { loginInteractive } from "../azure/index.js";
|
||||
import { Client } from "@microsoft/microsoft-graph-client";
|
||||
|
||||
/**
|
||||
* Initialize and return a Microsoft Graph client
|
||||
* along with the authentication token.
|
||||
*
|
||||
* @param { Object } options - Options for authentication
|
||||
* @param { string } options.tenantId - The Azure AD tenant ID
|
||||
* @param { string } options.clientId - The Azure AD client ID
|
||||
* @returns { Object } An object containing the Graph API token and client
|
||||
*/
|
||||
export async function getGraphClient({ tenantId, clientId }) {
|
||||
const graphApiToken = await loginInteractive({
|
||||
tenantId,
|
||||
clientId,
|
||||
scopes: ["https://graph.microsoft.com/.default"],
|
||||
});
|
||||
|
||||
const client = Client.init({
|
||||
authProvider: (done) => {
|
||||
done(null, graphApiToken.accessToken);
|
||||
},
|
||||
});
|
||||
|
||||
return { graphApiToken, client };
|
||||
}
|
||||
1
src/graph/index.d.ts
vendored
Normal file
1
src/graph/index.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
//
|
||||
3
src/graph/index.js
Normal file
3
src/graph/index.js
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from "./auth.js";
|
||||
export * from "./app.js";
|
||||
export * from "./sp.js";
|
||||
25
src/graph/sp.js
Normal file
25
src/graph/sp.js
Normal file
@@ -0,0 +1,25 @@
|
||||
export async function getServicePrincipal(client, appId) {
|
||||
const result = await client
|
||||
.api("/servicePrincipals")
|
||||
.filter(`appId eq '${appId}'`)
|
||||
.get();
|
||||
|
||||
// Return the first service principal found or null if none exists
|
||||
return result.value.length > 0 ? result.value[0] : null;
|
||||
}
|
||||
|
||||
export async function createSp(client, appId) {
|
||||
const sp = await client.api("/servicePrincipals").post({
|
||||
appId,
|
||||
});
|
||||
|
||||
if (!sp || !sp.id) {
|
||||
throw new Error("Failed to create service principal");
|
||||
}
|
||||
|
||||
return sp;
|
||||
}
|
||||
|
||||
export async function deleteSp(client, spId) {
|
||||
await client.api(`/servicePrincipals/${spId}`).delete();
|
||||
}
|
||||
Reference in New Issue
Block a user