Skip to content

Commit 7cdcfdb

Browse files
authored
Merge branch 'master' into updateNYC
2 parents 05cc2fc + c28faba commit 7cdcfdb

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

libraries/botbuilder/src/botFrameworkHttpClient.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class BotFrameworkHttpClient {
2929
*/
3030
private static readonly appCredentialMapCache: Map<string, MicrosoftAppCredentials> = new Map<string, MicrosoftAppCredentials>();
3131

32-
constructor(private readonly credentialProvider: ICredentialProvider, private readonly channelService?: string) {
32+
public constructor(private readonly credentialProvider: ICredentialProvider, private readonly channelService?: string) {
3333
if (!this.credentialProvider) {
3434
throw new Error('BotFrameworkHttpClient(): missing credentialProvider');
3535
}
@@ -96,6 +96,19 @@ export class BotFrameworkHttpClient {
9696
}
9797
}
9898

99+
protected async buildCredentials(appId: string, oAuthScope?: string): Promise<MicrosoftAppCredentials> {
100+
const appPassword = await this.credentialProvider.getAppPassword(appId);
101+
let appCredentials;
102+
if (JwtTokenValidation.isGovernment(this.channelService)) {
103+
appCredentials = new MicrosoftAppCredentials(appId, appPassword, this.channelService, oAuthScope);
104+
appCredentials.oAuthEndpoint = GovernmentConstants.ToChannelFromBotLoginUrl;
105+
appCredentials.oAuthScope = GovernmentConstants.ToChannelFromBotOAuthScope;
106+
} else {
107+
appCredentials = new MicrosoftAppCredentials(appId, appPassword, this.channelService, oAuthScope);
108+
}
109+
return appCredentials;
110+
}
111+
99112
/**
100113
* Gets the application credentials. App Credentials are cached so as to ensure we are not refreshing
101114
* token every time.
@@ -114,14 +127,8 @@ export class BotFrameworkHttpClient {
114127
return appCredentials;
115128
}
116129

117-
const appPassword = await this.credentialProvider.getAppPassword(appId);
118-
if (JwtTokenValidation.isGovernment(this.channelService)) {
119-
appCredentials = new MicrosoftAppCredentials(appId, appPassword, this.channelService, oAuthScope);
120-
appCredentials.oAuthEndpoint = GovernmentConstants.ToChannelFromBotLoginUrl;
121-
appCredentials.oAuthScope = GovernmentConstants.ToChannelFromBotOAuthScope;
122-
} else {
123-
appCredentials = new MicrosoftAppCredentials(appId, appPassword, this.channelService, oAuthScope);
124-
}
130+
// Credentials not found in cache, build them
131+
appCredentials = await this.buildCredentials(appId, oAuthScope);
125132

126133
// Cache the credentials for later use
127134
BotFrameworkHttpClient.appCredentialMapCache.set(cacheKey, appCredentials);

libraries/botbuilder/tests/botFrameworkHttpClient.test.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
const { strictEqual } = require('assert');
22
const { BotFrameworkHttpClient } = require('../');
3-
const { AuthenticationConstants, SimpleCredentialProvider } = require('botframework-connector');
3+
const { AuthenticationConstants, SimpleCredentialProvider, MicrosoftAppCredentials } = require('botframework-connector');
44
const nock = require('nock');
55

6+
class TestBotFrameworkHttpClient extends BotFrameworkHttpClient {
7+
constructor(credentialProvider, channelService) {
8+
super(credentialProvider, channelService);
9+
}
10+
async buildCredentials() {
11+
return new MicrosoftAppCredentials('', '');
12+
}
13+
}
14+
615
describe('BotFrameworkHttpClient', function() {
716
this.timeout(3000);
817
describe('constructor()', () => {
@@ -56,5 +65,18 @@ describe('BotFrameworkHttpClient', function() {
5665
const response = await client.postActivity(fromBotId, 'toBotId', 'http://skillUrl/api/bad', 'serviceUrl', 'conversationId', { type: 'message', conversation: { } });
5766
strictEqual(response.status, 404);
5867
});
68+
69+
it('should succeed to make call using override buildCredentials', async () => {
70+
71+
nock('http://skillUrl')
72+
.post('/api/good')
73+
.reply(200, { id: 'some-id' });
74+
75+
const credentialProvider = new SimpleCredentialProvider('this-is-not-the-app-id-your-looking-for', '1');
76+
const client = new TestBotFrameworkHttpClient(credentialProvider, 'channels');
77+
const fromBotId = 'this-is-not-the-app-id-your-looking-for';
78+
const response = await client.postActivity(fromBotId, 'toBotId', 'http://skillUrl/api/good', 'serviceUrl', 'conversationId', { type: 'message', conversation: { } });
79+
strictEqual(response.status, 200);
80+
});
5981
});
6082
});

0 commit comments

Comments
 (0)