-
Notifications
You must be signed in to change notification settings - Fork 281
/
endorsementsValidator.ts
52 lines (45 loc) · 2 KB
/
endorsementsValidator.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
/**
* @module botframework-connector
*/
import { AuthenticationError } from './authenticationError';
import { StatusCodes } from 'botframework-schema';
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
/**
* Contains helper methods for verifying JWT endorsements.
*/
export class EndorsementsValidator {
/**
* Verify that the set of ChannelIds, which come from the incoming activities,
* all match the endorsements found on the JWT Token.
* For example, if an Activity comes from webchat, that channelId says
* says "webchat" and the jwt token endorsement MUST match that.
*
* @param channelId The channel name, typically extracted from the activity.ChannelId field, that to which the Activity is affinitized.
* @param endorsements Whoever signed the JWT token is permitted to send activities only for
* some specific channels. That list is the endorsement list, and is validated here against the channelId.
* @returns {boolean} True is the channelId is found in the Endorsement set. False if the channelId is not found.
*/
static validate(channelId: string, endorsements: string[]): boolean {
// If the Activity came in and doesn't have a Channel ID then it's making no
// assertions as to who endorses it. This means it should pass.
if (channelId === null || channelId.trim() === '') {
return true;
}
if (endorsements === null) {
throw new AuthenticationError('endorsements required', StatusCodes.UNAUTHORIZED);
}
// The Call path to get here is:
// JwtTokenValidation.AuthenticateRequest
// ->
// JwtTokenValidation.ValidateAuthHeader
// ->
// ChannelValidation.AuthenticateChannelToken
// ->
// JWTTokenExtractor
// Does the set of endorsements match the channelId that was passed in?
return new Set(endorsements).has(channelId);
}
}