Built motion from commit dc14b02d.|2.6.27
[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 _0xb6f6=['localhost','socket.io-emitter','sendStatus','end','status','json','offset','undefined','limit','count','set','then','destroy','stack','name','send','padEnd','slice','map','rawAttributes','fieldName','type','key','model','query','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','sort','where','filter','merge','VIRTUAL','includeAll','include','findAll','rows','service','MicrosoftAzure','decryptString','data3','catch','show','params','keys','CloudProvider','options','find','create','body','Service\x20parameter\x20not\x20specified','values','includes','join','encryptString','oauth2MicrosoftAuthorize','generateMicrosoftAuthorizationUrl','user','state','base64','toString','ascii','isValidIdToken','id_token','oauth2Claims','Unable\x20to\x20validate\x20id_token','code','data5','access_token','data6','refresh_token','update','microsoftAuthorization:success','emit','microsoftAuthorization:error','error','Invalid\x20authentication\x20state\x20parameter','message','Authentication\x20failed\x20for\x20cloud\x20provider\x20#%s,\x20err:%s','lodash','ioredis','../../components/parsers/qs','../../config/environment','../../config/logger','api','../../mysqldb','./cloudProvider.oauth','redis','defaults'];(function(_0x415158,_0x4841d5){var _0xa677f0=function(_0x5b1a73){while(--_0x5b1a73){_0x415158['push'](_0x415158['shift']());}};_0xa677f0(++_0x4841d5);}(_0xb6f6,0x10d));var _0x6b6f=function(_0x5e4d93,_0x218315){_0x5e4d93=_0x5e4d93-0x0;var _0x1e3cac=_0xb6f6[_0x5e4d93];return _0x1e3cac;};'use strict';var _=require(_0x6b6f('0x0'));var Redis=require(_0x6b6f('0x1'));var qs=require(_0x6b6f('0x2'));var encryptor=require('../../components/encryptor');var config=require(_0x6b6f('0x3'));var logger=require(_0x6b6f('0x4'))(_0x6b6f('0x5'));var db=require(_0x6b6f('0x6'))['db'];var oauth=require(_0x6b6f('0x7'));config[_0x6b6f('0x8')]=_[_0x6b6f('0x9')](config[_0x6b6f('0x8')],{'host':_0x6b6f('0xa'),'port':0x18eb});var socket=require(_0x6b6f('0xb'))(new Redis(config['redis']));var redis=new Redis(config[_0x6b6f('0x8')]);function respondWithStatusCode(_0x35625e,_0xf2417a){_0xf2417a=_0xf2417a||0xcc;return function(_0x3c8269){if(_0x3c8269){return _0x35625e[_0x6b6f('0xc')](_0xf2417a);}return _0x35625e['status'](_0xf2417a)[_0x6b6f('0xd')]();};}function respondWithResult(_0x31e0ac,_0x3d1f37){_0x3d1f37=_0x3d1f37||0xc8;return function(_0x482b21){if(_0x482b21){return _0x31e0ac[_0x6b6f('0xe')](_0x3d1f37)[_0x6b6f('0xf')](_0x482b21);}};}function respondWithFilteredResult(_0x5b0597,_0x4a06d6){return function(_0x2414d4){if(_0x2414d4){var _0x385e5f=typeof _0x4a06d6[_0x6b6f('0x10')]===_0x6b6f('0x11')&&typeof _0x4a06d6[_0x6b6f('0x12')]===_0x6b6f('0x11');var _0x440509=_0x2414d4[_0x6b6f('0x13')];var _0x4c274a=_0x385e5f?0x0:_0x4a06d6['offset'];var _0x1a5d1c=_0x385e5f?_0x2414d4['count']:_0x4a06d6[_0x6b6f('0x10')]+_0x4a06d6[_0x6b6f('0x12')];var _0x895f65;if(_0x1a5d1c>=_0x440509){_0x1a5d1c=_0x440509;_0x895f65=0xc8;}else{_0x895f65=0xce;}_0x5b0597[_0x6b6f('0xe')](_0x895f65);return _0x5b0597[_0x6b6f('0x14')]('Content-Range',_0x4c274a+'-'+_0x1a5d1c+'/'+_0x440509)[_0x6b6f('0xf')](_0x2414d4);}return null;};}function saveUpdates(_0x689945){return function(_0x122da4){if(_0x122da4){return _0x122da4['update'](_0x689945)[_0x6b6f('0x15')](function(_0x44c125){return _0x44c125;});}return null;};}function removeEntity(_0x469779){return function(_0x1bb3c5){if(_0x1bb3c5){return _0x1bb3c5[_0x6b6f('0x16')]()[_0x6b6f('0x15')](function(){_0x469779[_0x6b6f('0xe')](0xcc)[_0x6b6f('0xd')]();});}};}function handleEntityNotFound(_0x5bf608){return function(_0x6e8359){if(!_0x6e8359){_0x5bf608['sendStatus'](0x194);}return _0x6e8359;};}function handleError(_0xb74391,_0x306f3e){_0x306f3e=_0x306f3e||0x1f4;return function(_0x1a5b74){logger['error'](_0x1a5b74[_0x6b6f('0x17')]);if(_0x1a5b74['name']){delete _0x1a5b74[_0x6b6f('0x18')];}_0xb74391['status'](_0x306f3e)[_0x6b6f('0x19')](_0x1a5b74);};}function maskClientSecret(_0x2e8703){return _[_0x6b6f('0x1a')](_0x2e8703[_0x6b6f('0x1b')](0x0,0x3),0x14,'*');}exports['index']=function(_0x57dbf7,_0x5b2df1){var _0x535452={},_0x4ee862={},_0x18f19f={'count':0x0,'rows':[]};var _0x40e586=_[_0x6b6f('0x1c')](db['CloudProvider'][_0x6b6f('0x1d')],function(_0x313d37){return{'name':_0x313d37[_0x6b6f('0x1e')],'type':_0x313d37[_0x6b6f('0x1f')][_0x6b6f('0x20')]};});_0x4ee862[_0x6b6f('0x21')]=_[_0x6b6f('0x1c')](_0x40e586,_0x6b6f('0x18'));_0x4ee862['query']=_['keys'](_0x57dbf7[_0x6b6f('0x22')]);_0x4ee862[_0x6b6f('0x23')]=_[_0x6b6f('0x24')](_0x4ee862[_0x6b6f('0x21')],_0x4ee862[_0x6b6f('0x22')]);_0x535452[_0x6b6f('0x25')]=_[_0x6b6f('0x24')](_0x4ee862[_0x6b6f('0x21')],qs[_0x6b6f('0x26')](_0x57dbf7['query'][_0x6b6f('0x26')]));_0x535452[_0x6b6f('0x25')]=_0x535452['attributes'][_0x6b6f('0x27')]?_0x535452['attributes']:_0x4ee862[_0x6b6f('0x21')];if(!_0x57dbf7['query'][_0x6b6f('0x28')](_0x6b6f('0x29'))){_0x535452[_0x6b6f('0x12')]=qs[_0x6b6f('0x12')](_0x57dbf7[_0x6b6f('0x22')][_0x6b6f('0x12')]);_0x535452[_0x6b6f('0x10')]=qs[_0x6b6f('0x10')](_0x57dbf7[_0x6b6f('0x22')][_0x6b6f('0x10')]);}_0x535452['order']=qs[_0x6b6f('0x2a')](_0x57dbf7[_0x6b6f('0x22')][_0x6b6f('0x2a')]);_0x535452[_0x6b6f('0x2b')]=qs[_0x6b6f('0x23')](_['pick'](_0x57dbf7[_0x6b6f('0x22')],_0x4ee862[_0x6b6f('0x23')]),_0x40e586);if(_0x57dbf7[_0x6b6f('0x22')][_0x6b6f('0x2c')]){_0x535452[_0x6b6f('0x2b')]=_[_0x6b6f('0x2d')](_0x535452[_0x6b6f('0x2b')],{'$or':_[_0x6b6f('0x1c')](_0x40e586,function(_0x1bf470){if(_0x1bf470[_0x6b6f('0x1f')]!==_0x6b6f('0x2e')){var _0x10d11c={};_0x10d11c[_0x1bf470[_0x6b6f('0x18')]]={'$like':'%'+_0x57dbf7[_0x6b6f('0x22')][_0x6b6f('0x2c')]+'%'};return _0x10d11c;}})});}_0x535452=_['merge']({},_0x535452,_0x57dbf7['options']);var _0x29075e={'where':_0x535452[_0x6b6f('0x2b')]};return db['CloudProvider'][_0x6b6f('0x13')](_0x29075e)[_0x6b6f('0x15')](function(_0x44e28b){_0x18f19f[_0x6b6f('0x13')]=_0x44e28b;if(_0x57dbf7[_0x6b6f('0x22')][_0x6b6f('0x2f')]){_0x535452[_0x6b6f('0x30')]=[{'all':!![]}];}return db['CloudProvider'][_0x6b6f('0x31')](_0x535452);})[_0x6b6f('0x15')](function(_0x244016){_0x18f19f[_0x6b6f('0x32')]=_[_0x6b6f('0x1c')](_0x244016,function(_0xbc106f){if(_0xbc106f[_0x6b6f('0x33')]===_0x6b6f('0x34')){var _0x4a7a68=encryptor[_0x6b6f('0x35')](_0xbc106f[_0x6b6f('0x36')]);_0xbc106f[_0x6b6f('0x36')]=maskClientSecret(_0x4a7a68);}return _0xbc106f;});return _0x18f19f;})[_0x6b6f('0x15')](respondWithFilteredResult(_0x5b2df1,_0x535452))[_0x6b6f('0x37')](handleError(_0x5b2df1,null));};exports[_0x6b6f('0x38')]=function(_0x354813,_0x2223a1){var _0x369f9b={'raw':!![],'where':{'id':_0x354813[_0x6b6f('0x39')]['id']}},_0x4fbfcb={};_0x4fbfcb[_0x6b6f('0x21')]=_[_0x6b6f('0x3a')](db[_0x6b6f('0x3b')][_0x6b6f('0x1d')]);_0x4fbfcb[_0x6b6f('0x22')]=_['keys'](_0x354813[_0x6b6f('0x22')]);_0x4fbfcb[_0x6b6f('0x23')]=_[_0x6b6f('0x24')](_0x4fbfcb[_0x6b6f('0x21')],_0x4fbfcb[_0x6b6f('0x22')]);_0x369f9b[_0x6b6f('0x25')]=_[_0x6b6f('0x24')](_0x4fbfcb['model'],qs[_0x6b6f('0x26')](_0x354813[_0x6b6f('0x22')]['fields']));_0x369f9b[_0x6b6f('0x25')]=_0x369f9b[_0x6b6f('0x25')][_0x6b6f('0x27')]?_0x369f9b[_0x6b6f('0x25')]:_0x4fbfcb[_0x6b6f('0x21')];if(_0x354813['query']['includeAll']){_0x369f9b[_0x6b6f('0x30')]=[{'all':!![]}];}_0x369f9b=_[_0x6b6f('0x2d')]({},_0x369f9b,_0x354813[_0x6b6f('0x3c')]);return db[_0x6b6f('0x3b')][_0x6b6f('0x3d')](_0x369f9b)[_0x6b6f('0x15')](handleEntityNotFound(_0x2223a1,null))['then'](function(_0x5dec95){if(_0x5dec95['service']===_0x6b6f('0x34')){var _0x331119=encryptor[_0x6b6f('0x35')](_0x5dec95[_0x6b6f('0x36')]);_0x5dec95[_0x6b6f('0x36')]=maskClientSecret(_0x331119);}return _0x5dec95;})[_0x6b6f('0x15')](respondWithResult(_0x2223a1,null))['catch'](handleError(_0x2223a1,null));};exports[_0x6b6f('0x3e')]=function(_0x1e5859,_0x43fa3d){if(!_0x1e5859[_0x6b6f('0x3f')]['name'])throw new Error('Name\x20parameter\x20not\x20specified');if(!_0x1e5859[_0x6b6f('0x3f')][_0x6b6f('0x33')])throw new Error(_0x6b6f('0x40'));var _0x1ba624=db['CloudProvider'][_0x6b6f('0x1d')][_0x6b6f('0x33')][_0x6b6f('0x41')];if(!_0x1ba624[_0x6b6f('0x42')](_0x1e5859[_0x6b6f('0x3f')][_0x6b6f('0x33')]))throw new Error('Invalid\x20service\x20parameter.\x20Allowed\x20values:\x20'+_0x1ba624[_0x6b6f('0x43')](',\x20'));if(_0x1e5859[_0x6b6f('0x3f')][_0x6b6f('0x33')]===_0x6b6f('0x34')&&_0x1e5859[_0x6b6f('0x3f')]['data3']){_0x1e5859['body']['data3']=encryptor[_0x6b6f('0x44')](_0x1e5859[_0x6b6f('0x3f')][_0x6b6f('0x36')]);}return db[_0x6b6f('0x3b')]['create'](_0x1e5859['body'],{})['then'](respondWithResult(_0x43fa3d,0xc9))[_0x6b6f('0x37')](handleError(_0x43fa3d,null));};exports['update']=function(_0x349ad8,_0x5cab8e){if(_0x349ad8[_0x6b6f('0x3f')]['id']){delete _0x349ad8[_0x6b6f('0x3f')]['id'];}return db[_0x6b6f('0x3b')][_0x6b6f('0x3d')]({'where':{'id':_0x349ad8[_0x6b6f('0x39')]['id']}})[_0x6b6f('0x15')](handleEntityNotFound(_0x5cab8e,null))[_0x6b6f('0x15')](function(_0x1ca870){if(_0x1ca870['service']===_0x6b6f('0x34')&&_0x349ad8[_0x6b6f('0x3f')][_0x6b6f('0x36')]){_0x349ad8[_0x6b6f('0x3f')][_0x6b6f('0x36')]=encryptor['encryptString'](_0x349ad8[_0x6b6f('0x3f')][_0x6b6f('0x36')]);}return _0x1ca870;})[_0x6b6f('0x15')](saveUpdates(_0x349ad8['body'],null))[_0x6b6f('0x15')](respondWithResult(_0x5cab8e,null))[_0x6b6f('0x37')](handleError(_0x5cab8e,null));};exports[_0x6b6f('0x16')]=function(_0x5bbccb,_0x4025c1){return db[_0x6b6f('0x3b')]['find']({'where':{'id':_0x5bbccb[_0x6b6f('0x39')]['id']}})[_0x6b6f('0x15')](handleEntityNotFound(_0x4025c1,null))[_0x6b6f('0x15')](removeEntity(_0x4025c1,null))['catch'](handleError(_0x4025c1,null));};exports[_0x6b6f('0x45')]=function(_0x2c0920,_0x41faff){return db['CloudProvider']['findOne']({'where':{'id':_0x2c0920[_0x6b6f('0x39')]['id']},'raw':!![]})[_0x6b6f('0x15')](function(_0x1383fb){if(!_0x1383fb)return _0x41faff[_0x6b6f('0xc')](0x194);return{'url':oauth[_0x6b6f('0x46')](_0x1383fb,_0x2c0920[_0x6b6f('0x47')]['id'])};})[_0x6b6f('0x15')](respondWithResult(_0x41faff,null))[_0x6b6f('0x37')](handleError(_0x41faff,null));};exports['oauth2MicrosoftCallback']=function(_0x25cf49,_0x3816e){var _0x1fac3a=_0x25cf49[_0x6b6f('0x3f')];var _0x20544b;var _0x9b055f;if(_0x1fac3a['error'])throw _0x1fac3a;redis['get'](_0x1fac3a[_0x6b6f('0x48')])[_0x6b6f('0x15')](function(_0x4e91ef){if(!_0x4e91ef)throw new Error('Invalid\x20state\x20parameter');_0x20544b=JSON['parse'](_0x4e91ef);var _0x253eba=Buffer['from'](_0x1fac3a['state'],_0x6b6f('0x49'))[_0x6b6f('0x4a')](_0x6b6f('0x4b'));_0x9b055f=JSON['parse'](_0x253eba);if(!oauth[_0x6b6f('0x4c')](_0x1fac3a[_0x6b6f('0x4d')],_0x20544b[_0x6b6f('0x4e')]))throw new Error(_0x6b6f('0x4f'));redis['del'](_0x1fac3a[_0x6b6f('0x48')]);return oauth['getOauth2MicrosoftAccessToken'](_0x1fac3a[_0x6b6f('0x50')],_0x20544b);})[_0x6b6f('0x15')](function(_0xecea44){_0x20544b[_0x6b6f('0x51')]=_0xecea44[_0x6b6f('0x52')];_0x20544b[_0x6b6f('0x53')]=_0xecea44[_0x6b6f('0x54')];return db[_0x6b6f('0x3b')][_0x6b6f('0x55')]({'data5':_0x20544b[_0x6b6f('0x51')],'data6':_0x20544b['data6']},{'where':{'id':_0x20544b['id']}});})[_0x6b6f('0x15')](function(){oauth['startRefreshInterval'](_0x20544b);socket['emit'](_0x6b6f('0x56'),{'id':_0x9b055f['id']});return;})[_0x6b6f('0x15')](respondWithStatusCode(_0x3816e,null))[_0x6b6f('0x37')](function(_0x5f5640){if(_0x9b055f){socket[_0x6b6f('0x57')](_0x6b6f('0x58'),{'id':_0x9b055f['id']});}if(!_0x20544b){logger[_0x6b6f('0x59')](_0x6b6f('0x5a'),_0x5f5640);return _0x3816e['status'](0x1f4)[_0x6b6f('0x19')](_0x5f5640[_0x6b6f('0x5b')]);}logger[_0x6b6f('0x59')](_0x6b6f('0x5c'),_0x20544b['id'],JSON['stringify'](_0x5f5640));});};