Built motion from commit (unavailable).|2.4.17
[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 _0x0618=['chatPause','mailPause','faxPause','smsPause','openchannelPause','lastLoginAt','lastPauseAt','passwordResetAt','alias','phoneBarAutoAnswer','phoneBarAutoAnswerDelay','phoneBarEnableRecording','phoneBarEnableDtmfTone','phoneBarPrefixRequired','phoneBarRemoteControl','phoneBarRemoteControlPort','hotdesk','isChatInteractionAuthorized','isAuthenticated','use','user','ChatInteraction','findOne','params','closed','disposition','then','status','unmanaged','Unmanaged.','Forbidden.','catch','startsWith','headers','User','authenticate','json','Wrong\x20credentials.','Unknown\x20authorization\x20format','query','apikey','getUuid','find','sub','apiKeyNonce','nonce','API\x20access\x20key\x20is\x20not\x20valid\x20anymore','User\x20object\x20not\x20found.','canUpdate','../../config/license/util','getLicense','update','Forbidden','isMiddleware','signToken','It\x20looks\x20like\x20you\x20aren\x27t\x20logged\x20in,\x20please\x20try\x20again.','cookie','motion.token','redirect','/dashboards/general','send','retrieveApiKey','isNil','generateApiKey','apiKeyIat','regenerateApiKey','Please\x20use\x20the\x20previous\x20API\x20access\x20key\x20to\x20generate\x20a\x20new\x20one','promisify','secret','payload','options','verify','randomBytes','hex','floor','now','toString','HS512','../../mysqldb','../../config/environment','lodash','jsonwebtoken','basic-auth','crypto','bluebird','secrets','session','role','fullname','name','internal','email','userpic','md5secret'];(function(_0x3a7a9c,_0x24f7d6){var _0x59156e=function(_0x5bc930){while(--_0x5bc930){_0x3a7a9c['push'](_0x3a7a9c['shift']());}};_0x59156e(++_0x24f7d6);}(_0x0618,0x1c6));var _0x8061=function(_0x5941b7,_0x499160){_0x5941b7=_0x5941b7-0x0;var _0x2ae4df=_0x0618[_0x5941b7];return _0x2ae4df;};'use strict';var db=require(_0x8061('0x0'))['db'];var config=require(_0x8061('0x1'));var hardwareConf=require('../../config/license/hardware');var _=require(_0x8061('0x2'));var jwt=require(_0x8061('0x3'));var expressJwt=require('express-jwt');var compose=require('composable-middleware');var basicAuth=require(_0x8061('0x4'));var crypto=require(_0x8061('0x5'));var BPromise=require(_0x8061('0x6'));var validateJwt=expressJwt({'secret':config[_0x8061('0x7')][_0x8061('0x8')]});var userAttributes=['id',_0x8061('0x9'),_0x8061('0xa'),_0x8061('0xb'),_0x8061('0xc'),_0x8061('0xd'),_0x8061('0xe'),'permissions',_0x8061('0xf'),'voicePause',_0x8061('0x10'),_0x8061('0x11'),_0x8061('0x12'),_0x8061('0x13'),_0x8061('0x14'),'pauseType','showWebBar',_0x8061('0x15'),_0x8061('0x16'),'crudPermissions','allowmessenger',_0x8061('0x17'),_0x8061('0x18'),_0x8061('0x19'),_0x8061('0x1a'),_0x8061('0x1b'),_0x8061('0x1c'),'phoneBarEnableSettings','phoneBarExpires',_0x8061('0x1d'),_0x8061('0x1e'),_0x8061('0x1f'),_0x8061('0x20'),'interface','userProfileId'];exports[_0x8061('0x21')]=function(){return this[_0x8061('0x22')](!![])[_0x8061('0x23')](function(_0x10b270,_0x127435,_0x3667a8){if(_0x10b270[_0x8061('0x24')]){_0x3667a8();}else{return db[_0x8061('0x25')][_0x8061('0x26')]({'where':{'id':_0x10b270[_0x8061('0x27')]['id']},'attributes':['id',_0x8061('0x28'),_0x8061('0x29')],'raw':!![]})[_0x8061('0x2a')](function(_0x58683d){if(_0x58683d&&_0x58683d['closed']){return _0x127435[_0x8061('0x2b')](_0x58683d[_0x8061('0x29')]===_0x8061('0x2c')?0x195:0x193)['json']({'message':_0x58683d['disposition']===_0x8061('0x2c')?_0x8061('0x2d'):_0x8061('0x2e')});}else{_0x3667a8();}})[_0x8061('0x2f')](function(_0x490531){_0x3667a8(_0x490531);});}});};exports['isAuthenticated']=function isAuthenticated(_0x1cd5ed){return compose()[_0x8061('0x23')](function(_0x4a2bed,_0x587dba,_0x3dbaea){if(_0x4a2bed['headers']['authorization']){if(_[_0x8061('0x30')](_0x4a2bed[_0x8061('0x31')]['authorization'],'Basic')){var _0x17dbff=basicAuth(_0x4a2bed);db[_0x8061('0x32')]['find']({'where':{'name':_0x17dbff[_0x8061('0xb')]}})[_0x8061('0x2a')](function(_0x39fb5c){if(!_0x39fb5c||!_0x39fb5c[_0x8061('0x33')](_0x17dbff['pass'])){return _0x587dba['status'](0x191)[_0x8061('0x34')]({'message':_0x8061('0x35')});}_0x4a2bed[_0x8061('0x24')]={'id':_0x39fb5c['id']};_0x3dbaea();})[_0x8061('0x2f')](function(_0x2803ba){_0x3dbaea(_0x2803ba);});}else if(_['startsWith'](_0x4a2bed['headers']['authorization'],'Bearer')){validateJwt(_0x4a2bed,_0x587dba,_0x3dbaea);}else{if(_0x1cd5ed){_0x3dbaea();}else{return _0x587dba[_0x8061('0x2b')](0x193)[_0x8061('0x34')]({'message':_0x8061('0x36')});}}}else if(_0x4a2bed[_0x8061('0x37')][_0x8061('0x38')]){var _0x34bc53={'audience':hardwareConf[_0x8061('0x39')](),'issuer':hardwareConf[_0x8061('0x39')]()};verifyJwt(_0x4a2bed[_0x8061('0x37')][_0x8061('0x38')],_0x34bc53)['then'](function(_0x4ea6f9){return db[_0x8061('0x32')][_0x8061('0x3a')]({'where':{'id':_0x4ea6f9[_0x8061('0x3b')]}})[_0x8061('0x2a')](function(_0x1202a3){if(!_0x1202a3||!_['isEqual'](_0x1202a3[_0x8061('0x3c')],_0x4ea6f9[_0x8061('0x3d')])){return _0x587dba['status'](0x193)['json']({'message':_0x8061('0x3e')});}_0x4a2bed[_0x8061('0x24')]={'id':_0x1202a3['id']};_0x3dbaea();});})['catch'](function(_0x1637fd){_0x3dbaea(_0x1637fd);});}else if(_0x1cd5ed){_0x3dbaea();}else{return _0x587dba[_0x8061('0x2b')](0x193)[_0x8061('0x34')]({'message':_0x8061('0x36')});}})[_0x8061('0x23')](function(_0x3b14b8,_0x30e217,_0x1e8f0d){if(_0x3b14b8[_0x8061('0x24')]){db['User'][_0x8061('0x3a')]({'where':{'id':_0x3b14b8[_0x8061('0x24')]['id']},'attributes':userAttributes})[_0x8061('0x2a')](function(_0x3e5eeb){if(!_0x3e5eeb){return _0x30e217['status'](0x194)['json']({'message':'User\x20not\x20found.'});}_0x3b14b8['user']=_0x3e5eeb;_0x1e8f0d();})['catch'](function(_0x36bb75){_0x1e8f0d(_0x36bb75);});}else if(_0x1cd5ed){_0x1e8f0d();}else{return _0x30e217['status'](0x194)[_0x8061('0x34')]({'message':_0x8061('0x3f')});}});};exports[_0x8061('0x40')]=function canUpdate(){return compose()[_0x8061('0x23')](function(_0xce3ea8,_0x5a8e8a,_0x2dbb19){return require(_0x8061('0x41'))[_0x8061('0x42')]()[_0x8061('0x2a')](function(_0xc85ded){if(_0xc85ded[_0x8061('0x43')]){_0x2dbb19();}else{return _0x5a8e8a[_0x8061('0x2b')](0x193)[_0x8061('0x34')]({'message':_0x8061('0x44')});}})[_0x8061('0x2f')](function(_0x1af8bd){_0x2dbb19(_0x1af8bd);});});};exports[_0x8061('0x45')]=function(_0x5adb6e,_0x5d8df2,_0xb72481){_0x5adb6e['isMiddleware']=!![];return _0xb72481();};exports[_0x8061('0x46')]=function signToken(_0x5f12a0){return signJwt(_0x5f12a0);};exports['setTokenCookie']=function(_0x105747,_0x152f09){if(!_0x105747[_0x8061('0x24')]){return _0x152f09[_0x8061('0x2b')](0x194)[_0x8061('0x34')]({'message':_0x8061('0x47')});}var _0x42f7e4={'payload':{'id':_0x105747[_0x8061('0x24')]['id'],'role':_0x105747[_0x8061('0x24')][_0x8061('0x9')]},'options':{'expiresIn':0x15180}};return signJwt(_0x42f7e4)['then'](function(_0x326f09){_0x152f09[_0x8061('0x48')](_0x8061('0x49'),_0x326f09);_0x152f09[_0x8061('0x4a')](_0x8061('0x4b'));})['catch'](function(_0x524dac){return _0x152f09[_0x8061('0x2b')](0x1f4)[_0x8061('0x4c')](_0x524dac);});};exports[_0x8061('0x4d')]=function(_0x456dfc){if(_[_0x8061('0x4e')](_0x456dfc[_0x8061('0x3c')])||_[_0x8061('0x4e')](_0x456dfc['apiKeyIat'])){return null;}else{return createJwt(_0x456dfc);}};exports[_0x8061('0x4f')]=function(_0x2f3c68){_0x2f3c68[_0x8061('0x3c')]=generateNonce();_0x2f3c68[_0x8061('0x50')]=generateIssuedAt();return createJwt(_0x2f3c68);};exports[_0x8061('0x51')]=function(_0x1c343e,_0x3f60a5){var _0x5a2b9a=_0x1c343e[_0x8061('0x37')]['apikey'];if(_0x5a2b9a){var _0x521181={'nonce':_0x3f60a5[_0x8061('0x3c')],'iat':_0x3f60a5[_0x8061('0x50')],'audience':hardwareConf['getUuid'](),'issuer':hardwareConf['getUuid']()};return verifyJwt(_0x5a2b9a,_0x521181)[_0x8061('0x2a')](function(){return generateApiKey(_0x3f60a5);});}else{throw{'message':_0x8061('0x52')};}};function signJwt(_0x297f29){var _0x3b9a75=BPromise[_0x8061('0x53')](jwt['sign'],{'context':jwt});var _0x54687a=_0x297f29[_0x8061('0x54')]||config['secrets'][_0x8061('0x8')];return new BPromise(function(_0x393bc9,_0x54546a){_0x3b9a75(_0x297f29[_0x8061('0x55')],_0x54687a,_0x297f29[_0x8061('0x56')])[_0x8061('0x2a')](function(_0x1208f1){_0x393bc9(_0x1208f1);})['catch'](function(_0x1dac0a){_0x54546a(_0x1dac0a);});});}function verifyJwt(_0xb1cc9d,_0x2eaab6,_0x1b7ca7){var _0x1edb35=BPromise[_0x8061('0x53')](jwt[_0x8061('0x57')],{'context':jwt});var _0x101372=_0x1b7ca7||config[_0x8061('0x7')][_0x8061('0x8')];return new BPromise(function(_0x4fc30,_0x1d9e75){_0x1edb35(_0xb1cc9d,_0x101372,_0x2eaab6)[_0x8061('0x2a')](function(_0x223916){_0x4fc30(_0x223916);})[_0x8061('0x2f')](function(_0x30771f){_0x1d9e75(_0x30771f);});});}function generateNonce(){return crypto[_0x8061('0x58')](0x10)['toString'](_0x8061('0x59'));}function generateIssuedAt(){return Math[_0x8061('0x5a')](Date[_0x8061('0x5b')]()/0x3e8)[_0x8061('0x5c')]();}function createJwt(_0x534aeb){var _0x2be970={'payload':{'iat':_0x534aeb[_0x8061('0x50')],'nonce':_0x534aeb[_0x8061('0x3c')]},'options':{'algorithm':_0x8061('0x5d'),'subject':_0x534aeb['id'][_0x8061('0x5c')](),'issuer':hardwareConf[_0x8061('0x39')](),'audience':hardwareConf[_0x8061('0x39')]()}};return signJwt(_0x2be970)[_0x8061('0x2a')](function(_0x108996){return{'iat':_0x534aeb[_0x8061('0x50')],'nonce':_0x534aeb[_0x8061('0x3c')],'token':_0x108996};});}