-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor sharepoint endpoints to use graph api
- Loading branch information
1 parent
61feae0
commit 7e87feb
Showing
16 changed files
with
425 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { FactoryProvider, HttpException, HttpStatus } from "@nestjs/common"; | ||
import { ConfigService } from "../config/config.service"; | ||
import msal from "@azure/msal-node"; | ||
|
||
export interface MsalProviderType { | ||
getGraphClientToken: () => Promise<msal.AuthenticationResult>; | ||
sharePointSiteUrl: string; | ||
} | ||
export const MSAL = Symbol("MSAL_SERVICE"); | ||
export const MsalProvider: FactoryProvider<MsalProviderType> = { | ||
provide: MSAL, | ||
inject: [ConfigService], | ||
useFactory: (config: ConfigService) => { | ||
const tenantId: string | undefined = config.get("TENANT_ID"); | ||
const clientId: string | undefined = config.get("SHAREPOINT_CLIENT_ID"); | ||
const clientSecret: string | undefined = config.get( | ||
"SHAREPOINT_CLIENT_SECRET" | ||
); | ||
const siteId: string | undefined = config.get("SHAREPOINT_SITE_ID"); | ||
if ( | ||
tenantId === undefined || | ||
clientId === undefined || | ||
clientSecret === undefined || | ||
siteId === undefined | ||
) { | ||
throw new Error("Missing SharePoint credential"); | ||
} | ||
|
||
const cca = new msal.ConfidentialClientApplication({ | ||
auth: { | ||
clientId, | ||
clientSecret, | ||
authority: `https://login.microsoftonline.com/${tenantId}` | ||
} | ||
}); | ||
const graphBaseUrl = "https://graph.microsoft.com"; | ||
const scopes = [`${graphBaseUrl}/.default`]; | ||
const sharePointSiteUrl = `${graphBaseUrl}/v1.0/sites/${siteId}`; | ||
|
||
// Method also checks for a cached token before calling security token service | ||
// https://github.com/MicrosoftDocs/entra-docs/blob/main/docs/identity-platform/msal-acquire-cache-tokens.md#recommended-call-pattern-for-public-client-applications | ||
const getGraphClientToken = () => { | ||
try { | ||
return cca.acquireTokenByClientCredential({ | ||
scopes | ||
}); | ||
} catch { | ||
throw new HttpException( | ||
{ | ||
code: "GRAPH_TOKEN_ERROR", | ||
title: "Error retrieving Graph token", | ||
detail: "Error retrieving Graph token" | ||
}, | ||
HttpStatus.INTERNAL_SERVER_ERROR | ||
); | ||
} | ||
}; | ||
|
||
return { | ||
getGraphClientToken, | ||
sharePointSiteUrl | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ConfigModule } from '../config/config.module'; | ||
import { SharepointService } from './sharepoint.service'; | ||
import { Module } from "@nestjs/common"; | ||
import { ConfigModule } from "../config/config.module"; | ||
import { SharepointService } from "./sharepoint.service"; | ||
import { MsalProvider } from "../provider/msal.provider"; | ||
|
||
@Module({ | ||
imports: [ | ||
ConfigModule, | ||
], | ||
providers: [ | ||
SharepointService, | ||
], | ||
exports: [ | ||
SharepointService, | ||
], | ||
controllers: [], | ||
imports: [ConfigModule], | ||
providers: [SharepointService, MsalProvider], | ||
exports: [SharepointService], | ||
controllers: [] | ||
}) | ||
export class SharepointModule {} |
Oops, something went wrong.