Built motion from commit 4c281f94.|2.6.31
[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 _0x5f33=['pass','Wrong\x20credentials.','Bearer','apikey','getUuid','sub','allowedLoginAttempts','isEqual','nonce','API\x20access\x20key\x20is\x20not\x20valid\x20anymore','disabled','blockedAt','add','blockDuration','minutes','Invalid\x20API\x20access\x20key','Unknown\x20authorization\x20format','User\x20object\x20not\x20found.','canUpdate','getLicense','Forbidden','isWebrtcLicence','webrtc','isMiddleware','signToken','It\x20looks\x20like\x20you\x20aren\x27t\x20logged\x20in,\x20please\x20try\x20again.','role','cookie','motion.token','redirect','/dashboards/general','retrieveApiKey','isNil','generateApiKey','apiKeyIat','apiKeyNonce','Please\x20use\x20the\x20previous\x20API\x20access\x20key\x20to\x20generate\x20a\x20new\x20one','test','ValidationError','The\x20password\x20must\x20be\x20at\x20least\x208\x20characters\x20long\x20and\x20have\x201\x20lowercase\x20character,\x201\x20uppercase\x20character,\x201\x20number\x20and\x201\x20special\x20character\x20~!@#$%^&-_=+[{]}.','validatePasswordHistory','decryptString','split','toLowerCase','format','splice','encryptString','promisify','secret','payload','verify','randomBytes','toString','hex','floor','now','HS512','../../mysqldb','../../config/environment','../../config/license/util','../encryptor','jsonwebtoken','composable-middleware','basic-auth','util','moment','secrets','session','fullname','name','internal','userpic','permissions','md5secret','voicePause','chatPause','smsPause','openchannelPause','lastLoginAt','crudPermissions','allowmessenger','passwordResetAt','alias','phoneBarAutoAnswer','phoneBarAutoAnswerDelay','phoneBarEnableRecording','phoneBarEnableDtmfTone','phoneBarEnableSettings','phoneBarPrefixRequired','phoneBarRemoteControl','phoneBarRemoteControlPort','hotdesk','interface','userProfileId','settingsEnabled','wssPort','downloadVoiceRecordings','downloadOmnichannelInteractions','downloadAttachments','ignorePauseForPreviewCalls','selectRecallMeCampaign','chatAutoanswerDelay','emailAutoanswer','emailAutoanswerDelay','smsAutoanswer','openchannelAutoanswer','openchannelAutoanswerDelay','faxAutoanswer','faxAutoanswerDelay','whatsappAutoanswer','whatsappAutoanswerDelay','isChatInteractionAuthorized','isAuthenticated','user','ChatInteraction','findOne','params','closed','disposition','then','query','forceDownload','status','unmanaged','json','Unmanaged.','Forbidden.','catch','use','authorization','startsWith','headers','User','find','authenticate'];(function(_0xf2c6bb,_0x3ebb2c){var _0x4cde19=function(_0x47ce13){while(--_0x47ce13){_0xf2c6bb['push'](_0xf2c6bb['shift']());}};_0x4cde19(++_0x3ebb2c);}(_0x5f33,0x147));var _0x35f3=function(_0x35779b,_0x4d5e22){_0x35779b=_0x35779b-0x0;var _0x441486=_0x5f33[_0x35779b];return _0x441486;};'use strict';var db=require(_0x35f3('0x0'))['db'];var config=require(_0x35f3('0x1'));var hardwareConf=require('../../config/license/hardware');var licenseUtil=require(_0x35f3('0x2'));var encryptor=require(_0x35f3('0x3'));var _=require('lodash');var jwt=require(_0x35f3('0x4'));var expressJwt=require('express-jwt');var compose=require(_0x35f3('0x5'));var basicAuth=require(_0x35f3('0x6'));var crypto=require('crypto');var BPromise=require('bluebird');var util=require(_0x35f3('0x7'));var moment=require(_0x35f3('0x8'));var validateJwt=expressJwt({'secret':config[_0x35f3('0x9')][_0x35f3('0xa')]});var userAttributes=['id','role',_0x35f3('0xb'),_0x35f3('0xc'),_0x35f3('0xd'),'email',_0x35f3('0xe'),_0x35f3('0xf'),_0x35f3('0x10'),_0x35f3('0x11'),_0x35f3('0x12'),'mailPause','faxPause',_0x35f3('0x13'),_0x35f3('0x14'),'pauseType','showWebBar',_0x35f3('0x15'),'lastPauseAt',_0x35f3('0x16'),_0x35f3('0x17'),_0x35f3('0x18'),_0x35f3('0x19'),_0x35f3('0x1a'),_0x35f3('0x1b'),'phoneBarDnd',_0x35f3('0x1c'),_0x35f3('0x1d'),_0x35f3('0x1e'),'phoneBarExpires',_0x35f3('0x1f'),_0x35f3('0x20'),_0x35f3('0x21'),_0x35f3('0x22'),_0x35f3('0x23'),_0x35f3('0x24'),'privacyEnabled',_0x35f3('0x25'),_0x35f3('0x26'),_0x35f3('0x27'),_0x35f3('0x28'),_0x35f3('0x29'),_0x35f3('0x2a'),_0x35f3('0x2b'),'chatAutoanswer',_0x35f3('0x2c'),_0x35f3('0x2d'),_0x35f3('0x2e'),_0x35f3('0x2f'),'smsAutoanswerDelay',_0x35f3('0x30'),_0x35f3('0x31'),_0x35f3('0x32'),_0x35f3('0x33'),_0x35f3('0x34'),_0x35f3('0x35'),'messengerSoundNotification'];exports[_0x35f3('0x36')]=function(){return this[_0x35f3('0x37')](!![])['use'](function(_0x437d64,_0x2fa8ff,_0x250ca3){if(_0x437d64[_0x35f3('0x38')]){_0x250ca3();}else{return db[_0x35f3('0x39')][_0x35f3('0x3a')]({'where':{'id':_0x437d64[_0x35f3('0x3b')]['id']},'attributes':['id',_0x35f3('0x3c'),_0x35f3('0x3d')],'raw':!![]})[_0x35f3('0x3e')](function(_0x3e96d9){if(_0x3e96d9&&_0x3e96d9[_0x35f3('0x3c')]&&!_0x437d64[_0x35f3('0x3f')][_0x35f3('0x40')]){return _0x2fa8ff[_0x35f3('0x41')](_0x3e96d9[_0x35f3('0x3d')]===_0x35f3('0x42')?0x195:0x193)[_0x35f3('0x43')]({'message':_0x3e96d9[_0x35f3('0x3d')]==='unmanaged'?_0x35f3('0x44'):_0x35f3('0x45')});}else{_0x250ca3();}})[_0x35f3('0x46')](function(_0x5c0b9d){_0x250ca3(_0x5c0b9d);});}});};exports[_0x35f3('0x37')]=function isAuthenticated(_0x4a9e39){return compose()[_0x35f3('0x47')](function(_0x4bae74,_0x18a123,_0x1dbbe1){var _0x187f3a;if(_0x4bae74['headers'][_0x35f3('0x48')]){if(_[_0x35f3('0x49')](_0x4bae74[_0x35f3('0x4a')][_0x35f3('0x48')],'Basic')){var _0x1f5ffb=basicAuth(_0x4bae74);db[_0x35f3('0x4b')][_0x35f3('0x4c')]({'where':{'name':_0x1f5ffb[_0x35f3('0xc')]}})['then'](function(_0x1258aa){if(!_0x1258aa||!_0x1258aa[_0x35f3('0x4d')](_0x1f5ffb[_0x35f3('0x4e')])){return _0x18a123['status'](0x191)[_0x35f3('0x43')]({'message':_0x35f3('0x4f')});}_0x4bae74[_0x35f3('0x38')]={'id':_0x1258aa['id']};_0x1dbbe1();})[_0x35f3('0x46')](function(_0x438ac8){_0x1dbbe1(_0x438ac8);});}else if(_[_0x35f3('0x49')](_0x4bae74[_0x35f3('0x4a')][_0x35f3('0x48')],_0x35f3('0x50'))){validateJwt(_0x4bae74,_0x18a123,_0x1dbbe1);}else{if(_0x4a9e39){_0x1dbbe1();}else{return _0x18a123[_0x35f3('0x41')](0x193)[_0x35f3('0x43')]({'message':'Unknown\x20authorization\x20format'});}}}else if(_0x4bae74[_0x35f3('0x3f')][_0x35f3('0x51')]){try{var _0x8dad72={'audience':hardwareConf[_0x35f3('0x52')](),'issuer':hardwareConf[_0x35f3('0x52')]()};verifyJwt(_0x4bae74[_0x35f3('0x3f')][_0x35f3('0x51')],_0x8dad72)[_0x35f3('0x3e')](function(_0xda18bb){return db[_0x35f3('0x4b')][_0x35f3('0x4c')]({'where':{'id':_0xda18bb[_0x35f3('0x53')]}})[_0x35f3('0x3e')](function(_0x2752ab){_0x187f3a=_0x2752ab;return db['Setting'][_0x35f3('0x3a')]({'where':{'id':0x1},'attributes':[_0x35f3('0x54'),'blockDuration'],'raw':!![]});})[_0x35f3('0x3e')](function(_0x5380f5){if(!_0x187f3a||!_[_0x35f3('0x55')](_0x187f3a['apiKeyNonce'],_0xda18bb[_0x35f3('0x56')])){return _0x18a123[_0x35f3('0x41')](0x191)[_0x35f3('0x43')]({'message':_0x35f3('0x57')});}if(_0x187f3a[_0x35f3('0x58')]){return _0x18a123[_0x35f3('0x41')](0x191)[_0x35f3('0x43')]({'message':'Invalid\x20API\x20access\x20key'});}if(_0x187f3a['blocked']){if(_0x5380f5['blockDuration']>0x0){if(moment(_0x187f3a[_0x35f3('0x59')])[_0x35f3('0x5a')](_0x5380f5[_0x35f3('0x5b')],_0x35f3('0x5c'))>moment()){return _0x18a123[_0x35f3('0x41')](0x191)['json']({'message':'Invalid\x20API\x20access\x20key'});}}else{return _0x18a123[_0x35f3('0x41')](0x191)[_0x35f3('0x43')]({'message':'Invalid\x20API\x20access\x20key'});}}_0x4bae74[_0x35f3('0x38')]={'id':_0x187f3a['id']};_0x1dbbe1();});})[_0x35f3('0x46')](function(){return _0x18a123[_0x35f3('0x41')](0x191)[_0x35f3('0x43')]({'message':_0x35f3('0x5d')});});}catch(_0x479562){_0x1dbbe1(_0x479562);}}else if(_0x4a9e39){_0x1dbbe1();}else{return _0x18a123['status'](0x193)[_0x35f3('0x43')]({'message':_0x35f3('0x5e')});}})[_0x35f3('0x47')](function(_0x47817d,_0x413cd8,_0x5bdb83){if(_0x47817d[_0x35f3('0x38')]){db[_0x35f3('0x4b')][_0x35f3('0x4c')]({'where':{'id':_0x47817d['user']['id']},'attributes':userAttributes})[_0x35f3('0x3e')](function(_0x38c3e7){if(!_0x38c3e7){return _0x413cd8[_0x35f3('0x41')](0x194)[_0x35f3('0x43')]({'message':'User\x20not\x20found.'});}_0x47817d[_0x35f3('0x38')]=_0x38c3e7;_0x5bdb83();})['catch'](function(_0x312d9d){_0x5bdb83(_0x312d9d);});}else if(_0x4a9e39){_0x5bdb83();}else{return _0x413cd8['status'](0x194)[_0x35f3('0x43')]({'message':_0x35f3('0x5f')});}});};exports[_0x35f3('0x60')]=function canUpdate(){return compose()['use'](function(_0x39c5c0,_0xa753ea,_0x392df2){return licenseUtil[_0x35f3('0x61')]()['then'](function(_0x4a26c4){if(_0x4a26c4['update']){_0x392df2();}else{return _0xa753ea[_0x35f3('0x41')](0x193)[_0x35f3('0x43')]({'message':_0x35f3('0x62')});}})[_0x35f3('0x46')](function(_0x5791b9){_0x392df2(_0x5791b9);});});};exports[_0x35f3('0x63')]=function isWebrtcLicence(){return compose()[_0x35f3('0x47')](function(_0x1afe13,_0x233cf4,_0x41f109){return licenseUtil['getLicense']()[_0x35f3('0x3e')](function(_0x4f2099){if(_0x4f2099[_0x35f3('0x64')]){_0x41f109();}else{return _0x233cf4[_0x35f3('0x41')](0x193)['json']({'message':_0x35f3('0x62')});}})[_0x35f3('0x46')](function(_0x1e5d19){_0x41f109(_0x1e5d19);});});};exports[_0x35f3('0x65')]=function(_0xb8b5bb,_0x5b6f29,_0x24a61f){_0xb8b5bb[_0x35f3('0x65')]=!![];return _0x24a61f();};exports[_0x35f3('0x66')]=function signToken(_0x43909e){return signJwt(_0x43909e);};exports['setTokenCookie']=function(_0x254a49,_0x1a988c){if(!_0x254a49[_0x35f3('0x38')]){return _0x1a988c[_0x35f3('0x41')](0x194)['json']({'message':_0x35f3('0x67')});}var _0xdbb24a={'payload':{'id':_0x254a49[_0x35f3('0x38')]['id'],'role':_0x254a49[_0x35f3('0x38')][_0x35f3('0x68')]},'options':{'expiresIn':0x15180}};return signJwt(_0xdbb24a)[_0x35f3('0x3e')](function(_0x11d00e){_0x1a988c[_0x35f3('0x69')](_0x35f3('0x6a'),_0x11d00e);_0x1a988c[_0x35f3('0x6b')](_0x35f3('0x6c'));})[_0x35f3('0x46')](function(_0x36e8bf){return _0x1a988c['status'](0x1f4)['send'](_0x36e8bf);});};exports[_0x35f3('0x6d')]=function(_0x137293){if(_[_0x35f3('0x6e')](_0x137293['apiKeyNonce'])||_[_0x35f3('0x6e')](_0x137293['apiKeyIat'])){return null;}else{return createJwt(_0x137293);}};exports[_0x35f3('0x6f')]=function(_0x1b311c){_0x1b311c['apiKeyNonce']=generateNonce();_0x1b311c[_0x35f3('0x70')]=generateIssuedAt();return createJwt(_0x1b311c);};exports['regenerateApiKey']=function(_0x1abfa7,_0x102504){var _0x269ff6=_0x1abfa7[_0x35f3('0x3f')][_0x35f3('0x51')];if(_0x269ff6){var _0x1b34a7={'nonce':_0x102504[_0x35f3('0x71')],'iat':_0x102504[_0x35f3('0x70')],'audience':hardwareConf[_0x35f3('0x52')](),'issuer':hardwareConf['getUuid']()};return verifyJwt(_0x269ff6,_0x1b34a7)[_0x35f3('0x3e')](function(){return generateApiKey(_0x102504);});}else{throw{'message':_0x35f3('0x72')};}};exports['validatePasswordPattern']=function(_0x2d1afd){var _0x3fd50a=new RegExp(/(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[?!@#\$%\^&\*~\-_=+[{\]\}])(?=.{8,})/);if(!_0x3fd50a[_0x35f3('0x73')](_0x2d1afd))throw new db['Sequelize'][(_0x35f3('0x74'))](_0x35f3('0x75'));return;};exports[_0x35f3('0x76')]=function(_0xd34290,_0x406770,_0x3be69b){var _0x1cc9b0=encryptor[_0x35f3('0x77')](_0x406770)[_0x35f3('0x78')](',');for(var _0x53665e=0x0;_0x53665e<_0x3be69b;_0x53665e++){if(!_0x1cc9b0[_0x53665e])break;if(_0xd34290[_0x35f3('0x79')]()===_0x1cc9b0[_0x53665e][_0x35f3('0x79')]()){var _0x469890=util[_0x35f3('0x7a')]('The\x20password\x20must\x20be\x20different\x20from\x20the\x20previous\x20%d\x20passwords.\x20Please\x20choose\x20another\x20one.',_0x3be69b);if(_0x3be69b===0x1){_0x469890='The\x20password\x20must\x20be\x20different\x20from\x20the\x20last\x20one.\x20Please\x20choose\x20another\x20one.';}throw new db['Sequelize'][(_0x35f3('0x74'))](_0x469890);}}return;};exports['updatePasswordsHistory']=function(_0x3be96f,_0x5b876f){var _0x90f032=_0x5b876f?encryptor[_0x35f3('0x77')](_0x5b876f)[_0x35f3('0x78')](','):[];if(_0x90f032['length']===0x5){_0x90f032[_0x35f3('0x7b')](-0x1,0x1);}_0x90f032['unshift'](_0x3be96f);return encryptor[_0x35f3('0x7c')](_0x90f032['join'](','));};function signJwt(_0x1632f6){var _0x3c5dc3=BPromise[_0x35f3('0x7d')](jwt['sign'],{'context':jwt});var _0x4acc84=_0x1632f6[_0x35f3('0x7e')]||config[_0x35f3('0x9')][_0x35f3('0xa')];return new BPromise(function(_0x273b72,_0x498c89){_0x3c5dc3(_0x1632f6[_0x35f3('0x7f')],_0x4acc84,_0x1632f6['options'])[_0x35f3('0x3e')](function(_0x37916a){_0x273b72(_0x37916a);})[_0x35f3('0x46')](function(_0x1fc623){_0x498c89(_0x1fc623);});});}function verifyJwt(_0x3706d0,_0x1abc9d,_0x50604c){var _0x1c2243=BPromise[_0x35f3('0x7d')](jwt[_0x35f3('0x80')],{'context':jwt});var _0x5385df=_0x50604c||config[_0x35f3('0x9')][_0x35f3('0xa')];return new BPromise(function(_0x3af1b5,_0x5c9e63){_0x1c2243(_0x3706d0,_0x5385df,_0x1abc9d)[_0x35f3('0x3e')](function(_0x261f6c){_0x3af1b5(_0x261f6c);})['catch'](function(_0x326cdd){_0x5c9e63(_0x326cdd);});});}function generateNonce(){return crypto[_0x35f3('0x81')](0x10)[_0x35f3('0x82')](_0x35f3('0x83'));}function generateIssuedAt(){return Math[_0x35f3('0x84')](Date[_0x35f3('0x85')]()/0x3e8)['toString']();}function createJwt(_0x596c9e){var _0x3232e2={'payload':{'iat':_0x596c9e[_0x35f3('0x70')],'nonce':_0x596c9e['apiKeyNonce']},'options':{'algorithm':_0x35f3('0x86'),'subject':_0x596c9e['id'][_0x35f3('0x82')](),'issuer':hardwareConf[_0x35f3('0x52')](),'audience':hardwareConf[_0x35f3('0x52')]()}};return signJwt(_0x3232e2)[_0x35f3('0x3e')](function(_0x252cb4){return{'iat':_0x596c9e['apiKeyIat'],'nonce':_0x596c9e[_0x35f3('0x71')],'token':_0x252cb4};});}