-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
62 lines (55 loc) · 1.61 KB
/
helper.js
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
const AWS = require('aws-sdk')
const { MicrosoftAppCredentials, ConnectorClient } = require('botframework-connector')
const credentials = new MicrosoftAppCredentials(process.env.APP_ID, process.env.APP_PASSWORD)
const { SimpleCredentialProvider, JwtTokenValidation } = require('botframework-connector')
const credentialsProvider = new SimpleCredentialProvider(process.env.APP_ID, process.env.APP_PASSWORD)
const logger = require('./logger')
/**
* Creates an arn from topic name
* @param {String} topic
*/
function getArnForTopic (topic) {
return `arn:aws:sns:${process.env.SNS_REGION}:${process.env.SNS_ACCOUNT_ID}:${topic}`
}
/**
* Returns an instance of the sns client
*/
function getSnsClient () {
const snsConfig = {
region: process.env.SNS_REGION
}
// use endpoint value when doing local setup
if (process.env.IS_OFFLINE) {
snsConfig.endpoint = process.env.SNS_ENDPOINT
}
return new AWS.SNS(snsConfig)
}
/**
* Validates incoming request from teams
* @param {Object} body
* @param {String} authHeader
*/
async function authenticateTeamsRequest (body, authHeader) {
try {
const identity = await JwtTokenValidation.authenticateRequest(body, authHeader, credentialsProvider, '')
return identity.isAuthenticated
} catch (e) {
logger.logFullError(e)
return false
}
}
/**
* Returns an instance of the slack web api client
*/
function getTeamsClient (serviceUrl) {
MicrosoftAppCredentials.trustServiceUrl(serviceUrl)
return new ConnectorClient(credentials, {
baseUri: serviceUrl
})
}
module.exports = {
getTeamsClient,
getSnsClient,
authenticateTeamsRequest,
getArnForTopic
}