Built motion from commit 0900f080.|2.5.45
[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 _0xb493=['Invalid\x20authentication\x20state\x20parameter','message','stringify','lodash','ioredis','../../components/parsers/qs','../../components/encryptor','../../config/environment','../../config/logger','api','./cloudProvider.oauth','redis','localhost','socket.io-emitter','sendStatus','status','json','offset','count','limit','set','Content-Range','update','then','destroy','error','stack','name','send','padEnd','slice','index','map','CloudProvider','fieldName','key','query','keys','filters','intersection','model','attributes','fields','length','order','sort','where','pick','filter','merge','type','VIRTUAL','options','includeAll','include','findAll','MicrosoftAzure','decryptString','data3','catch','show','params','rawAttributes','find','service','create','body','Name\x20parameter\x20not\x20specified','Service\x20parameter\x20not\x20specified','values','includes','Invalid\x20service\x20parameter.\x20Allowed\x20values:\x20','encryptString','oauth2MicrosoftAuthorize','findOne','generateMicrosoftAuthorizationUrl','user','oauth2MicrosoftCallback','get','Invalid\x20state\x20parameter','parse','from','toString','isValidIdToken','id_token','oauth2Claims','del','state','getOauth2MicrosoftAccessToken','code','data5','access_token','data6','refresh_token','microsoftAuthorization:success','emit','microsoftAuthorization:error'];(function(_0x475eac,_0x2fa495){var _0x4ad158=function(_0x2d7c4c){while(--_0x2d7c4c){_0x475eac['push'](_0x475eac['shift']());}};_0x4ad158(++_0x2fa495);}(_0xb493,0x126));var _0x3b49=function(_0x39db9e,_0x4569ae){_0x39db9e=_0x39db9e-0x0;var _0x5a7a01=_0xb493[_0x39db9e];return _0x5a7a01;};'use strict';var _=require(_0x3b49('0x0'));var Redis=require(_0x3b49('0x1'));var qs=require(_0x3b49('0x2'));var encryptor=require(_0x3b49('0x3'));var config=require(_0x3b49('0x4'));var logger=require(_0x3b49('0x5'))(_0x3b49('0x6'));var db=require('../../mysqldb')['db'];var oauth=require(_0x3b49('0x7'));config[_0x3b49('0x8')]=_['defaults'](config['redis'],{'host':_0x3b49('0x9'),'port':0x18eb});var socket=require(_0x3b49('0xa'))(new Redis(config[_0x3b49('0x8')]));var redis=new Redis(config[_0x3b49('0x8')]);function respondWithStatusCode(_0x429dd1,_0x8f4e4b){_0x8f4e4b=_0x8f4e4b||0xcc;return function(_0x2767a7){if(_0x2767a7){return _0x429dd1[_0x3b49('0xb')](_0x8f4e4b);}return _0x429dd1[_0x3b49('0xc')](_0x8f4e4b)['end']();};}function respondWithResult(_0x34f3ee,_0x4ff51a){_0x4ff51a=_0x4ff51a||0xc8;return function(_0x1d76a9){if(_0x1d76a9){return _0x34f3ee[_0x3b49('0xc')](_0x4ff51a)[_0x3b49('0xd')](_0x1d76a9);}};}function respondWithFilteredResult(_0x2dc168,_0x3382f1){return function(_0x22b408){if(_0x22b408){var _0x5488dd=typeof _0x3382f1[_0x3b49('0xe')]==='undefined'&&typeof _0x3382f1['limit']==='undefined';var _0x2ffcd6=_0x22b408[_0x3b49('0xf')];var _0x2de575=_0x5488dd?0x0:_0x3382f1[_0x3b49('0xe')];var _0x4e960f=_0x5488dd?_0x22b408[_0x3b49('0xf')]:_0x3382f1['offset']+_0x3382f1[_0x3b49('0x10')];var _0x58cf1d;if(_0x4e960f>=_0x2ffcd6){_0x4e960f=_0x2ffcd6;_0x58cf1d=0xc8;}else{_0x58cf1d=0xce;}_0x2dc168[_0x3b49('0xc')](_0x58cf1d);return _0x2dc168[_0x3b49('0x11')](_0x3b49('0x12'),_0x2de575+'-'+_0x4e960f+'/'+_0x2ffcd6)[_0x3b49('0xd')](_0x22b408);}return null;};}function saveUpdates(_0x3808e5){return function(_0x29979e){if(_0x29979e){return _0x29979e[_0x3b49('0x13')](_0x3808e5)[_0x3b49('0x14')](function(_0x236c28){return _0x236c28;});}return null;};}function removeEntity(_0x48a983){return function(_0x205896){if(_0x205896){return _0x205896[_0x3b49('0x15')]()[_0x3b49('0x14')](function(){_0x48a983['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0x124292){return function(_0x5e4474){if(!_0x5e4474){_0x124292[_0x3b49('0xb')](0x194);}return _0x5e4474;};}function handleError(_0x4756e1,_0x4be1f3){_0x4be1f3=_0x4be1f3||0x1f4;return function(_0xfc76f8){logger[_0x3b49('0x16')](_0xfc76f8[_0x3b49('0x17')]);if(_0xfc76f8[_0x3b49('0x18')]){delete _0xfc76f8[_0x3b49('0x18')];}_0x4756e1[_0x3b49('0xc')](_0x4be1f3)[_0x3b49('0x19')](_0xfc76f8);};}function maskClientSecret(_0xc129ef){return _[_0x3b49('0x1a')](_0xc129ef[_0x3b49('0x1b')](0x0,0x3),0x14,'*');}exports[_0x3b49('0x1c')]=function(_0x259091,_0xe6d40a){var _0x3fdd0a={},_0x466d6b={},_0x3eb6af={'count':0x0,'rows':[]};var _0x9b5204=_[_0x3b49('0x1d')](db[_0x3b49('0x1e')]['rawAttributes'],function(_0x4c2d26){return{'name':_0x4c2d26[_0x3b49('0x1f')],'type':_0x4c2d26['type'][_0x3b49('0x20')]};});_0x466d6b['model']=_[_0x3b49('0x1d')](_0x9b5204,'name');_0x466d6b[_0x3b49('0x21')]=_[_0x3b49('0x22')](_0x259091[_0x3b49('0x21')]);_0x466d6b[_0x3b49('0x23')]=_[_0x3b49('0x24')](_0x466d6b[_0x3b49('0x25')],_0x466d6b[_0x3b49('0x21')]);_0x3fdd0a[_0x3b49('0x26')]=_['intersection'](_0x466d6b[_0x3b49('0x25')],qs['fields'](_0x259091[_0x3b49('0x21')][_0x3b49('0x27')]));_0x3fdd0a[_0x3b49('0x26')]=_0x3fdd0a[_0x3b49('0x26')][_0x3b49('0x28')]?_0x3fdd0a[_0x3b49('0x26')]:_0x466d6b[_0x3b49('0x25')];if(!_0x259091[_0x3b49('0x21')]['hasOwnProperty']('nolimit')){_0x3fdd0a['limit']=qs[_0x3b49('0x10')](_0x259091[_0x3b49('0x21')]['limit']);_0x3fdd0a[_0x3b49('0xe')]=qs['offset'](_0x259091['query']['offset']);}_0x3fdd0a[_0x3b49('0x29')]=qs['sort'](_0x259091[_0x3b49('0x21')][_0x3b49('0x2a')]);_0x3fdd0a[_0x3b49('0x2b')]=qs['filters'](_[_0x3b49('0x2c')](_0x259091[_0x3b49('0x21')],_0x466d6b[_0x3b49('0x23')]),_0x9b5204);if(_0x259091['query'][_0x3b49('0x2d')]){_0x3fdd0a['where']=_[_0x3b49('0x2e')](_0x3fdd0a[_0x3b49('0x2b')],{'$or':_[_0x3b49('0x1d')](_0x9b5204,function(_0x1daddc){if(_0x1daddc[_0x3b49('0x2f')]!==_0x3b49('0x30')){var _0x1edd0e={};_0x1edd0e[_0x1daddc[_0x3b49('0x18')]]={'$like':'%'+_0x259091[_0x3b49('0x21')][_0x3b49('0x2d')]+'%'};return _0x1edd0e;}})});}_0x3fdd0a=_[_0x3b49('0x2e')]({},_0x3fdd0a,_0x259091[_0x3b49('0x31')]);var _0x5afc73={'where':_0x3fdd0a[_0x3b49('0x2b')]};return db[_0x3b49('0x1e')][_0x3b49('0xf')](_0x5afc73)['then'](function(_0x547172){_0x3eb6af[_0x3b49('0xf')]=_0x547172;if(_0x259091[_0x3b49('0x21')][_0x3b49('0x32')]){_0x3fdd0a[_0x3b49('0x33')]=[{'all':!![]}];}return db[_0x3b49('0x1e')][_0x3b49('0x34')](_0x3fdd0a);})['then'](function(_0xcbdba9){_0x3eb6af['rows']=_[_0x3b49('0x1d')](_0xcbdba9,function(_0x261868){if(_0x261868['service']===_0x3b49('0x35')){var _0x4b1a22=encryptor[_0x3b49('0x36')](_0x261868[_0x3b49('0x37')]);_0x261868[_0x3b49('0x37')]=maskClientSecret(_0x4b1a22);}return _0x261868;});return _0x3eb6af;})[_0x3b49('0x14')](respondWithFilteredResult(_0xe6d40a,_0x3fdd0a))[_0x3b49('0x38')](handleError(_0xe6d40a,null));};exports[_0x3b49('0x39')]=function(_0xe5fc48,_0xaee5da){var _0x3c93c4={'raw':!![],'where':{'id':_0xe5fc48[_0x3b49('0x3a')]['id']}},_0x22698d={};_0x22698d[_0x3b49('0x25')]=_['keys'](db[_0x3b49('0x1e')][_0x3b49('0x3b')]);_0x22698d['query']=_[_0x3b49('0x22')](_0xe5fc48['query']);_0x22698d[_0x3b49('0x23')]=_[_0x3b49('0x24')](_0x22698d[_0x3b49('0x25')],_0x22698d['query']);_0x3c93c4['attributes']=_[_0x3b49('0x24')](_0x22698d[_0x3b49('0x25')],qs[_0x3b49('0x27')](_0xe5fc48[_0x3b49('0x21')][_0x3b49('0x27')]));_0x3c93c4[_0x3b49('0x26')]=_0x3c93c4[_0x3b49('0x26')]['length']?_0x3c93c4[_0x3b49('0x26')]:_0x22698d[_0x3b49('0x25')];if(_0xe5fc48['query'][_0x3b49('0x32')]){_0x3c93c4[_0x3b49('0x33')]=[{'all':!![]}];}_0x3c93c4=_['merge']({},_0x3c93c4,_0xe5fc48[_0x3b49('0x31')]);return db[_0x3b49('0x1e')][_0x3b49('0x3c')](_0x3c93c4)[_0x3b49('0x14')](handleEntityNotFound(_0xaee5da,null))['then'](function(_0x576b17){if(_0x576b17[_0x3b49('0x3d')]===_0x3b49('0x35')){var _0x8dcee1=encryptor['decryptString'](_0x576b17[_0x3b49('0x37')]);_0x576b17[_0x3b49('0x37')]=maskClientSecret(_0x8dcee1);}return _0x576b17;})[_0x3b49('0x14')](respondWithResult(_0xaee5da,null))[_0x3b49('0x38')](handleError(_0xaee5da,null));};exports[_0x3b49('0x3e')]=function(_0x47bada,_0x136383){if(!_0x47bada[_0x3b49('0x3f')]['name'])throw new Error(_0x3b49('0x40'));if(!_0x47bada['body'][_0x3b49('0x3d')])throw new Error(_0x3b49('0x41'));var _0x3c6c92=db['CloudProvider'][_0x3b49('0x3b')][_0x3b49('0x3d')][_0x3b49('0x42')];if(!_0x3c6c92[_0x3b49('0x43')](_0x47bada['body'][_0x3b49('0x3d')]))throw new Error(_0x3b49('0x44')+_0x3c6c92['join'](',\x20'));if(_0x47bada[_0x3b49('0x3f')][_0x3b49('0x3d')]===_0x3b49('0x35')&&_0x47bada[_0x3b49('0x3f')][_0x3b49('0x37')]){_0x47bada[_0x3b49('0x3f')][_0x3b49('0x37')]=encryptor['encryptString'](_0x47bada[_0x3b49('0x3f')][_0x3b49('0x37')]);}return db[_0x3b49('0x1e')][_0x3b49('0x3e')](_0x47bada['body'],{})[_0x3b49('0x14')](respondWithResult(_0x136383,0xc9))[_0x3b49('0x38')](handleError(_0x136383,null));};exports['update']=function(_0x358e90,_0x4ea5a8){if(_0x358e90[_0x3b49('0x3f')]['id']){delete _0x358e90[_0x3b49('0x3f')]['id'];}return db['CloudProvider']['find']({'where':{'id':_0x358e90[_0x3b49('0x3a')]['id']}})[_0x3b49('0x14')](handleEntityNotFound(_0x4ea5a8,null))[_0x3b49('0x14')](function(_0x3b2c96){if(_0x3b2c96[_0x3b49('0x3d')]===_0x3b49('0x35')&&_0x358e90[_0x3b49('0x3f')][_0x3b49('0x37')]){_0x358e90['body']['data3']=encryptor[_0x3b49('0x45')](_0x358e90['body'][_0x3b49('0x37')]);}return _0x3b2c96;})[_0x3b49('0x14')](saveUpdates(_0x358e90[_0x3b49('0x3f')],null))[_0x3b49('0x14')](respondWithResult(_0x4ea5a8,null))[_0x3b49('0x38')](handleError(_0x4ea5a8,null));};exports['destroy']=function(_0x195119,_0x90272e){return db['CloudProvider'][_0x3b49('0x3c')]({'where':{'id':_0x195119[_0x3b49('0x3a')]['id']}})['then'](handleEntityNotFound(_0x90272e,null))[_0x3b49('0x14')](removeEntity(_0x90272e,null))['catch'](handleError(_0x90272e,null));};exports[_0x3b49('0x46')]=function(_0x23e782,_0x5a6912){return db[_0x3b49('0x1e')][_0x3b49('0x47')]({'where':{'id':_0x23e782[_0x3b49('0x3a')]['id']},'raw':!![]})[_0x3b49('0x14')](function(_0x57042f){if(!_0x57042f)return _0x5a6912[_0x3b49('0xb')](0x194);return{'url':oauth[_0x3b49('0x48')](_0x57042f,_0x23e782[_0x3b49('0x49')]['id'])};})[_0x3b49('0x14')](respondWithResult(_0x5a6912,null))[_0x3b49('0x38')](handleError(_0x5a6912,null));};exports[_0x3b49('0x4a')]=function(_0x3ad606,_0x2b8a63){var _0x558c72=_0x3ad606[_0x3b49('0x3f')];var _0x522e84;var _0x16930f;if(_0x558c72[_0x3b49('0x16')])throw _0x558c72;redis[_0x3b49('0x4b')](_0x558c72['state'])[_0x3b49('0x14')](function(_0xd0f7d1){if(!_0xd0f7d1)throw new Error(_0x3b49('0x4c'));_0x522e84=JSON[_0x3b49('0x4d')](_0xd0f7d1);var _0x31cf45=Buffer[_0x3b49('0x4e')](_0x558c72['state'],'base64')[_0x3b49('0x4f')]('ascii');_0x16930f=JSON[_0x3b49('0x4d')](_0x31cf45);if(!oauth[_0x3b49('0x50')](_0x558c72[_0x3b49('0x51')],_0x522e84[_0x3b49('0x52')]))throw new Error('Unable\x20to\x20validate\x20id_token');redis[_0x3b49('0x53')](_0x558c72[_0x3b49('0x54')]);return oauth[_0x3b49('0x55')](_0x558c72[_0x3b49('0x56')],_0x522e84);})[_0x3b49('0x14')](function(_0x51669a){_0x522e84[_0x3b49('0x57')]=_0x51669a[_0x3b49('0x58')];_0x522e84[_0x3b49('0x59')]=_0x51669a[_0x3b49('0x5a')];return db[_0x3b49('0x1e')][_0x3b49('0x13')]({'data5':_0x522e84[_0x3b49('0x57')],'data6':_0x522e84[_0x3b49('0x59')]},{'where':{'id':_0x522e84['id']}});})[_0x3b49('0x14')](function(){oauth['startRefreshInterval'](_0x522e84);socket['emit'](_0x3b49('0x5b'),{'id':_0x16930f['id']});return;})[_0x3b49('0x14')](respondWithStatusCode(_0x2b8a63,null))['catch'](function(_0x26e373){if(_0x16930f){socket[_0x3b49('0x5c')](_0x3b49('0x5d'),{'id':_0x16930f['id']});}if(!_0x522e84){logger[_0x3b49('0x16')](_0x3b49('0x5e'),_0x26e373);return _0x2b8a63[_0x3b49('0xc')](0x1f4)[_0x3b49('0x19')](_0x26e373[_0x3b49('0x5f')]);}logger[_0x3b49('0x16')]('Authentication\x20failed\x20for\x20cloud\x20provider\x20#%s,\x20err:%s',_0x522e84['id'],JSON[_0x3b49('0x60')](_0x26e373));});};