Built motion from commit 9e1399f3.|2.5.37
[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 _0x31f8=['randomBytes','hex','floor','now','HS512','../../config/environment','../../config/license/hardware','../../config/license/util','../encryptor','lodash','jsonwebtoken','express-jwt','composable-middleware','basic-auth','bluebird','util','moment','session','role','fullname','name','userpic','md5secret','chatPause','mailPause','faxPause','smsPause','openchannelPause','pauseType','showWebBar','lastLoginAt','lastPauseAt','passwordResetAt','phoneBarAutoAnswerDelay','phoneBarDnd','phoneBarEnableDtmfTone','phoneBarEnableSettings','phoneBarPrefixRequired','phoneBarRemoteControl','interface','userProfileId','privacyEnabled','wssPort','downloadVoiceRecordings','downloadOmnichannelInteractions','ignorePauseForPreviewCalls','selectRecallMeCampaign','isChatInteractionAuthorized','user','ChatInteraction','params','closed','then','unmanaged','disposition','Unmanaged.','Forbidden.','isAuthenticated','authorization','startsWith','headers','Basic','User','find','authenticate','pass','json','Wrong\x20credentials.','catch','status','Unknown\x20authorization\x20format','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','User\x20not\x20found.','User\x20object\x20not\x20found.','canUpdate','use','getLicense','update','Forbidden','webrtc','isMiddleware','signToken','setTokenCookie','It\x20looks\x20like\x20you\x20aren\x27t\x20logged\x20in,\x20please\x20try\x20again.','cookie','motion.token','redirect','retrieveApiKey','isNil','apiKeyIat','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\x20previous\x20%d\x20passwords.\x20Please\x20choose\x20another\x20one.','The\x20password\x20must\x20be\x20different\x20from\x20the\x20last\x20one.\x20Please\x20choose\x20another\x20one.','length','splice','encryptString','join','promisify','sign','secrets','payload','options','verify'];(function(_0x51e804,_0x4c4b89){var _0x40b251=function(_0x556ba1){while(--_0x556ba1){_0x51e804['push'](_0x51e804['shift']());}};_0x40b251(++_0x4c4b89);}(_0x31f8,0x86));var _0x831f=function(_0x321bb2,_0x202794){_0x321bb2=_0x321bb2-0x0;var _0x9c12ed=_0x31f8[_0x321bb2];return _0x9c12ed;};'use strict';var db=require('../../mysqldb')['db'];var config=require(_0x831f('0x0'));var hardwareConf=require(_0x831f('0x1'));var licenseUtil=require(_0x831f('0x2'));var encryptor=require(_0x831f('0x3'));var _=require(_0x831f('0x4'));var jwt=require(_0x831f('0x5'));var expressJwt=require(_0x831f('0x6'));var compose=require(_0x831f('0x7'));var basicAuth=require(_0x831f('0x8'));var crypto=require('crypto');var BPromise=require(_0x831f('0x9'));var util=require(_0x831f('0xa'));var moment=require(_0x831f('0xb'));var validateJwt=expressJwt({'secret':config['secrets'][_0x831f('0xc')]});var userAttributes=['id',_0x831f('0xd'),_0x831f('0xe'),_0x831f('0xf'),'internal','email',_0x831f('0x10'),'permissions',_0x831f('0x11'),'voicePause',_0x831f('0x12'),_0x831f('0x13'),_0x831f('0x14'),_0x831f('0x15'),_0x831f('0x16'),_0x831f('0x17'),_0x831f('0x18'),_0x831f('0x19'),_0x831f('0x1a'),'crudPermissions','allowmessenger',_0x831f('0x1b'),'alias','phoneBarAutoAnswer',_0x831f('0x1c'),_0x831f('0x1d'),'phoneBarEnableRecording',_0x831f('0x1e'),_0x831f('0x1f'),'phoneBarExpires',_0x831f('0x20'),_0x831f('0x21'),'phoneBarRemoteControlPort','hotdesk',_0x831f('0x22'),_0x831f('0x23'),_0x831f('0x24'),'settingsEnabled',_0x831f('0x25'),_0x831f('0x26'),_0x831f('0x27'),'downloadAttachments',_0x831f('0x28'),_0x831f('0x29')];exports[_0x831f('0x2a')]=function(){return this['isAuthenticated'](!![])['use'](function(_0x2f8368,_0x1e872f,_0x63836a){if(_0x2f8368[_0x831f('0x2b')]){_0x63836a();}else{return db[_0x831f('0x2c')]['findOne']({'where':{'id':_0x2f8368[_0x831f('0x2d')]['id']},'attributes':['id',_0x831f('0x2e'),'disposition'],'raw':!![]})[_0x831f('0x2f')](function(_0x1e98f1){if(_0x1e98f1&&_0x1e98f1['closed']){return _0x1e872f['status'](_0x1e98f1['disposition']===_0x831f('0x30')?0x195:0x193)['json']({'message':_0x1e98f1[_0x831f('0x31')]===_0x831f('0x30')?_0x831f('0x32'):_0x831f('0x33')});}else{_0x63836a();}})['catch'](function(_0x2a5d53){_0x63836a(_0x2a5d53);});}});};exports[_0x831f('0x34')]=function isAuthenticated(_0x1998f3){return compose()['use'](function(_0x26bf62,_0x5b36a0,_0x2b7c8d){var _0x56a323;if(_0x26bf62['headers'][_0x831f('0x35')]){if(_[_0x831f('0x36')](_0x26bf62[_0x831f('0x37')][_0x831f('0x35')],_0x831f('0x38'))){var _0x26fabd=basicAuth(_0x26bf62);db[_0x831f('0x39')][_0x831f('0x3a')]({'where':{'name':_0x26fabd[_0x831f('0xf')]}})[_0x831f('0x2f')](function(_0x7209e6){if(!_0x7209e6||!_0x7209e6[_0x831f('0x3b')](_0x26fabd[_0x831f('0x3c')])){return _0x5b36a0['status'](0x191)[_0x831f('0x3d')]({'message':_0x831f('0x3e')});}_0x26bf62['user']={'id':_0x7209e6['id']};_0x2b7c8d();})[_0x831f('0x3f')](function(_0x18ad69){_0x2b7c8d(_0x18ad69);});}else if(_[_0x831f('0x36')](_0x26bf62['headers'][_0x831f('0x35')],'Bearer')){validateJwt(_0x26bf62,_0x5b36a0,_0x2b7c8d);}else{if(_0x1998f3){_0x2b7c8d();}else{return _0x5b36a0[_0x831f('0x40')](0x193)[_0x831f('0x3d')]({'message':_0x831f('0x41')});}}}else if(_0x26bf62[_0x831f('0x42')][_0x831f('0x43')]){try{var _0x5cfa92={'audience':hardwareConf['getUuid'](),'issuer':hardwareConf[_0x831f('0x44')]()};verifyJwt(_0x26bf62[_0x831f('0x42')]['apikey'],_0x5cfa92)[_0x831f('0x2f')](function(_0x5693af){return db['User']['find']({'where':{'id':_0x5693af[_0x831f('0x45')]}})[_0x831f('0x2f')](function(_0x127776){_0x56a323=_0x127776;return db[_0x831f('0x46')][_0x831f('0x47')]({'where':{'id':0x1},'attributes':[_0x831f('0x48'),_0x831f('0x49')],'raw':!![]});})['then'](function(_0x3f338c){if(!_0x56a323||!_[_0x831f('0x4a')](_0x56a323[_0x831f('0x4b')],_0x5693af[_0x831f('0x4c')])){return _0x5b36a0[_0x831f('0x40')](0x191)[_0x831f('0x3d')]({'message':_0x831f('0x4d')});}if(_0x56a323[_0x831f('0x4e')]){return _0x5b36a0[_0x831f('0x40')](0x191)[_0x831f('0x3d')]({'message':_0x831f('0x4f')});}if(_0x56a323[_0x831f('0x50')]){if(_0x3f338c['blockDuration']>0x0){if(moment(_0x56a323[_0x831f('0x51')])[_0x831f('0x52')](_0x3f338c[_0x831f('0x49')],'minutes')>moment()){return _0x5b36a0[_0x831f('0x40')](0x191)['json']({'message':'Invalid\x20API\x20access\x20key'});}}else{return _0x5b36a0[_0x831f('0x40')](0x191)[_0x831f('0x3d')]({'message':_0x831f('0x4f')});}}_0x26bf62[_0x831f('0x2b')]={'id':_0x56a323['id']};_0x2b7c8d();});})[_0x831f('0x3f')](function(){return _0x5b36a0[_0x831f('0x40')](0x191)['json']({'message':_0x831f('0x4f')});});}catch(_0x416419){_0x2b7c8d(_0x416419);}}else if(_0x1998f3){_0x2b7c8d();}else{return _0x5b36a0[_0x831f('0x40')](0x193)[_0x831f('0x3d')]({'message':_0x831f('0x41')});}})['use'](function(_0x19518c,_0x4c865d,_0x181218){if(_0x19518c['user']){db[_0x831f('0x39')][_0x831f('0x3a')]({'where':{'id':_0x19518c[_0x831f('0x2b')]['id']},'attributes':userAttributes})['then'](function(_0x28f68e){if(!_0x28f68e){return _0x4c865d['status'](0x194)['json']({'message':_0x831f('0x53')});}_0x19518c[_0x831f('0x2b')]=_0x28f68e;_0x181218();})[_0x831f('0x3f')](function(_0x2e766d){_0x181218(_0x2e766d);});}else if(_0x1998f3){_0x181218();}else{return _0x4c865d[_0x831f('0x40')](0x194)[_0x831f('0x3d')]({'message':_0x831f('0x54')});}});};exports[_0x831f('0x55')]=function canUpdate(){return compose()[_0x831f('0x56')](function(_0x115d67,_0x57b069,_0x292035){return licenseUtil[_0x831f('0x57')]()[_0x831f('0x2f')](function(_0x3ca6bc){if(_0x3ca6bc[_0x831f('0x58')]){_0x292035();}else{return _0x57b069['status'](0x193)[_0x831f('0x3d')]({'message':_0x831f('0x59')});}})[_0x831f('0x3f')](function(_0x112b0a){_0x292035(_0x112b0a);});});};exports['isWebrtcLicence']=function isWebrtcLicence(){return compose()['use'](function(_0x4c9f9d,_0x5063d0,_0x1a1c6a){return licenseUtil[_0x831f('0x57')]()[_0x831f('0x2f')](function(_0x33e60e){if(_0x33e60e[_0x831f('0x5a')]){_0x1a1c6a();}else{return _0x5063d0[_0x831f('0x40')](0x193)[_0x831f('0x3d')]({'message':_0x831f('0x59')});}})[_0x831f('0x3f')](function(_0x2e697d){_0x1a1c6a(_0x2e697d);});});};exports[_0x831f('0x5b')]=function(_0x47d2fd,_0x18a248,_0x4c7c44){_0x47d2fd['isMiddleware']=!![];return _0x4c7c44();};exports[_0x831f('0x5c')]=function signToken(_0x26085d){return signJwt(_0x26085d);};exports[_0x831f('0x5d')]=function(_0x29ea29,_0x5b4cd9){if(!_0x29ea29[_0x831f('0x2b')]){return _0x5b4cd9[_0x831f('0x40')](0x194)[_0x831f('0x3d')]({'message':_0x831f('0x5e')});}var _0x9712b={'payload':{'id':_0x29ea29['user']['id'],'role':_0x29ea29[_0x831f('0x2b')][_0x831f('0xd')]},'options':{'expiresIn':0x15180}};return signJwt(_0x9712b)[_0x831f('0x2f')](function(_0x4291d2){_0x5b4cd9[_0x831f('0x5f')](_0x831f('0x60'),_0x4291d2);_0x5b4cd9[_0x831f('0x61')]('/dashboards/general');})[_0x831f('0x3f')](function(_0x22d51c){return _0x5b4cd9['status'](0x1f4)['send'](_0x22d51c);});};exports[_0x831f('0x62')]=function(_0x33063d){if(_['isNil'](_0x33063d[_0x831f('0x4b')])||_[_0x831f('0x63')](_0x33063d[_0x831f('0x64')])){return null;}else{return createJwt(_0x33063d);}};exports['generateApiKey']=function(_0x1d6d54){_0x1d6d54[_0x831f('0x4b')]=generateNonce();_0x1d6d54[_0x831f('0x64')]=generateIssuedAt();return createJwt(_0x1d6d54);};exports[_0x831f('0x65')]=function(_0x49d733,_0x3ae943){var _0x12d3c4=_0x49d733[_0x831f('0x42')][_0x831f('0x43')];if(_0x12d3c4){var _0x35b9a4={'nonce':_0x3ae943[_0x831f('0x4b')],'iat':_0x3ae943['apiKeyIat'],'audience':hardwareConf[_0x831f('0x44')](),'issuer':hardwareConf[_0x831f('0x44')]()};return verifyJwt(_0x12d3c4,_0x35b9a4)['then'](function(){return generateApiKey(_0x3ae943);});}else{throw{'message':_0x831f('0x66')};}};exports[_0x831f('0x67')]=function(_0x6d937b){var _0x1e310b=new RegExp(/(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[?!@#\$%\^&\*~\-_=+[{\]\}])(?=.{8,})/);if(!_0x1e310b[_0x831f('0x68')](_0x6d937b))throw new db[(_0x831f('0x69'))][(_0x831f('0x6a'))](_0x831f('0x6b'));return;};exports[_0x831f('0x6c')]=function(_0x1efe75,_0x3e6ed2,_0x136d7c){var _0x560b78=encryptor[_0x831f('0x6d')](_0x3e6ed2)[_0x831f('0x6e')](',');for(var _0x8325bb=0x0;_0x8325bb<_0x136d7c;_0x8325bb++){if(!_0x560b78[_0x8325bb])break;if(_0x1efe75['toLowerCase']()===_0x560b78[_0x8325bb][_0x831f('0x6f')]()){var _0x5cf93d=util['format'](_0x831f('0x70'),_0x136d7c);if(_0x136d7c===0x1){_0x5cf93d=_0x831f('0x71');}throw new db['Sequelize'][(_0x831f('0x6a'))](_0x5cf93d);}}return;};exports['updatePasswordsHistory']=function(_0x22ba7c,_0x1b45d8){var _0x39ff93=_0x1b45d8?encryptor['decryptString'](_0x1b45d8)['split'](','):[];if(_0x39ff93[_0x831f('0x72')]===0x5){_0x39ff93[_0x831f('0x73')](-0x1,0x1);}_0x39ff93['unshift'](_0x22ba7c);return encryptor[_0x831f('0x74')](_0x39ff93[_0x831f('0x75')](','));};function signJwt(_0x2409b3){var _0x32aec6=BPromise[_0x831f('0x76')](jwt[_0x831f('0x77')],{'context':jwt});var _0x3111b0=_0x2409b3['secret']||config[_0x831f('0x78')][_0x831f('0xc')];return new BPromise(function(_0x80288e,_0x279d6e){_0x32aec6(_0x2409b3[_0x831f('0x79')],_0x3111b0,_0x2409b3[_0x831f('0x7a')])['then'](function(_0xf481c0){_0x80288e(_0xf481c0);})['catch'](function(_0x40f601){_0x279d6e(_0x40f601);});});}function verifyJwt(_0x1162f8,_0x349430,_0x17d3f4){var _0x506576=BPromise[_0x831f('0x76')](jwt[_0x831f('0x7b')],{'context':jwt});var _0x513f79=_0x17d3f4||config[_0x831f('0x78')][_0x831f('0xc')];return new BPromise(function(_0x4492dd,_0x20ebc6){_0x506576(_0x1162f8,_0x513f79,_0x349430)['then'](function(_0x216924){_0x4492dd(_0x216924);})[_0x831f('0x3f')](function(_0x104a12){_0x20ebc6(_0x104a12);});});}function generateNonce(){return crypto[_0x831f('0x7c')](0x10)['toString'](_0x831f('0x7d'));}function generateIssuedAt(){return Math[_0x831f('0x7e')](Date[_0x831f('0x7f')]()/0x3e8)['toString']();}function createJwt(_0x5a6936){var _0x15f1b7={'payload':{'iat':_0x5a6936[_0x831f('0x64')],'nonce':_0x5a6936[_0x831f('0x4b')]},'options':{'algorithm':_0x831f('0x80'),'subject':_0x5a6936['id']['toString'](),'issuer':hardwareConf['getUuid'](),'audience':hardwareConf[_0x831f('0x44')]()}};return signJwt(_0x15f1b7)['then'](function(_0x555997){return{'iat':_0x5a6936[_0x831f('0x64')],'nonce':_0x5a6936[_0x831f('0x4b')],'token':_0x555997};});}