-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ElectronContextBridgeProvider (#2914)
* feat: add ElectronContextBridgeProvider * fix: missing export for ElectronContextBridgeProvider --------- Co-authored-by: Gavin Barron <gavinbarron@microsoft.com>
- Loading branch information
1 parent
ebf5ed3
commit 8900eb4
Showing
3 changed files
with
192 additions
and
1 deletion.
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
109 changes: 109 additions & 0 deletions
109
packages/providers/mgt-electron-provider/src/Provider/ElectronContextBridgeProvider.ts
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,109 @@ | ||
/** | ||
* ------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. | ||
* See License in the project root for license information. | ||
* ------------------------------------------------------------------------------------------- | ||
*/ | ||
|
||
import { | ||
GraphEndpoint, | ||
IProvider, | ||
MICROSOFT_GRAPH_DEFAULT_ENDPOINT, | ||
ProviderState, | ||
Providers, | ||
createFromProvider | ||
} from '@microsoft/mgt-element'; | ||
import { AuthenticationProviderOptions } from '@microsoft/microsoft-graph-client'; | ||
import type { IpcRendererEvent } from 'electron'; | ||
|
||
/** | ||
* The interface that describes the shape of the context bridge | ||
* that is used to communicate between the main and renderer processes. | ||
*/ | ||
export interface IContextBridgeImpl { | ||
mgtAuthState: (callback: (event: IpcRendererEvent, authState: string) => void) => void; | ||
token: (options?: AuthenticationProviderOptions) => Promise<string>; | ||
login: () => Promise<void>; | ||
logout: () => Promise<void>; | ||
} | ||
|
||
/** | ||
* ElectronProvider class to be instantiated in the a preload script. | ||
* Responsible for communicating with ElectronAuthenticator in the main process to acquire tokens. | ||
* | ||
* This class uses the `contextBridge` to communicate with the main process, | ||
* which must be passed in as a constructor parameter. | ||
* | ||
* @export | ||
* @class ElectronProvider | ||
* @extends {IProvider} | ||
*/ | ||
export class ElectronContextBridgeProvider extends IProvider { | ||
/** | ||
* Name used for analytics | ||
* | ||
* @readonly | ||
* @memberof IProvider | ||
*/ | ||
public get name() { | ||
return 'MgtElectronContextBridgeProvider'; | ||
} | ||
|
||
private readonly contextBridge: IContextBridgeImpl; | ||
|
||
constructor(contextBridge: IContextBridgeImpl, baseUrl: GraphEndpoint = MICROSOFT_GRAPH_DEFAULT_ENDPOINT) { | ||
super(); | ||
this.baseURL = baseUrl; | ||
this.contextBridge = contextBridge; | ||
this.graph = createFromProvider(this); | ||
this.setupProvider(); | ||
} | ||
|
||
/** | ||
* Sets up messaging between main and renderer to receive SignedIn/SignedOut state information | ||
* | ||
* @memberof ElectronProvider | ||
*/ | ||
setupProvider() { | ||
this.contextBridge.mgtAuthState((event, authState) => { | ||
if (authState === 'logged_in') { | ||
Providers.globalProvider.setState(ProviderState.SignedIn); | ||
} else if (authState === 'logged_out') { | ||
Providers.globalProvider.setState(ProviderState.SignedOut); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Gets access token (called by MGT components) | ||
* | ||
* @param {AuthenticationProviderOptions} [options] | ||
* @return {*} {Promise<string>} | ||
* @memberof ElectronProvider | ||
*/ | ||
async getAccessToken(options?: AuthenticationProviderOptions): Promise<string> { | ||
const token = await this.contextBridge.token(options); | ||
return token; | ||
} | ||
|
||
/** | ||
* Log in to set account information (called by mgt-login) | ||
* | ||
* @return {*} {Promise<void>} | ||
* @memberof ElectronProvider | ||
*/ | ||
async login(): Promise<void> { | ||
Providers.globalProvider.setState(ProviderState.Loading); | ||
await this.contextBridge.login(); | ||
} | ||
|
||
/** | ||
* Log out (called by mgt-login) | ||
* | ||
* @return {*} {Promise<void>} | ||
* @memberof ElectronProvider | ||
*/ | ||
async logout(): Promise<void> { | ||
await this.contextBridge.logout(); | ||
} | ||
} |
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