Built motion from commit 6a0adb95.|2.6.31
[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 _0x7bc7=['includes','Invalid\x20service\x20parameter.\x20Allowed\x20values:\x20','join','encryptString','create','find','oauth2MicrosoftAuthorize','findOne','user','oauth2MicrosoftCallback','state','parse','from','toString','ascii','isValidIdToken','id_token','oauth2Claims','Unable\x20to\x20validate\x20id_token','del','getOauth2MicrosoftAccessToken','code','data5','access_token','data6','refresh_token','startRefreshInterval','emit','microsoftAuthorization:success','microsoftAuthorization:error','send','Authentication\x20failed\x20for\x20cloud\x20provider\x20#%s,\x20err:%s','stringify','lodash','ioredis','../../components/parsers/qs','../../components/encryptor','../../config/environment','../../config/logger','api','../../mysqldb','./cloudProvider.oauth','redis','defaults','localhost','socket.io-emitter','sendStatus','status','end','undefined','limit','count','offset','set','Content-Range','json','update','then','destroy','error','stack','name','padEnd','slice','map','CloudProvider','rawAttributes','fieldName','type','key','model','query','filters','intersection','attributes','fields','nolimit','order','sort','where','pick','VIRTUAL','merge','options','includeAll','include','findAll','rows','service','MicrosoftAzure','decryptString','data3','catch','show','params','keys','length','body','Service\x20parameter\x20not\x20specified','values'];(function(_0x3469ec,_0x3672c5){var _0x402259=function(_0x6fc453){while(--_0x6fc453){_0x3469ec['push'](_0x3469ec['shift']());}};_0x402259(++_0x3672c5);}(_0x7bc7,0x14d));var _0x77bc=function(_0x1a5765,_0x2b3c48){_0x1a5765=_0x1a5765-0x0;var _0x19de75=_0x7bc7[_0x1a5765];return _0x19de75;};'use strict';var _=require(_0x77bc('0x0'));var Redis=require(_0x77bc('0x1'));var qs=require(_0x77bc('0x2'));var encryptor=require(_0x77bc('0x3'));var config=require(_0x77bc('0x4'));var logger=require(_0x77bc('0x5'))(_0x77bc('0x6'));var db=require(_0x77bc('0x7'))['db'];var oauth=require(_0x77bc('0x8'));config[_0x77bc('0x9')]=_[_0x77bc('0xa')](config['redis'],{'host':_0x77bc('0xb'),'port':0x18eb});var socket=require(_0x77bc('0xc'))(new Redis(config[_0x77bc('0x9')]));var redis=new Redis(config['redis']);function respondWithStatusCode(_0x80e030,_0x3ac71b){_0x3ac71b=_0x3ac71b||0xcc;return function(_0x7a333f){if(_0x7a333f){return _0x80e030[_0x77bc('0xd')](_0x3ac71b);}return _0x80e030[_0x77bc('0xe')](_0x3ac71b)[_0x77bc('0xf')]();};}function respondWithResult(_0x359d79,_0x55dc56){_0x55dc56=_0x55dc56||0xc8;return function(_0x37f95b){if(_0x37f95b){return _0x359d79[_0x77bc('0xe')](_0x55dc56)['json'](_0x37f95b);}};}function respondWithFilteredResult(_0xda9286,_0x5d8cde){return function(_0x304295){if(_0x304295){var _0x53ecf9=typeof _0x5d8cde['offset']===_0x77bc('0x10')&&typeof _0x5d8cde[_0x77bc('0x11')]===_0x77bc('0x10');var _0x567933=_0x304295[_0x77bc('0x12')];var _0x230873=_0x53ecf9?0x0:_0x5d8cde[_0x77bc('0x13')];var _0x2d2e43=_0x53ecf9?_0x304295[_0x77bc('0x12')]:_0x5d8cde[_0x77bc('0x13')]+_0x5d8cde[_0x77bc('0x11')];var _0x14215f;if(_0x2d2e43>=_0x567933){_0x2d2e43=_0x567933;_0x14215f=0xc8;}else{_0x14215f=0xce;}_0xda9286[_0x77bc('0xe')](_0x14215f);return _0xda9286[_0x77bc('0x14')](_0x77bc('0x15'),_0x230873+'-'+_0x2d2e43+'/'+_0x567933)[_0x77bc('0x16')](_0x304295);}return null;};}function saveUpdates(_0x2d6c39){return function(_0x1f06f2){if(_0x1f06f2){return _0x1f06f2[_0x77bc('0x17')](_0x2d6c39)[_0x77bc('0x18')](function(_0x5a5459){return _0x5a5459;});}return null;};}function removeEntity(_0x45144c){return function(_0x4d4a5c){if(_0x4d4a5c){return _0x4d4a5c[_0x77bc('0x19')]()[_0x77bc('0x18')](function(){_0x45144c['status'](0xcc)[_0x77bc('0xf')]();});}};}function handleEntityNotFound(_0x590614){return function(_0x59adc5){if(!_0x59adc5){_0x590614[_0x77bc('0xd')](0x194);}return _0x59adc5;};}function handleError(_0x1f615b,_0x34eb72){_0x34eb72=_0x34eb72||0x1f4;return function(_0x44f5d6){logger[_0x77bc('0x1a')](_0x44f5d6[_0x77bc('0x1b')]);if(_0x44f5d6['name']){delete _0x44f5d6[_0x77bc('0x1c')];}_0x1f615b[_0x77bc('0xe')](_0x34eb72)['send'](_0x44f5d6);};}function maskClientSecret(_0x1d0fd9){return _[_0x77bc('0x1d')](_0x1d0fd9[_0x77bc('0x1e')](0x0,0x3),0x14,'*');}exports['index']=function(_0x53f89b,_0x1e9c8d){var _0x5e2f3e={},_0x521f59={},_0xaf8a0b={'count':0x0,'rows':[]};var _0x52ee8a=_[_0x77bc('0x1f')](db[_0x77bc('0x20')][_0x77bc('0x21')],function(_0x549536){return{'name':_0x549536[_0x77bc('0x22')],'type':_0x549536[_0x77bc('0x23')][_0x77bc('0x24')]};});_0x521f59[_0x77bc('0x25')]=_[_0x77bc('0x1f')](_0x52ee8a,_0x77bc('0x1c'));_0x521f59[_0x77bc('0x26')]=_['keys'](_0x53f89b[_0x77bc('0x26')]);_0x521f59[_0x77bc('0x27')]=_[_0x77bc('0x28')](_0x521f59[_0x77bc('0x25')],_0x521f59[_0x77bc('0x26')]);_0x5e2f3e[_0x77bc('0x29')]=_['intersection'](_0x521f59['model'],qs[_0x77bc('0x2a')](_0x53f89b[_0x77bc('0x26')]['fields']));_0x5e2f3e['attributes']=_0x5e2f3e['attributes']['length']?_0x5e2f3e[_0x77bc('0x29')]:_0x521f59[_0x77bc('0x25')];if(!_0x53f89b['query']['hasOwnProperty'](_0x77bc('0x2b'))){_0x5e2f3e[_0x77bc('0x11')]=qs[_0x77bc('0x11')](_0x53f89b[_0x77bc('0x26')][_0x77bc('0x11')]);_0x5e2f3e['offset']=qs[_0x77bc('0x13')](_0x53f89b[_0x77bc('0x26')][_0x77bc('0x13')]);}_0x5e2f3e[_0x77bc('0x2c')]=qs['sort'](_0x53f89b[_0x77bc('0x26')][_0x77bc('0x2d')]);_0x5e2f3e[_0x77bc('0x2e')]=qs[_0x77bc('0x27')](_[_0x77bc('0x2f')](_0x53f89b[_0x77bc('0x26')],_0x521f59[_0x77bc('0x27')]),_0x52ee8a);if(_0x53f89b[_0x77bc('0x26')]['filter']){_0x5e2f3e[_0x77bc('0x2e')]=_['merge'](_0x5e2f3e['where'],{'$or':_[_0x77bc('0x1f')](_0x52ee8a,function(_0x377d8b){if(_0x377d8b[_0x77bc('0x23')]!==_0x77bc('0x30')){var _0x7c264f={};_0x7c264f[_0x377d8b[_0x77bc('0x1c')]]={'$like':'%'+_0x53f89b[_0x77bc('0x26')]['filter']+'%'};return _0x7c264f;}})});}_0x5e2f3e=_[_0x77bc('0x31')]({},_0x5e2f3e,_0x53f89b[_0x77bc('0x32')]);var _0x4c23ac={'where':_0x5e2f3e[_0x77bc('0x2e')]};return db[_0x77bc('0x20')][_0x77bc('0x12')](_0x4c23ac)[_0x77bc('0x18')](function(_0x305ec6){_0xaf8a0b[_0x77bc('0x12')]=_0x305ec6;if(_0x53f89b[_0x77bc('0x26')][_0x77bc('0x33')]){_0x5e2f3e[_0x77bc('0x34')]=[{'all':!![]}];}return db[_0x77bc('0x20')][_0x77bc('0x35')](_0x5e2f3e);})[_0x77bc('0x18')](function(_0x5e41d1){_0xaf8a0b[_0x77bc('0x36')]=_['map'](_0x5e41d1,function(_0x40ae08){if(_0x40ae08[_0x77bc('0x37')]===_0x77bc('0x38')){var _0x286d6a=encryptor[_0x77bc('0x39')](_0x40ae08[_0x77bc('0x3a')]);_0x40ae08[_0x77bc('0x3a')]=maskClientSecret(_0x286d6a);}return _0x40ae08;});return _0xaf8a0b;})[_0x77bc('0x18')](respondWithFilteredResult(_0x1e9c8d,_0x5e2f3e))[_0x77bc('0x3b')](handleError(_0x1e9c8d,null));};exports[_0x77bc('0x3c')]=function(_0x40f3bd,_0x36c6e8){var _0x3bc714={'raw':!![],'where':{'id':_0x40f3bd[_0x77bc('0x3d')]['id']}},_0x16182c={};_0x16182c['model']=_[_0x77bc('0x3e')](db['CloudProvider'][_0x77bc('0x21')]);_0x16182c[_0x77bc('0x26')]=_['keys'](_0x40f3bd[_0x77bc('0x26')]);_0x16182c[_0x77bc('0x27')]=_[_0x77bc('0x28')](_0x16182c[_0x77bc('0x25')],_0x16182c[_0x77bc('0x26')]);_0x3bc714['attributes']=_[_0x77bc('0x28')](_0x16182c['model'],qs[_0x77bc('0x2a')](_0x40f3bd[_0x77bc('0x26')][_0x77bc('0x2a')]));_0x3bc714[_0x77bc('0x29')]=_0x3bc714[_0x77bc('0x29')][_0x77bc('0x3f')]?_0x3bc714[_0x77bc('0x29')]:_0x16182c['model'];if(_0x40f3bd[_0x77bc('0x26')]['includeAll']){_0x3bc714[_0x77bc('0x34')]=[{'all':!![]}];}_0x3bc714=_[_0x77bc('0x31')]({},_0x3bc714,_0x40f3bd['options']);return db[_0x77bc('0x20')]['find'](_0x3bc714)[_0x77bc('0x18')](handleEntityNotFound(_0x36c6e8,null))[_0x77bc('0x18')](function(_0x4c7596){if(_0x4c7596[_0x77bc('0x37')]==='MicrosoftAzure'){var _0x4c6247=encryptor[_0x77bc('0x39')](_0x4c7596[_0x77bc('0x3a')]);_0x4c7596['data3']=maskClientSecret(_0x4c6247);}return _0x4c7596;})[_0x77bc('0x18')](respondWithResult(_0x36c6e8,null))[_0x77bc('0x3b')](handleError(_0x36c6e8,null));};exports['create']=function(_0xac5c5,_0x5a5386){if(!_0xac5c5[_0x77bc('0x40')]['name'])throw new Error('Name\x20parameter\x20not\x20specified');if(!_0xac5c5[_0x77bc('0x40')]['service'])throw new Error(_0x77bc('0x41'));var _0x165036=db['CloudProvider'][_0x77bc('0x21')][_0x77bc('0x37')][_0x77bc('0x42')];if(!_0x165036[_0x77bc('0x43')](_0xac5c5[_0x77bc('0x40')][_0x77bc('0x37')]))throw new Error(_0x77bc('0x44')+_0x165036[_0x77bc('0x45')](',\x20'));if(_0xac5c5[_0x77bc('0x40')][_0x77bc('0x37')]===_0x77bc('0x38')&&_0xac5c5['body'][_0x77bc('0x3a')]){_0xac5c5[_0x77bc('0x40')][_0x77bc('0x3a')]=encryptor[_0x77bc('0x46')](_0xac5c5['body']['data3']);}return db[_0x77bc('0x20')][_0x77bc('0x47')](_0xac5c5['body'],{})[_0x77bc('0x18')](respondWithResult(_0x5a5386,0xc9))[_0x77bc('0x3b')](handleError(_0x5a5386,null));};exports[_0x77bc('0x17')]=function(_0x33a372,_0x17357e){if(_0x33a372[_0x77bc('0x40')]['id']){delete _0x33a372['body']['id'];}return db[_0x77bc('0x20')][_0x77bc('0x48')]({'where':{'id':_0x33a372['params']['id']}})['then'](handleEntityNotFound(_0x17357e,null))['then'](function(_0x138cc0){if(_0x138cc0['service']===_0x77bc('0x38')&&_0x33a372['body'][_0x77bc('0x3a')]){_0x33a372[_0x77bc('0x40')][_0x77bc('0x3a')]=encryptor[_0x77bc('0x46')](_0x33a372[_0x77bc('0x40')]['data3']);}return _0x138cc0;})[_0x77bc('0x18')](saveUpdates(_0x33a372[_0x77bc('0x40')],null))['then'](respondWithResult(_0x17357e,null))['catch'](handleError(_0x17357e,null));};exports['destroy']=function(_0x5123fc,_0x4abe2a){return db[_0x77bc('0x20')][_0x77bc('0x48')]({'where':{'id':_0x5123fc[_0x77bc('0x3d')]['id']}})[_0x77bc('0x18')](handleEntityNotFound(_0x4abe2a,null))['then'](removeEntity(_0x4abe2a,null))['catch'](handleError(_0x4abe2a,null));};exports[_0x77bc('0x49')]=function(_0x1dad9e,_0x2e3146){return db[_0x77bc('0x20')][_0x77bc('0x4a')]({'where':{'id':_0x1dad9e[_0x77bc('0x3d')]['id']},'raw':!![]})[_0x77bc('0x18')](function(_0x40187c){if(!_0x40187c)return _0x2e3146['sendStatus'](0x194);return{'url':oauth['generateMicrosoftAuthorizationUrl'](_0x40187c,_0x1dad9e[_0x77bc('0x4b')]['id'])};})[_0x77bc('0x18')](respondWithResult(_0x2e3146,null))[_0x77bc('0x3b')](handleError(_0x2e3146,null));};exports[_0x77bc('0x4c')]=function(_0x569c9d,_0x5d0315){var _0x2d7526=_0x569c9d['body'];var _0x1d06f6;var _0x40cd63;if(_0x2d7526['error'])throw _0x2d7526;redis['get'](_0x2d7526[_0x77bc('0x4d')])[_0x77bc('0x18')](function(_0x2df298){if(!_0x2df298)throw new Error('Invalid\x20state\x20parameter');_0x1d06f6=JSON[_0x77bc('0x4e')](_0x2df298);var _0x283f29=Buffer[_0x77bc('0x4f')](_0x2d7526[_0x77bc('0x4d')],'base64')[_0x77bc('0x50')](_0x77bc('0x51'));_0x40cd63=JSON[_0x77bc('0x4e')](_0x283f29);if(!oauth[_0x77bc('0x52')](_0x2d7526[_0x77bc('0x53')],_0x1d06f6[_0x77bc('0x54')]))throw new Error(_0x77bc('0x55'));redis[_0x77bc('0x56')](_0x2d7526['state']);return oauth[_0x77bc('0x57')](_0x2d7526[_0x77bc('0x58')],_0x1d06f6);})[_0x77bc('0x18')](function(_0x45a4c3){_0x1d06f6[_0x77bc('0x59')]=_0x45a4c3[_0x77bc('0x5a')];_0x1d06f6[_0x77bc('0x5b')]=_0x45a4c3[_0x77bc('0x5c')];return db[_0x77bc('0x20')][_0x77bc('0x17')]({'data5':_0x1d06f6['data5'],'data6':_0x1d06f6['data6']},{'where':{'id':_0x1d06f6['id']}});})['then'](function(){oauth[_0x77bc('0x5d')](_0x1d06f6);socket[_0x77bc('0x5e')](_0x77bc('0x5f'),{'id':_0x40cd63['id']});return;})[_0x77bc('0x18')](respondWithStatusCode(_0x5d0315,null))[_0x77bc('0x3b')](function(_0x2f670c){if(_0x40cd63){socket[_0x77bc('0x5e')](_0x77bc('0x60'),{'id':_0x40cd63['id']});}if(!_0x1d06f6){logger[_0x77bc('0x1a')]('Invalid\x20authentication\x20state\x20parameter',_0x2f670c);return _0x5d0315['status'](0x1f4)[_0x77bc('0x61')](_0x2f670c['message']);}logger[_0x77bc('0x1a')](_0x77bc('0x62'),_0x1d06f6['id'],JSON[_0x77bc('0x63')](_0x2f670c));});};