4379238d049392de79212e36a70e3bab7cb3dd08
[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 _0x1ff8=['data3','catch','show','params','keys','length','find','MicrosoftAzure','create','body','Name\x20parameter\x20not\x20specified','Service\x20parameter\x20not\x20specified','values','includes','Invalid\x20service\x20parameter.\x20Allowed\x20values:\x20','join','encryptString','oauth2MicrosoftAuthorize','findOne','generateMicrosoftAuthorizationUrl','user','oauth2MicrosoftCallback','get','state','Invalid\x20state\x20parameter','parse','from','ascii','Unable\x20to\x20validate\x20id_token','code','data5','data6','startRefreshInterval','microsoftAuthorization:success','emit','microsoftAuthorization:error','Invalid\x20authentication\x20state\x20parameter','Authentication\x20failed\x20for\x20cloud\x20provider\x20#%s,\x20err:%s','stringify','lodash','ioredis','../../components/parsers/qs','../../components/encryptor','../../config/logger','api','../../mysqldb','./cloudProvider.oauth','redis','defaults','localhost','socket.io-emitter','sendStatus','status','end','json','offset','limit','count','set','Content-Range','update','destroy','then','error','name','send','slice','index','map','CloudProvider','rawAttributes','fieldName','model','filters','intersection','fields','query','attributes','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','type','VIRTUAL','options','includeAll','include','findAll','rows','service','decryptString'];(function(_0x434e05,_0x243a35){var _0x422b3c=function(_0x702507){while(--_0x702507){_0x434e05['push'](_0x434e05['shift']());}};_0x422b3c(++_0x243a35);}(_0x1ff8,0x86));var _0x81ff=function(_0x65c903,_0x41e920){_0x65c903=_0x65c903-0x0;var _0x23e1d7=_0x1ff8[_0x65c903];return _0x23e1d7;};'use strict';var _=require(_0x81ff('0x0'));var Redis=require(_0x81ff('0x1'));var qs=require(_0x81ff('0x2'));var encryptor=require(_0x81ff('0x3'));var config=require('../../config/environment');var logger=require(_0x81ff('0x4'))(_0x81ff('0x5'));var db=require(_0x81ff('0x6'))['db'];var oauth=require(_0x81ff('0x7'));config[_0x81ff('0x8')]=_[_0x81ff('0x9')](config[_0x81ff('0x8')],{'host':_0x81ff('0xa'),'port':0x18eb});var socket=require(_0x81ff('0xb'))(new Redis(config[_0x81ff('0x8')]));var redis=new Redis(config[_0x81ff('0x8')]);function respondWithStatusCode(_0x33bb82,_0x416ebd){_0x416ebd=_0x416ebd||0xcc;return function(_0x3c7d38){if(_0x3c7d38){return _0x33bb82[_0x81ff('0xc')](_0x416ebd);}return _0x33bb82[_0x81ff('0xd')](_0x416ebd)[_0x81ff('0xe')]();};}function respondWithResult(_0x239050,_0x31be77){_0x31be77=_0x31be77||0xc8;return function(_0x1fa1fc){if(_0x1fa1fc){return _0x239050[_0x81ff('0xd')](_0x31be77)[_0x81ff('0xf')](_0x1fa1fc);}};}function respondWithFilteredResult(_0x15a3df,_0x305a46){return function(_0x45b154){if(_0x45b154){var _0x521ad1=typeof _0x305a46[_0x81ff('0x10')]==='undefined'&&typeof _0x305a46[_0x81ff('0x11')]==='undefined';var _0x3f591c=_0x45b154[_0x81ff('0x12')];var _0x4f4804=_0x521ad1?0x0:_0x305a46[_0x81ff('0x10')];var _0xf3cb54=_0x521ad1?_0x45b154[_0x81ff('0x12')]:_0x305a46[_0x81ff('0x10')]+_0x305a46[_0x81ff('0x11')];var _0x261f35;if(_0xf3cb54>=_0x3f591c){_0xf3cb54=_0x3f591c;_0x261f35=0xc8;}else{_0x261f35=0xce;}_0x15a3df[_0x81ff('0xd')](_0x261f35);return _0x15a3df[_0x81ff('0x13')](_0x81ff('0x14'),_0x4f4804+'-'+_0xf3cb54+'/'+_0x3f591c)[_0x81ff('0xf')](_0x45b154);}return null;};}function saveUpdates(_0x3a6a17){return function(_0x1564e1){if(_0x1564e1){return _0x1564e1[_0x81ff('0x15')](_0x3a6a17)['then'](function(_0x3284ff){return _0x3284ff;});}return null;};}function removeEntity(_0x4f6795){return function(_0x3febc8){if(_0x3febc8){return _0x3febc8[_0x81ff('0x16')]()[_0x81ff('0x17')](function(){_0x4f6795['status'](0xcc)[_0x81ff('0xe')]();});}};}function handleEntityNotFound(_0x2aedb9){return function(_0x19698d){if(!_0x19698d){_0x2aedb9[_0x81ff('0xc')](0x194);}return _0x19698d;};}function handleError(_0x4177eb,_0x49c7b8){_0x49c7b8=_0x49c7b8||0x1f4;return function(_0xc2dc05){logger[_0x81ff('0x18')](_0xc2dc05['stack']);if(_0xc2dc05[_0x81ff('0x19')]){delete _0xc2dc05[_0x81ff('0x19')];}_0x4177eb[_0x81ff('0xd')](_0x49c7b8)[_0x81ff('0x1a')](_0xc2dc05);};}function maskClientSecret(_0x42dc92){return _['padEnd'](_0x42dc92[_0x81ff('0x1b')](0x0,0x3),0x14,'*');}exports[_0x81ff('0x1c')]=function(_0xa382a9,_0x2c14ad){var _0x4f4576={},_0x74e4e5={},_0x153a1e={'count':0x0,'rows':[]};var _0x427939=_[_0x81ff('0x1d')](db[_0x81ff('0x1e')][_0x81ff('0x1f')],function(_0x2be28d){return{'name':_0x2be28d[_0x81ff('0x20')],'type':_0x2be28d['type']['key']};});_0x74e4e5[_0x81ff('0x21')]=_[_0x81ff('0x1d')](_0x427939,_0x81ff('0x19'));_0x74e4e5['query']=_['keys'](_0xa382a9['query']);_0x74e4e5[_0x81ff('0x22')]=_[_0x81ff('0x23')](_0x74e4e5['model'],_0x74e4e5['query']);_0x4f4576['attributes']=_[_0x81ff('0x23')](_0x74e4e5[_0x81ff('0x21')],qs[_0x81ff('0x24')](_0xa382a9[_0x81ff('0x25')]['fields']));_0x4f4576[_0x81ff('0x26')]=_0x4f4576[_0x81ff('0x26')]['length']?_0x4f4576[_0x81ff('0x26')]:_0x74e4e5[_0x81ff('0x21')];if(!_0xa382a9[_0x81ff('0x25')][_0x81ff('0x27')](_0x81ff('0x28'))){_0x4f4576[_0x81ff('0x11')]=qs[_0x81ff('0x11')](_0xa382a9[_0x81ff('0x25')][_0x81ff('0x11')]);_0x4f4576[_0x81ff('0x10')]=qs['offset'](_0xa382a9[_0x81ff('0x25')][_0x81ff('0x10')]);}_0x4f4576[_0x81ff('0x29')]=qs[_0x81ff('0x2a')](_0xa382a9[_0x81ff('0x25')]['sort']);_0x4f4576[_0x81ff('0x2b')]=qs[_0x81ff('0x22')](_[_0x81ff('0x2c')](_0xa382a9[_0x81ff('0x25')],_0x74e4e5['filters']),_0x427939);if(_0xa382a9[_0x81ff('0x25')][_0x81ff('0x2d')]){_0x4f4576[_0x81ff('0x2b')]=_[_0x81ff('0x2e')](_0x4f4576[_0x81ff('0x2b')],{'$or':_[_0x81ff('0x1d')](_0x427939,function(_0x23a129){if(_0x23a129[_0x81ff('0x2f')]!==_0x81ff('0x30')){var _0x4c66a0={};_0x4c66a0[_0x23a129['name']]={'$like':'%'+_0xa382a9[_0x81ff('0x25')][_0x81ff('0x2d')]+'%'};return _0x4c66a0;}})});}_0x4f4576=_[_0x81ff('0x2e')]({},_0x4f4576,_0xa382a9[_0x81ff('0x31')]);var _0x2a347f={'where':_0x4f4576[_0x81ff('0x2b')]};return db['CloudProvider'][_0x81ff('0x12')](_0x2a347f)['then'](function(_0x4ee248){_0x153a1e[_0x81ff('0x12')]=_0x4ee248;if(_0xa382a9[_0x81ff('0x25')][_0x81ff('0x32')]){_0x4f4576[_0x81ff('0x33')]=[{'all':!![]}];}return db[_0x81ff('0x1e')][_0x81ff('0x34')](_0x4f4576);})[_0x81ff('0x17')](function(_0x408c69){_0x153a1e[_0x81ff('0x35')]=_['map'](_0x408c69,function(_0x252568){if(_0x252568[_0x81ff('0x36')]==='MicrosoftAzure'){var _0x2863b7=encryptor[_0x81ff('0x37')](_0x252568[_0x81ff('0x38')]);_0x252568[_0x81ff('0x38')]=maskClientSecret(_0x2863b7);}return _0x252568;});return _0x153a1e;})[_0x81ff('0x17')](respondWithFilteredResult(_0x2c14ad,_0x4f4576))[_0x81ff('0x39')](handleError(_0x2c14ad,null));};exports[_0x81ff('0x3a')]=function(_0x20b4a1,_0x5543e5){var _0xe1d873={'raw':!![],'where':{'id':_0x20b4a1[_0x81ff('0x3b')]['id']}},_0x2b19af={};_0x2b19af[_0x81ff('0x21')]=_[_0x81ff('0x3c')](db[_0x81ff('0x1e')][_0x81ff('0x1f')]);_0x2b19af[_0x81ff('0x25')]=_[_0x81ff('0x3c')](_0x20b4a1[_0x81ff('0x25')]);_0x2b19af[_0x81ff('0x22')]=_[_0x81ff('0x23')](_0x2b19af[_0x81ff('0x21')],_0x2b19af[_0x81ff('0x25')]);_0xe1d873[_0x81ff('0x26')]=_['intersection'](_0x2b19af[_0x81ff('0x21')],qs[_0x81ff('0x24')](_0x20b4a1['query']['fields']));_0xe1d873[_0x81ff('0x26')]=_0xe1d873[_0x81ff('0x26')][_0x81ff('0x3d')]?_0xe1d873[_0x81ff('0x26')]:_0x2b19af['model'];if(_0x20b4a1[_0x81ff('0x25')]['includeAll']){_0xe1d873[_0x81ff('0x33')]=[{'all':!![]}];}_0xe1d873=_['merge']({},_0xe1d873,_0x20b4a1[_0x81ff('0x31')]);return db[_0x81ff('0x1e')][_0x81ff('0x3e')](_0xe1d873)[_0x81ff('0x17')](handleEntityNotFound(_0x5543e5,null))[_0x81ff('0x17')](function(_0x442f04){if(_0x442f04[_0x81ff('0x36')]===_0x81ff('0x3f')){var _0x53152c=encryptor[_0x81ff('0x37')](_0x442f04[_0x81ff('0x38')]);_0x442f04[_0x81ff('0x38')]=maskClientSecret(_0x53152c);}return _0x442f04;})[_0x81ff('0x17')](respondWithResult(_0x5543e5,null))['catch'](handleError(_0x5543e5,null));};exports[_0x81ff('0x40')]=function(_0x256159,_0x1bbd2d){if(!_0x256159[_0x81ff('0x41')][_0x81ff('0x19')])throw new Error(_0x81ff('0x42'));if(!_0x256159['body'][_0x81ff('0x36')])throw new Error(_0x81ff('0x43'));var _0x434634=db[_0x81ff('0x1e')][_0x81ff('0x1f')][_0x81ff('0x36')][_0x81ff('0x44')];if(!_0x434634[_0x81ff('0x45')](_0x256159[_0x81ff('0x41')][_0x81ff('0x36')]))throw new Error(_0x81ff('0x46')+_0x434634[_0x81ff('0x47')](',\x20'));if(_0x256159[_0x81ff('0x41')]['service']===_0x81ff('0x3f')&&_0x256159[_0x81ff('0x41')]['data3']){_0x256159['body'][_0x81ff('0x38')]=encryptor[_0x81ff('0x48')](_0x256159[_0x81ff('0x41')][_0x81ff('0x38')]);}return db[_0x81ff('0x1e')][_0x81ff('0x40')](_0x256159[_0x81ff('0x41')],{})[_0x81ff('0x17')](respondWithResult(_0x1bbd2d,0xc9))['catch'](handleError(_0x1bbd2d,null));};exports[_0x81ff('0x15')]=function(_0xa60f67,_0x6af4de){if(_0xa60f67['body']['id']){delete _0xa60f67[_0x81ff('0x41')]['id'];}return db[_0x81ff('0x1e')]['find']({'where':{'id':_0xa60f67['params']['id']}})[_0x81ff('0x17')](handleEntityNotFound(_0x6af4de,null))[_0x81ff('0x17')](function(_0x8eb2f4){if(_0x8eb2f4['service']===_0x81ff('0x3f')&&_0xa60f67[_0x81ff('0x41')][_0x81ff('0x38')]){_0xa60f67[_0x81ff('0x41')][_0x81ff('0x38')]=encryptor[_0x81ff('0x48')](_0xa60f67[_0x81ff('0x41')]['data3']);}return _0x8eb2f4;})[_0x81ff('0x17')](saveUpdates(_0xa60f67[_0x81ff('0x41')],null))[_0x81ff('0x17')](respondWithResult(_0x6af4de,null))[_0x81ff('0x39')](handleError(_0x6af4de,null));};exports[_0x81ff('0x16')]=function(_0x238276,_0x4ef55c){return db[_0x81ff('0x1e')][_0x81ff('0x3e')]({'where':{'id':_0x238276[_0x81ff('0x3b')]['id']}})[_0x81ff('0x17')](handleEntityNotFound(_0x4ef55c,null))['then'](removeEntity(_0x4ef55c,null))[_0x81ff('0x39')](handleError(_0x4ef55c,null));};exports[_0x81ff('0x49')]=function(_0x265798,_0x5f7337){return db[_0x81ff('0x1e')][_0x81ff('0x4a')]({'where':{'id':_0x265798['params']['id']},'raw':!![]})[_0x81ff('0x17')](function(_0x3fe019){if(!_0x3fe019)return _0x5f7337[_0x81ff('0xc')](0x194);return{'url':oauth[_0x81ff('0x4b')](_0x3fe019,_0x265798[_0x81ff('0x4c')]['id'])};})['then'](respondWithResult(_0x5f7337,null))[_0x81ff('0x39')](handleError(_0x5f7337,null));};exports[_0x81ff('0x4d')]=function(_0x383105,_0x334410){var _0x472c68=_0x383105[_0x81ff('0x41')];var _0x2e183a;var _0x1f0fc6;if(_0x472c68['error'])throw _0x472c68;redis[_0x81ff('0x4e')](_0x472c68[_0x81ff('0x4f')])[_0x81ff('0x17')](function(_0xe5cf55){if(!_0xe5cf55)throw new Error(_0x81ff('0x50'));_0x2e183a=JSON[_0x81ff('0x51')](_0xe5cf55);var _0x20cfa5=Buffer[_0x81ff('0x52')](_0x472c68[_0x81ff('0x4f')],'base64')['toString'](_0x81ff('0x53'));_0x1f0fc6=JSON[_0x81ff('0x51')](_0x20cfa5);if(!oauth['isValidIdToken'](_0x472c68['id_token'],_0x2e183a['oauth2Claims']))throw new Error(_0x81ff('0x54'));redis['del'](_0x472c68['state']);return oauth['getOauth2MicrosoftAccessToken'](_0x472c68[_0x81ff('0x55')],_0x2e183a);})['then'](function(_0x15355b){_0x2e183a[_0x81ff('0x56')]=_0x15355b['access_token'];_0x2e183a[_0x81ff('0x57')]=_0x15355b['refresh_token'];return db[_0x81ff('0x1e')]['update']({'data5':_0x2e183a[_0x81ff('0x56')],'data6':_0x2e183a['data6']},{'where':{'id':_0x2e183a['id']}});})[_0x81ff('0x17')](function(){oauth[_0x81ff('0x58')](_0x2e183a);socket['emit'](_0x81ff('0x59'),{'id':_0x1f0fc6['id']});return;})['then'](respondWithStatusCode(_0x334410,null))[_0x81ff('0x39')](function(_0x5517a0){if(_0x1f0fc6){socket[_0x81ff('0x5a')](_0x81ff('0x5b'),{'id':_0x1f0fc6['id']});}if(!_0x2e183a){logger['error'](_0x81ff('0x5c'),_0x5517a0);return _0x334410['status'](0x1f4)['send'](_0x5517a0['message']);}logger['error'](_0x81ff('0x5d'),_0x2e183a['id'],JSON[_0x81ff('0x5e')](_0x5517a0));});};