Skip to content

Commit 6a4a604

Browse files
authored
Merge branch 'next/fluentui' into merge/main-to-fluentui
2 parents 7546483 + 1edf635 commit 6a4a604

File tree

16 files changed

+178
-49
lines changed

16 files changed

+178
-49
lines changed

packages/mgt-element/src/Graph.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
TelemetryHandler
1818
} from '@microsoft/microsoft-graph-client';
1919

20-
import { IGraph } from './IGraph';
20+
import { IGraph, MICROSOFT_GRAPH_DEFAULT_ENDPOINT } from './IGraph';
2121
import { IProvider } from './providers/IProvider';
2222
import { Batch } from './utils/Batch';
2323
import { ComponentMiddlewareOptions } from './utils/ComponentMiddlewareOptions';
@@ -155,8 +155,10 @@ export function createFromProvider(provider: IProvider, version?: string, compon
155155
new HTTPMessageHandler()
156156
];
157157

158+
let baseURL = provider.baseURL ? provider.baseURL : MICROSOFT_GRAPH_DEFAULT_ENDPOINT;
158159
const client = Client.initWithMiddleware({
159-
middleware: chainMiddleware(...middleware)
160+
middleware: chainMiddleware(...middleware),
161+
baseUrl: baseURL
160162
});
161163

162164
const graph = new Graph(client, version);

packages/mgt-element/src/IGraph.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,25 @@ export interface IGraph {
6767
createBatch(): IBatch;
6868
}
6969

70+
/**
71+
* GraphEndpoint is a valid URL that is used to access the Graph.
72+
*/
7073
export type GraphEndpoint =
7174
| 'https://graph.microsoft.com'
7275
| 'https://graph.microsoft.us'
7376
| 'https://dod-graph.microsoft.us'
7477
| 'https://graph.microsoft.de'
7578
| 'https://microsoftgraph.chinacloudapi.cn';
7679

80+
/**
81+
* MICROSOFT_GRAPH_ENDPOINTS is a set of all the valid Graph URL endpoints.
82+
*/
7783
export const MICROSOFT_GRAPH_ENDPOINTS: Set<GraphEndpoint> = new Set<GraphEndpoint>();
84+
85+
/**
86+
* MICROSOFT_GRAPH_DEFAULT_ENDPOINT is the default Graph endpoint that is silently set on
87+
* the providers as the baseURL.
88+
*/
7889
export const MICROSOFT_GRAPH_DEFAULT_ENDPOINT: GraphEndpoint = 'https://graph.microsoft.com';
7990

