This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BotBuilder-Skills][TypeScript] Update with latest changes (#1263)
* Add dependency and temporal registry * Add new classes * Add auth methods * Implement skillHttpAdapter * Fix issue with skillDialog * Fix TSLint issues * Add WebSocket implementation * Add WebSocket implementation * Fix TSLint issues * Latest changes * Fix npm-shrinkwrap.json
- Loading branch information
Showing
38 changed files
with
2,462 additions
and
1,370 deletions.
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
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,13 @@ | ||
/** | ||
* Copyright(c) Microsoft Corporation.All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
import { InvokeResponse, TurnContext } from 'botbuilder'; | ||
import { Activity } from 'botframework-schema'; | ||
|
||
export type BotCallbackHandler = (turnContext: TurnContext) => Promise<void>; | ||
|
||
export interface IActivityHandler { | ||
processActivity(activity: Activity, callback: BotCallbackHandler): Promise<InvokeResponse>; | ||
} |
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
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
54 changes: 0 additions & 54 deletions
54
lib/typescript/botbuilder-skills/src/auth/jwtClaimAuthProvider.ts
This file was deleted.
Oops, something went wrong.
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
67 changes: 67 additions & 0 deletions
67
lib/typescript/botbuilder-skills/src/auth/msJWTAuthenticationProvider.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,67 @@ | ||
/** | ||
* Copyright(c) Microsoft Corporation.All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
// Extends verify definitions to be compatible with callback handler to resolve signingKey | ||
declare module 'jsonwebtoken' { | ||
export type signingKeyResolver = (headers: jwks.Headers, cb: (err: Error, signingKey: string) => void) => void; | ||
|
||
export function verify( | ||
token: string, | ||
secretOrPublicKey: signingKeyResolver, | ||
callback?: VerifyCallback | ||
): void; | ||
} | ||
|
||
import { HttpOperationResponse, ServiceClient } from '@azure/ms-rest-js'; | ||
import { signingKeyResolver, verify } from 'jsonwebtoken'; | ||
import * as jwks from 'jwks-rsa'; | ||
import { IAuthenticationProvider } from './authenticationProvider'; | ||
|
||
export class MsJWTAuthenticationProvider implements IAuthenticationProvider { | ||
private readonly openIdMetadataUrl: string = 'https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration'; | ||
private readonly jwtIssuer: string = 'https://login.microsoftonline.com/72f988bf-86f1-41af-91ab-2d7cd011db47/v2.0'; | ||
private readonly httpClient: ServiceClient; | ||
private readonly appId: string; | ||
|
||
constructor(appId: string) { | ||
this.httpClient = new ServiceClient(); | ||
this.appId = appId; | ||
} | ||
|
||
public async authenticate(authHeader: string): Promise<boolean> { | ||
try { | ||
const token: string = authHeader.includes(' ') ? authHeader.split(' ')[1] : authHeader; | ||
|
||
const jwksInfo: HttpOperationResponse = await this.httpClient.sendRequest({ | ||
method: 'GET', | ||
url: this.openIdMetadataUrl | ||
}); | ||
|
||
const jwksUri: string = <string>jwksInfo.parsedBody.jwks_uri; | ||
const jwksClient: jwks.JwksClient = jwks({ jwksUri: jwksUri }); | ||
|
||
const getKey: signingKeyResolver = (headers: jwks.Headers, cb: (err: Error, signingKey: string) => void): void => { | ||
jwksClient.getSigningKey(headers.kid, (err: Error, key: jwks.Jwk) => { | ||
cb(err, key.publicKey || key.rsaPublicKey || ''); | ||
}); | ||
}; | ||
|
||
// tslint:disable-next-line:typedef | ||
const decoder: Promise<{[key: string]: Object}> = new Promise((resolve, reject) => { | ||
verify(token, getKey, (err: Error, decodedObj: Object) => { | ||
if (err) { reject(err); } | ||
const result: {[key: string]: Object} = <{[key: string]: Object}>decodedObj; | ||
resolve(result); | ||
}); | ||
}); | ||
|
||
const decoded: { [key: string]: Object } = await decoder; | ||
|
||
return decoded.appid === this.appId; | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
lib/typescript/botbuilder-skills/src/auth/serviceClientCredentials.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,11 @@ | ||
/** | ||
* Copyright(c) Microsoft Corporation.All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
import { WebResource } from '@azure/ms-rest-js'; | ||
|
||
export interface IServiceClientCredentials { | ||
getToken(forceRefresh?: boolean): Promise<string>; | ||
processHttpRequest(request: WebResource): Promise<void>; | ||
} |
10 changes: 0 additions & 10 deletions
10
lib/typescript/botbuilder-skills/src/auth/skillAuthProvider.ts
This file was deleted.
Oops, something went wrong.
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,8 @@ | ||
/** | ||
* Copyright(c) Microsoft Corporation.All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
export * from './skillHttpAdapter'; | ||
export * from './skillHttpBotAdapter'; | ||
export * from './skillHttpTransport'; |
104 changes: 104 additions & 0 deletions
104
lib/typescript/botbuilder-skills/src/http/skillHttpAdapter.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,104 @@ | ||
import { BotFrameworkAdapter, BotFrameworkAdapterSettings, BotTelemetryClient, InvokeResponse, | ||
Severity, TurnContext, WebRequest, WebResponse } from 'botbuilder'; | ||
import { TelemetryExtensions } from 'botbuilder-solutions'; | ||
import { Activity } from 'botframework-schema'; | ||
import { IActivityHandler } from '../activityHandler'; | ||
import { IAuthenticationProvider } from '../auth'; | ||
import { SkillHttpBotAdapter } from './skillHttpBotAdapter'; | ||
|
||
/** | ||
* This adapter is responsible for accepting a bot-to-bot call over http transport. | ||
* It'll perform the following tasks: | ||
* 1. Authentication. | ||
* 2. Call SkillHttpBotAdapter to process the incoming activity. | ||
*/ | ||
export class SkillHttpAdapter extends BotFrameworkAdapter { | ||
private readonly authHeaderName: string = 'Authorization'; | ||
|
||
private readonly botAdapter: IActivityHandler; | ||
private readonly authenticationProvider?: IAuthenticationProvider; | ||
private readonly telemetryClient?: BotTelemetryClient; | ||
|
||
constructor( | ||
botAdapter: SkillHttpBotAdapter, | ||
authenticationProvider?: IAuthenticationProvider, | ||
telemetryClient?: BotTelemetryClient, | ||
config?: Partial<BotFrameworkAdapterSettings> | ||
) { | ||
super(config); | ||
this.botAdapter = botAdapter; | ||
this.authenticationProvider = authenticationProvider; | ||
this.telemetryClient = telemetryClient; | ||
} | ||
|
||
// tslint:disable-next-line:no-any | ||
public async processActivity(req: WebRequest, res: WebResponse, logic: (context: TurnContext) => Promise<any>): Promise<void> { | ||
if (this.authenticationProvider) { | ||
// grab the auth header from the inbound http request | ||
const headers: { [header: string]: string | string[] | undefined } = req.headers; | ||
const authHeader: string = <string> headers[this.authHeaderName]; | ||
const authenticated: boolean = await this.authenticationProvider.authenticate(authHeader); | ||
|
||
if (!authenticated) { | ||
res.status(401); | ||
res.end(); | ||
|
||
return; | ||
} | ||
} | ||
|
||
// deserialize the incoming Activity | ||
const activity: Activity = await parseRequest(req); | ||
|
||
if (this.telemetryClient) { | ||
const message: string = `SkillHttpAdapter: Processing incoming activity. Activity id: ${activity.id}`; | ||
TelemetryExtensions.trackTraceEx(this.telemetryClient, message, Severity.Information, activity); | ||
} | ||
|
||
// process the inbound activity with the bot | ||
const invokeResponse: InvokeResponse = await this.botAdapter.processActivity(activity, logic); | ||
|
||
// write the response, potentially serializing the InvokeResponse | ||
res.status(invokeResponse.status); | ||
if (invokeResponse.body) { | ||
res.send(invokeResponse.body); | ||
} | ||
|
||
res.end(); | ||
} | ||
} | ||
|
||
function parseRequest(req: WebRequest): Promise<Activity> { | ||
// tslint:disable-next-line:typedef | ||
return new Promise((resolve, reject): void => { | ||
function returnActivity(activity: Activity): void { | ||
if (typeof activity !== 'object') { throw new Error(`BotFrameworkAdapter.parseRequest(): invalid request body.`); } | ||
if (typeof activity.type !== 'string') { throw new Error(`BotFrameworkAdapter.parseRequest(): missing activity type.`); } | ||
if (typeof activity.timestamp === 'string') { activity.timestamp = new Date(activity.timestamp); } | ||
if (typeof activity.localTimestamp === 'string') { activity.localTimestamp = new Date(activity.localTimestamp); } | ||
if (typeof activity.expiration === 'string') { activity.expiration = new Date(activity.expiration); } | ||
resolve(activity); | ||
} | ||
|
||
if (req.body) { | ||
try { | ||
returnActivity(req.body); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
} else { | ||
let requestData: string = ''; | ||
req.on('data', (chunk: string) => { | ||
requestData += chunk; | ||
}); | ||
req.on('end', () => { | ||
try { | ||
req.body = JSON.parse(requestData); | ||
returnActivity(req.body); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
}); | ||
} | ||
}); | ||
} |
Oops, something went wrong.