Skip to content

Commit

Permalink
Fixed #1268 - blank child window when using auth while offline (#1311)
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Wilaby committed Feb 11, 2019
1 parent 445f75c commit 77d9813
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 27 deletions.
41 changes: 30 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions packages/app/main/src/ngrok.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ export async function connect(opts: Partial<NgrokOptions>): Promise<{ url; inspe
return pendingConnection;
}
await getNgrokInspectUrl(options);
pendingConnection = runTunnel(options);

return pendingConnection;
return runTunnel(options);
}

async function getNgrokInspectUrl(opts: NgrokOptions): Promise<{ inspectUrl: string }> {
Expand Down
1 change: 0 additions & 1 deletion packages/emulator/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"npmlog": "^4.1.2",
"on-error-resume-next": "^1.0.0",
"rsa-pem-from-mod-exp": "^0.8.4",
"sha.js": "2.4.11",
"simple-update-in": "^1.1.1",
"uuid": "^3.3.2"
},
Expand Down
120 changes: 120 additions & 0 deletions packages/emulator/core/src/utils/oauthLinkEncoder.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { AttachmentContentTypes } from '@bfemulator/sdk-shared';
import OAuthLinkEncoder from './oauthLinkEncoder';
jest.mock('./uniqueId', () => () => 'fgfdsggf5432534');
const mockArgsSentToFetch = [];
let ok = true;
let statusText = '';
let shouldThrow = false;
describe('The OauthLinkEncoder', () => {
let encoder: OAuthLinkEncoder;
beforeAll(() => {
(global as any).fetch = async (...args) => {
mockArgsSentToFetch.push(args);
if (shouldThrow) {
throw new Error("I'm in your throw!");
}
return {
text: async () => 'im in your text!',
ok,
statusText,
};
};
});

beforeEach(() => {
ok = true;
statusText = '';
shouldThrow = false;
mockArgsSentToFetch.length = 0;
const emulator = {
getServiceUrl: async () => 'http://localhost',
facilities: {
conversations: {
conversationById: () => ({
codeVerifier: '5432654365475677647655676542524352563457',
botEndpoint: {
botUrl: 'http://botbot.bot',
},
conversationId: 'testConversation',
}),
},
},
};
encoder = new OAuthLinkEncoder(
emulator as any,
'Bearer 54k52n',
{
attachments: [{ contentType: AttachmentContentTypes.oAuthCard }],
text: 'a message',
},
'testConversation'
);
});

it('should resolveOAuthCards as expected with the happy path', async () => {
const mockActivity = {
attachments: [
{
contentType: AttachmentContentTypes.oAuthCard,
content: {
buttons: [{ type: 'signin' }],
},
},
],
text: 'a message',
};
await encoder.resolveOAuthCards(mockActivity);
expect(mockActivity.attachments[0].content.buttons[0]).toEqual({
type: 'openUrl',
value: 'oauthlink://im in your text!&&&testConversation',
});
});

it('should throw when an error occurs retrieving the link while calling resolveOAuthCards', async () => {
ok = false;
statusText = 'oh noes!';
const mockActivity = {
attachments: [
{
contentType: AttachmentContentTypes.oAuthCard,
content: {
buttons: [{ type: 'signin' }],
},
},
],
text: 'a message',
};
try {
await encoder.resolveOAuthCards(mockActivity);
expect(false);
} catch (e) {
expect(e.message).toEqual(statusText);
}
});

it('should throw if fetch throws', async () => {
shouldThrow = true;
const mockActivity = {
attachments: [
{
contentType: AttachmentContentTypes.oAuthCard,
content: {
buttons: [{ type: 'signin' }],
},
},
],
text: 'a message',
};
try {
await encoder.resolveOAuthCards(mockActivity);
expect(false);
} catch (e) {
expect(e.message).toEqual("I'm in your throw!");
}
});

it('should generateCodeVerifier as expected', async () => {
const v = encoder.generateCodeVerifier('testConversation');
expect(v).toBe('84731c2a08da84c59261d2a79a2b1a0bc6ca70b1d1fc2ce9eb74f4ad979d7dad');
});
});
31 changes: 19 additions & 12 deletions packages/emulator/core/src/utils/oauthLinkEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@
import { Attachment, AttachmentContentTypes, GenericActivity, OAuthCard } from '@bfemulator/sdk-shared';

import { BotEmulator } from '../botEmulator';
import uniqueId from '../utils/uniqueId';
import uniqueId from './uniqueId';

// eslint-disable-next-line typescript/no-var-requires
const shajs = require('sha.js');
import * as crypto from 'crypto';

export default class OAuthLinkEncoder {
public static OAuthUrlProtocol: string = 'oauthlink:';
Expand Down Expand Up @@ -91,11 +90,10 @@ export default class OAuthLinkEncoder {
const conversation = this.botEmulator.facilities.conversations.conversationById(conversationId);
conversation.codeVerifier = codeVerifier;

const codeChallenge: string = shajs('sha256')
return crypto
.createHash('sha256')
.update(codeVerifier)
.digest('hex');

return codeChallenge;
}

private async getSignInLink(connectionName: string, codeChallenge: string): Promise<string> {
Expand Down Expand Up @@ -125,12 +123,21 @@ export default class OAuthLinkEncoder {
emulatorUrl +
'&code_challenge=' +
codeChallenge;
let errorMessage: string;
try {
const response = await fetch(url, {
headers,
method: 'GET',
});
if (response.ok) {
const link = await response.text();
return OAuthLinkEncoder.OAuthUrlProtocol + '//' + link + '&&&' + this.conversationId;
}
errorMessage = response.statusText;
} catch (e) {
errorMessage = e.message;
}

const response = await fetch(url, {
headers,
method: 'GET',
});
const link = await response.text();
return OAuthLinkEncoder.OAuthUrlProtocol + '//' + link + '&&&' + this.conversationId;
throw new Error(errorMessage);
}
}

0 comments on commit 77d9813

Please sign in to comment.