-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86aa76a
commit 8a4754d
Showing
10 changed files
with
220 additions
and
36 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
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,30 @@ | ||
module.exports = { | ||
|
||
// authTypeInbound can be 'OAuth2' or 'Signature' | ||
authTypeInbound: process.env.AUTH_TYPE_INBOUND, | ||
//OAuth config for inbound | ||
oauthConfig: { | ||
tokenUrl: process.env.CONVO_INSTANCE_URL | ||
|| `${process.env.CONVO_WEBHOOK_URL && new URL(process.env.CONVO_WEBHOOK_URL).origin}/oauth/token`, | ||
clientId: process.env.CLIENT_ID, | ||
clientSecret: process.env.CLIENT_SECRET, | ||
}, | ||
//for authTypeInbound Signature | ||
secret: process.env.SHARED_SECRET, | ||
|
||
// authType can be 'OAuth2' or 'API-Token' | ||
authTypeOutbound: process.env.AUTH_TYPE_OUTBOUND, | ||
|
||
// for outbound request API-Token verification (i.e. requests coming from MC) | ||
accessToken: process.env.ACCESS_TOKEN, | ||
|
||
// This is the OAuth client configuration (adapter acting as client to MC as OAuth server) | ||
// This is for a dummy OAuth server, that will be used to issue this fixed access token | ||
// and verify that MC sends it in the Authorization header | ||
oauthServer: { | ||
tokenPath: '/token', | ||
clients: { | ||
'ConversationsClient': 'S3cr3t123!' | ||
} | ||
} | ||
}; |
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
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,54 @@ | ||
const got = require('got'); | ||
const Cache = require('ttl'); | ||
const qs = require('querystring'); | ||
|
||
const cache = new Cache({ | ||
ttl: 3600 * 1000 | ||
}); | ||
if (process.env.NODE_ENV !== 'production') { | ||
cache.on('hit', (key, val) => { | ||
console.log(`Cache hit for key: ${key}; Value: ${val}`); | ||
}); | ||
cache.on('miss', (key) => { | ||
console.log(`Cache miss for key: ${key}`); | ||
}); | ||
cache.on('put', (key, val, ttl) => { | ||
console.log(`Cache put for key: ${key}; Value: ${val} with ttl: ${ttl}`); | ||
}); | ||
} | ||
|
||
async function getAccessToken(authSettings) { | ||
let token = null; | ||
if (authSettings.oauthConfig) { | ||
const { tokenUrl, clientId, clientSecret } = authSettings.oauthConfig; | ||
token = cache.get(clientId); | ||
if (!token) { | ||
const payload = { grant_type: 'client_credentials', client_id: clientId }; | ||
const cred = Buffer.from(`${clientId}:${clientSecret}`, 'utf8').toString('base64'); | ||
console.log('Fetching new access token for client:', clientId, 'from token URL:', tokenUrl); | ||
try { | ||
const { body } = await got.post(tokenUrl, { | ||
body: qs.encode(payload), | ||
responseType: 'json', | ||
headers: { | ||
Authorization: `Basic ${cred}`, | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
} | ||
}); | ||
console.log('Received /token response from MC: ', body); | ||
const res = JSON.parse(body); | ||
token = res.access_token; | ||
cache.put(clientId, token, res.expires_in * 1000); | ||
} catch (e) { | ||
console.error(`Error fetching access token from ${tokenUrl}`, e); | ||
} | ||
} else { | ||
console.log(`Returning cached token: ${token} for client: ${clientId}`); | ||
} | ||
} | ||
return token; | ||
} | ||
|
||
module.exports = { | ||
getAccessToken | ||
}; |
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,46 @@ | ||
// test OAuth server that supports only client_credentials grant type | ||
// with a fixed set of client id and secret values configured in auth-settings.js | ||
|
||
const basicAuth = require('express-basic-auth'); | ||
const crypto = require('crypto'); | ||
const router = require('express').Router(); | ||
|
||
const Cache = require('ttl'); | ||
const { oauthServer } = require('../../auth-settings'); | ||
|
||
const cache = new Cache({ | ||
ttl: 3600 * 1000 | ||
}); | ||
|
||
const staticAuth = basicAuth({ | ||
users: oauthServer.clients | ||
}); | ||
|
||
router.post(oauthServer.tokenPath, staticAuth, (req, res) => { | ||
const grantType = req.body.grant_type; | ||
if (!grantType || grantType !== 'client_credentials') { | ||
res.status(400).send({ error: 'invalid_grant' }); | ||
} else { | ||
const token = crypto.randomBytes(16).toString('hex'); | ||
const { auth } = req; | ||
if (auth.user) cache.put(token, auth.user); | ||
console.info(`Issued new access token: ${token} for client ${auth.user || 'unknown'}`); | ||
res.status(200).send({ access_token: token, expires_in: 3600 }); | ||
} | ||
}); | ||
|
||
// This is just to confirm the token is valid and get the client info for the token | ||
router.get('/userInfo', (req, res) => { | ||
const token = req.query.token; | ||
const user = cache.get(token); | ||
return user ? res.status(200).send({ user }) : res.sendStatus(400); | ||
}); | ||
|
||
function isTokenValid(token) { | ||
return cache.get(token) || false; | ||
} | ||
|
||
module.exports = { | ||
router, | ||
isTokenValid | ||
}; |
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
Oops, something went wrong.