1098c1afca9153023d88aeb9c7b802277047fdab
[motion2.git] / server / api / cloudProvider / cloudProvider.controller.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 _0x7f24=['find','oauth2MicrosoftAuthorize','generateMicrosoftAuthorizationUrl','user','oauth2MicrosoftCallback','get','state','Invalid\x20state\x20parameter','parse','from','toString','ascii','id_token','oauth2Claims','Unable\x20to\x20validate\x20id_token','del','getOauth2MicrosoftAccessToken','code','data5','data6','refresh_token','startRefreshInterval','emit','microsoftAuthorization:success','Invalid\x20authentication\x20state\x20parameter','Authentication\x20failed\x20for\x20cloud\x20provider\x20#%s,\x20err:%s','stringify','lodash','../../components/encryptor','../../config/logger','api','./cloudProvider.oauth','redis','defaults','localhost','socket.io-emitter','sendStatus','status','end','json','offset','undefined','count','limit','set','Content-Range','update','then','destroy','error','stack','name','padEnd','slice','index','CloudProvider','fieldName','type','key','model','map','query','keys','intersection','attributes','fields','length','order','sort','filters','pick','filter','where','merge','VIRTUAL','options','includeAll','include','findAll','rows','service','MicrosoftAzure','decryptString','data3','catch','show','params','rawAttributes','create','body','Service\x20parameter\x20not\x20specified','values','includes','join','encryptString'];(function(_0x4e9d00,_0x2bdcc5){var _0x5771f4=function(_0x1f8541){while(--_0x1f8541){_0x4e9d00['push'](_0x4e9d00['shift']());}};_0x5771f4(++_0x2bdcc5);}(_0x7f24,0x197));var _0x47f2=function(_0x5c8a11,_0x10805f){_0x5c8a11=_0x5c8a11-0x0;var _0x116b22=_0x7f24[_0x5c8a11];return _0x116b22;};'use strict';var _=require(_0x47f2('0x0'));var Redis=require('ioredis');var qs=require('../../components/parsers/qs');var encryptor=require(_0x47f2('0x1'));var config=require('../../config/environment');var logger=require(_0x47f2('0x2'))(_0x47f2('0x3'));var db=require('../../mysqldb')['db'];var oauth=require(_0x47f2('0x4'));config[_0x47f2('0x5')]=_[_0x47f2('0x6')](config[_0x47f2('0x5')],{'host':_0x47f2('0x7'),'port':0x18eb});var socket=require(_0x47f2('0x8'))(new Redis(config['redis']));var redis=new Redis(config[_0x47f2('0x5')]);function respondWithStatusCode(_0x4f53f3,_0x337244){_0x337244=_0x337244||0xcc;return function(_0x4ed042){if(_0x4ed042){return _0x4f53f3[_0x47f2('0x9')](_0x337244);}return _0x4f53f3[_0x47f2('0xa')](_0x337244)[_0x47f2('0xb')]();};}function respondWithResult(_0x65f1de,_0x24f28d){_0x24f28d=_0x24f28d||0xc8;return function(_0x7fa6cb){if(_0x7fa6cb){return _0x65f1de[_0x47f2('0xa')](_0x24f28d)[_0x47f2('0xc')](_0x7fa6cb);}};}function respondWithFilteredResult(_0x148730,_0x35a5e9){return function(_0x5eb4e1){if(_0x5eb4e1){var _0x361731=typeof _0x35a5e9[_0x47f2('0xd')]===_0x47f2('0xe')&&typeof _0x35a5e9['limit']==='undefined';var _0x12c117=_0x5eb4e1[_0x47f2('0xf')];var _0x56f95a=_0x361731?0x0:_0x35a5e9[_0x47f2('0xd')];var _0x33e4a3=_0x361731?_0x5eb4e1[_0x47f2('0xf')]:_0x35a5e9['offset']+_0x35a5e9[_0x47f2('0x10')];var _0x242c1f;if(_0x33e4a3>=_0x12c117){_0x33e4a3=_0x12c117;_0x242c1f=0xc8;}else{_0x242c1f=0xce;}_0x148730[_0x47f2('0xa')](_0x242c1f);return _0x148730[_0x47f2('0x11')](_0x47f2('0x12'),_0x56f95a+'-'+_0x33e4a3+'/'+_0x12c117)[_0x47f2('0xc')](_0x5eb4e1);}return null;};}function saveUpdates(_0x547bb9){return function(_0x15eb82){if(_0x15eb82){return _0x15eb82[_0x47f2('0x13')](_0x547bb9)[_0x47f2('0x14')](function(_0x20b0f5){return _0x20b0f5;});}return null;};}function removeEntity(_0x305974){return function(_0x259266){if(_0x259266){return _0x259266[_0x47f2('0x15')]()[_0x47f2('0x14')](function(){_0x305974[_0x47f2('0xa')](0xcc)[_0x47f2('0xb')]();});}};}function handleEntityNotFound(_0x1a1df5){return function(_0x121550){if(!_0x121550){_0x1a1df5[_0x47f2('0x9')](0x194);}return _0x121550;};}function handleError(_0x4de355,_0x46dfc1){_0x46dfc1=_0x46dfc1||0x1f4;return function(_0x5bb041){logger[_0x47f2('0x16')](_0x5bb041[_0x47f2('0x17')]);if(_0x5bb041[_0x47f2('0x18')]){delete _0x5bb041[_0x47f2('0x18')];}_0x4de355[_0x47f2('0xa')](_0x46dfc1)['send'](_0x5bb041);};}function maskClientSecret(_0x53eb2e){return _[_0x47f2('0x19')](_0x53eb2e[_0x47f2('0x1a')](0x0,0x3),0x14,'*');}exports[_0x47f2('0x1b')]=function(_0x37fe8a,_0x21c739){var _0x43b42a={},_0x14df2b={},_0x458d86={'count':0x0,'rows':[]};var _0x3b4429=_['map'](db[_0x47f2('0x1c')]['rawAttributes'],function(_0x406e9a){return{'name':_0x406e9a[_0x47f2('0x1d')],'type':_0x406e9a[_0x47f2('0x1e')][_0x47f2('0x1f')]};});_0x14df2b[_0x47f2('0x20')]=_[_0x47f2('0x21')](_0x3b4429,_0x47f2('0x18'));_0x14df2b[_0x47f2('0x22')]=_[_0x47f2('0x23')](_0x37fe8a[_0x47f2('0x22')]);_0x14df2b['filters']=_[_0x47f2('0x24')](_0x14df2b[_0x47f2('0x20')],_0x14df2b[_0x47f2('0x22')]);_0x43b42a[_0x47f2('0x25')]=_['intersection'](_0x14df2b[_0x47f2('0x20')],qs[_0x47f2('0x26')](_0x37fe8a[_0x47f2('0x22')][_0x47f2('0x26')]));_0x43b42a[_0x47f2('0x25')]=_0x43b42a['attributes'][_0x47f2('0x27')]?_0x43b42a['attributes']:_0x14df2b[_0x47f2('0x20')];if(!_0x37fe8a[_0x47f2('0x22')]['hasOwnProperty']('nolimit')){_0x43b42a[_0x47f2('0x10')]=qs['limit'](_0x37fe8a[_0x47f2('0x22')][_0x47f2('0x10')]);_0x43b42a[_0x47f2('0xd')]=qs['offset'](_0x37fe8a[_0x47f2('0x22')]['offset']);}_0x43b42a[_0x47f2('0x28')]=qs[_0x47f2('0x29')](_0x37fe8a[_0x47f2('0x22')][_0x47f2('0x29')]);_0x43b42a['where']=qs[_0x47f2('0x2a')](_[_0x47f2('0x2b')](_0x37fe8a[_0x47f2('0x22')],_0x14df2b['filters']),_0x3b4429);if(_0x37fe8a['query'][_0x47f2('0x2c')]){_0x43b42a[_0x47f2('0x2d')]=_[_0x47f2('0x2e')](_0x43b42a[_0x47f2('0x2d')],{'$or':_[_0x47f2('0x21')](_0x3b4429,function(_0xecc9f5){if(_0xecc9f5['type']!==_0x47f2('0x2f')){var _0x79520a={};_0x79520a[_0xecc9f5[_0x47f2('0x18')]]={'$like':'%'+_0x37fe8a['query']['filter']+'%'};return _0x79520a;}})});}_0x43b42a=_[_0x47f2('0x2e')]({},_0x43b42a,_0x37fe8a[_0x47f2('0x30')]);var _0x2614be={'where':_0x43b42a[_0x47f2('0x2d')]};return db[_0x47f2('0x1c')][_0x47f2('0xf')](_0x2614be)[_0x47f2('0x14')](function(_0x110ff0){_0x458d86[_0x47f2('0xf')]=_0x110ff0;if(_0x37fe8a['query'][_0x47f2('0x31')]){_0x43b42a[_0x47f2('0x32')]=[{'all':!![]}];}return db[_0x47f2('0x1c')][_0x47f2('0x33')](_0x43b42a);})['then'](function(_0xc0a9ee){_0x458d86[_0x47f2('0x34')]=_['map'](_0xc0a9ee,function(_0x410c24){if(_0x410c24[_0x47f2('0x35')]===_0x47f2('0x36')){var _0xefce7a=encryptor[_0x47f2('0x37')](_0x410c24[_0x47f2('0x38')]);_0x410c24[_0x47f2('0x38')]=maskClientSecret(_0xefce7a);}return _0x410c24;});return _0x458d86;})[_0x47f2('0x14')](respondWithFilteredResult(_0x21c739,_0x43b42a))[_0x47f2('0x39')](handleError(_0x21c739,null));};exports[_0x47f2('0x3a')]=function(_0x33c81e,_0x5a8e9d){var _0x38ed38={'raw':!![],'where':{'id':_0x33c81e[_0x47f2('0x3b')]['id']}},_0x2755d5={};_0x2755d5[_0x47f2('0x20')]=_['keys'](db[_0x47f2('0x1c')][_0x47f2('0x3c')]);_0x2755d5[_0x47f2('0x22')]=_[_0x47f2('0x23')](_0x33c81e[_0x47f2('0x22')]);_0x2755d5[_0x47f2('0x2a')]=_['intersection'](_0x2755d5[_0x47f2('0x20')],_0x2755d5[_0x47f2('0x22')]);_0x38ed38[_0x47f2('0x25')]=_[_0x47f2('0x24')](_0x2755d5[_0x47f2('0x20')],qs[_0x47f2('0x26')](_0x33c81e[_0x47f2('0x22')][_0x47f2('0x26')]));_0x38ed38[_0x47f2('0x25')]=_0x38ed38[_0x47f2('0x25')]['length']?_0x38ed38[_0x47f2('0x25')]:_0x2755d5[_0x47f2('0x20')];if(_0x33c81e[_0x47f2('0x22')]['includeAll']){_0x38ed38['include']=[{'all':!![]}];}_0x38ed38=_['merge']({},_0x38ed38,_0x33c81e[_0x47f2('0x30')]);return db[_0x47f2('0x1c')]['find'](_0x38ed38)['then'](handleEntityNotFound(_0x5a8e9d,null))['then'](function(_0xea74c7){if(_0xea74c7[_0x47f2('0x35')]==='MicrosoftAzure'){var _0x560944=encryptor[_0x47f2('0x37')](_0xea74c7['data3']);_0xea74c7[_0x47f2('0x38')]=maskClientSecret(_0x560944);}return _0xea74c7;})['then'](respondWithResult(_0x5a8e9d,null))[_0x47f2('0x39')](handleError(_0x5a8e9d,null));};exports[_0x47f2('0x3d')]=function(_0x6e881d,_0x11c133){if(!_0x6e881d[_0x47f2('0x3e')]['name'])throw new Error('Name\x20parameter\x20not\x20specified');if(!_0x6e881d['body'][_0x47f2('0x35')])throw new Error(_0x47f2('0x3f'));var _0x121fb6=db[_0x47f2('0x1c')][_0x47f2('0x3c')][_0x47f2('0x35')][_0x47f2('0x40')];if(!_0x121fb6[_0x47f2('0x41')](_0x6e881d[_0x47f2('0x3e')][_0x47f2('0x35')]))throw new Error('Invalid\x20service\x20parameter.\x20Allowed\x20values:\x20'+_0x121fb6[_0x47f2('0x42')](',\x20'));if(_0x6e881d[_0x47f2('0x3e')]['service']===_0x47f2('0x36')&&_0x6e881d[_0x47f2('0x3e')]['data3']){_0x6e881d[_0x47f2('0x3e')][_0x47f2('0x38')]=encryptor[_0x47f2('0x43')](_0x6e881d[_0x47f2('0x3e')][_0x47f2('0x38')]);}return db[_0x47f2('0x1c')][_0x47f2('0x3d')](_0x6e881d[_0x47f2('0x3e')],{})[_0x47f2('0x14')](respondWithResult(_0x11c133,0xc9))['catch'](handleError(_0x11c133,null));};exports[_0x47f2('0x13')]=function(_0x3f9569,_0x35448f){if(_0x3f9569[_0x47f2('0x3e')]['id']){delete _0x3f9569[_0x47f2('0x3e')]['id'];}return db[_0x47f2('0x1c')]['find']({'where':{'id':_0x3f9569[_0x47f2('0x3b')]['id']}})[_0x47f2('0x14')](handleEntityNotFound(_0x35448f,null))[_0x47f2('0x14')](function(_0x545480){if(_0x545480['service']===_0x47f2('0x36')&&_0x3f9569[_0x47f2('0x3e')][_0x47f2('0x38')]){_0x3f9569['body'][_0x47f2('0x38')]=encryptor[_0x47f2('0x43')](_0x3f9569['body'][_0x47f2('0x38')]);}return _0x545480;})[_0x47f2('0x14')](saveUpdates(_0x3f9569[_0x47f2('0x3e')],null))[_0x47f2('0x14')](respondWithResult(_0x35448f,null))[_0x47f2('0x39')](handleError(_0x35448f,null));};exports['destroy']=function(_0x2b14fb,_0x12844e){return db['CloudProvider'][_0x47f2('0x44')]({'where':{'id':_0x2b14fb[_0x47f2('0x3b')]['id']}})[_0x47f2('0x14')](handleEntityNotFound(_0x12844e,null))['then'](removeEntity(_0x12844e,null))[_0x47f2('0x39')](handleError(_0x12844e,null));};exports[_0x47f2('0x45')]=function(_0xbac2b0,_0x37fcbf){return db[_0x47f2('0x1c')]['findOne']({'where':{'id':_0xbac2b0['params']['id']},'raw':!![]})[_0x47f2('0x14')](function(_0x690e1f){if(!_0x690e1f)return _0x37fcbf[_0x47f2('0x9')](0x194);return{'url':oauth[_0x47f2('0x46')](_0x690e1f,_0xbac2b0[_0x47f2('0x47')]['id'])};})['then'](respondWithResult(_0x37fcbf,null))[_0x47f2('0x39')](handleError(_0x37fcbf,null));};exports[_0x47f2('0x48')]=function(_0x18f32b,_0x27e0c7){var _0x2c095c=_0x18f32b[_0x47f2('0x3e')];var _0xf07c4a;var _0x9c09c4;if(_0x2c095c[_0x47f2('0x16')])throw _0x2c095c;redis[_0x47f2('0x49')](_0x2c095c[_0x47f2('0x4a')])[_0x47f2('0x14')](function(_0x5ea597){if(!_0x5ea597)throw new Error(_0x47f2('0x4b'));_0xf07c4a=JSON[_0x47f2('0x4c')](_0x5ea597);var _0x1d105b=Buffer[_0x47f2('0x4d')](_0x2c095c[_0x47f2('0x4a')],'base64')[_0x47f2('0x4e')](_0x47f2('0x4f'));_0x9c09c4=JSON[_0x47f2('0x4c')](_0x1d105b);if(!oauth['isValidIdToken'](_0x2c095c[_0x47f2('0x50')],_0xf07c4a[_0x47f2('0x51')]))throw new Error(_0x47f2('0x52'));redis[_0x47f2('0x53')](_0x2c095c[_0x47f2('0x4a')]);return oauth[_0x47f2('0x54')](_0x2c095c[_0x47f2('0x55')],_0xf07c4a);})['then'](function(_0x15f010){_0xf07c4a[_0x47f2('0x56')]=_0x15f010['access_token'];_0xf07c4a[_0x47f2('0x57')]=_0x15f010[_0x47f2('0x58')];return db[_0x47f2('0x1c')]['update']({'data5':_0xf07c4a['data5'],'data6':_0xf07c4a[_0x47f2('0x57')]},{'where':{'id':_0xf07c4a['id']}});})['then'](function(){oauth[_0x47f2('0x59')](_0xf07c4a);socket[_0x47f2('0x5a')](_0x47f2('0x5b'),{'id':_0x9c09c4['id']});return;})[_0x47f2('0x14')](respondWithStatusCode(_0x27e0c7,null))[_0x47f2('0x39')](function(_0x328a33){if(_0x9c09c4){socket['emit']('microsoftAuthorization:error',{'id':_0x9c09c4['id']});}if(!_0xf07c4a){logger['error'](_0x47f2('0x5c'),_0x328a33);return _0x27e0c7[_0x47f2('0xa')](0x1f4)['send'](_0x328a33['message']);}logger[_0x47f2('0x16')](_0x47f2('0x5d'),_0xf07c4a['id'],JSON[_0x47f2('0x5e')](_0x328a33));});};