8274d1408c483a55dae4b36cd290f54e7ae0e8e4
[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 _0xccfb=['canUpdate','getLicense','update','Forbidden','isWebrtcLicence','webrtc','isMiddleware','setTokenCookie','It\x20looks\x20like\x20you\x20aren\x27t\x20logged\x20in,\x20please\x20try\x20again.','role','cookie','motion.token','redirect','/dashboards/general','send','retrieveApiKey','isNil','apiKeyIat','generateApiKey','regenerateApiKey','Please\x20use\x20the\x20previous\x20API\x20access\x20key\x20to\x20generate\x20a\x20new\x20one','validatePasswordPattern','test','Sequelize','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','The\x20password\x20must\x20be\x20different\x20from\x20the\x20last\x20one.\x20Please\x20choose\x20another\x20one.','length','splice','unshift','join','promisify','sign','secret','payload','options','verify','randomBytes','toString','hex','floor','now','HS512','../../mysqldb','../../config/license/hardware','../encryptor','lodash','jsonwebtoken','express-jwt','composable-middleware','bluebird','moment','secrets','session','userpic','permissions','voicePause','chatPause','faxPause','openchannelPause','showWebBar','lastPauseAt','crudPermissions','allowmessenger','passwordResetAt','alias','phoneBarDnd','phoneBarEnableRecording','phoneBarEnableDtmfTone','phoneBarEnableSettings','phoneBarPrefixRequired','phoneBarRemoteControl','phoneBarRemoteControlPort','hotdesk','interface','userProfileId','privacyEnabled','settingsEnabled','wssPort','downloadOmnichannelInteractions','downloadAttachments','selectRecallMeCampaign','isChatInteractionAuthorized','isAuthenticated','use','user','ChatInteraction','params','closed','then','status','unmanaged','disposition','Unmanaged.','catch','startsWith','headers','authorization','Basic','User','find','name','authenticate','pass','json','Wrong\x20credentials.','Bearer','query','apikey','getUuid','sub','Setting','findOne','allowedLoginAttempts','blockDuration','isEqual','apiKeyNonce','nonce','API\x20access\x20key\x20is\x20not\x20valid\x20anymore','disabled','Invalid\x20API\x20access\x20key','blocked','blockedAt','add','minutes','Unknown\x20authorization\x20format','User\x20not\x20found.','User\x20object\x20not\x20found.'];(function(_0x2ce189,_0x4b6664){var _0x185d63=function(_0xf87688){while(--_0xf87688){_0x2ce189['push'](_0x2ce189['shift']());}};_0x185d63(++_0x4b6664);}(_0xccfb,0xb3));var _0xbccf=function(_0x3e5450,_0x3fcf22){_0x3e5450=_0x3e5450-0x0;var _0x2f7078=_0xccfb[_0x3e5450];return _0x2f7078;};'use strict';var db=require(_0xbccf('0x0'))['db'];var config=require('../../config/environment');var hardwareConf=require(_0xbccf('0x1'));var licenseUtil=require('../../config/license/util');var encryptor=require(_0xbccf('0x2'));var _=require(_0xbccf('0x3'));var jwt=require(_0xbccf('0x4'));var expressJwt=require(_0xbccf('0x5'));var compose=require(_0xbccf('0x6'));var basicAuth=require('basic-auth');var crypto=require('crypto');var BPromise=require(_0xbccf('0x7'));var util=require('util');var moment=require(_0xbccf('0x8'));var validateJwt=expressJwt({'secret':config[_0xbccf('0x9')][_0xbccf('0xa')]});var userAttributes=['id','role','fullname','name','internal','email',_0xbccf('0xb'),_0xbccf('0xc'),'md5secret',_0xbccf('0xd'),_0xbccf('0xe'),'mailPause',_0xbccf('0xf'),'smsPause',_0xbccf('0x10'),'pauseType',_0xbccf('0x11'),'lastLoginAt',_0xbccf('0x12'),_0xbccf('0x13'),_0xbccf('0x14'),_0xbccf('0x15'),_0xbccf('0x16'),'phoneBarAutoAnswer','phoneBarAutoAnswerDelay',_0xbccf('0x17'),_0xbccf('0x18'),_0xbccf('0x19'),_0xbccf('0x1a'),'phoneBarExpires',_0xbccf('0x1b'),_0xbccf('0x1c'),_0xbccf('0x1d'),_0xbccf('0x1e'),_0xbccf('0x1f'),_0xbccf('0x20'),_0xbccf('0x21'),_0xbccf('0x22'),_0xbccf('0x23'),'downloadVoiceRecordings',_0xbccf('0x24'),_0xbccf('0x25'),'ignorePauseForPreviewCalls',_0xbccf('0x26')];exports[_0xbccf('0x27')]=function(){return this[_0xbccf('0x28')](!![])[_0xbccf('0x29')](function(_0x5f526a,_0x1e142e,_0x50d881){if(_0x5f526a[_0xbccf('0x2a')]){_0x50d881();}else{return db[_0xbccf('0x2b')]['findOne']({'where':{'id':_0x5f526a[_0xbccf('0x2c')]['id']},'attributes':['id',_0xbccf('0x2d'),'disposition'],'raw':!![]})[_0xbccf('0x2e')](function(_0x281291){if(_0x281291&&_0x281291[_0xbccf('0x2d')]){return _0x1e142e[_0xbccf('0x2f')](_0x281291['disposition']===_0xbccf('0x30')?0x195:0x193)['json']({'message':_0x281291[_0xbccf('0x31')]===_0xbccf('0x30')?_0xbccf('0x32'):'Forbidden.'});}else{_0x50d881();}})[_0xbccf('0x33')](function(_0x3615fc){_0x50d881(_0x3615fc);});}});};exports['isAuthenticated']=function isAuthenticated(_0x5deb64){return compose()[_0xbccf('0x29')](function(_0x142304,_0xcfbe9b,_0x18e36f){var _0x312fc8;if(_0x142304['headers']['authorization']){if(_[_0xbccf('0x34')](_0x142304[_0xbccf('0x35')][_0xbccf('0x36')],_0xbccf('0x37'))){var _0x4993a7=basicAuth(_0x142304);db[_0xbccf('0x38')][_0xbccf('0x39')]({'where':{'name':_0x4993a7[_0xbccf('0x3a')]}})[_0xbccf('0x2e')](function(_0x2344af){if(!_0x2344af||!_0x2344af[_0xbccf('0x3b')](_0x4993a7[_0xbccf('0x3c')])){return _0xcfbe9b[_0xbccf('0x2f')](0x191)[_0xbccf('0x3d')]({'message':_0xbccf('0x3e')});}_0x142304[_0xbccf('0x2a')]={'id':_0x2344af['id']};_0x18e36f();})[_0xbccf('0x33')](function(_0x174dc8){_0x18e36f(_0x174dc8);});}else if(_[_0xbccf('0x34')](_0x142304[_0xbccf('0x35')]['authorization'],_0xbccf('0x3f'))){validateJwt(_0x142304,_0xcfbe9b,_0x18e36f);}else{if(_0x5deb64){_0x18e36f();}else{return _0xcfbe9b['status'](0x193)[_0xbccf('0x3d')]({'message':'Unknown\x20authorization\x20format'});}}}else if(_0x142304[_0xbccf('0x40')][_0xbccf('0x41')]){try{var _0x3ecb69={'audience':hardwareConf[_0xbccf('0x42')](),'issuer':hardwareConf[_0xbccf('0x42')]()};verifyJwt(_0x142304[_0xbccf('0x40')][_0xbccf('0x41')],_0x3ecb69)[_0xbccf('0x2e')](function(_0x1339fa){return db['User']['find']({'where':{'id':_0x1339fa[_0xbccf('0x43')]}})[_0xbccf('0x2e')](function(_0x28c5dc){_0x312fc8=_0x28c5dc;return db[_0xbccf('0x44')][_0xbccf('0x45')]({'where':{'id':0x1},'attributes':[_0xbccf('0x46'),_0xbccf('0x47')],'raw':!![]});})[_0xbccf('0x2e')](function(_0x19fd96){if(!_0x312fc8||!_[_0xbccf('0x48')](_0x312fc8[_0xbccf('0x49')],_0x1339fa[_0xbccf('0x4a')])){return _0xcfbe9b[_0xbccf('0x2f')](0x191)[_0xbccf('0x3d')]({'message':_0xbccf('0x4b')});}if(_0x312fc8[_0xbccf('0x4c')]){return _0xcfbe9b[_0xbccf('0x2f')](0x191)[_0xbccf('0x3d')]({'message':_0xbccf('0x4d')});}if(_0x312fc8[_0xbccf('0x4e')]){if(_0x19fd96[_0xbccf('0x47')]>0x0){if(moment(_0x312fc8[_0xbccf('0x4f')])[_0xbccf('0x50')](_0x19fd96[_0xbccf('0x47')],_0xbccf('0x51'))>moment()){return _0xcfbe9b['status'](0x191)[_0xbccf('0x3d')]({'message':_0xbccf('0x4d')});}}else{return _0xcfbe9b['status'](0x191)[_0xbccf('0x3d')]({'message':'Invalid\x20API\x20access\x20key'});}}_0x142304[_0xbccf('0x2a')]={'id':_0x312fc8['id']};_0x18e36f();});})[_0xbccf('0x33')](function(){return _0xcfbe9b[_0xbccf('0x2f')](0x191)['json']({'message':_0xbccf('0x4d')});});}catch(_0x49988e){_0x18e36f(_0x49988e);}}else if(_0x5deb64){_0x18e36f();}else{return _0xcfbe9b[_0xbccf('0x2f')](0x193)[_0xbccf('0x3d')]({'message':_0xbccf('0x52')});}})[_0xbccf('0x29')](function(_0x260e40,_0x162b57,_0x179aeb){if(_0x260e40['user']){db[_0xbccf('0x38')]['find']({'where':{'id':_0x260e40[_0xbccf('0x2a')]['id']},'attributes':userAttributes})['then'](function(_0x3cc4fc){if(!_0x3cc4fc){return _0x162b57[_0xbccf('0x2f')](0x194)[_0xbccf('0x3d')]({'message':_0xbccf('0x53')});}_0x260e40[_0xbccf('0x2a')]=_0x3cc4fc;_0x179aeb();})['catch'](function(_0x220c5e){_0x179aeb(_0x220c5e);});}else if(_0x5deb64){_0x179aeb();}else{return _0x162b57[_0xbccf('0x2f')](0x194)[_0xbccf('0x3d')]({'message':_0xbccf('0x54')});}});};exports[_0xbccf('0x55')]=function canUpdate(){return compose()[_0xbccf('0x29')](function(_0x615e4d,_0x1d2dc9,_0x5c86b4){return licenseUtil[_0xbccf('0x56')]()[_0xbccf('0x2e')](function(_0x25080e){if(_0x25080e[_0xbccf('0x57')]){_0x5c86b4();}else{return _0x1d2dc9[_0xbccf('0x2f')](0x193)['json']({'message':_0xbccf('0x58')});}})[_0xbccf('0x33')](function(_0x313830){_0x5c86b4(_0x313830);});});};exports[_0xbccf('0x59')]=function isWebrtcLicence(){return compose()['use'](function(_0x2f9534,_0x111800,_0x8f5686){return licenseUtil['getLicense']()[_0xbccf('0x2e')](function(_0x526016){if(_0x526016[_0xbccf('0x5a')]){_0x8f5686();}else{return _0x111800['status'](0x193)[_0xbccf('0x3d')]({'message':_0xbccf('0x58')});}})[_0xbccf('0x33')](function(_0x3fd60a){_0x8f5686(_0x3fd60a);});});};exports[_0xbccf('0x5b')]=function(_0x575140,_0x367594,_0xd6d897){_0x575140[_0xbccf('0x5b')]=!![];return _0xd6d897();};exports['signToken']=function signToken(_0x460bcd){return signJwt(_0x460bcd);};exports[_0xbccf('0x5c')]=function(_0x22bf53,_0x66223e){if(!_0x22bf53[_0xbccf('0x2a')]){return _0x66223e[_0xbccf('0x2f')](0x194)[_0xbccf('0x3d')]({'message':_0xbccf('0x5d')});}var _0x4b0c6a={'payload':{'id':_0x22bf53['user']['id'],'role':_0x22bf53[_0xbccf('0x2a')][_0xbccf('0x5e')]},'options':{'expiresIn':0x15180}};return signJwt(_0x4b0c6a)[_0xbccf('0x2e')](function(_0x52666c){_0x66223e[_0xbccf('0x5f')](_0xbccf('0x60'),_0x52666c);_0x66223e[_0xbccf('0x61')](_0xbccf('0x62'));})['catch'](function(_0x381168){return _0x66223e[_0xbccf('0x2f')](0x1f4)[_0xbccf('0x63')](_0x381168);});};exports[_0xbccf('0x64')]=function(_0x57fc52){if(_[_0xbccf('0x65')](_0x57fc52[_0xbccf('0x49')])||_[_0xbccf('0x65')](_0x57fc52[_0xbccf('0x66')])){return null;}else{return createJwt(_0x57fc52);}};exports[_0xbccf('0x67')]=function(_0x4f955c){_0x4f955c[_0xbccf('0x49')]=generateNonce();_0x4f955c['apiKeyIat']=generateIssuedAt();return createJwt(_0x4f955c);};exports[_0xbccf('0x68')]=function(_0x68303,_0x17e559){var _0x478add=_0x68303[_0xbccf('0x40')][_0xbccf('0x41')];if(_0x478add){var _0x37f6ba={'nonce':_0x17e559[_0xbccf('0x49')],'iat':_0x17e559[_0xbccf('0x66')],'audience':hardwareConf[_0xbccf('0x42')](),'issuer':hardwareConf[_0xbccf('0x42')]()};return verifyJwt(_0x478add,_0x37f6ba)['then'](function(){return generateApiKey(_0x17e559);});}else{throw{'message':_0xbccf('0x69')};}};exports[_0xbccf('0x6a')]=function(_0x4540b6){var _0x109581=new RegExp(/(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[?!@#\$%\^&\*~\-_=+[{\]\}])(?=.{8,})/);if(!_0x109581[_0xbccf('0x6b')](_0x4540b6))throw new db[(_0xbccf('0x6c'))][(_0xbccf('0x6d'))](_0xbccf('0x6e'));return;};exports[_0xbccf('0x6f')]=function(_0x2c5272,_0x1a51a6,_0x147a82){var _0x2c3b4e=encryptor[_0xbccf('0x70')](_0x1a51a6)[_0xbccf('0x71')](',');for(var _0x12d620=0x0;_0x12d620<_0x147a82;_0x12d620++){if(!_0x2c3b4e[_0x12d620])break;if(_0x2c5272['toLowerCase']()===_0x2c3b4e[_0x12d620][_0xbccf('0x72')]()){var _0x50e940=util['format']('The\x20password\x20must\x20be\x20different\x20from\x20the\x20previous\x20%d\x20passwords.\x20Please\x20choose\x20another\x20one.',_0x147a82);if(_0x147a82===0x1){_0x50e940=_0xbccf('0x73');}throw new db[(_0xbccf('0x6c'))]['ValidationError'](_0x50e940);}}return;};exports['updatePasswordsHistory']=function(_0x10088b,_0x24cf62){var _0xe18897=_0x24cf62?encryptor[_0xbccf('0x70')](_0x24cf62)[_0xbccf('0x71')](','):[];if(_0xe18897[_0xbccf('0x74')]===0x5){_0xe18897[_0xbccf('0x75')](-0x1,0x1);}_0xe18897[_0xbccf('0x76')](_0x10088b);return encryptor['encryptString'](_0xe18897[_0xbccf('0x77')](','));};function signJwt(_0x2facbb){var _0x4ccbb2=BPromise[_0xbccf('0x78')](jwt[_0xbccf('0x79')],{'context':jwt});var _0x1a87ca=_0x2facbb[_0xbccf('0x7a')]||config['secrets']['session'];return new BPromise(function(_0x126eb2,_0xb0721d){_0x4ccbb2(_0x2facbb[_0xbccf('0x7b')],_0x1a87ca,_0x2facbb[_0xbccf('0x7c')])['then'](function(_0xd129){_0x126eb2(_0xd129);})[_0xbccf('0x33')](function(_0x3fcf66){_0xb0721d(_0x3fcf66);});});}function verifyJwt(_0x13f927,_0x5c54d9,_0x1c5144){var _0x235f82=BPromise['promisify'](jwt[_0xbccf('0x7d')],{'context':jwt});var _0x1958c6=_0x1c5144||config['secrets']['session'];return new BPromise(function(_0x25ceab,_0x2132e1){_0x235f82(_0x13f927,_0x1958c6,_0x5c54d9)[_0xbccf('0x2e')](function(_0x378bf6){_0x25ceab(_0x378bf6);})['catch'](function(_0x17bf1d){_0x2132e1(_0x17bf1d);});});}function generateNonce(){return crypto[_0xbccf('0x7e')](0x10)[_0xbccf('0x7f')](_0xbccf('0x80'));}function generateIssuedAt(){return Math[_0xbccf('0x81')](Date[_0xbccf('0x82')]()/0x3e8)[_0xbccf('0x7f')]();}function createJwt(_0x57cfee){var _0x48fb54={'payload':{'iat':_0x57cfee['apiKeyIat'],'nonce':_0x57cfee[_0xbccf('0x49')]},'options':{'algorithm':_0xbccf('0x83'),'subject':_0x57cfee['id'][_0xbccf('0x7f')](),'issuer':hardwareConf['getUuid'](),'audience':hardwareConf[_0xbccf('0x42')]()}};return signJwt(_0x48fb54)['then'](function(_0x4bd4a9){return{'iat':_0x57cfee[_0xbccf('0x66')],'nonce':_0x57cfee[_0xbccf('0x49')],'token':_0x4bd4a9};});}