-
Notifications
You must be signed in to change notification settings - Fork 279
/
parameterizedBotFrameworkAuthentication.ts
538 lines (474 loc) · 22.6 KB
/
parameterizedBotFrameworkAuthentication.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { Activity, Channels, RoleTypes, StatusCodes } from 'botframework-schema';
import { AuthenticateRequestResult } from './authenticateRequestResult';
import type { AuthenticationConfiguration } from './authenticationConfiguration';
import { AuthenticationConstants } from './authenticationConstants';
import { AuthenticationError } from './authenticationError';
import { BotFrameworkAuthentication } from './botFrameworkAuthentication';
import { ConnectorClientOptions } from '../connectorApi/models';
import type { ConnectorFactory } from './connectorFactory';
import { ConnectorFactoryImpl } from './connectorFactoryImpl';
import type { BotFrameworkClient } from '../skills';
import { BotFrameworkClientImpl } from './botFrameworkClientImpl';
import { Claim, ClaimsIdentity } from './claimsIdentity';
import { EmulatorValidation } from './emulatorValidation';
import { JwtTokenExtractor } from './jwtTokenExtractor';
import { JwtTokenValidation } from './jwtTokenValidation';
import type { ServiceClientCredentialsFactory } from './serviceClientCredentialsFactory';
import { SkillValidation } from './skillValidation';
import { ToBotFromBotOrEmulatorTokenValidationParameters } from './tokenValidationParameters';
import { UserTokenClientImpl } from './userTokenClientImpl';
import type { UserTokenClient } from './userTokenClient';
import { VerifyOptions } from 'jsonwebtoken';
function getAppId(claimsIdentity: ClaimsIdentity): string | undefined {
// For requests from channel App Id is in Audience claim of JWT token. For emulator it is in AppId claim. For
// unauthenticated requests we have anonymous claimsIdentity provided auth is disabled.
// For Activities coming from Emulator AppId claim contains the Bot's AAD AppId.
return (
claimsIdentity.getClaimValue(AuthenticationConstants.AudienceClaim) ??
claimsIdentity.getClaimValue(AuthenticationConstants.AppIdClaim) ??
undefined
);
}
/**
* @internal
* Parameterized [BotFrameworkAuthentication](xref:botframework-connector.BotFrameworkAuthentication) used to authenticate Bot Framework Protocol network calls within this environment.
*/
export class ParameterizedBotFrameworkAuthentication extends BotFrameworkAuthentication {
/**
* @param validateAuthority The validate authority value to use.
* @param toChannelFromBotLoginUrl The to Channel from bot login url.
* @param toChannelFromBotOAuthScope The to Channel from bot oauth scope.
* @param toBotFromChannelTokenIssuer The to bot from Channel Token Issuer.
* @param oAuthUrl The OAuth url.
* @param toBotFromChannelOpenIdMetadataUrl The to bot from Channel Open Id Metadata url.
* @param toBotFromEmulatorOpenIdMetadataUrl The to bot from Emulator Open Id Metadata url.
* @param callerId The callerId set on an authenticated [Activities](xref:botframework-schema.Activity).
* @param credentialsFactory The [ServiceClientCredentialsFactory](xref:botframework-connector.ServiceClientCredentialsFactory) to use to create credentials.
* @param authConfiguration The [AuthenticationConfiguration](xref:botframework-connector.AuthenticationConfiguration) to use.
* @param botFrameworkClientFetch The fetch to use in BotFrameworkClient.
* @param connectorClientOptions The [ConnectorClientOptions](xref:botframework-connector.ConnectorClientOptions) to use when creating ConnectorClients.
*/
constructor(
private readonly validateAuthority: boolean,
private readonly toChannelFromBotLoginUrl: string,
private readonly toChannelFromBotOAuthScope: string,
private readonly toBotFromChannelTokenIssuer: string,
private readonly oAuthUrl: string,
private readonly toBotFromChannelOpenIdMetadataUrl: string,
private readonly toBotFromEmulatorOpenIdMetadataUrl: string,
private readonly callerId: string,
private readonly credentialsFactory: ServiceClientCredentialsFactory,
private readonly authConfiguration: AuthenticationConfiguration,
private readonly botFrameworkClientFetch?: (input: RequestInfo, init?: RequestInit) => Promise<Response>,
private readonly connectorClientOptions: ConnectorClientOptions = {}
) {
super();
}
/**
* Gets the originating audience from Bot OAuth scope.
*
* @returns The originating audience.
*/
getOriginatingAudience(): string {
return this.toChannelFromBotOAuthScope;
}
/**
* @param authHeader The http auth header received in the skill request.
* @returns The identity validation result.
*/
async authenticateChannelRequest(authHeader: string): Promise<ClaimsIdentity> {
if (!authHeader.trim()) {
const isAuthDisabled = await this.credentialsFactory.isAuthenticationDisabled();
if (!isAuthDisabled) {
throw new AuthenticationError(
'Unauthorized Access. Request is not authorized',
StatusCodes.UNAUTHORIZED
);
}
// In the scenario where auth is disabled, we still want to have the isAuthenticated flag set in the
// ClaimsIdentity. To do this requires adding in an empty claim. Since ChannelServiceHandler calls are
// always a skill callback call, we set the skill claim too.
return SkillValidation.createAnonymousSkillClaim();
}
return this.JwtTokenValidation_validateAuthHeader(authHeader, 'unknown', null);
}
/**
* Validate Bot Framework Protocol requests.
*
* @param activity The inbound Activity.
* @param authHeader The http auth header received in the skill request.
* @returns Promise with AuthenticateRequestResult.
*/
async authenticateRequest(activity: Activity, authHeader: string): Promise<AuthenticateRequestResult> {
const claimsIdentity = await this.JwtTokenValidation_authenticateRequest(activity, authHeader);
const outboundAudience = SkillValidation.isSkillClaim(claimsIdentity.claims)
? JwtTokenValidation.getAppIdFromClaims(claimsIdentity.claims)
: this.toChannelFromBotOAuthScope;
const callerId = await this.generateCallerId(this.credentialsFactory, claimsIdentity, this.callerId);
const connectorFactory = new ConnectorFactoryImpl(
getAppId(claimsIdentity),
this.toChannelFromBotOAuthScope,
this.toChannelFromBotLoginUrl,
this.validateAuthority,
this.credentialsFactory
);
return {
audience: outboundAudience,
callerId,
claimsIdentity,
connectorFactory,
};
}
/**
* Validate Bot Framework Protocol requests.
*
* @param authHeader The http auth header received in the skill request.
* @param channelIdHeader The channel Id HTTP header.
* @returns Promise with AuthenticateRequestResult.
*/
async authenticateStreamingRequest(
authHeader: string,
channelIdHeader: string
): Promise<AuthenticateRequestResult> {
if (!channelIdHeader?.trim() && !(await this.credentialsFactory.isAuthenticationDisabled())) {
throw new AuthenticationError("'channelIdHeader' required.", StatusCodes.UNAUTHORIZED);
}
const claimsIdentity = await this.JwtTokenValidation_validateAuthHeader(authHeader, channelIdHeader, null);
const outboundAudience = SkillValidation.isSkillClaim(claimsIdentity.claims)
? JwtTokenValidation.getAppIdFromClaims(claimsIdentity.claims)
: this.toChannelFromBotOAuthScope;
const callerId = await this.generateCallerId(this.credentialsFactory, claimsIdentity, this.callerId);
return { audience: outboundAudience, callerId, claimsIdentity };
}
/**
* Creates the appropriate UserTokenClient instance.
*
* @param claimsIdentity The inbound Activity's ClaimsIdentity.
* @returns Promise with UserTokenClient instance.
*/
async createUserTokenClient(claimsIdentity: ClaimsIdentity): Promise<UserTokenClient> {
const appId = getAppId(claimsIdentity);
const credentials = await this.credentialsFactory.createCredentials(
appId,
this.toChannelFromBotOAuthScope,
this.toChannelFromBotLoginUrl,
this.validateAuthority
);
return new UserTokenClientImpl(appId, credentials, this.oAuthUrl, this.connectorClientOptions);
}
/**
* Creates a ConnectorFactory that can be used to create ConnectorClients that can use credentials from this particular Cloud Environment.
*
* @param claimsIdentity The inbound Activity's ClaimsIdentity.
* @returns A ConnectorFactory.
*/
createConnectorFactory(claimsIdentity: ClaimsIdentity): ConnectorFactory {
return new ConnectorFactoryImpl(
getAppId(claimsIdentity),
this.toChannelFromBotOAuthScope,
this.toChannelFromBotLoginUrl,
this.validateAuthority,
this.credentialsFactory
);
}
/**
* Creates a BotFrameworkClient used for calling Skills.
*
* @returns A BotFrameworkClient instance to call Skills.
*/
createBotFrameworkClient(): BotFrameworkClient {
return new BotFrameworkClientImpl(
this.credentialsFactory,
this.toChannelFromBotLoginUrl,
this.botFrameworkClientFetch
);
}
private async JwtTokenValidation_authenticateRequest(
activity: Partial<Activity>,
authHeader: string
): Promise<ClaimsIdentity> {
if (!authHeader.trim()) {
const isAuthDisabled = await this.credentialsFactory.isAuthenticationDisabled();
if (!isAuthDisabled) {
throw new AuthenticationError(
'Unauthorized Access. Request is not authorized',
StatusCodes.UNAUTHORIZED
);
}
// Check if the activity is for a skill call and is coming from the Emulator.
if (activity.channelId === Channels.Emulator && activity.recipient?.role === RoleTypes.Skill) {
return SkillValidation.createAnonymousSkillClaim();
}
// In the scenario where Auth is disabled, we still want to have the
// IsAuthenticated flag set in the ClaimsIdentity. To do this requires
// adding in an empty claim.
return new ClaimsIdentity([], AuthenticationConstants.AnonymousAuthType);
}
const claimsIdentity: ClaimsIdentity = await this.JwtTokenValidation_validateAuthHeader(
authHeader,
activity.channelId,
activity.serviceUrl
);
return claimsIdentity;
}
private async JwtTokenValidation_validateAuthHeader(
authHeader: string,
channelId: string,
serviceUrl = ''
): Promise<ClaimsIdentity> {
const identity = await this.JwtTokenValidation_authenticateToken(authHeader, channelId, serviceUrl);
await this.JwtTokenValidation_validateClaims(identity.claims);
return identity;
}
private async JwtTokenValidation_validateClaims(claims: Claim[] = []): Promise<void> {
if (this.authConfiguration.validateClaims) {
// Call the validation method if defined (it should throw an exception if the validation fails)
await this.authConfiguration.validateClaims(claims);
} else if (SkillValidation.isSkillClaim(claims)) {
// Skill claims must be validated using AuthenticationConfiguration validateClaims
throw new AuthenticationError(
'Unauthorized Access. Request is not authorized. Skill Claims require validation.',
StatusCodes.UNAUTHORIZED
);
}
}
private async JwtTokenValidation_authenticateToken(
authHeader: string,
channelId: string,
serviceUrl: string
): Promise<ClaimsIdentity | undefined> {
if (SkillValidation.isSkillToken(authHeader)) {
return this.SkillValidation_authenticateChannelToken(authHeader, channelId);
}
if (EmulatorValidation.isTokenFromEmulator(authHeader)) {
return this.EmulatorValidation_authenticateEmulatorToken(authHeader, channelId);
}
// Handle requests from BotFramework Channels
return this.ChannelValidation_authenticateChannelToken(authHeader, serviceUrl, channelId);
}
private async SkillValidation_authenticateChannelToken(
authHeader: string,
channelId: string
): Promise<ClaimsIdentity> {
// Add allowed token issuers from configuration.
const verifyOptions: VerifyOptions = {
...ToBotFromBotOrEmulatorTokenValidationParameters,
issuer: [
...ToBotFromBotOrEmulatorTokenValidationParameters.issuer,
...(this.authConfiguration.validTokenIssuers ?? []),
],
};
const tokenExtractor = new JwtTokenExtractor(
verifyOptions,
this.toBotFromEmulatorOpenIdMetadataUrl,
AuthenticationConstants.AllowedSigningAlgorithms
);
const parts: string[] = authHeader.split(' ');
const identity = await tokenExtractor.getIdentity(
parts[0],
parts[1],
channelId,
this.authConfiguration.requiredEndorsements
);
await this.SkillValidation_ValidateIdentity(identity);
return identity;
}
private async SkillValidation_ValidateIdentity(identity: ClaimsIdentity): Promise<void> {
if (!identity) {
// No valid identity. Not Authorized.
throw new AuthenticationError(
'SkillValidation.validateIdentity(): Invalid identity',
StatusCodes.UNAUTHORIZED
);
}
if (!identity.isAuthenticated) {
// The token is in some way invalid. Not Authorized.
throw new AuthenticationError(
'SkillValidation.validateIdentity(): Token not authenticated',
StatusCodes.UNAUTHORIZED
);
}
const versionClaim = identity.getClaimValue(AuthenticationConstants.VersionClaim);
if (!versionClaim) {
// No version claim
throw new AuthenticationError(
`SkillValidation.validateIdentity(): '${AuthenticationConstants.VersionClaim}' claim is required on skill Tokens.`,
StatusCodes.UNAUTHORIZED
);
}
// Look for the "aud" claim, but only if issued from the Bot Framework
const audienceClaim = identity.getClaimValue(AuthenticationConstants.AudienceClaim);
if (!audienceClaim) {
// Claim is not present or doesn't have a value. Not Authorized.
throw new AuthenticationError(
`SkillValidation.validateIdentity(): '${AuthenticationConstants.AudienceClaim}' claim is required on skill Tokens.`,
StatusCodes.UNAUTHORIZED
);
}
if (!(await this.credentialsFactory.isValidAppId(audienceClaim))) {
// The AppId is not valid. Not Authorized.
throw new AuthenticationError(
'SkillValidation.validateIdentity(): Invalid audience.',
StatusCodes.UNAUTHORIZED
);
}
const appId = JwtTokenValidation.getAppIdFromClaims(identity.claims);
if (!appId) {
// Invalid appId
throw new AuthenticationError(
'SkillValidation.validateIdentity(): Invalid appId.',
StatusCodes.UNAUTHORIZED
);
}
}
private async EmulatorValidation_authenticateEmulatorToken(
authHeader: string,
channelId: string
): Promise<ClaimsIdentity> {
// Add allowed token issuers from configuration.
const verifyOptions: VerifyOptions = {
...ToBotFromBotOrEmulatorTokenValidationParameters,
issuer: [
...ToBotFromBotOrEmulatorTokenValidationParameters.issuer,
...(this.authConfiguration.validTokenIssuers ?? []),
],
};
const tokenExtractor: JwtTokenExtractor = new JwtTokenExtractor(
verifyOptions,
this.toBotFromEmulatorOpenIdMetadataUrl,
AuthenticationConstants.AllowedSigningAlgorithms
);
const identity: ClaimsIdentity = await tokenExtractor.getIdentityFromAuthHeader(
authHeader,
channelId,
this.authConfiguration.requiredEndorsements
);
if (!identity) {
// No valid identity. Not Authorized.
throw new AuthenticationError('Unauthorized. No valid identity.', StatusCodes.UNAUTHORIZED);
}
if (!identity.isAuthenticated) {
// The token is in some way invalid. Not Authorized.
throw new AuthenticationError('Unauthorized. Is not authenticated', StatusCodes.UNAUTHORIZED);
}
// Now check that the AppID in the claimset matches
// what we're looking for. Note that in a multi-tenant bot, this value
// comes from developer code that may be reaching out to a service, hence the
// Async validation.
const versionClaim: string = identity.getClaimValue(AuthenticationConstants.VersionClaim);
if (versionClaim === null) {
throw new AuthenticationError(
'Unauthorized. "ver" claim is required on Emulator Tokens.',
StatusCodes.UNAUTHORIZED
);
}
let appId = '';
// The Emulator, depending on Version, sends the AppId via either the
// appid claim (Version 1) or the Authorized Party claim (Version 2).
if (!versionClaim || versionClaim === '1.0') {
// either no Version or a version of "1.0" means we should look for
// the claim in the "appid" claim.
const appIdClaim: string = identity.getClaimValue(AuthenticationConstants.AppIdClaim);
if (!appIdClaim) {
// No claim around AppID. Not Authorized.
throw new AuthenticationError(
'Unauthorized. "appid" claim is required on Emulator Token version "1.0".',
StatusCodes.UNAUTHORIZED
);
}
appId = appIdClaim;
} else if (versionClaim === '2.0') {
// Emulator, "2.0" puts the AppId in the "azp" claim.
const appZClaim: string = identity.getClaimValue(AuthenticationConstants.AuthorizedParty);
if (!appZClaim) {
// No claim around AppID. Not Authorized.
throw new AuthenticationError(
'Unauthorized. "azp" claim is required on Emulator Token version "2.0".',
StatusCodes.UNAUTHORIZED
);
}
appId = appZClaim;
} else {
// Unknown Version. Not Authorized.
throw new AuthenticationError(
`Unauthorized. Unknown Emulator Token version "${versionClaim}".`,
StatusCodes.UNAUTHORIZED
);
}
if (!(await this.credentialsFactory.isValidAppId(appId))) {
throw new AuthenticationError(
`Unauthorized. Invalid AppId passed on token: ${appId}`,
StatusCodes.UNAUTHORIZED
);
}
return identity;
}
private async ChannelValidation_authenticateChannelToken(
authHeader: string,
serviceUrl: string,
channelId: string
): Promise<ClaimsIdentity> {
const tokenValidationParameters = this.ChannelValidation_GetTokenValidationParameters();
const tokenExtractor: JwtTokenExtractor = new JwtTokenExtractor(
tokenValidationParameters,
this.toBotFromChannelOpenIdMetadataUrl,
AuthenticationConstants.AllowedSigningAlgorithms
);
const identity: ClaimsIdentity = await tokenExtractor.getIdentityFromAuthHeader(
authHeader,
channelId,
this.authConfiguration.requiredEndorsements
);
return this.governmentChannelValidation_ValidateIdentity(identity, serviceUrl);
}
private ChannelValidation_GetTokenValidationParameters(): VerifyOptions {
return {
issuer: [this.toBotFromChannelTokenIssuer],
audience: undefined, // Audience validation takes place manually in code.
clockTolerance: 5 * 60,
ignoreExpiration: false,
};
}
private async governmentChannelValidation_ValidateIdentity(
identity: ClaimsIdentity,
serviceUrl: string
): Promise<ClaimsIdentity> {
if (!identity) {
// No valid identity. Not Authorized.
throw new AuthenticationError('Unauthorized. No valid identity.', StatusCodes.UNAUTHORIZED);
}
if (!identity.isAuthenticated) {
// The token is in some way invalid. Not Authorized.
throw new AuthenticationError('Unauthorized. Is not authenticated', StatusCodes.UNAUTHORIZED);
}
// Now check that the AppID in the claimset matches
// what we're looking for. Note that in a multi-tenant bot, this value
// comes from developer code that may be reaching out to a service, hence the
// Async validation.
// Look for the "aud" claim, but only if issued from the Bot Framework
if (identity.getClaimValue(AuthenticationConstants.IssuerClaim) !== this.toBotFromChannelTokenIssuer) {
// The relevant Audiance Claim MUST be present. Not Authorized.
throw new AuthenticationError('Unauthorized. Issuer Claim MUST be present.', StatusCodes.UNAUTHORIZED);
}
// The AppId from the claim in the token must match the AppId specified by the developer.
// In this case, the token is destined for the app, so we find the app ID in the audience claim.
const audClaim: string = identity.getClaimValue(AuthenticationConstants.AudienceClaim);
if (!(await this.credentialsFactory.isValidAppId(audClaim || ''))) {
// The AppId is not valid or not present. Not Authorized.
throw new AuthenticationError(
`Unauthorized. Invalid AppId passed on token: ${audClaim}`,
StatusCodes.UNAUTHORIZED
);
}
if (serviceUrl) {
const serviceUrlClaim = identity.getClaimValue(AuthenticationConstants.ServiceUrlClaim);
if (serviceUrlClaim !== serviceUrl) {
// Claim must match. Not Authorized.
throw new AuthenticationError('Unauthorized. ServiceUrl claim do not match.', StatusCodes.UNAUTHORIZED);
}
}
return identity;
}
}