Skip to content

Commit effdf42

Browse files
authored
Merge pull request #339 from microsoftgraph/feat/add-graph-authetication-providers
feat: adds graph allowed hosts
2 parents e8df435 + 2725aac commit effdf42

File tree

8 files changed

+171
-0
lines changed

8 files changed

+171
-0
lines changed

package-lock.json

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@
7070
"vitest": "^3.0.1"
7171
},
7272
"dependencies": {
73+
"@azure/core-auth": "^1.9.0",
7374
"@microsoft/kiota-abstractions": "^1.0.0-preview.39",
75+
"@microsoft/kiota-authentication-azure": "^1.0.0-preview.81",
7476
"@microsoft/kiota-http-fetchlibrary": "^1.0.0-preview.38",
7577
"@microsoft/kiota-serialization-form": "^1.0.0-preview.28",
7678
"@microsoft/kiota-serialization-json": "^1.0.0-preview.39",
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { GetTokenOptions, TokenCredential } from "@azure/core-auth";
2+
import { AzureIdentityAccessTokenProvider } from "@microsoft/kiota-authentication-azure";
3+
import { ObservabilityOptions } from "@microsoft/kiota-authentication-azure/dist/es/src/observabilityOptions";
4+
5+
export class GraphAzureIdentityAccessTokenProvider extends AzureIdentityAccessTokenProvider {
6+
public constructor(
7+
credentials: TokenCredential,
8+
scopes?: string[],
9+
options?: GetTokenOptions | undefined,
10+
allowedHosts?: Set<string>,
11+
observabilityOptions?: ObservabilityOptions,
12+
isCaeEnabled?: boolean,
13+
) {
14+
if (!allowedHosts || allowedHosts.size === 0) {
15+
allowedHosts = new Set([
16+
"graph.microsoft.com",
17+
"graph.microsoft.us",
18+
"dod-graph.microsoft.us",
19+
"graph.microsoft.de",
20+
"microsoftgraph.chinacloudapi.cn",
21+
"canary.graph.microsoft.com",
22+
]);
23+
}
24+
super(credentials, scopes, options, allowedHosts, observabilityOptions, isCaeEnabled);
25+
}
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { GetTokenOptions, TokenCredential } from "@azure/core-auth";
2+
import { AzureIdentityAuthenticationProvider } from "@microsoft/kiota-authentication-azure";
3+
import { ObservabilityOptions } from "@microsoft/kiota-authentication-azure/dist/es/src/observabilityOptions";
4+
5+
export class GraphAzureIdentityAuthenticationProvider extends AzureIdentityAuthenticationProvider {
6+
// create a constructor with TokenCredential
7+
public constructor(
8+
credentials: TokenCredential,
9+
scopes?: string[],
10+
options?: GetTokenOptions | undefined,
11+
allowedHosts?: Set<string>,
12+
observabilityOptions?: ObservabilityOptions,
13+
isCaeEnabled?: boolean,
14+
) {
15+
if (!allowedHosts || allowedHosts.size === 0) {
16+
allowedHosts = new Set([
17+
"graph.microsoft.com",
18+
"graph.microsoft.us",
19+
"dod-graph.microsoft.us",
20+
"graph.microsoft.de",
21+
"microsoftgraph.chinacloudapi.cn",
22+
"canary.graph.microsoft.com",
23+
]);
24+
}
25+
super(credentials, scopes, options, allowedHosts, observabilityOptions, isCaeEnabled);
26+
}
27+
}

src/authentication/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./AzureIdentityAccessTokenProvider.js";
2+
export * from "./AzureIdentityAuthenticationProvider.js";

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from "./adapter/index.js";
22
export * from "./http/index.js";
33
export * from "./middleware/index.js";
4+
export * from "./authentication/index.js";
45
export * from "./utils/Constants.js";
56
export * from "./utils/Version.js";
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { assert, describe, it } from "vitest";
2+
import { GraphAzureIdentityAccessTokenProvider } from "../../src/authentication/AzureIdentityAccessTokenProvider";
3+
import { GraphTelemetryOption } from "../../src/middleware/GraphTelemetryOption";
4+
import { AzureIdentityAccessTokenProvider } from "@microsoft/kiota-authentication-azure";
5+
import { TokenCredential } from "@azure/core-auth";
6+
7+
const options: GraphTelemetryOption = {
8+
graphServiceTargetVersion: "v1",
9+
graphProductPrefix: "graph-typescript-test",
10+
graphServiceLibraryClientVersion: "0.0.0",
11+
};
12+
13+
describe("GraphAzureIdentityAccessTokenProvider tests", () => {
14+
it("should implement AzureIdentityAccessTokenProvider", () => {
15+
const credential = {} as TokenCredential; // Replace with actual TokenCredential implementation
16+
const provider = new GraphAzureIdentityAccessTokenProvider(credential);
17+
assert.instanceOf(
18+
provider,
19+
AzureIdentityAccessTokenProvider,
20+
"Provider does not implement AzureIdentityAccessTokenProvider",
21+
);
22+
});
23+
24+
it("should add default hosts", async () => {
25+
const credential = {} as TokenCredential; // Replace with actual TokenCredential implementation
26+
const provider = new GraphAzureIdentityAccessTokenProvider(credential);
27+
const allowedHosts = provider.getAllowedHostsValidator().getAllowedHosts();
28+
assert.equal(allowedHosts.length, 6, "Unexpected number of allowed hosts");
29+
});
30+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { assert, describe, it } from "vitest";
2+
import { GraphAzureIdentityAuthenticationProvider } from "../../src/authentication/AzureIdentityAuthenticationProvider";
3+
import { GraphTelemetryOption } from "../../src/middleware/GraphTelemetryOption";
4+
import { AzureIdentityAuthenticationProvider } from "@microsoft/kiota-authentication-azure";
5+
import { TokenCredential } from "@azure/core-auth";
6+
7+
const options: GraphTelemetryOption = {
8+
graphServiceTargetVersion: "v1",
9+
graphProductPrefix: "graph-typescript-test",
10+
graphServiceLibraryClientVersion: "0.0.0",
11+
};
12+
13+
describe("GraphAzureIdentityAuthenticationProvider tests", () => {
14+
it("should implement AzureIdentityAccessTokenProvider", () => {
15+
const credential = {} as TokenCredential; // Replace with actual TokenCredential implementation
16+
const provider = new GraphAzureIdentityAuthenticationProvider(credential);
17+
assert.instanceOf(
18+
provider,
19+
AzureIdentityAuthenticationProvider,
20+
"Provider does not implement AzureIdentityAccessTokenProvider",
21+
);
22+
});
23+
24+
it("should add default hosts", async () => {
25+
const credential = {} as TokenCredential; // Replace with actual TokenCredential implementation
26+
const provider = new GraphAzureIdentityAuthenticationProvider(credential);
27+
const allowedHosts = provider.accessTokenProvider.getAllowedHostsValidator().getAllowedHosts();
28+
assert.equal(allowedHosts.length, 6, "Unexpected number of allowed hosts");
29+
});
30+
});

0 commit comments

Comments
 (0)