8091
(() => {

packages/mgt-element/src/components/baseProvider.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import { property } from 'lit/decorators.js';
99
import { MgtBaseComponent } from './baseComponent';
1010
import { IProvider } from '../providers/IProvider';
11+
import { GraphEndpoint } from '../IGraph';
1112

1213
/**
1314
* Abstract implementation for provider component
@@ -65,6 +66,17 @@ export abstract class MgtBaseProvider extends MgtBaseComponent {
6566
})
6667
public dependsOn: MgtBaseProvider;
6768

69+
/**
70+
* The base URL that should be used in the graph client config.
71+
*
72+
* @memberof MgtBaseProvider
73+
*/
74+
@property({
75+
attribute: 'base-url',
76+
type: String
77+
})
78+
public baseUrl: GraphEndpoint;
79+
6880
private _provider: IProvider;
6981

7082
/**

packages/mgt-element/src/providers/IProvider.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
*/
77

88
import { AuthenticationProvider, AuthenticationProviderOptions } from '@microsoft/microsoft-graph-client';
9-
import { IGraph } from '../IGraph';
9+
import { validateBaseURL } from '../utils/GraphHelpers';
10+
import { GraphEndpoint, IGraph, MICROSOFT_GRAPH_DEFAULT_ENDPOINT } from '../IGraph';
1011
import { EventDispatcher, EventHandler } from '../utils/EventDispatcher';
1112

1213
/**
@@ -48,6 +49,23 @@ export abstract class IProvider implements AuthenticationProvider {
4849
private _state: ProviderState;
4950
private _loginChangedDispatcher = new EventDispatcher<LoginChangedEvent>();
5051
private _activeAccountChangedDispatcher = new EventDispatcher<ActiveAccountChanged>();
52+
private _baseURL: GraphEndpoint = MICROSOFT_GRAPH_DEFAULT_ENDPOINT;
53+
54+
/**
55+
* The base URL to be used in the graph client config.
56+
*/
57+
public set baseURL(url: GraphEndpoint) {
58+
if (validateBaseURL(url)) {
59+
this._baseURL = url;
60+
return;
61+
} else {
62+
throw new Error(`${url} is not a valid Graph URL endpoint.`);
63+
}
64+
}
65+
66+
public get baseURL(): GraphEndpoint {
67+
return this._baseURL;
68+
}
5169

5270
/**
5371
* Enable/Disable incremental consent

packages/providers/mgt-electron-provider/src/Authenticator/ElectronAuthenticator.ts

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
PublicClientApplication
1818
} from '@azure/msal-node';
1919
import { AuthenticationProviderOptions } from '@microsoft/microsoft-graph-client';
20+
import { GraphEndpoint } from '@microsoft/mgt-element';
2021
import { BrowserWindow, ipcMain } from 'electron';
2122
import { CustomFileProtocolListener } from './CustomFileProtocol';
2223
import { REDIRECT_URI, COMMON_AUTHORITY_URL } from './Constants';
@@ -65,6 +66,10 @@ export interface MsalElectronConfig {
6566
* @memberof MsalElectronConfig
6667
*/
6768
cachePlugin?: ICachePlugin;
69+
/**
70+
* The base URL for the graph client
71+
*/
72+
baseURL?: GraphEndpoint;
6873
}
6974

7075
/**
@@ -86,6 +91,11 @@ enum AuthState {
8691
LOGGED_OUT = 'logged_out'
8792
}
8893

94+
/**
95+
* AccountDetails defines the available AccountInfo or undefined.
96+
*/
97+
type AccountDetails = AccountInfo | undefined;
98+
8999
/**
90100
* ElectronAuthenticator class to be instantiated in the main process.
91101
* Responsible for MSAL authentication flow and token acqusition.
@@ -133,10 +143,10 @@ export class ElectronAuthenticator {
133143
* Logged in account
134144
*
135145
* @private
136-
* @type {AccountInfo}
146+
* @type {AccountDetails}
137147
* @memberof ElectronAuthenticator
138148
*/
139-
private account: AccountInfo;
149+
private account: AccountDetails;
140150

141151
/**
142152
* Params to generate the URL for MSAL auth
@@ -182,7 +192,7 @@ export class ElectronAuthenticator {
182192
*/
183193
private constructor(config: MsalElectronConfig) {
184194
this.setConfig(config);
185-
this.account = null;
195+
this.account = undefined;
186196
this.mainWindow = config.mainWindow;
187197
this.setRequestObjects(config.scopes);
188198
this.setupProvider();
@@ -224,7 +234,7 @@ export class ElectronAuthenticator {
224234
clientId: config.clientId,
225235
authority: config.authority ? config.authority : COMMON_AUTHORITY_URL
226236
},
227-
cache: config.cachePlugin ? { cachePlugin: config.cachePlugin } : null,
237+
cache: config.cachePlugin ? { cachePlugin: config.cachePlugin } : undefined,
228238
system: {
229239
loggerOptions: {
230240
loggerCallback(loglevel, message, containsPii) {},
@@ -255,7 +265,7 @@ export class ElectronAuthenticator {
255265
this.authCodeRequest = {
256266
scopes: requestScopes,
257267
redirectUri,
258-
code: null
268+
code: ''
259269
};
260270
}
261271

@@ -311,7 +321,7 @@ export class ElectronAuthenticator {
311321
* @return {*} {Promise<string>}
312322
* @memberof ElectronAuthenticator
313323
*/
314-
protected async getAccessToken(options?: AuthenticationProviderOptions): Promise<string> {
324+
protected async getAccessToken(options?: AuthenticationProviderOptions): Promise<string | undefined> {
315325
let authResponse;
316326
const scopes = options && options.scopes ? options.scopes : this.authCodeUrlParams.scopes;
317327
const account = this.account || (await this.getAccount());
@@ -326,6 +336,7 @@ export class ElectronAuthenticator {
326336
if (authResponse) {
327337
return authResponse.accessToken;
328338
}
339+
return undefined;
329340
}
330341

331342
/**
@@ -337,7 +348,7 @@ export class ElectronAuthenticator {
337348
* @return {*} {Promise<AuthenticationResult>}
338349
* @memberof ElectronAuthenticator
339350
*/
340-
protected async getTokenSilent(tokenRequest, scopes?): Promise<AuthenticationResult> {
351+
protected async getTokenSilent(tokenRequest, scopes?): Promise<AuthenticationResult | null> {
341352
try {
342353
return await this.clientApplication.acquireTokenSilent(tokenRequest);
343354
} catch (error) {
@@ -366,7 +377,7 @@ export class ElectronAuthenticator {
366377
protected async logout(): Promise<void> {
367378
if (this.account) {
368379
await this.clientApplication.getTokenCache().removeAccount(this.account);
369-
this.account = null;
380+
this.account = undefined;
370381
}
371382
}
372383

@@ -379,8 +390,8 @@ export class ElectronAuthenticator {
379390
* @memberof ElectronAuthenticator
380391
*/
381392
private async setAccountFromResponse(response: AuthenticationResult) {
382-
if (response !== null) {
383-
this.account = response.account;
393+
if (response) {
394+
this.account = response?.account || undefined;
384395
} else {
385396
this.account = await this.getAccount();
386397
}
@@ -412,7 +423,7 @@ export class ElectronAuthenticator {
412423
.acquireTokenByCode({
413424
...this.authCodeRequest,
414425
scopes: requestScopes,
415-
code: authCode
426+
code: authCode || ''
416427
})
417428
.catch((e: AuthError) => {
418429
throw e;
@@ -429,7 +440,7 @@ export class ElectronAuthenticator {
429440
* @return {*} {Promise<string>}
430441
* @memberof ElectronAuthenticator
431442
*/
432-
private async listenForAuthCode(navigateUrl: string, prompt_type: promptType): Promise<string> {
443+
private async listenForAuthCode(navigateUrl: string, prompt_type: promptType): Promise<string | null> {
433444
this.setAuthWindow(true);
434445
await this.authWindow.loadURL(navigateUrl);
435446
return new Promise((resolve, reject) => {
@@ -474,20 +485,12 @@ export class ElectronAuthenticator {
474485
* @return {*} {Promise<AccountInfo>}
475486
* @memberof ElectronAuthenticator
476487
*/
477-
private async getAccount(): Promise<AccountInfo> {
488+
private async getAccount(): Promise<AccountDetails> {
478489
const cache = this.clientApplication.getTokenCache();
479490
const currentAccounts = await cache.getAllAccounts();
480-
481-
if (currentAccounts === null) {
482-
return null;
483-
}
484-
485-
if (currentAccounts.length > 1) {
491+
if (currentAccounts?.length >= 1) {
486492
return currentAccounts[0];
487-
} else if (currentAccounts.length === 1) {
488-
return currentAccounts[0];
489-
} else {
490-
return null;
491493
}
494+
return undefined;
492495
}
493496
}

packages/providers/mgt-electron-provider/src/Provider/ElectronProvider.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
* -------------------------------------------------------------------------------------------
66
*/
77

8-
import { IProvider, Providers, ProviderState, createFromProvider } from '@microsoft/mgt-element';
8+
import {
9+
IProvider,
10+
Providers,
11+
ProviderState,
12+
createFromProvider,
13+
GraphEndpoint,
14+
MICROSOFT_GRAPH_DEFAULT_ENDPOINT
15+
} from '@microsoft/mgt-element';
916
import { AuthenticationProviderOptions } from '@microsoft/microsoft-graph-client';
1017
import { ipcRenderer } from 'electron';
1118

@@ -28,8 +35,9 @@ export class ElectronProvider extends IProvider {
2835
return 'MgtElectronProvider';
2936
}
3037

31-
constructor() {
38+
constructor(baseUrl: GraphEndpoint = MICROSOFT_GRAPH_DEFAULT_ENDPOINT) {
3239
super();
40+
this.baseURL = baseUrl;
3341
this.graph = createFromProvider(this);
3442
this.setupProvider();
3543
}

packages/providers/mgt-msal-provider/src/MsalProvider.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import { AuthenticationProviderOptions } from '@microsoft/microsoft-graph-client';
9-
import { IProvider, LoginType, ProviderState, createFromProvider } from '@microsoft/mgt-element';
9+
import { IProvider, LoginType, ProviderState, createFromProvider, GraphEndpoint } from '@microsoft/mgt-element';
1010
import { AuthenticationParameters, AuthError, AuthResponse, Configuration, UserAgentApplication } from 'msal';
1111

1212
/**
@@ -51,6 +51,10 @@ interface MsalConfigBase {
5151
* @memberof MsalConfigBase
5252
*/
5353
prompt?: string;
54+
/**
55+
* The base URL for the graph client
56+
*/
57+
baseURL?: GraphEndpoint;
5458
}
5559

5660
/**
@@ -439,6 +443,7 @@ export class MsalProvider extends IProvider {
439443
}
440444

441445
this.clientId = clientId;
446+
this.baseURL = typeof config.baseURL !== 'undefined' ? config.baseURL : this.baseURL;
442447

443448
this._userAgentApplication = userAgentApplication;
444449
this._userAgentApplication.handleRedirectCallback(

packages/providers/mgt-msal-provider/src/mgt-msal-provider.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* -------------------------------------------------------------------------------------------
66
*/
77
import { customElement, property } from 'lit/decorators.js';
8-
import { Providers, LoginType, MgtBaseProvider } from '@microsoft/mgt-element';
8+
import { Providers, LoginType, MgtBaseProvider, validateBaseURL } from '@microsoft/mgt-element';
99
import { MsalConfig, MsalProvider } from './MsalProvider';
1010
/**
1111
* Authentication Library Provider for Microsoft personal accounts
@@ -135,6 +135,10 @@ export class MgtMsalProvider extends MgtBaseProvider {
135135
config.redirectUri = this.redirectUri;
136136
}
137137

138+
if (this.baseUrl) {
139+
config.baseURL = this.baseUrl;
140+
}
141+
138142
this.provider = new MsalProvider(config);
139143
Providers.globalProvider = this.provider;
140144
}

packages/providers/mgt-msal2-provider/src/Msal2Provider.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
* -------------------------------------------------------------------------------------------
66
*/
77

8-
import { IProvider, LoginType, ProviderState, createFromProvider, IProviderAccount } from '@microsoft/mgt-element';
8+
import {
9+
IProvider,
10+
LoginType,
11+
ProviderState,
12+
createFromProvider,
13+
IProviderAccount,
14+
GraphEndpoint
15+
} from '@microsoft/mgt-element';
916
import {
1017
Configuration,
1118
PublicClientApplication,
@@ -133,6 +140,11 @@ export interface Msal2Config extends Msal2ConfigBase {
133140
* @memberof Msal2Config
134141
*/
135142
isMultiAccountEnabled?: boolean;
143+
144+
/**
145+
* The base URL for the graph client
146+
*/
147+
baseURL?: GraphEndpoint;
136148
}
137149

138150
/**
@@ -376,6 +388,7 @@ export class Msal2Provider extends IProvider {
376388
const msal2config = config as Msal2Config;
377389
this.isMultipleAccountEnabled =
378390
typeof msal2config.isMultiAccountEnabled !== 'undefined' ? msal2config.isMultiAccountEnabled : true;
391+
this.baseURL = typeof msal2config.baseURL !== 'undefined' ? msal2config.baseURL : this.baseURL;
379392

380393
this.graph = createFromProvider(this);
381394
try {

packages/providers/mgt-msal2-provider/src/mgt-msal2-provider.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import { customElement, property } from 'lit/decorators.js';
9-
import { Providers, LoginType, MgtBaseProvider } from '@microsoft/mgt-element';
9+
import { Providers, LoginType, MgtBaseProvider, validateBaseURL } from '@microsoft/mgt-element';
1010
import { Msal2Config, Msal2Provider, PromptType } from './Msal2Provider';
1111
/**
1212
* Authentication Library Provider for Microsoft personal accounts
@@ -188,6 +188,11 @@ export class MgtMsal2Provider extends MgtBaseProvider {
188188
if (this.isMultiAccountDisabled) {
189189
config.isMultiAccountEnabled = false;
190190
}
191+
192+
if (this.baseUrl) {
193+
config.baseURL = this.baseUrl;
194+
}
195+
191196
this.provider = new Msal2Provider(config);
192197
Providers.globalProvider = this.provider;
193198
}

0 commit comments

Comments
 (0)