Built motion from commit (unavailable).|2.4.4
[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 _0xb5ba=['cookie','motion.user','redirect','/dashboards/general','retrieveApiKey','isNil','generateApiKey','randomBytes','toString','apiKeyIat','floor','now','regenerateApiKey','getUuid','Please\x20use\x20the\x20previous\x20API\x20access\x20key\x20to\x20generate\x20a\x20new\x20one','HS512','../../mysqldb','../../config/environment','../../config/license/hardware','lodash','jsonwebtoken','express-jwt','composable-middleware','basic-auth','ipaddr.js','secrets','session','role','fullname','internal','email','permissions','voicePause','chatPause','mailPause','faxPause','smsPause','openchannelPause','pauseType','showWebBar','phoneBarRemoteControlPort','interface','userProfileId','isChatInteractionAuthorized','use','user','ChatInteraction','findOne','closed','disposition','then','status','unmanaged','Unmanaged.','Forbidden.','catch','headers','authorization','User','name','authenticate','pass','json','Wrong\x20credentials.','startsWith','Bearer','Unknown\x20authorization\x20format','query','apikey','verify','find','isEqual','apiKeyNonce','nonce','API\x20access\x20key\x20is\x20not\x20valid\x20anymore','log','Unrecognized\x20API\x20access\x20key','User\x20not\x20found.','User\x20object\x20not\x20found.','isAuthenticated','../../config/license/util','update','Forbidden','canUpdate','sign','signToken','hasRole','Required\x20role\x20needs\x20to\x20be\x20set','userRoles','indexOf','setTokenCookie','It\x20looks\x20like\x20you\x20aren\x27t\x20logged\x20in,\x20please\x20try\x20again.','motion.token'];(function(_0x18ac3f,_0x386630){var _0x4c6563=function(_0xcc161f){while(--_0xcc161f){_0x18ac3f['push'](_0x18ac3f['shift']());}};_0x4c6563(++_0x386630);}(_0xb5ba,0x127));var _0xab5b=function(_0x819464,_0x46fc91){_0x819464=_0x819464-0x0;var _0x49dcc0=_0xb5ba[_0x819464];return _0x49dcc0;};'use strict';var db=require(_0xab5b('0x0'))['db'];var config=require(_0xab5b('0x1'));var hardwareConf=require(_0xab5b('0x2'));var _=require(_0xab5b('0x3'));var jwt=require(_0xab5b('0x4'));var expressJwt=require(_0xab5b('0x5'));var compose=require(_0xab5b('0x6'));var basicAuth=require(_0xab5b('0x7'));var crypto=require('crypto');var ipaddr=require(_0xab5b('0x8'));var validateJwt=expressJwt({'secret':config[_0xab5b('0x9')][_0xab5b('0xa')]});var userAttributes=['id',_0xab5b('0xb'),_0xab5b('0xc'),'name',_0xab5b('0xd'),_0xab5b('0xe'),'userpic',_0xab5b('0xf'),_0xab5b('0x10'),_0xab5b('0x11'),_0xab5b('0x12'),_0xab5b('0x13'),_0xab5b('0x14'),_0xab5b('0x15'),_0xab5b('0x16'),_0xab5b('0x17'),_0xab5b('0x18'),'phoneBarRemoteControl',_0xab5b('0x19'),_0xab5b('0x1a')];exports[_0xab5b('0x1b')]=function(){return isAuthenticated(!![])[_0xab5b('0x1c')](function(_0x2cb64d,_0x153ef0,_0x1b72bb){if(_0x2cb64d[_0xab5b('0x1d')]){_0x1b72bb();}else{return db[_0xab5b('0x1e')][_0xab5b('0x1f')]({'where':{'id':_0x2cb64d['params']['id']},'attributes':['id',_0xab5b('0x20'),_0xab5b('0x21')],'raw':!![]})[_0xab5b('0x22')](function(_0x2bf7c4){if(_0x2bf7c4&&_0x2bf7c4[_0xab5b('0x20')]){return _0x153ef0[_0xab5b('0x23')](_0x2bf7c4['disposition']==='unmanaged'?0x195:0x193)['json']({'message':_0x2bf7c4['disposition']===_0xab5b('0x24')?_0xab5b('0x25'):_0xab5b('0x26')});}else{_0x1b72bb();}})[_0xab5b('0x27')](function(_0x5720ce){_0x1b72bb(_0x5720ce);});}});};function isAuthenticated(_0x1f4b53){return compose()[_0xab5b('0x1c')](function(_0x4ff611,_0x3b4fc1,_0x1479e4){if(_0x4ff611[_0xab5b('0x28')][_0xab5b('0x29')]){if(_['startsWith'](_0x4ff611[_0xab5b('0x28')][_0xab5b('0x29')],'Basic')){var _0x4bac0f=basicAuth(_0x4ff611);db[_0xab5b('0x2a')]['find']({'where':{'name':_0x4bac0f[_0xab5b('0x2b')]}})[_0xab5b('0x22')](function(_0x2000df){if(!_0x2000df||!_0x2000df[_0xab5b('0x2c')](_0x4bac0f[_0xab5b('0x2d')])){return _0x3b4fc1[_0xab5b('0x23')](0x191)[_0xab5b('0x2e')]({'message':_0xab5b('0x2f')});}_0x4ff611[_0xab5b('0x1d')]={'id':_0x2000df['id']};_0x1479e4();})[_0xab5b('0x27')](function(_0x2ee8a8){_0x1479e4(_0x2ee8a8);});}else if(_[_0xab5b('0x30')](_0x4ff611[_0xab5b('0x28')][_0xab5b('0x29')],_0xab5b('0x31'))){validateJwt(_0x4ff611,_0x3b4fc1,_0x1479e4);}else{if(_0x1f4b53){_0x1479e4();}else{return _0x3b4fc1[_0xab5b('0x23')](0x193)[_0xab5b('0x2e')]({'message':_0xab5b('0x32')});}}}else if(_0x4ff611[_0xab5b('0x33')][_0xab5b('0x34')]){try{var _0x479d78=jwt[_0xab5b('0x35')](_0x4ff611[_0xab5b('0x33')][_0xab5b('0x34')],config['secrets']['session'],{'audience':hardwareConf['getUuid'](),'issuer':hardwareConf['getUuid']()});db[_0xab5b('0x2a')][_0xab5b('0x36')]({'where':{'id':_0x479d78['sub']}})[_0xab5b('0x22')](function(_0xbaafca){if(!_0xbaafca||!_[_0xab5b('0x37')](_0xbaafca[_0xab5b('0x38')],_0x479d78[_0xab5b('0x39')])){return _0x3b4fc1['status'](0x193)[_0xab5b('0x2e')]({'message':_0xab5b('0x3a')});}_0x4ff611['user']={'id':_0xbaafca['id']};_0x1479e4();})['catch'](function(_0x51289f){_0x1479e4(_0x51289f);});}catch(_0x2d9cee){console[_0xab5b('0x3b')](_0x2d9cee);return _0x3b4fc1[_0xab5b('0x23')](0x193)[_0xab5b('0x2e')]({'message':_0xab5b('0x3c')});}}else if(_0x1f4b53){_0x1479e4();}else{return _0x3b4fc1[_0xab5b('0x23')](0x193)['json']({'message':_0xab5b('0x32')});}})[_0xab5b('0x1c')](function(_0x3d45a2,_0x4affd6,_0xd5d187){if(_0x3d45a2[_0xab5b('0x1d')]){db['User']['find']({'where':{'id':_0x3d45a2[_0xab5b('0x1d')]['id']},'attributes':userAttributes})[_0xab5b('0x22')](function(_0x17ff0f){if(!_0x17ff0f){return _0x4affd6['status'](0x194)[_0xab5b('0x2e')]({'message':_0xab5b('0x3d')});}_0x3d45a2[_0xab5b('0x1d')]=_0x17ff0f;_0xd5d187();})[_0xab5b('0x27')](function(_0x25ed4b){_0xd5d187(_0x25ed4b);});}else if(_0x1f4b53){_0xd5d187();}else{return _0x4affd6[_0xab5b('0x23')](0x194)[_0xab5b('0x2e')]({'message':_0xab5b('0x3e')});}});}exports[_0xab5b('0x3f')]=isAuthenticated;function canUpdate(){return compose()['use'](function(_0x505ae2,_0x27e597,_0x2911e6){return require(_0xab5b('0x40'))['getLicense']()[_0xab5b('0x22')](function(_0x51fcbc){if(_0x51fcbc[_0xab5b('0x41')]){_0x2911e6();}else{return _0x27e597['status'](0x193)[_0xab5b('0x2e')]({'message':_0xab5b('0x42')});}})[_0xab5b('0x27')](function(_0x4d6395){_0x2911e6(_0x4d6395);});});}exports[_0xab5b('0x43')]=canUpdate;function signToken(_0x4b7294,_0x437a58){return jwt[_0xab5b('0x44')]({'id':_0x4b7294,'role':_0x437a58},config[_0xab5b('0x9')][_0xab5b('0xa')],{'expiresIn':0x15180});}exports[_0xab5b('0x45')]=signToken;exports[_0xab5b('0x46')]=function(_0x53fe43){if(!_0x53fe43){throw new Error(_0xab5b('0x47'));}return compose()['use'](isAuthenticated())['use'](function meetsRequirements(_0x42409f,_0x37c688,_0xb4f5f2){if(config[_0xab5b('0x48')][_0xab5b('0x49')](_0x42409f[_0xab5b('0x1d')][_0xab5b('0xb')])>=config[_0xab5b('0x48')]['indexOf'](_0x53fe43)){return _0xb4f5f2();}else{return _0x37c688[_0xab5b('0x23')](0x193)[_0xab5b('0x2e')]({'message':_0xab5b('0x42')});}});};exports['isMiddleware']=function(_0x4e04cf,_0x292926,_0x5a8cf1){_0x4e04cf['isMiddleware']=!![];return _0x5a8cf1();};exports[_0xab5b('0x4a')]=function(_0x391cd5,_0x405c81){if(!_0x391cd5[_0xab5b('0x1d')]){return _0x405c81[_0xab5b('0x23')](0x194)[_0xab5b('0x2e')]({'message':_0xab5b('0x4b')});}var _0x1fa2c4=signToken(_0x391cd5['user']['id'],_0x391cd5[_0xab5b('0x1d')][_0xab5b('0xb')]);_0x405c81['cookie'](_0xab5b('0x4c'),_0x1fa2c4);_0x405c81[_0xab5b('0x4d')](_0xab5b('0x4e'),JSON['stringify'](_['pick'](_0x391cd5[_0xab5b('0x1d')],userAttributes)));_0x405c81[_0xab5b('0x4f')](_0xab5b('0x50'));};exports[_0xab5b('0x51')]=function(_0x42ee76){if(_[_0xab5b('0x52')](_0x42ee76['apiKeyNonce'])||_[_0xab5b('0x52')](_0x42ee76['apiKeyIat']))return null;else return createJwt(_0x42ee76);};exports[_0xab5b('0x53')]=function(_0x57852b){_0x57852b[_0xab5b('0x38')]=crypto[_0xab5b('0x54')](0x10)[_0xab5b('0x55')]('hex');_0x57852b[_0xab5b('0x56')]=Math[_0xab5b('0x57')](Date[_0xab5b('0x58')]()/0x3e8)['toString']();return createJwt(_0x57852b);};exports[_0xab5b('0x59')]=function(_0x3d6251,_0x3d613a){if(_0x3d6251['query'][_0xab5b('0x34')]){jwt['verify'](_0x3d6251[_0xab5b('0x33')][_0xab5b('0x34')],config[_0xab5b('0x9')][_0xab5b('0xa')],{'nonce':_0x3d613a[_0xab5b('0x38')],'iat':_0x3d613a[_0xab5b('0x56')],'audience':hardwareConf[_0xab5b('0x5a')](),'issuer':hardwareConf[_0xab5b('0x5a')]()});return generateApiKey(_0x3d613a);}else{throw{'message':_0xab5b('0x5b')};}};function createJwt(_0xd7b2c5){return jwt['sign']({'iat':_0xd7b2c5[_0xab5b('0x56')],'nonce':_0xd7b2c5[_0xab5b('0x38')]},config['secrets'][_0xab5b('0xa')],{'algorithm':_0xab5b('0x5c'),'subject':_0xd7b2c5['id']['toString'](),'issuer':hardwareConf[_0xab5b('0x5a')](),'audience':hardwareConf[_0xab5b('0x5a')]()});}