Built motion from commit a679711e.|2.6.15
[motion2.git] / server / api / cloudProvider / cloudProvider.oauth.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0x778f=['../../config/schedule/cloud-provider','redis','defaults','localhost','https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0','profile','email','offline_access','https://outlook.office365.com/IMAP.AccessAsUser.All','https://outlook.office.com/POP.AccessAsUser.All','https://outlook.office.com/SMTP.Send','openid','/authorize','/token','map','type','Dynamics365','push','data7','join','Outlook365','/.default','replace','{TENANT_ID}','data2','code','id_token','stringify','toString','base64','randomBytes','hex','client_id','data1','response_type','redirect_uri','data4','form_post','scope','state','nonce','prompt','key','value','oauth2Claims','oauth2/','set','payload','iss','aud','audience','isAfter','exp','data3','CloudProvider','findOne','then','refresh_token','data6','decryptString','data5','access_token','update','catch','authorization_code','intervals','bind','all','inspect','[CLOUD_PROVIDER]\x20Error\x20while\x20refreshing\x20the\x20tokens\x20after\x20service\x20restart\x20error:%s','exports','lodash','jsonwebtoken','moment','ioredis','request-promise','util','../../components/encryptor','../../config/environment'];(function(_0x438bd2,_0x22dddc){var _0x4922b0=function(_0x4e50c7){while(--_0x4e50c7){_0x438bd2['push'](_0x438bd2['shift']());}};_0x4922b0(++_0x22dddc);}(_0x778f,0x134));var _0xf778=function(_0x17928f,_0x4ad33f){_0x17928f=_0x17928f-0x0;var _0x500b63=_0x778f[_0x17928f];return _0x500b63;};'use strict';var _=require(_0xf778('0x0'));var crypto=require('crypto');var jwt=require(_0xf778('0x1'));var moment=require(_0xf778('0x2'));var Redis=require(_0xf778('0x3'));var rp=require(_0xf778('0x4'));var util=require(_0xf778('0x5'));var encryptor=require(_0xf778('0x6'));var config=require(_0xf778('0x7'));var logger=require('../../config/logger')('api');var schedule=require(_0xf778('0x8'));var db=require('../../mysqldb')['db'];config[_0xf778('0x9')]=_[_0xf778('0xa')](config['redis'],{'host':_0xf778('0xb'),'port':0x18eb});var redis=new Redis(config['redis']);var MICROSOFT_AUTH_URL=_0xf778('0xc');var AZURE_AUTH_SCOPES={'Outlook365':['openid',_0xf778('0xd'),_0xf778('0xe'),_0xf778('0xf'),_0xf778('0x10'),_0xf778('0x11'),_0xf778('0x12')],'Dynamics365':[_0xf778('0x13'),_0xf778('0xf')]};var MICROSOFT_AUTH_ENDPOINT=_0xf778('0x14');var MICROSOFT_TOKEN_ENDPOINT=_0xf778('0x15');var OAUTH_REFRESH_INTERVAL=0xfa;function getAuthorizationScopes(_0x3759ae){var _0xf7c0c5=_[_0xf778('0x16')](AZURE_AUTH_SCOPES[_0x3759ae['type']]);if(_0x3759ae[_0xf778('0x17')]===_0xf778('0x18'))_0xf7c0c5[_0xf778('0x19')](_0x3759ae[_0xf778('0x1a')]+'/.default');return _0xf7c0c5[_0xf778('0x1b')]('\x20');}function getAccessTokenScope(_0x36e350){if(_0x36e350[_0xf778('0x17')]===_0xf778('0x1c'))return _0xf778('0x10');if(_0x36e350['type']===_0xf778('0x18'))return _0x36e350[_0xf778('0x1a')]+_0xf778('0x1d');}function generateMicrosoftAuthorizationUrl(_0x579577,_0x5026a9){var _0x42f012=MICROSOFT_AUTH_URL[_0xf778('0x1e')](_0xf778('0x1f'),_0x579577[_0xf778('0x20')]);var _0x12a601=[_0xf778('0x21'),_0xf778('0x22')];var _0x3f0d51=Buffer['from'](JSON[_0xf778('0x23')]({'id':_0x5026a9}))[_0xf778('0x24')](_0xf778('0x25'));var _0x492fd8=crypto[_0xf778('0x26')](0x10)[_0xf778('0x24')](_0xf778('0x27'));var _0x599dac=getAuthorizationScopes(_0x579577);var _0x5b5402=[{'key':_0xf778('0x28'),'value':_0x579577[_0xf778('0x29')]},{'key':_0xf778('0x2a'),'value':encodeURIComponent(_0x12a601[_0xf778('0x1b')]('\x20'))},{'key':_0xf778('0x2b'),'value':_0x579577[_0xf778('0x2c')]},{'key':'response_mode','value':_0xf778('0x2d')},{'key':_0xf778('0x2e'),'value':encodeURIComponent(_0x599dac)},{'key':_0xf778('0x2f'),'value':_0x3f0d51},{'key':_0xf778('0x30'),'value':_0x492fd8},{'key':_0xf778('0x31'),'value':'login'}];var _0x4c9e17=_0x42f012+MICROSOFT_AUTH_ENDPOINT+'?'+_['map'](_0x5b5402,function(_0x1f527b){return _0x1f527b[_0xf778('0x32')]+'='+_0x1f527b[_0xf778('0x33')];})['join']('&');_0x579577[_0xf778('0x34')]={'issuer':_0x42f012[_0xf778('0x1e')](_0xf778('0x35'),''),'audience':_0x579577[_0xf778('0x29')],'state':_0x3f0d51,'nonce':_0x492fd8};redis[_0xf778('0x36')](_0x3f0d51,JSON[_0xf778('0x23')](_0x579577));return _0x4c9e17;}function isValidIdToken(_0x49fc2d,_0x197d4c){try{var _0x986268=jwt['decode'](_0x49fc2d,{'complete':!![]});var _0x3e63fd=_0x986268[_0xf778('0x37')];if(_0x3e63fd[_0xf778('0x38')]!==_0x197d4c['issuer'])return![];if(_0x3e63fd[_0xf778('0x39')]!==_0x197d4c[_0xf778('0x3a')])return![];if(_0x3e63fd[_0xf778('0x30')]!==_0x197d4c['nonce'])return![];if(moment()[_0xf778('0x3b')](moment['unix'](_0x3e63fd[_0xf778('0x3c')])))return![];return!![];}catch(_0x329b5b){throw _0x329b5b;}}function refreshOauth2MicrosoftAccessToken(_0x238d18){return Promise['resolve']()['then'](function(){if(_0x238d18[_0xf778('0x3d')])return _0x238d18;return db[_0xf778('0x3e')][_0xf778('0x3f')]({'where':{'id':_0x238d18['id']},'raw':!![]});})[_0xf778('0x40')](function(_0x1f94c6){var _0xe478d6={'grant_type':_0xf778('0x41'),'refresh_token':_0x1f94c6[_0xf778('0x42')],'scope':getAccessTokenScope(_0x1f94c6),'redirect_uri':_0x1f94c6[_0xf778('0x2c')],'client_id':_0x1f94c6[_0xf778('0x29')],'client_secret':encryptor[_0xf778('0x43')](_0x1f94c6[_0xf778('0x3d')])};var _0x3ab67d={'method':'POST','uri':MICROSOFT_AUTH_URL[_0xf778('0x1e')]('{TENANT_ID}',_0x1f94c6[_0xf778('0x20')])+MICROSOFT_TOKEN_ENDPOINT,'form':_0xe478d6,'json':!![]};return rp(_0x3ab67d);})[_0xf778('0x40')](function(_0x29b139){_0x238d18[_0xf778('0x44')]=_0x29b139[_0xf778('0x45')];_0x238d18[_0xf778('0x42')]=_0x29b139['refresh_token'];return db['CloudProvider'][_0xf778('0x46')]({'data5':_0x238d18[_0xf778('0x44')],'data6':_0x238d18[_0xf778('0x42')]},{'where':{'id':_0x238d18['id']}});})['then'](function(){return _0x238d18;})[_0xf778('0x47')](function(_0xbb8467){logger['error']('Failed\x20to\x20refresh\x20access\x20token\x20for\x20cloud\x20provider\x20#%s,\x20-\x20err:%s',_0x238d18['id'],_0xbb8467);});}function getOauth2MicrosoftAccessToken(_0x709647,_0x1c12a6){var _0x5ee8e1={'grant_type':_0xf778('0x48'),'code':_0x709647,'scope':getAccessTokenScope(_0x1c12a6),'redirect_uri':_0x1c12a6[_0xf778('0x2c')],'client_id':_0x1c12a6[_0xf778('0x29')],'client_secret':encryptor[_0xf778('0x43')](_0x1c12a6['data3'])};var _0x1881a1={'method':'POST','uri':MICROSOFT_AUTH_URL['replace']('{TENANT_ID}',_0x1c12a6[_0xf778('0x20')])+MICROSOFT_TOKEN_ENDPOINT,'form':_0x5ee8e1,'json':!![]};return rp(_0x1881a1);}function startRefreshInterval(_0x3464b0){var _0x11bcf1=schedule[_0xf778('0x49')];if(_0x11bcf1[_0x3464b0['id']])clearInterval(_0x11bcf1[_0x3464b0['id']]);_0x11bcf1[_0x3464b0['id']]=setInterval(refreshOauth2MicrosoftAccessToken[_0xf778('0x4a')](this,{'id':_0x3464b0['id']}),OAUTH_REFRESH_INTERVAL*0x3e8);schedule[_0xf778('0x49')]=_0x11bcf1;}function startAllRefreshIntervals(){return db['CloudProvider']['findAll']({'where':{'data6':{'$ne':null}},'raw':!![]})[_0xf778('0x40')](function(_0x3a3039){var _0x5881e9=_0x3a3039[_0xf778('0x16')](function(_0x2896b5){return refreshOauth2MicrosoftAccessToken(_0x2896b5)[_0xf778('0x40')](function(_0x55e59f){startRefreshInterval(_0x55e59f);});});return Promise[_0xf778('0x4b')](_0x5881e9);})['catch'](function(_0x3a6ad5){var _0x3d66f3=_0x3a6ad5?util[_0xf778('0x4c')](_0x3a6ad5,{'showHidden':![],'depth':null}):'';logger['error'](_0xf778('0x4d'),_0x3d66f3);});}module[_0xf778('0x4e')]={'generateMicrosoftAuthorizationUrl':generateMicrosoftAuthorizationUrl,'getOauth2MicrosoftAccessToken':getOauth2MicrosoftAccessToken,'isValidIdToken':isValidIdToken,'startAllRefreshIntervals':startAllRefreshIntervals,'refreshOauth2MicrosoftAccessToken':refreshOauth2MicrosoftAccessToken,'startRefreshInterval':startRefreshInterval};