Skip to content

Commit

Permalink
Merge pull request #4253 from ki0ki0/electron-sample-proper-fileProtocol
Browse files Browse the repository at this point in the history
handle request on custom protocol instead of 'will-redirect' event
  • Loading branch information
jasonnutter committed Jan 28, 2022
2 parents 2c550c9 + d4dad35 commit dc27ea9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ export abstract class AuthCodeListener {
return this.hostName;
}

public abstract start(): void;
public abstract start(): Promise<string>;
public abstract close(): void;
}
19 changes: 6 additions & 13 deletions samples/msal-node-samples/ElectronTestApp/src/AuthProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export default class AuthProvider {
private authCodeRequest: AuthorizationCodeRequest;
private silentProfileRequest: SilentFlowRequest;
private silentMailRequest: SilentFlowRequest;
private authCodeListener: AuthCodeListener;
constructor() {
this.clientApplication = new PublicClientApplication(MSAL_CONFIG);
this.account = null;
Expand Down Expand Up @@ -134,9 +133,6 @@ export default class AuthProvider {
// Get Auth Code URL
const authCodeUrl = await this.clientApplication.getAuthCodeUrl(authCodeUrlParams);

// Set up custom file protocol to listen for redirect response
this.authCodeListener = new CustomFileProtocolListener(CUSTOM_FILE_PROTOCOL_NAME);
this.authCodeListener.start();
const authCode = await this.listenForAuthCode(authCodeUrl, authWindow);

// Use Authorization Code and PKCE Code verifier to make token request
Expand Down Expand Up @@ -168,16 +164,13 @@ export default class AuthProvider {
}

private async listenForAuthCode(navigateUrl: string, authWindow: BrowserWindow): Promise<string> {
// Set up custom file protocol to listen for redirect response
const authCodeListener = new CustomFileProtocolListener(CUSTOM_FILE_PROTOCOL_NAME);
const codePromise = authCodeListener.start();
authWindow.loadURL(navigateUrl);
return new Promise((resolve, reject) => {
authWindow.webContents.on('will-redirect', (event, responseUrl) => {
const parsedUrl = new URL(responseUrl);
const authCode = parsedUrl.searchParams.get('code');
if (authCode) {
resolve(authCode);
}
});
});
const code = await codePromise;
authCodeListener.close();
return code;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { AuthCodeListener } from './AuthCodeListener';

import { protocol } from 'electron';
import * as path from 'path';
import * as url from 'url';

/**
* CustomFileProtocolListener can be instantiated in order
Expand All @@ -21,11 +20,22 @@ export class CustomFileProtocolListener extends AuthCodeListener {
* Registers a custom file protocol on which the library will
* listen for Auth Code response.
*/
public start(): void {
protocol.registerFileProtocol(this.host, (req, callback) => {
const requestUrl = url.parse(req.url, true);
callback(path.normalize(`${__dirname}/${requestUrl.path}`));
public start(): Promise<string> {
const codePromise = new Promise<string>((resolve, reject) => {
protocol.registerFileProtocol(this.host, (req, callback) => {
const requestUrl = new URL(req.url);
const authCode = requestUrl.searchParams.get('code');
if (authCode) {
resolve(authCode);
}
else {
reject(new Error("no code in URL"));
}
callback(path.normalize(`${__dirname}/${requestUrl.pathname}`));
});
});

return codePromise;
}

/**
Expand Down

0 comments on commit dc27ea9

Please sign in to comment.