From 2725aace6a5d4ff713e2a8bf213e7e22f985dcde Mon Sep 17 00:00:00 2001 From: rkodev <43806892+rkodev@users.noreply.github.com> Date: Mon, 3 Feb 2025 23:34:18 +0300 Subject: [PATCH] feat: adds graph allowed hosts --- package-lock.json | 53 +++++++++++++++++++ package.json | 2 + .../AzureIdentityAccessTokenProvider.ts | 26 +++++++++ .../AzureIdentityAuthenticationProvider.ts | 27 ++++++++++ src/authentication/index.ts | 2 + src/index.ts | 1 + .../AzureIdentityAccessTokenProvider.ts | 30 +++++++++++ .../AzureIdentityAutheticationProvider.ts | 30 +++++++++++ 8 files changed, 171 insertions(+) create mode 100644 src/authentication/AzureIdentityAccessTokenProvider.ts create mode 100644 src/authentication/AzureIdentityAuthenticationProvider.ts create mode 100644 src/authentication/index.ts create mode 100644 test/authentication/AzureIdentityAccessTokenProvider.ts create mode 100644 test/authentication/AzureIdentityAutheticationProvider.ts diff --git a/package-lock.json b/package-lock.json index 0218054..01e4070 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,9 @@ "version": "1.0.0-preview.8", "license": "MIT", "dependencies": { + "@azure/core-auth": "^1.9.0", "@microsoft/kiota-abstractions": "^1.0.0-preview.39", + "@microsoft/kiota-authentication-azure": "^1.0.0-preview.81", "@microsoft/kiota-http-fetchlibrary": "^1.0.0-preview.38", "@microsoft/kiota-serialization-form": "^1.0.0-preview.28", "@microsoft/kiota-serialization-json": "^1.0.0-preview.39", @@ -60,6 +62,45 @@ "node": ">=6.0.0" } }, + "node_modules/@azure/abort-controller": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/@azure/abort-controller/-/abort-controller-2.1.2.tgz", + "integrity": "sha512-nBrLsEWm4J2u5LpAPjxADTlq3trDgVZZXHNKabeXZtpq3d3AbN/KGO82R87rdDz5/lYB024rtEf10/q0urNgsA==", + "license": "MIT", + "dependencies": { + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@azure/core-auth": { + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/@azure/core-auth/-/core-auth-1.9.0.tgz", + "integrity": "sha512-FPwHpZywuyasDSLMqJ6fhbOK3TqUdviZNF8OqRGA4W5Ewib2lEEZ+pBsYcBa88B2NGO/SEnYPGhyBqNlE8ilSw==", + "license": "MIT", + "dependencies": { + "@azure/abort-controller": "^2.0.0", + "@azure/core-util": "^1.11.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@azure/core-util": { + "version": "1.11.0", + "resolved": "https://registry.npmjs.org/@azure/core-util/-/core-util-1.11.0.tgz", + "integrity": "sha512-DxOSLua+NdpWoSqULhjDyAZTXFdP/LKkqtYuxxz1SCN289zk3OG8UOpnCQAz/tygyACBtWp/BoO72ptK7msY8g==", + "license": "MIT", + "dependencies": { + "@azure/abort-controller": "^2.0.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, "node_modules/@babel/helper-string-parser": { "version": "7.25.9", "resolved": "https://registry.npmjs.org/@babel/helper-string-parser/-/helper-string-parser-7.25.9.tgz", @@ -849,6 +890,18 @@ "uuid": "^11.0.2" } }, + "node_modules/@microsoft/kiota-authentication-azure": { + "version": "1.0.0-preview.81", + "resolved": "https://registry.npmjs.org/@microsoft/kiota-authentication-azure/-/kiota-authentication-azure-1.0.0-preview.81.tgz", + "integrity": "sha512-O5lLaGqTz70gdY9DanZaW1ZacLppREhQOH4hkOQ9FdGe9ctGyJm48xEZxAigGY+ciroaGdFMn9fchgoSL2BQ5Q==", + "license": "MIT", + "dependencies": { + "@azure/core-auth": "^1.5.0", + "@microsoft/kiota-abstractions": "^1.0.0-preview.81", + "@opentelemetry/api": "^1.7.0", + "tslib": "^2.6.2" + } + }, "node_modules/@microsoft/kiota-http-fetchlibrary": { "version": "1.0.0-preview.81", "resolved": "https://registry.npmjs.org/@microsoft/kiota-http-fetchlibrary/-/kiota-http-fetchlibrary-1.0.0-preview.81.tgz", diff --git a/package.json b/package.json index 425bd10..9487076 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,9 @@ "vitest": "^3.0.1" }, "dependencies": { + "@azure/core-auth": "^1.9.0", "@microsoft/kiota-abstractions": "^1.0.0-preview.39", + "@microsoft/kiota-authentication-azure": "^1.0.0-preview.81", "@microsoft/kiota-http-fetchlibrary": "^1.0.0-preview.38", "@microsoft/kiota-serialization-form": "^1.0.0-preview.28", "@microsoft/kiota-serialization-json": "^1.0.0-preview.39", diff --git a/src/authentication/AzureIdentityAccessTokenProvider.ts b/src/authentication/AzureIdentityAccessTokenProvider.ts new file mode 100644 index 0000000..c6da41d --- /dev/null +++ b/src/authentication/AzureIdentityAccessTokenProvider.ts @@ -0,0 +1,26 @@ +import { GetTokenOptions, TokenCredential } from "@azure/core-auth"; +import { AzureIdentityAccessTokenProvider } from "@microsoft/kiota-authentication-azure"; +import { ObservabilityOptions } from "@microsoft/kiota-authentication-azure/dist/es/src/observabilityOptions"; + +export class GraphAzureIdentityAccessTokenProvider extends AzureIdentityAccessTokenProvider { + public constructor( + credentials: TokenCredential, + scopes?: string[], + options?: GetTokenOptions | undefined, + allowedHosts?: Set, + observabilityOptions?: ObservabilityOptions, + isCaeEnabled?: boolean, + ) { + if (!allowedHosts || allowedHosts.size === 0) { + allowedHosts = new Set([ + "graph.microsoft.com", + "graph.microsoft.us", + "dod-graph.microsoft.us", + "graph.microsoft.de", + "microsoftgraph.chinacloudapi.cn", + "canary.graph.microsoft.com", + ]); + } + super(credentials, scopes, options, allowedHosts, observabilityOptions, isCaeEnabled); + } +} diff --git a/src/authentication/AzureIdentityAuthenticationProvider.ts b/src/authentication/AzureIdentityAuthenticationProvider.ts new file mode 100644 index 0000000..08d57b8 --- /dev/null +++ b/src/authentication/AzureIdentityAuthenticationProvider.ts @@ -0,0 +1,27 @@ +import { GetTokenOptions, TokenCredential } from "@azure/core-auth"; +import { AzureIdentityAuthenticationProvider } from "@microsoft/kiota-authentication-azure"; +import { ObservabilityOptions } from "@microsoft/kiota-authentication-azure/dist/es/src/observabilityOptions"; + +export class GraphAzureIdentityAuthenticationProvider extends AzureIdentityAuthenticationProvider { + // create a constructor with TokenCredential + public constructor( + credentials: TokenCredential, + scopes?: string[], + options?: GetTokenOptions | undefined, + allowedHosts?: Set, + observabilityOptions?: ObservabilityOptions, + isCaeEnabled?: boolean, + ) { + if (!allowedHosts || allowedHosts.size === 0) { + allowedHosts = new Set([ + "graph.microsoft.com", + "graph.microsoft.us", + "dod-graph.microsoft.us", + "graph.microsoft.de", + "microsoftgraph.chinacloudapi.cn", + "canary.graph.microsoft.com", + ]); + } + super(credentials, scopes, options, allowedHosts, observabilityOptions, isCaeEnabled); + } +} diff --git a/src/authentication/index.ts b/src/authentication/index.ts new file mode 100644 index 0000000..acf0173 --- /dev/null +++ b/src/authentication/index.ts @@ -0,0 +1,2 @@ +export * from "./AzureIdentityAccessTokenProvider.js"; +export * from "./AzureIdentityAuthenticationProvider.js"; diff --git a/src/index.ts b/src/index.ts index 23ac00d..f5da039 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ export * from "./adapter/index.js"; export * from "./http/index.js"; export * from "./middleware/index.js"; +export * from "./authentication/index.js"; export * from "./utils/Constants.js"; export * from "./utils/Version.js"; diff --git a/test/authentication/AzureIdentityAccessTokenProvider.ts b/test/authentication/AzureIdentityAccessTokenProvider.ts new file mode 100644 index 0000000..81f42f5 --- /dev/null +++ b/test/authentication/AzureIdentityAccessTokenProvider.ts @@ -0,0 +1,30 @@ +import { assert, describe, it } from "vitest"; +import { GraphAzureIdentityAccessTokenProvider } from "../../src/authentication/AzureIdentityAccessTokenProvider"; +import { GraphTelemetryOption } from "../../src/middleware/GraphTelemetryOption"; +import { AzureIdentityAccessTokenProvider } from "@microsoft/kiota-authentication-azure"; +import { TokenCredential } from "@azure/core-auth"; + +const options: GraphTelemetryOption = { + graphServiceTargetVersion: "v1", + graphProductPrefix: "graph-typescript-test", + graphServiceLibraryClientVersion: "0.0.0", +}; + +describe("GraphAzureIdentityAccessTokenProvider tests", () => { + it("should implement AzureIdentityAccessTokenProvider", () => { + const credential = {} as TokenCredential; // Replace with actual TokenCredential implementation + const provider = new GraphAzureIdentityAccessTokenProvider(credential); + assert.instanceOf( + provider, + AzureIdentityAccessTokenProvider, + "Provider does not implement AzureIdentityAccessTokenProvider", + ); + }); + + it("should add default hosts", async () => { + const credential = {} as TokenCredential; // Replace with actual TokenCredential implementation + const provider = new GraphAzureIdentityAccessTokenProvider(credential); + const allowedHosts = provider.getAllowedHostsValidator().getAllowedHosts(); + assert.equal(allowedHosts.length, 6, "Unexpected number of allowed hosts"); + }); +}); diff --git a/test/authentication/AzureIdentityAutheticationProvider.ts b/test/authentication/AzureIdentityAutheticationProvider.ts new file mode 100644 index 0000000..4791bd5 --- /dev/null +++ b/test/authentication/AzureIdentityAutheticationProvider.ts @@ -0,0 +1,30 @@ +import { assert, describe, it } from "vitest"; +import { GraphAzureIdentityAuthenticationProvider } from "../../src/authentication/AzureIdentityAuthenticationProvider"; +import { GraphTelemetryOption } from "../../src/middleware/GraphTelemetryOption"; +import { AzureIdentityAuthenticationProvider } from "@microsoft/kiota-authentication-azure"; +import { TokenCredential } from "@azure/core-auth"; + +const options: GraphTelemetryOption = { + graphServiceTargetVersion: "v1", + graphProductPrefix: "graph-typescript-test", + graphServiceLibraryClientVersion: "0.0.0", +}; + +describe("GraphAzureIdentityAuthenticationProvider tests", () => { + it("should implement AzureIdentityAccessTokenProvider", () => { + const credential = {} as TokenCredential; // Replace with actual TokenCredential implementation + const provider = new GraphAzureIdentityAuthenticationProvider(credential); + assert.instanceOf( + provider, + AzureIdentityAuthenticationProvider, + "Provider does not implement AzureIdentityAccessTokenProvider", + ); + }); + + it("should add default hosts", async () => { + const credential = {} as TokenCredential; // Replace with actual TokenCredential implementation + const provider = new GraphAzureIdentityAuthenticationProvider(credential); + const allowedHosts = provider.accessTokenProvider.getAllowedHostsValidator().getAllowedHosts(); + assert.equal(allowedHosts.length, 6, "Unexpected number of allowed hosts"); + }); +});