Built motion from commit ffdca78c.|2.6.24
[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 _0x8f42=['encryptString','oauth2MicrosoftAuthorize','generateMicrosoftAuthorizationUrl','user','get','Invalid\x20state\x20parameter','parse','from','state','toString','ascii','id_token','del','getOauth2MicrosoftAccessToken','code','data5','data6','refresh_token','emit','microsoftAuthorization:success','microsoftAuthorization:error','Invalid\x20authentication\x20state\x20parameter','Authentication\x20failed\x20for\x20cloud\x20provider\x20#%s,\x20err:%s','stringify','lodash','ioredis','../../components/parsers/qs','../../components/encryptor','../../config/environment','../../config/logger','./cloudProvider.oauth','redis','defaults','socket.io-emitter','sendStatus','end','json','offset','undefined','limit','count','status','Content-Range','update','then','destroy','error','name','send','padEnd','slice','index','CloudProvider','rawAttributes','fieldName','type','key','model','query','keys','filters','intersection','attributes','fields','hasOwnProperty','nolimit','sort','filter','where','merge','map','VIRTUAL','options','includeAll','findAll','rows','service','MicrosoftAzure','data3','show','params','length','include','find','decryptString','catch','create','body','Service\x20parameter\x20not\x20specified','values','includes'];(function(_0x401676,_0x3f1436){var _0x4f13fe=function(_0xd2f197){while(--_0xd2f197){_0x401676['push'](_0x401676['shift']());}};_0x4f13fe(++_0x3f1436);}(_0x8f42,0x184));var _0x28f4=function(_0x27ec50,_0x1d8698){_0x27ec50=_0x27ec50-0x0;var _0x2da0f5=_0x8f42[_0x27ec50];return _0x2da0f5;};'use strict';var _=require(_0x28f4('0x0'));var Redis=require(_0x28f4('0x1'));var qs=require(_0x28f4('0x2'));var encryptor=require(_0x28f4('0x3'));var config=require(_0x28f4('0x4'));var logger=require(_0x28f4('0x5'))('api');var db=require('../../mysqldb')['db'];var oauth=require(_0x28f4('0x6'));config[_0x28f4('0x7')]=_[_0x28f4('0x8')](config[_0x28f4('0x7')],{'host':'localhost','port':0x18eb});var socket=require(_0x28f4('0x9'))(new Redis(config['redis']));var redis=new Redis(config[_0x28f4('0x7')]);function respondWithStatusCode(_0x42f375,_0x4aead5){_0x4aead5=_0x4aead5||0xcc;return function(_0x197c4e){if(_0x197c4e){return _0x42f375[_0x28f4('0xa')](_0x4aead5);}return _0x42f375['status'](_0x4aead5)[_0x28f4('0xb')]();};}function respondWithResult(_0x5d5afe,_0x57afb5){_0x57afb5=_0x57afb5||0xc8;return function(_0x340a72){if(_0x340a72){return _0x5d5afe['status'](_0x57afb5)[_0x28f4('0xc')](_0x340a72);}};}function respondWithFilteredResult(_0x18d462,_0x3bc618){return function(_0x310850){if(_0x310850){var _0x4eaadd=typeof _0x3bc618[_0x28f4('0xd')]===_0x28f4('0xe')&&typeof _0x3bc618[_0x28f4('0xf')]===_0x28f4('0xe');var _0x140597=_0x310850[_0x28f4('0x10')];var _0x5df20e=_0x4eaadd?0x0:_0x3bc618[_0x28f4('0xd')];var _0xdd85e9=_0x4eaadd?_0x310850[_0x28f4('0x10')]:_0x3bc618[_0x28f4('0xd')]+_0x3bc618[_0x28f4('0xf')];var _0x1208ce;if(_0xdd85e9>=_0x140597){_0xdd85e9=_0x140597;_0x1208ce=0xc8;}else{_0x1208ce=0xce;}_0x18d462[_0x28f4('0x11')](_0x1208ce);return _0x18d462['set'](_0x28f4('0x12'),_0x5df20e+'-'+_0xdd85e9+'/'+_0x140597)[_0x28f4('0xc')](_0x310850);}return null;};}function saveUpdates(_0x591ab0){return function(_0x12d049){if(_0x12d049){return _0x12d049[_0x28f4('0x13')](_0x591ab0)[_0x28f4('0x14')](function(_0x359759){return _0x359759;});}return null;};}function removeEntity(_0x4cb7fc){return function(_0x133f20){if(_0x133f20){return _0x133f20[_0x28f4('0x15')]()[_0x28f4('0x14')](function(){_0x4cb7fc[_0x28f4('0x11')](0xcc)[_0x28f4('0xb')]();});}};}function handleEntityNotFound(_0x1d3b1d){return function(_0x3cf798){if(!_0x3cf798){_0x1d3b1d[_0x28f4('0xa')](0x194);}return _0x3cf798;};}function handleError(_0x3bae35,_0x49327b){_0x49327b=_0x49327b||0x1f4;return function(_0x28afe6){logger[_0x28f4('0x16')](_0x28afe6['stack']);if(_0x28afe6[_0x28f4('0x17')]){delete _0x28afe6['name'];}_0x3bae35[_0x28f4('0x11')](_0x49327b)[_0x28f4('0x18')](_0x28afe6);};}function maskClientSecret(_0x55f0ff){return _[_0x28f4('0x19')](_0x55f0ff[_0x28f4('0x1a')](0x0,0x3),0x14,'*');}exports[_0x28f4('0x1b')]=function(_0x32f6a6,_0x1d4fd1){var _0x22cdea={},_0x5f426b={},_0xe9f18e={'count':0x0,'rows':[]};var _0x5b374a=_['map'](db[_0x28f4('0x1c')][_0x28f4('0x1d')],function(_0x58ff78){return{'name':_0x58ff78[_0x28f4('0x1e')],'type':_0x58ff78[_0x28f4('0x1f')][_0x28f4('0x20')]};});_0x5f426b[_0x28f4('0x21')]=_['map'](_0x5b374a,_0x28f4('0x17'));_0x5f426b[_0x28f4('0x22')]=_[_0x28f4('0x23')](_0x32f6a6[_0x28f4('0x22')]);_0x5f426b[_0x28f4('0x24')]=_[_0x28f4('0x25')](_0x5f426b[_0x28f4('0x21')],_0x5f426b[_0x28f4('0x22')]);_0x22cdea[_0x28f4('0x26')]=_[_0x28f4('0x25')](_0x5f426b['model'],qs[_0x28f4('0x27')](_0x32f6a6['query'][_0x28f4('0x27')]));_0x22cdea['attributes']=_0x22cdea['attributes']['length']?_0x22cdea['attributes']:_0x5f426b['model'];if(!_0x32f6a6[_0x28f4('0x22')][_0x28f4('0x28')](_0x28f4('0x29'))){_0x22cdea['limit']=qs['limit'](_0x32f6a6[_0x28f4('0x22')]['limit']);_0x22cdea[_0x28f4('0xd')]=qs[_0x28f4('0xd')](_0x32f6a6['query'][_0x28f4('0xd')]);}_0x22cdea['order']=qs[_0x28f4('0x2a')](_0x32f6a6[_0x28f4('0x22')]['sort']);_0x22cdea['where']=qs[_0x28f4('0x24')](_['pick'](_0x32f6a6[_0x28f4('0x22')],_0x5f426b[_0x28f4('0x24')]),_0x5b374a);if(_0x32f6a6[_0x28f4('0x22')][_0x28f4('0x2b')]){_0x22cdea[_0x28f4('0x2c')]=_[_0x28f4('0x2d')](_0x22cdea[_0x28f4('0x2c')],{'$or':_[_0x28f4('0x2e')](_0x5b374a,function(_0x570299){if(_0x570299[_0x28f4('0x1f')]!==_0x28f4('0x2f')){var _0x5e5cda={};_0x5e5cda[_0x570299['name']]={'$like':'%'+_0x32f6a6['query'][_0x28f4('0x2b')]+'%'};return _0x5e5cda;}})});}_0x22cdea=_['merge']({},_0x22cdea,_0x32f6a6[_0x28f4('0x30')]);var _0x2e7d51={'where':_0x22cdea['where']};return db[_0x28f4('0x1c')][_0x28f4('0x10')](_0x2e7d51)['then'](function(_0x31fdf1){_0xe9f18e[_0x28f4('0x10')]=_0x31fdf1;if(_0x32f6a6[_0x28f4('0x22')][_0x28f4('0x31')]){_0x22cdea['include']=[{'all':!![]}];}return db[_0x28f4('0x1c')][_0x28f4('0x32')](_0x22cdea);})[_0x28f4('0x14')](function(_0x33b329){_0xe9f18e[_0x28f4('0x33')]=_[_0x28f4('0x2e')](_0x33b329,function(_0x13e0c4){if(_0x13e0c4[_0x28f4('0x34')]===_0x28f4('0x35')){var _0x27072a=encryptor['decryptString'](_0x13e0c4[_0x28f4('0x36')]);_0x13e0c4[_0x28f4('0x36')]=maskClientSecret(_0x27072a);}return _0x13e0c4;});return _0xe9f18e;})['then'](respondWithFilteredResult(_0x1d4fd1,_0x22cdea))['catch'](handleError(_0x1d4fd1,null));};exports[_0x28f4('0x37')]=function(_0xe60a31,_0xcf640f){var _0x345a88={'raw':!![],'where':{'id':_0xe60a31[_0x28f4('0x38')]['id']}},_0x2bfb10={};_0x2bfb10['model']=_[_0x28f4('0x23')](db[_0x28f4('0x1c')][_0x28f4('0x1d')]);_0x2bfb10['query']=_[_0x28f4('0x23')](_0xe60a31[_0x28f4('0x22')]);_0x2bfb10['filters']=_[_0x28f4('0x25')](_0x2bfb10[_0x28f4('0x21')],_0x2bfb10[_0x28f4('0x22')]);_0x345a88[_0x28f4('0x26')]=_[_0x28f4('0x25')](_0x2bfb10[_0x28f4('0x21')],qs['fields'](_0xe60a31[_0x28f4('0x22')][_0x28f4('0x27')]));_0x345a88['attributes']=_0x345a88[_0x28f4('0x26')][_0x28f4('0x39')]?_0x345a88[_0x28f4('0x26')]:_0x2bfb10[_0x28f4('0x21')];if(_0xe60a31['query'][_0x28f4('0x31')]){_0x345a88[_0x28f4('0x3a')]=[{'all':!![]}];}_0x345a88=_[_0x28f4('0x2d')]({},_0x345a88,_0xe60a31['options']);return db['CloudProvider'][_0x28f4('0x3b')](_0x345a88)[_0x28f4('0x14')](handleEntityNotFound(_0xcf640f,null))['then'](function(_0x36caa7){if(_0x36caa7['service']===_0x28f4('0x35')){var _0x269c0e=encryptor[_0x28f4('0x3c')](_0x36caa7[_0x28f4('0x36')]);_0x36caa7[_0x28f4('0x36')]=maskClientSecret(_0x269c0e);}return _0x36caa7;})[_0x28f4('0x14')](respondWithResult(_0xcf640f,null))[_0x28f4('0x3d')](handleError(_0xcf640f,null));};exports[_0x28f4('0x3e')]=function(_0x3a940a,_0x4a8ccd){if(!_0x3a940a[_0x28f4('0x3f')]['name'])throw new Error('Name\x20parameter\x20not\x20specified');if(!_0x3a940a[_0x28f4('0x3f')][_0x28f4('0x34')])throw new Error(_0x28f4('0x40'));var _0x373981=db['CloudProvider']['rawAttributes'][_0x28f4('0x34')][_0x28f4('0x41')];if(!_0x373981[_0x28f4('0x42')](_0x3a940a[_0x28f4('0x3f')][_0x28f4('0x34')]))throw new Error('Invalid\x20service\x20parameter.\x20Allowed\x20values:\x20'+_0x373981['join'](',\x20'));if(_0x3a940a[_0x28f4('0x3f')][_0x28f4('0x34')]===_0x28f4('0x35')&&_0x3a940a[_0x28f4('0x3f')][_0x28f4('0x36')]){_0x3a940a['body']['data3']=encryptor[_0x28f4('0x43')](_0x3a940a[_0x28f4('0x3f')][_0x28f4('0x36')]);}return db['CloudProvider']['create'](_0x3a940a['body'],{})['then'](respondWithResult(_0x4a8ccd,0xc9))[_0x28f4('0x3d')](handleError(_0x4a8ccd,null));};exports[_0x28f4('0x13')]=function(_0x31a61d,_0x56dfe8){if(_0x31a61d['body']['id']){delete _0x31a61d[_0x28f4('0x3f')]['id'];}return db[_0x28f4('0x1c')][_0x28f4('0x3b')]({'where':{'id':_0x31a61d[_0x28f4('0x38')]['id']}})[_0x28f4('0x14')](handleEntityNotFound(_0x56dfe8,null))[_0x28f4('0x14')](function(_0x2fd0fd){if(_0x2fd0fd[_0x28f4('0x34')]===_0x28f4('0x35')&&_0x31a61d[_0x28f4('0x3f')]['data3']){_0x31a61d[_0x28f4('0x3f')]['data3']=encryptor[_0x28f4('0x43')](_0x31a61d[_0x28f4('0x3f')]['data3']);}return _0x2fd0fd;})[_0x28f4('0x14')](saveUpdates(_0x31a61d[_0x28f4('0x3f')],null))[_0x28f4('0x14')](respondWithResult(_0x56dfe8,null))[_0x28f4('0x3d')](handleError(_0x56dfe8,null));};exports[_0x28f4('0x15')]=function(_0x469aa0,_0x35ec2c){return db[_0x28f4('0x1c')][_0x28f4('0x3b')]({'where':{'id':_0x469aa0[_0x28f4('0x38')]['id']}})[_0x28f4('0x14')](handleEntityNotFound(_0x35ec2c,null))[_0x28f4('0x14')](removeEntity(_0x35ec2c,null))[_0x28f4('0x3d')](handleError(_0x35ec2c,null));};exports[_0x28f4('0x44')]=function(_0xab072,_0x3271f1){return db[_0x28f4('0x1c')]['findOne']({'where':{'id':_0xab072[_0x28f4('0x38')]['id']},'raw':!![]})['then'](function(_0xa17f71){if(!_0xa17f71)return _0x3271f1[_0x28f4('0xa')](0x194);return{'url':oauth[_0x28f4('0x45')](_0xa17f71,_0xab072[_0x28f4('0x46')]['id'])};})[_0x28f4('0x14')](respondWithResult(_0x3271f1,null))[_0x28f4('0x3d')](handleError(_0x3271f1,null));};exports['oauth2MicrosoftCallback']=function(_0x393382,_0x194e57){var _0xd9b54f=_0x393382[_0x28f4('0x3f')];var _0x32fe23;var _0x516457;if(_0xd9b54f[_0x28f4('0x16')])throw _0xd9b54f;redis[_0x28f4('0x47')](_0xd9b54f['state'])[_0x28f4('0x14')](function(_0x2ce22d){if(!_0x2ce22d)throw new Error(_0x28f4('0x48'));_0x32fe23=JSON[_0x28f4('0x49')](_0x2ce22d);var _0x3d4056=Buffer[_0x28f4('0x4a')](_0xd9b54f[_0x28f4('0x4b')],'base64')[_0x28f4('0x4c')](_0x28f4('0x4d'));_0x516457=JSON[_0x28f4('0x49')](_0x3d4056);if(!oauth['isValidIdToken'](_0xd9b54f[_0x28f4('0x4e')],_0x32fe23['oauth2Claims']))throw new Error('Unable\x20to\x20validate\x20id_token');redis[_0x28f4('0x4f')](_0xd9b54f[_0x28f4('0x4b')]);return oauth[_0x28f4('0x50')](_0xd9b54f[_0x28f4('0x51')],_0x32fe23);})['then'](function(_0x5499d7){_0x32fe23[_0x28f4('0x52')]=_0x5499d7['access_token'];_0x32fe23[_0x28f4('0x53')]=_0x5499d7[_0x28f4('0x54')];return db[_0x28f4('0x1c')][_0x28f4('0x13')]({'data5':_0x32fe23[_0x28f4('0x52')],'data6':_0x32fe23[_0x28f4('0x53')]},{'where':{'id':_0x32fe23['id']}});})[_0x28f4('0x14')](function(){oauth['startRefreshInterval'](_0x32fe23);socket[_0x28f4('0x55')](_0x28f4('0x56'),{'id':_0x516457['id']});return;})[_0x28f4('0x14')](respondWithStatusCode(_0x194e57,null))['catch'](function(_0x463074){if(_0x516457){socket[_0x28f4('0x55')](_0x28f4('0x57'),{'id':_0x516457['id']});}if(!_0x32fe23){logger[_0x28f4('0x16')](_0x28f4('0x58'),_0x463074);return _0x194e57[_0x28f4('0x11')](0x1f4)[_0x28f4('0x18')](_0x463074['message']);}logger[_0x28f4('0x16')](_0x28f4('0x59'),_0x32fe23['id'],JSON[_0x28f4('0x5a')](_0x463074));});};