Skip to content

Commit 58162f5

Browse files
committed
- removes graph specific defaults
1 parent 2c6f8e5 commit 58162f5

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

.vscode/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"cSpell.words": [
33
"kiota",
4-
"opentelemetry"
4+
"opentelemetry",
5+
"Spfx"
56
]
67
}

packages/authentication/azure/src/azureIdentityAccessTokenProvider.ts

+35-13
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,16 @@ export class AzureIdentityAccessTokenProvider implements AccessTokenProvider {
2222
*/
2323
public constructor(
2424
private readonly credentials: TokenCredential,
25-
private readonly scopes: string[] = [
26-
"https://graph.microsoft.com/.default",
27-
],
25+
private readonly scopes: string[] = [],
2826
private readonly options?: GetTokenOptions,
29-
allowedHosts: Set<string> = new Set<string>([
30-
"graph.microsoft.com",
31-
"graph.microsoft.us",
32-
"dod-graph.microsoft.us",
33-
"graph.microsoft.de",
34-
"microsoftgraph.chinacloudapi.cn",
35-
"canary.graph.microsoft.com",
36-
]),
27+
allowedHosts: Set<string> = new Set<string>(),
3728
private readonly observabilityOptions: ObservabilityOptions = new ObservabilityOptionsImpl()
3829
) {
3930
if (!credentials) {
4031
throw new Error("parameter credentials cannot be null");
4132
}
42-
if (!scopes || scopes.length === 0) {
43-
throw new Error("scopes cannot be null or empty");
33+
if (!scopes) {
34+
throw new Error("scopes cannot be null");
4435
}
4536
if (!observabilityOptions) {
4637
throw new Error("observabilityOptions cannot be null");
@@ -104,13 +95,44 @@ export class AzureIdentityAccessTokenProvider implements AccessTokenProvider {
10495
if (decodedClaims) {
10596
(localOptions as any).claims = decodedClaims; // the field is defined in a derived interface for some reason https://github.com/Azure/azure-sdk-for-js/blob/4498fecbede71563fee5daae2ad537ff57de3640/sdk/identity/identity/src/msal/credentials.ts#L29
10697
}
98+
if (this.scopes.length === 0) {
99+
const [scheme, host] = this.getSchemeAndHostFromUrl(url);
100+
this.scopes.push(`${scheme}://${host}/.default`);
101+
}
107102
span?.setAttribute(
108103
"com.microsoft.kiota.authentication.scopes",
109104
this.scopes.join(",")
110105
);
111106
const result = await this.credentials.getToken(this.scopes, localOptions);
112107
return result?.token ?? "";
113108
};
109+
private getSchemeAndHostFromUrl = (url: string): string[] => {
110+
const urlParts = url.split("://");
111+
if (urlParts.length === 0) {
112+
// relative url
113+
return [this.getSchemeFromLocation(), this.getHostFromLocation()];
114+
} else if (urlParts.length === 1) {
115+
// protocol relative url
116+
return [this.getSchemeFromLocation(), urlParts[0].split("/")[0]];
117+
} else if (urlParts.length >= 2) {
118+
// absolute url
119+
return [urlParts[0], urlParts[1].split("/")[0]];
120+
} else {
121+
throw new Error("invalid url");
122+
}
123+
};
124+
private getSchemeFromLocation = (): string => {
125+
if (window && window.location && window.location.protocol) {
126+
return window.location.protocol.replace(":", "");
127+
}
128+
return "";
129+
};
130+
private getHostFromLocation = (): string => {
131+
if (window && window.location && window.location.host) {
132+
return window.location.host;
133+
}
134+
return "";
135+
};
114136
/**
115137
* @inheritdoc
116138
*/

packages/authentication/spfx/src/azureAdSpfxAccessTokenProvider.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,7 @@ export class AzureAdSpfxAccessTokenProvider implements AccessTokenProvider {
2424
public constructor(
2525
private readonly tokenProvider: AadTokenProvider,
2626
private readonly applicationIdUri: string,
27-
allowedHosts: Set<string> = new Set<string>([
28-
"graph.microsoft.com",
29-
"graph.microsoft.us",
30-
"dod-graph.microsoft.us",
31-
"graph.microsoft.de",
32-
"microsoftgraph.chinacloudapi.cn",
33-
"canary.graph.microsoft.com",
34-
]),
27+
allowedHosts: Set<string> = new Set<string>(),
3528
private readonly useCachedToken?: boolean,
3629
private readonly observabilityOptions: ObservabilityOptions = new ObservabilityOptionsImpl()
3730
) {

0 commit comments

Comments
 (0)