Built motion from commit 67e5df37.|2.0.66
[motion2.git] / server / components / auth / service.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 _0x6dd6=['update','canUpdate','sign','session','signToken','Required\x20role\x20needs\x20to\x20be\x20set','indexOf','userRoles','Forbidden','setTokenCookie','It\x20looks\x20like\x20you\x20aren\x27t\x20logged\x20in,\x20please\x20try\x20again.','cookie','motion.token','motion.user','stringify','redirect','/dashboards/general','../../mysqldb','../../config/environment','lodash','express-jwt','composable-middleware','basic-auth','secrets','role','fullname','internal','email','voicePause','chatPause','mailPause','faxPause','smsPause','openchannelPause','pauseType','showWebBar','phoneBarRemoteControlPort','phoneBarRemoteControl','use','user','ChatInteraction','findOne','disposition','then','status','unmanaged','Unmanaged.','catch','startsWith','headers','authorization','Basic','User','find','name','authenticate','pass','json','Wrong\x20credentials.','Bearer','isAuthenticated','../../config/license/util','getLicense'];(function(_0x2641f9,_0x26715f){var _0x585741=function(_0x2d1462){while(--_0x2d1462){_0x2641f9['push'](_0x2641f9['shift']());}};_0x585741(++_0x26715f);}(_0x6dd6,0x14c));var _0x66dd=function(_0x3d8ca8,_0x1a9764){_0x3d8ca8=_0x3d8ca8-0x0;var _0x517e45=_0x6dd6[_0x3d8ca8];return _0x517e45;};'use strict';var db=require(_0x66dd('0x0'))['db'];var config=require(_0x66dd('0x1'));var _=require(_0x66dd('0x2'));var jwt=require('jsonwebtoken');var expressJwt=require(_0x66dd('0x3'));var compose=require(_0x66dd('0x4'));var basicAuth=require(_0x66dd('0x5'));var ipaddr=require('ipaddr.js');var validateJwt=expressJwt({'secret':config[_0x66dd('0x6')]['session']});var userAttributes=['id',_0x66dd('0x7'),_0x66dd('0x8'),'name',_0x66dd('0x9'),_0x66dd('0xa'),'userpic','permissions',_0x66dd('0xb'),_0x66dd('0xc'),_0x66dd('0xd'),_0x66dd('0xe'),_0x66dd('0xf'),_0x66dd('0x10'),_0x66dd('0x11'),_0x66dd('0x12'),_0x66dd('0x13'),_0x66dd('0x14')];exports['isChatInteractionAuthorized']=function(){return isAuthenticated(!![])[_0x66dd('0x15')](function(_0x2f164e,_0x3c490e,_0x459e58){if(_0x2f164e[_0x66dd('0x16')]){_0x459e58();}else{return db[_0x66dd('0x17')][_0x66dd('0x18')]({'where':{'id':_0x2f164e['params']['id']},'attributes':['id','closed',_0x66dd('0x19')],'raw':!![]})[_0x66dd('0x1a')](function(_0xbf40d5){if(_0xbf40d5&&_0xbf40d5['closed']){return _0x3c490e[_0x66dd('0x1b')](_0xbf40d5[_0x66dd('0x19')]===_0x66dd('0x1c')?0x195:0x193)['json']({'message':_0xbf40d5[_0x66dd('0x19')]===_0x66dd('0x1c')?_0x66dd('0x1d'):'Forbidden.'});}else{_0x459e58();}})[_0x66dd('0x1e')](function(_0x1dede5){_0x459e58(_0x1dede5);});}});};function isAuthenticated(_0x45b84b){return compose()['use'](function(_0x27653e,_0x1c81f2,_0x2ee610){if(_0x27653e['headers']['authorization']){if(_[_0x66dd('0x1f')](_0x27653e[_0x66dd('0x20')][_0x66dd('0x21')],_0x66dd('0x22'))){var _0x33919b=basicAuth(_0x27653e);db[_0x66dd('0x23')][_0x66dd('0x24')]({'where':{'name':_0x33919b[_0x66dd('0x25')]}})[_0x66dd('0x1a')](function(_0x4036e0){if(!_0x4036e0||!_0x4036e0[_0x66dd('0x26')](_0x33919b[_0x66dd('0x27')])){return _0x1c81f2['status'](0x191)[_0x66dd('0x28')]({'message':_0x66dd('0x29')});}_0x27653e[_0x66dd('0x16')]={'id':_0x4036e0['id']};_0x2ee610();})[_0x66dd('0x1e')](function(_0x4b2cd1){_0x2ee610(_0x4b2cd1);});}else if(_[_0x66dd('0x1f')](_0x27653e[_0x66dd('0x20')][_0x66dd('0x21')],_0x66dd('0x2a'))){validateJwt(_0x27653e,_0x1c81f2,_0x2ee610);}}else{if(_0x45b84b){_0x2ee610();}else{return _0x1c81f2['status'](0x191)['json']({'message':'Unknown\x20authorization\x20format.'});}}})[_0x66dd('0x15')](function(_0x5e9246,_0x1a7acc,_0x3f5942){if(_0x5e9246[_0x66dd('0x16')]){db[_0x66dd('0x23')][_0x66dd('0x24')]({'where':{'id':_0x5e9246['user']['id']},'attributes':userAttributes})[_0x66dd('0x1a')](function(_0x3dcbbf){if(!_0x3dcbbf){return _0x1a7acc[_0x66dd('0x1b')](0x194)[_0x66dd('0x28')]({'message':'User\x20not\x20found.'});}_0x5e9246[_0x66dd('0x16')]=_0x3dcbbf;_0x3f5942();})[_0x66dd('0x1e')](function(_0x5a0dab){_0x3f5942(_0x5a0dab);});}else if(_0x45b84b){_0x3f5942();}else{return _0x1a7acc[_0x66dd('0x1b')](0x194)['json']({'message':'User\x20object\x20not\x20found.'});}});}exports[_0x66dd('0x2b')]=isAuthenticated;function canUpdate(){return compose()[_0x66dd('0x15')](function(_0x42fbe4,_0xcb0475,_0x37af4f){return require(_0x66dd('0x2c'))[_0x66dd('0x2d')]()[_0x66dd('0x1a')](function(_0x26e3a3){if(_0x26e3a3[_0x66dd('0x2e')]){_0x37af4f();}else{return _0xcb0475[_0x66dd('0x1b')](0x193)['json']({'message':'Forbidden'});}})['catch'](function(_0x590445){_0x37af4f(_0x590445);});});}exports[_0x66dd('0x2f')]=canUpdate;function signToken(_0x22f42c,_0x4f7113){return jwt[_0x66dd('0x30')]({'id':_0x22f42c,'role':_0x4f7113},config[_0x66dd('0x6')][_0x66dd('0x31')],{'expiresIn':0x15180});}exports[_0x66dd('0x32')]=signToken;exports['hasRole']=function(_0x43aaeb){if(!_0x43aaeb){throw new Error(_0x66dd('0x33'));}return compose()['use'](isAuthenticated())[_0x66dd('0x15')](function meetsRequirements(_0x5a3b53,_0x38baa2,_0x223bfd){if(config['userRoles'][_0x66dd('0x34')](_0x5a3b53[_0x66dd('0x16')]['role'])>=config[_0x66dd('0x35')][_0x66dd('0x34')](_0x43aaeb)){return _0x223bfd();}else{return _0x38baa2[_0x66dd('0x1b')](0x193)['json']({'message':_0x66dd('0x36')});}});};exports[_0x66dd('0x37')]=function(_0x5b91b1,_0x5240f2){if(!_0x5b91b1[_0x66dd('0x16')]){return _0x5240f2[_0x66dd('0x1b')](0x194)[_0x66dd('0x28')]({'message':_0x66dd('0x38')});}var _0x1e46ce=signToken(_0x5b91b1[_0x66dd('0x16')]['id'],_0x5b91b1[_0x66dd('0x16')][_0x66dd('0x7')]);_0x5240f2[_0x66dd('0x39')](_0x66dd('0x3a'),_0x1e46ce);_0x5240f2[_0x66dd('0x39')](_0x66dd('0x3b'),JSON[_0x66dd('0x3c')](_['pick'](_0x5b91b1[_0x66dd('0x16')],userAttributes)));_0x5240f2[_0x66dd('0x3d')](_0x66dd('0x3e'));};