diff --git a/src/adapters/bullhorn/index.js b/src/adapters/bullhorn/index.js index e614e2b9..89f58dce 100644 --- a/src/adapters/bullhorn/index.js +++ b/src/adapters/bullhorn/index.js @@ -7,6 +7,56 @@ function getAuthType() { return 'oauth'; } +async function authValidation({ user }) { + let commentActionListResponse; + try { + commentActionListResponse = await axios.get( + `${user.platformAdditionalInfo.restUrl}settings/commentActionList`, + { + headers: { + BhRestToken: user.platformAdditionalInfo.bhRestToken + } + }); + return { + successful: true + } + } + catch (e) { + if (isAuthError(e.response.status)) { + user = await refreshSessionToken(user); + try { + commentActionListResponse = await axios.get(`${user.platformAdditionalInfo.restUrl}settings/commentActionList`, + { + headers: { + BhRestToken: user.platformAdditionalInfo.bhRestToken + } + }); + return { + successful: true + } + } + catch (e) { + return { + successful: false, + returnMessage: { + messageType: 'warning', + message: 'It seems like your Bullhorn session has expired. Please re-authenticate.', + ttl: 3000 + } + } + } + } + return { + successful: false, + returnMessage: { + messageType: 'warning', + message: 'It seems like your Bullhorn session has expired. Please re-authenticate.', + ttl: 3000 + } + } + } +} + async function getOauthInfo({ tokenUrl }) { return { clientId: process.env.BULLHORN_CLIENT_ID, @@ -758,6 +808,7 @@ function upsertTranscript({ body, transcript }) { } exports.getAuthType = getAuthType; +exports.authValidation = authValidation; exports.getOauthInfo = getOauthInfo; exports.getOverridingOAuthOption = getOverridingOAuthOption; exports.getUserInfo = getUserInfo; diff --git a/src/core/auth.js b/src/core/auth.js index a0bcd16b..32fc84dc 100644 --- a/src/core/auth.js +++ b/src/core/auth.js @@ -122,5 +122,32 @@ async function saveUserInfo({ platformUserInfo, platform, hostname, accessToken, }; } +async function authValidation({ platform, userId }) { + const existingUser = await UserModel.findOne({ + where: { + [Op.and]: [ + { + id: userId, + platform + } + ] + } + }); + if (!!existingUser) { + const platformModule = require(`../adapters/${platform}`); + const { successful, returnMessage } = await platformModule.authValidation({ user: existingUser }); + return { + successful, + returnMessage + } + } + else { + return { + successful: false + } + } +} + exports.onOAuthCallback = onOAuthCallback; -exports.onApiKeyLogin = onApiKeyLogin; \ No newline at end of file +exports.onApiKeyLogin = onApiKeyLogin; +exports.authValidation = authValidation; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 94583faf..910fc9a8 100644 --- a/src/index.js +++ b/src/index.js @@ -74,6 +74,51 @@ app.get('/crmManifest', (req, res) => { app.get('/is-alive', (req, res) => { res.send(`OK`); }); + +app.get('/authValidation', async (req, res) => { + const requestStartTime = new Date().getTime(); + let platformName = null; + let success = false; + let validationPass = false; + const { hashedExtensionId, hashedAccountId, userAgent, ip, author } = getAnalyticsVariablesInReqHeaders({ headers: req.headers }) + try { + const jwtToken = req.query.jwtToken; + if (!!jwtToken) { + const { id: userId, platform } = jwt.decodeJwt(jwtToken); + platformName = platform; + const { successful, returnMessage } = await authCore.authValidation({ platform, userId }); + success = true; + validationPass = successful; + res.status(200).send({ successful, returnMessage }); + } + else { + res.status(400).send('Please go to Settings and authorize CRM platform'); + success = false; + } + } + catch (e) { + console.log(`platform: ${platformName} \n${e.stack}`); + res.status(400).send(e); + success = false; + } + const requestEndTime = new Date().getTime(); + analytics.track({ + eventName: 'Auth validation', + interfaceName: 'authValidation', + adapterName: platformName, + accountId: hashedAccountId, + extensionId: hashedExtensionId, + success, + requestDuration: (requestEndTime - requestStartTime) / 1000, + userAgent, + ip, + author, + extras: { + validationPass + } + }); +}); + app.get('/serverVersionInfo', (req, res) => { const defaultCrmManifest = require('./adapters/manifest.json'); res.send({ version: defaultCrmManifest.version }); @@ -112,7 +157,6 @@ app.delete('/pipedrive-redirect', async function (req, res) { app.post('/admin/settings', async function (req, res) { const requestStartTime = new Date().getTime(); - let platformName = null; let success = false; const { hashedExtensionId, hashedAccountId, userAgent, ip, author } = getAnalyticsVariablesInReqHeaders({ headers: req.headers }) try { @@ -136,7 +180,6 @@ app.post('/admin/settings', async function (req, res) { analytics.track({ eventName: 'Set admin settings', interfaceName: 'setAdminSettings', - adapterName: platformName, accountId: hashedAccountId, extensionId: hashedExtensionId, success,