-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OAuth2 support #1
Open
edevalais-medallia
wants to merge
2
commits into
medallia:master
Choose a base branch
from
edevalais-medallia:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,27 @@ | ||
module.exports = { | ||
// OAuth config for inbound | ||
oauthConfig: { | ||
tokenUrl: `${process.env.CONVO_API_GATEWAY}/oauth/token`, | ||
clientId: process.env.CLIENT_ID, | ||
clientSecret: process.env.CLIENT_SECRET | ||
}, | ||
|
||
// authTypeOutbound can be 'OAuth' or 'API-Token' | ||
authTypeOutbound: process.env.AUTH_TYPE_OUTBOUND, | ||
|
||
// Default OAuth Expire time in secs | ||
defaultOAuthExpiresSecs: process.env.DEFAULT_OAUTH_EXPIRES_SECS, | ||
|
||
// For requests coming from Medallia Conversations with API-Token verification | ||
accessToken: process.env.ACCESS_TOKEN, | ||
|
||
// This is the OAuth 2.0 configuration used by Medallia Conversations to connect with the channel adapter. | ||
// This is for a dummy OAuth server that will be used to issue this fixed access token | ||
// and verify that Medallia Conversations 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,8 @@ | ||
const Cache = require('ttl'); | ||
const { defaultOAuthExpiresSecs } = require('../../auth-settings'); | ||
|
||
const cache = new Cache({ | ||
ttl: defaultOAuthExpiresSecs * 1000 | ||
}); | ||
|
||
module.exports = cache; |
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,53 @@ | ||
const got = require('got'); | ||
const qs = require('querystring'); | ||
const cache = require('./cache'); | ||
const { defaultOAuthExpiresSecs } = require('../../auth-settings'); | ||
|
||
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'}; | ||
const oauthTokenRequestCredentials = 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 ${oauthTokenRequestCredentials}`, | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
} | ||
}); | ||
console.log('Received /token response from Medallia Conversations: ', body); | ||
const res = JSON.parse(body); | ||
const expiresIn = res.expires_in || defaultOAuthExpiresSecs; | ||
token = res.access_token; | ||
cache.put(clientId, token, expiresIn * 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,38 @@ | ||
// This implements a basic OAuth 2.0-compatible token server for use with this reference implementation. | ||
// It only supports client_credentials grants and uses the static client_id/client_secret values that are | ||
// configured in auth-settings.js. | ||
|
||
const basicAuth = require('express-basic-auth'); | ||
const crypto = require('crypto'); | ||
const router = require('express').Router(); | ||
|
||
const { oauthServer, defaultOAuthExpiresSecs } = require('../../auth-settings'); | ||
const cache = require('./cache'); | ||
|
||
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: defaultOAuthExpiresSecs }); | ||
} | ||
}); | ||
|
||
function isTokenValid(token) { | ||
return cache.get(token) || false; | ||
} | ||
|
||
module.exports = { | ||
router, | ||
isTokenValid | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (...) {
add spaces for style compliance.