Built motion from commit 85328c08.|2.5.50
[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 _0x8143=['response_mode','form_post','scope','state','nonce','map','key','oauth2Claims','oauth2/','payload','iss','issuer','aud','audience','isAfter','exp','then','data3','CloudProvider','findOne','refresh_token','data6','data4','decryptString','replace','data5','update','Failed\x20to\x20refresh\x20access\x20token\x20for\x20cloud\x20provider\x20#%s,\x20-\x20err:%s','authorization_code','POST','intervals','bind','findAll','all','catch','error','[CLOUD_PROVIDER]\x20Error\x20while\x20refreshing\x20the\x20tokens\x20after\x20service\x20restart\x20error:%s','exports','lodash','jsonwebtoken','ioredis','util','../../components/encryptor','../../config/environment','../../config/logger','api','../../config/schedule/cloud-provider','../../mysqldb','redis','defaults','localhost','https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0','openid','profile','offline_access','https://outlook.office365.com/IMAP.AccessAsUser.All','https://outlook.office.com/POP.AccessAsUser.All','https://outlook.office.com/SMTP.Send','type','Dynamics365','push','/.default','Outlook365','data7','{TENANT_ID}','data2','code','id_token','from','stringify','base64','randomBytes','toString','hex','client_id','data1','join'];(function(_0x4e6ae6,_0x49dba6){var _0x23c108=function(_0x1bc103){while(--_0x1bc103){_0x4e6ae6['push'](_0x4e6ae6['shift']());}};_0x23c108(++_0x49dba6);}(_0x8143,0x73));var _0x3814=function(_0x19950b,_0x53d0d7){_0x19950b=_0x19950b-0x0;var _0x3c6b41=_0x8143[_0x19950b];return _0x3c6b41;};'use strict';var _=require(_0x3814('0x0'));var crypto=require('crypto');var jwt=require(_0x3814('0x1'));var moment=require('moment');var Redis=require(_0x3814('0x2'));var rp=require('request-promise');var util=require(_0x3814('0x3'));var encryptor=require(_0x3814('0x4'));var config=require(_0x3814('0x5'));var logger=require(_0x3814('0x6'))(_0x3814('0x7'));var schedule=require(_0x3814('0x8'));var db=require(_0x3814('0x9'))['db'];config[_0x3814('0xa')]=_[_0x3814('0xb')](config[_0x3814('0xa')],{'host':_0x3814('0xc'),'port':0x18eb});var redis=new Redis(config['redis']);var MICROSOFT_AUTH_URL=_0x3814('0xd');var AZURE_AUTH_SCOPES={'Outlook365':[_0x3814('0xe'),_0x3814('0xf'),'email',_0x3814('0x10'),_0x3814('0x11'),_0x3814('0x12'),_0x3814('0x13')],'Dynamics365':[_0x3814('0xe'),'offline_access']};var MICROSOFT_AUTH_ENDPOINT='/authorize';var MICROSOFT_TOKEN_ENDPOINT='/token';var OAUTH_REFRESH_INTERVAL=0xfa;function getAuthorizationScopes(_0x4e93c6){var _0x29afce=_['map'](AZURE_AUTH_SCOPES[_0x4e93c6['type']]);if(_0x4e93c6[_0x3814('0x14')]===_0x3814('0x15'))_0x29afce[_0x3814('0x16')](_0x4e93c6['data7']+_0x3814('0x17'));return _0x29afce['join']('\x20');}function getAccessTokenScope(_0x126652){if(_0x126652['type']===_0x3814('0x18'))return _0x3814('0x11');if(_0x126652[_0x3814('0x14')]===_0x3814('0x15'))return _0x126652[_0x3814('0x19')]+_0x3814('0x17');}function generateMicrosoftAuthorizationUrl(_0x55bf86,_0x5cc6ef){var _0x4ed6bd=MICROSOFT_AUTH_URL['replace'](_0x3814('0x1a'),_0x55bf86[_0x3814('0x1b')]);var _0xfc2486=[_0x3814('0x1c'),_0x3814('0x1d')];var _0x4c2df5=Buffer[_0x3814('0x1e')](JSON[_0x3814('0x1f')]({'id':_0x5cc6ef}))['toString'](_0x3814('0x20'));var _0x58dd48=crypto[_0x3814('0x21')](0x10)[_0x3814('0x22')](_0x3814('0x23'));var _0x4aa74d=getAuthorizationScopes(_0x55bf86);var _0x120362=[{'key':_0x3814('0x24'),'value':_0x55bf86[_0x3814('0x25')]},{'key':'response_type','value':encodeURIComponent(_0xfc2486[_0x3814('0x26')]('\x20'))},{'key':'redirect_uri','value':_0x55bf86['data4']},{'key':_0x3814('0x27'),'value':_0x3814('0x28')},{'key':_0x3814('0x29'),'value':encodeURIComponent(_0x4aa74d)},{'key':_0x3814('0x2a'),'value':_0x4c2df5},{'key':_0x3814('0x2b'),'value':_0x58dd48},{'key':'prompt','value':'login'}];var _0x2700d0=_0x4ed6bd+MICROSOFT_AUTH_ENDPOINT+'?'+_[_0x3814('0x2c')](_0x120362,function(_0x4f6b54){return _0x4f6b54[_0x3814('0x2d')]+'='+_0x4f6b54['value'];})['join']('&');_0x55bf86[_0x3814('0x2e')]={'issuer':_0x4ed6bd['replace'](_0x3814('0x2f'),''),'audience':_0x55bf86[_0x3814('0x25')],'state':_0x4c2df5,'nonce':_0x58dd48};redis['set'](_0x4c2df5,JSON[_0x3814('0x1f')](_0x55bf86));return _0x2700d0;}function isValidIdToken(_0x29cca2,_0x1530d7){try{var _0x3d40c8=jwt['decode'](_0x29cca2,{'complete':!![]});var _0x4c864b=_0x3d40c8[_0x3814('0x30')];if(_0x4c864b[_0x3814('0x31')]!==_0x1530d7[_0x3814('0x32')])return![];if(_0x4c864b[_0x3814('0x33')]!==_0x1530d7[_0x3814('0x34')])return![];if(_0x4c864b[_0x3814('0x2b')]!==_0x1530d7[_0x3814('0x2b')])return![];if(moment()[_0x3814('0x35')](moment['unix'](_0x4c864b[_0x3814('0x36')])))return![];return!![];}catch(_0x54b461){throw _0x54b461;}}function refreshOauth2MicrosoftAccessToken(_0x53bf14){return Promise['resolve']()[_0x3814('0x37')](function(){if(_0x53bf14[_0x3814('0x38')])return _0x53bf14;return db[_0x3814('0x39')][_0x3814('0x3a')]({'where':{'id':_0x53bf14['id']},'raw':!![]});})[_0x3814('0x37')](function(_0x66e419){var _0x31e794={'grant_type':_0x3814('0x3b'),'refresh_token':_0x66e419[_0x3814('0x3c')],'scope':getAccessTokenScope(_0x66e419),'redirect_uri':_0x66e419[_0x3814('0x3d')],'client_id':_0x66e419[_0x3814('0x25')],'client_secret':encryptor[_0x3814('0x3e')](_0x66e419[_0x3814('0x38')])};var _0x27073f={'method':'POST','uri':MICROSOFT_AUTH_URL[_0x3814('0x3f')]('{TENANT_ID}',_0x66e419['data2'])+MICROSOFT_TOKEN_ENDPOINT,'form':_0x31e794,'json':!![]};return rp(_0x27073f);})['then'](function(_0x3f9d97){_0x53bf14[_0x3814('0x40')]=_0x3f9d97['access_token'];_0x53bf14[_0x3814('0x3c')]=_0x3f9d97[_0x3814('0x3b')];return db[_0x3814('0x39')][_0x3814('0x41')]({'data5':_0x53bf14[_0x3814('0x40')],'data6':_0x53bf14[_0x3814('0x3c')]},{'where':{'id':_0x53bf14['id']}});})[_0x3814('0x37')](function(){return _0x53bf14;})['catch'](function(_0x2239e1){logger['error'](_0x3814('0x42'),_0x53bf14['id'],_0x2239e1);});}function getOauth2MicrosoftAccessToken(_0x1e6e63,_0xe1d356){var _0x309c5a={'grant_type':_0x3814('0x43'),'code':_0x1e6e63,'scope':getAccessTokenScope(_0xe1d356),'redirect_uri':_0xe1d356[_0x3814('0x3d')],'client_id':_0xe1d356[_0x3814('0x25')],'client_secret':encryptor[_0x3814('0x3e')](_0xe1d356[_0x3814('0x38')])};var _0x468d8b={'method':_0x3814('0x44'),'uri':MICROSOFT_AUTH_URL[_0x3814('0x3f')](_0x3814('0x1a'),_0xe1d356[_0x3814('0x1b')])+MICROSOFT_TOKEN_ENDPOINT,'form':_0x309c5a,'json':!![]};return rp(_0x468d8b);}function startRefreshInterval(_0x29423b){var _0x29ae83=schedule[_0x3814('0x45')];if(_0x29ae83[_0x29423b['id']])clearInterval(_0x29ae83[_0x29423b['id']]);_0x29ae83[_0x29423b['id']]=setInterval(refreshOauth2MicrosoftAccessToken[_0x3814('0x46')](this,{'id':_0x29423b['id']}),OAUTH_REFRESH_INTERVAL*0x3e8);schedule[_0x3814('0x45')]=_0x29ae83;}function startAllRefreshIntervals(){return db[_0x3814('0x39')][_0x3814('0x47')]({'where':{'data6':{'$ne':null}},'raw':!![]})[_0x3814('0x37')](function(_0x4325ac){var _0x5ac3ef=_0x4325ac[_0x3814('0x2c')](function(_0x43b7be){return refreshOauth2MicrosoftAccessToken(_0x43b7be)[_0x3814('0x37')](function(_0x2f91c7){startRefreshInterval(_0x2f91c7);});});return Promise[_0x3814('0x48')](_0x5ac3ef);})[_0x3814('0x49')](function(_0x16b8aa){var _0x480c8e=_0x16b8aa?util['inspect'](_0x16b8aa,{'showHidden':![],'depth':null}):'';logger[_0x3814('0x4a')](_0x3814('0x4b'),_0x480c8e);});}module[_0x3814('0x4c')]={'generateMicrosoftAuthorizationUrl':generateMicrosoftAuthorizationUrl,'getOauth2MicrosoftAccessToken':getOauth2MicrosoftAccessToken,'isValidIdToken':isValidIdToken,'startAllRefreshIntervals':startAllRefreshIntervals,'refreshOauth2MicrosoftAccessToken':refreshOauth2MicrosoftAccessToken,'startRefreshInterval':startRefreshInterval};