1cb66c4a60d60ee92c391c444a2a49b613b307a9
[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 _0x0f61=['error','name','slice','CloudProvider','rawAttributes','fieldName','type','key','model','map','query','keys','filters','intersection','attributes','fields','hasOwnProperty','nolimit','sort','where','pick','filter','merge','VIRTUAL','options','findAll','rows','service','MicrosoftAzure','data3','catch','show','length','includeAll','include','decryptString','body','Name\x20parameter\x20not\x20specified','Service\x20parameter\x20not\x20specified','values','includes','Invalid\x20service\x20parameter.\x20Allowed\x20values:\x20','join','encryptString','update','find','params','destroy','oauth2MicrosoftAuthorize','findOne','user','get','state','Invalid\x20state\x20parameter','parse','base64','toString','ascii','isValidIdToken','oauth2Claims','Unable\x20to\x20validate\x20id_token','getOauth2MicrosoftAccessToken','code','data5','data6','refresh_token','startRefreshInterval','emit','lodash','ioredis','../../components/parsers/qs','../../config/environment','../../config/logger','api','../../mysqldb','./cloudProvider.oauth','redis','defaults','localhost','socket.io-emitter','sendStatus','status','end','json','offset','undefined','limit','count','Content-Range','then'];(function(_0x2adb42,_0xed8b12){var _0x101363=function(_0x1a9d3d){while(--_0x1a9d3d){_0x2adb42['push'](_0x2adb42['shift']());}};_0x101363(++_0xed8b12);}(_0x0f61,0x1ac));var _0x10f6=function(_0x2132f9,_0x12bb36){_0x2132f9=_0x2132f9-0x0;var _0x21fe79=_0x0f61[_0x2132f9];return _0x21fe79;};'use strict';var _=require(_0x10f6('0x0'));var Redis=require(_0x10f6('0x1'));var qs=require(_0x10f6('0x2'));var encryptor=require('../../components/encryptor');var config=require(_0x10f6('0x3'));var logger=require(_0x10f6('0x4'))(_0x10f6('0x5'));var db=require(_0x10f6('0x6'))['db'];var oauth=require(_0x10f6('0x7'));config[_0x10f6('0x8')]=_[_0x10f6('0x9')](config[_0x10f6('0x8')],{'host':_0x10f6('0xa'),'port':0x18eb});var socket=require(_0x10f6('0xb'))(new Redis(config['redis']));var redis=new Redis(config[_0x10f6('0x8')]);function respondWithStatusCode(_0x56ba9b,_0x40dd2f){_0x40dd2f=_0x40dd2f||0xcc;return function(_0x340d53){if(_0x340d53){return _0x56ba9b[_0x10f6('0xc')](_0x40dd2f);}return _0x56ba9b[_0x10f6('0xd')](_0x40dd2f)[_0x10f6('0xe')]();};}function respondWithResult(_0x8c0760,_0x44b148){_0x44b148=_0x44b148||0xc8;return function(_0x202463){if(_0x202463){return _0x8c0760[_0x10f6('0xd')](_0x44b148)[_0x10f6('0xf')](_0x202463);}};}function respondWithFilteredResult(_0x411f63,_0x4c1dcf){return function(_0x4b5c51){if(_0x4b5c51){var _0x1cd1a0=typeof _0x4c1dcf[_0x10f6('0x10')]===_0x10f6('0x11')&&typeof _0x4c1dcf[_0x10f6('0x12')]===_0x10f6('0x11');var _0x351777=_0x4b5c51['count'];var _0x391004=_0x1cd1a0?0x0:_0x4c1dcf[_0x10f6('0x10')];var _0x1242e9=_0x1cd1a0?_0x4b5c51[_0x10f6('0x13')]:_0x4c1dcf[_0x10f6('0x10')]+_0x4c1dcf['limit'];var _0x8211f0;if(_0x1242e9>=_0x351777){_0x1242e9=_0x351777;_0x8211f0=0xc8;}else{_0x8211f0=0xce;}_0x411f63[_0x10f6('0xd')](_0x8211f0);return _0x411f63['set'](_0x10f6('0x14'),_0x391004+'-'+_0x1242e9+'/'+_0x351777)[_0x10f6('0xf')](_0x4b5c51);}return null;};}function saveUpdates(_0x1282b4){return function(_0x223c80){if(_0x223c80){return _0x223c80['update'](_0x1282b4)[_0x10f6('0x15')](function(_0x54fe05){return _0x54fe05;});}return null;};}function removeEntity(_0x108244){return function(_0xa337ee){if(_0xa337ee){return _0xa337ee['destroy']()[_0x10f6('0x15')](function(){_0x108244[_0x10f6('0xd')](0xcc)[_0x10f6('0xe')]();});}};}function handleEntityNotFound(_0x24cece){return function(_0x4177f4){if(!_0x4177f4){_0x24cece[_0x10f6('0xc')](0x194);}return _0x4177f4;};}function handleError(_0x2b5d39,_0x37b583){_0x37b583=_0x37b583||0x1f4;return function(_0x1d7985){logger[_0x10f6('0x16')](_0x1d7985['stack']);if(_0x1d7985['name']){delete _0x1d7985[_0x10f6('0x17')];}_0x2b5d39['status'](_0x37b583)['send'](_0x1d7985);};}function maskClientSecret(_0x389da3){return _['padEnd'](_0x389da3[_0x10f6('0x18')](0x0,0x3),0x14,'*');}exports['index']=function(_0x207c4b,_0x57dc90){var _0x6ebace={},_0x43c1b0={},_0x35cb9e={'count':0x0,'rows':[]};var _0x33a55e=_['map'](db[_0x10f6('0x19')][_0x10f6('0x1a')],function(_0x53b8cb){return{'name':_0x53b8cb[_0x10f6('0x1b')],'type':_0x53b8cb[_0x10f6('0x1c')][_0x10f6('0x1d')]};});_0x43c1b0[_0x10f6('0x1e')]=_[_0x10f6('0x1f')](_0x33a55e,'name');_0x43c1b0[_0x10f6('0x20')]=_[_0x10f6('0x21')](_0x207c4b[_0x10f6('0x20')]);_0x43c1b0[_0x10f6('0x22')]=_[_0x10f6('0x23')](_0x43c1b0[_0x10f6('0x1e')],_0x43c1b0[_0x10f6('0x20')]);_0x6ebace[_0x10f6('0x24')]=_[_0x10f6('0x23')](_0x43c1b0['model'],qs[_0x10f6('0x25')](_0x207c4b[_0x10f6('0x20')][_0x10f6('0x25')]));_0x6ebace[_0x10f6('0x24')]=_0x6ebace[_0x10f6('0x24')]['length']?_0x6ebace[_0x10f6('0x24')]:_0x43c1b0[_0x10f6('0x1e')];if(!_0x207c4b[_0x10f6('0x20')][_0x10f6('0x26')](_0x10f6('0x27'))){_0x6ebace[_0x10f6('0x12')]=qs[_0x10f6('0x12')](_0x207c4b['query']['limit']);_0x6ebace[_0x10f6('0x10')]=qs[_0x10f6('0x10')](_0x207c4b[_0x10f6('0x20')][_0x10f6('0x10')]);}_0x6ebace['order']=qs[_0x10f6('0x28')](_0x207c4b[_0x10f6('0x20')]['sort']);_0x6ebace[_0x10f6('0x29')]=qs[_0x10f6('0x22')](_[_0x10f6('0x2a')](_0x207c4b[_0x10f6('0x20')],_0x43c1b0[_0x10f6('0x22')]),_0x33a55e);if(_0x207c4b[_0x10f6('0x20')][_0x10f6('0x2b')]){_0x6ebace[_0x10f6('0x29')]=_[_0x10f6('0x2c')](_0x6ebace['where'],{'$or':_[_0x10f6('0x1f')](_0x33a55e,function(_0x42db5a){if(_0x42db5a[_0x10f6('0x1c')]!==_0x10f6('0x2d')){var _0x135e23={};_0x135e23[_0x42db5a['name']]={'$like':'%'+_0x207c4b[_0x10f6('0x20')][_0x10f6('0x2b')]+'%'};return _0x135e23;}})});}_0x6ebace=_[_0x10f6('0x2c')]({},_0x6ebace,_0x207c4b[_0x10f6('0x2e')]);var _0x21aae9={'where':_0x6ebace['where']};return db[_0x10f6('0x19')][_0x10f6('0x13')](_0x21aae9)['then'](function(_0x2ee416){_0x35cb9e['count']=_0x2ee416;if(_0x207c4b[_0x10f6('0x20')]['includeAll']){_0x6ebace['include']=[{'all':!![]}];}return db[_0x10f6('0x19')][_0x10f6('0x2f')](_0x6ebace);})[_0x10f6('0x15')](function(_0x22d293){_0x35cb9e[_0x10f6('0x30')]=_[_0x10f6('0x1f')](_0x22d293,function(_0x2a4e25){if(_0x2a4e25[_0x10f6('0x31')]===_0x10f6('0x32')){var _0x26bf85=encryptor['decryptString'](_0x2a4e25[_0x10f6('0x33')]);_0x2a4e25[_0x10f6('0x33')]=maskClientSecret(_0x26bf85);}return _0x2a4e25;});return _0x35cb9e;})[_0x10f6('0x15')](respondWithFilteredResult(_0x57dc90,_0x6ebace))[_0x10f6('0x34')](handleError(_0x57dc90,null));};exports[_0x10f6('0x35')]=function(_0x4c746e,_0x24d8e3){var _0xad069e={'raw':!![],'where':{'id':_0x4c746e['params']['id']}},_0x47af7f={};_0x47af7f['model']=_[_0x10f6('0x21')](db['CloudProvider']['rawAttributes']);_0x47af7f[_0x10f6('0x20')]=_['keys'](_0x4c746e[_0x10f6('0x20')]);_0x47af7f[_0x10f6('0x22')]=_['intersection'](_0x47af7f[_0x10f6('0x1e')],_0x47af7f[_0x10f6('0x20')]);_0xad069e[_0x10f6('0x24')]=_['intersection'](_0x47af7f['model'],qs['fields'](_0x4c746e[_0x10f6('0x20')][_0x10f6('0x25')]));_0xad069e[_0x10f6('0x24')]=_0xad069e[_0x10f6('0x24')][_0x10f6('0x36')]?_0xad069e[_0x10f6('0x24')]:_0x47af7f[_0x10f6('0x1e')];if(_0x4c746e[_0x10f6('0x20')][_0x10f6('0x37')]){_0xad069e[_0x10f6('0x38')]=[{'all':!![]}];}_0xad069e=_[_0x10f6('0x2c')]({},_0xad069e,_0x4c746e['options']);return db[_0x10f6('0x19')]['find'](_0xad069e)[_0x10f6('0x15')](handleEntityNotFound(_0x24d8e3,null))['then'](function(_0x3b92ae){if(_0x3b92ae[_0x10f6('0x31')]===_0x10f6('0x32')){var _0x218ba0=encryptor[_0x10f6('0x39')](_0x3b92ae[_0x10f6('0x33')]);_0x3b92ae[_0x10f6('0x33')]=maskClientSecret(_0x218ba0);}return _0x3b92ae;})['then'](respondWithResult(_0x24d8e3,null))[_0x10f6('0x34')](handleError(_0x24d8e3,null));};exports['create']=function(_0x468e5c,_0x35ec84){if(!_0x468e5c[_0x10f6('0x3a')][_0x10f6('0x17')])throw new Error(_0x10f6('0x3b'));if(!_0x468e5c[_0x10f6('0x3a')]['service'])throw new Error(_0x10f6('0x3c'));var _0x22ddea=db[_0x10f6('0x19')]['rawAttributes']['service'][_0x10f6('0x3d')];if(!_0x22ddea[_0x10f6('0x3e')](_0x468e5c[_0x10f6('0x3a')]['service']))throw new Error(_0x10f6('0x3f')+_0x22ddea[_0x10f6('0x40')](',\x20'));if(_0x468e5c[_0x10f6('0x3a')][_0x10f6('0x31')]===_0x10f6('0x32')&&_0x468e5c[_0x10f6('0x3a')]['data3']){_0x468e5c[_0x10f6('0x3a')][_0x10f6('0x33')]=encryptor[_0x10f6('0x41')](_0x468e5c['body'][_0x10f6('0x33')]);}return db[_0x10f6('0x19')]['create'](_0x468e5c['body'],{})[_0x10f6('0x15')](respondWithResult(_0x35ec84,0xc9))[_0x10f6('0x34')](handleError(_0x35ec84,null));};exports[_0x10f6('0x42')]=function(_0x18447b,_0x37f2f9){if(_0x18447b[_0x10f6('0x3a')]['id']){delete _0x18447b[_0x10f6('0x3a')]['id'];}return db[_0x10f6('0x19')][_0x10f6('0x43')]({'where':{'id':_0x18447b[_0x10f6('0x44')]['id']}})['then'](handleEntityNotFound(_0x37f2f9,null))[_0x10f6('0x15')](function(_0x51df38){if(_0x51df38[_0x10f6('0x31')]===_0x10f6('0x32')&&_0x18447b[_0x10f6('0x3a')][_0x10f6('0x33')]){_0x18447b[_0x10f6('0x3a')][_0x10f6('0x33')]=encryptor[_0x10f6('0x41')](_0x18447b[_0x10f6('0x3a')][_0x10f6('0x33')]);}return _0x51df38;})[_0x10f6('0x15')](saveUpdates(_0x18447b['body'],null))[_0x10f6('0x15')](respondWithResult(_0x37f2f9,null))[_0x10f6('0x34')](handleError(_0x37f2f9,null));};exports[_0x10f6('0x45')]=function(_0xb4100c,_0x1c1c2e){return db[_0x10f6('0x19')]['find']({'where':{'id':_0xb4100c['params']['id']}})[_0x10f6('0x15')](handleEntityNotFound(_0x1c1c2e,null))[_0x10f6('0x15')](removeEntity(_0x1c1c2e,null))[_0x10f6('0x34')](handleError(_0x1c1c2e,null));};exports[_0x10f6('0x46')]=function(_0x29204d,_0x5d5404){return db[_0x10f6('0x19')][_0x10f6('0x47')]({'where':{'id':_0x29204d[_0x10f6('0x44')]['id']},'raw':!![]})[_0x10f6('0x15')](function(_0x5a324e){if(!_0x5a324e)return _0x5d5404['sendStatus'](0x194);return{'url':oauth['generateMicrosoftAuthorizationUrl'](_0x5a324e,_0x29204d[_0x10f6('0x48')]['id'])};})[_0x10f6('0x15')](respondWithResult(_0x5d5404,null))[_0x10f6('0x34')](handleError(_0x5d5404,null));};exports['oauth2MicrosoftCallback']=function(_0x13b317,_0x3ad42c){var _0x3fb6c9=_0x13b317[_0x10f6('0x3a')];var _0x3454aa;var _0x90cf29;if(_0x3fb6c9[_0x10f6('0x16')])throw _0x3fb6c9;redis[_0x10f6('0x49')](_0x3fb6c9[_0x10f6('0x4a')])[_0x10f6('0x15')](function(_0x30fad7){if(!_0x30fad7)throw new Error(_0x10f6('0x4b'));_0x3454aa=JSON[_0x10f6('0x4c')](_0x30fad7);var _0x49debe=Buffer['from'](_0x3fb6c9['state'],_0x10f6('0x4d'))[_0x10f6('0x4e')](_0x10f6('0x4f'));_0x90cf29=JSON[_0x10f6('0x4c')](_0x49debe);if(!oauth[_0x10f6('0x50')](_0x3fb6c9['id_token'],_0x3454aa[_0x10f6('0x51')]))throw new Error(_0x10f6('0x52'));redis['del'](_0x3fb6c9[_0x10f6('0x4a')]);return oauth[_0x10f6('0x53')](_0x3fb6c9[_0x10f6('0x54')],_0x3454aa);})[_0x10f6('0x15')](function(_0x69283c){_0x3454aa[_0x10f6('0x55')]=_0x69283c['access_token'];_0x3454aa[_0x10f6('0x56')]=_0x69283c[_0x10f6('0x57')];return db['CloudProvider']['update']({'data5':_0x3454aa[_0x10f6('0x55')],'data6':_0x3454aa[_0x10f6('0x56')]},{'where':{'id':_0x3454aa['id']}});})[_0x10f6('0x15')](function(){oauth[_0x10f6('0x58')](_0x3454aa);socket['emit']('microsoftAuthorization:success',{'id':_0x90cf29['id']});return;})[_0x10f6('0x15')](respondWithStatusCode(_0x3ad42c,null))['catch'](function(_0x4192a8){if(_0x90cf29){socket[_0x10f6('0x59')]('microsoftAuthorization:error',{'id':_0x90cf29['id']});}if(!_0x3454aa){logger['error']('Invalid\x20authentication\x20state\x20parameter',_0x4192a8);return _0x3ad42c[_0x10f6('0xd')](0x1f4)['send'](_0x4192a8['message']);}logger[_0x10f6('0x16')]('Authentication\x20failed\x20for\x20cloud\x20provider\x20#%s,\x20err:%s',_0x3454aa['id'],JSON['stringify'](_0x4192a8));});};