Built motion from commit (unavailable).|2.5.15
[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 _0x2372=['join','promisify','sign','secret','session','payload','options','verify','toString','hex','floor','now','HS512','../../mysqldb','../../config/environment','../../config/license/util','../encryptor','lodash','composable-middleware','basic-auth','bluebird','util','secrets','role','fullname','internal','email','userpic','permissions','md5secret','voicePause','chatPause','mailPause','faxPause','pauseType','lastPauseAt','crudPermissions','allowmessenger','passwordResetAt','phoneBarAutoAnswer','phoneBarAutoAnswerDelay','phoneBarDnd','phoneBarEnableDtmfTone','phoneBarPrefixRequired','phoneBarRemoteControl','phoneBarRemoteControlPort','hotdesk','interface','userProfileId','settingsEnabled','isChatInteractionAuthorized','isAuthenticated','use','user','ChatInteraction','findOne','params','closed','disposition','then','status','unmanaged','json','Forbidden.','headers','authorization','startsWith','Basic','User','find','name','authenticate','Wrong\x20credentials.','catch','Bearer','Unknown\x20authorization\x20format','query','getUuid','sub','Setting','blockDuration','isEqual','apiKeyNonce','nonce','API\x20access\x20key\x20is\x20not\x20valid\x20anymore','disabled','blockedAt','add','minutes','Invalid\x20API\x20access\x20key','User\x20object\x20not\x20found.','update','isMiddleware','signToken','setTokenCookie','It\x20looks\x20like\x20you\x20aren\x27t\x20logged\x20in,\x20please\x20try\x20again.','motion.token','send','retrieveApiKey','isNil','apiKeyIat','generateApiKey','regenerateApiKey','apikey','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','format','updatePasswordsHistory','length','unshift','encryptString'];(function(_0x8de54f,_0x271987){var _0x14cba4=function(_0x58ed41){while(--_0x58ed41){_0x8de54f['push'](_0x8de54f['shift']());}};_0x14cba4(++_0x271987);}(_0x2372,0x16f));var _0x2237=function(_0x16062f,_0xe5e781){_0x16062f=_0x16062f-0x0;var _0x22889c=_0x2372[_0x16062f];return _0x22889c;};'use strict';var db=require(_0x2237('0x0'))['db'];var config=require(_0x2237('0x1'));var hardwareConf=require('../../config/license/hardware');var licenseUtil=require(_0x2237('0x2'));var encryptor=require(_0x2237('0x3'));var _=require(_0x2237('0x4'));var jwt=require('jsonwebtoken');var expressJwt=require('express-jwt');var compose=require(_0x2237('0x5'));var basicAuth=require(_0x2237('0x6'));var crypto=require('crypto');var BPromise=require(_0x2237('0x7'));var util=require(_0x2237('0x8'));var moment=require('moment');var validateJwt=expressJwt({'secret':config[_0x2237('0x9')]['session']});var userAttributes=['id',_0x2237('0xa'),_0x2237('0xb'),'name',_0x2237('0xc'),_0x2237('0xd'),_0x2237('0xe'),_0x2237('0xf'),_0x2237('0x10'),_0x2237('0x11'),_0x2237('0x12'),_0x2237('0x13'),_0x2237('0x14'),'smsPause','openchannelPause',_0x2237('0x15'),'showWebBar','lastLoginAt',_0x2237('0x16'),_0x2237('0x17'),_0x2237('0x18'),_0x2237('0x19'),'alias',_0x2237('0x1a'),_0x2237('0x1b'),_0x2237('0x1c'),'phoneBarEnableRecording',_0x2237('0x1d'),'phoneBarEnableSettings','phoneBarExpires',_0x2237('0x1e'),_0x2237('0x1f'),_0x2237('0x20'),_0x2237('0x21'),_0x2237('0x22'),_0x2237('0x23'),_0x2237('0x24')];exports[_0x2237('0x25')]=function(){return this[_0x2237('0x26')](!![])[_0x2237('0x27')](function(_0x32d19b,_0x199d47,_0x4b58ac){if(_0x32d19b[_0x2237('0x28')]){_0x4b58ac();}else{return db[_0x2237('0x29')][_0x2237('0x2a')]({'where':{'id':_0x32d19b[_0x2237('0x2b')]['id']},'attributes':['id',_0x2237('0x2c'),_0x2237('0x2d')],'raw':!![]})[_0x2237('0x2e')](function(_0xb6e375){if(_0xb6e375&&_0xb6e375[_0x2237('0x2c')]){return _0x199d47[_0x2237('0x2f')](_0xb6e375[_0x2237('0x2d')]===_0x2237('0x30')?0x195:0x193)[_0x2237('0x31')]({'message':_0xb6e375[_0x2237('0x2d')]===_0x2237('0x30')?'Unmanaged.':_0x2237('0x32')});}else{_0x4b58ac();}})['catch'](function(_0x365abc){_0x4b58ac(_0x365abc);});}});};exports[_0x2237('0x26')]=function isAuthenticated(_0xaef43b){return compose()[_0x2237('0x27')](function(_0x390624,_0x12c588,_0x427271){var _0x320b88;if(_0x390624[_0x2237('0x33')][_0x2237('0x34')]){if(_[_0x2237('0x35')](_0x390624[_0x2237('0x33')][_0x2237('0x34')],_0x2237('0x36'))){var _0x5e516b=basicAuth(_0x390624);db[_0x2237('0x37')][_0x2237('0x38')]({'where':{'name':_0x5e516b[_0x2237('0x39')]}})[_0x2237('0x2e')](function(_0x4f7bd3){if(!_0x4f7bd3||!_0x4f7bd3[_0x2237('0x3a')](_0x5e516b['pass'])){return _0x12c588[_0x2237('0x2f')](0x191)['json']({'message':_0x2237('0x3b')});}_0x390624[_0x2237('0x28')]={'id':_0x4f7bd3['id']};_0x427271();})[_0x2237('0x3c')](function(_0x3b933f){_0x427271(_0x3b933f);});}else if(_[_0x2237('0x35')](_0x390624['headers'][_0x2237('0x34')],_0x2237('0x3d'))){validateJwt(_0x390624,_0x12c588,_0x427271);}else{if(_0xaef43b){_0x427271();}else{return _0x12c588['status'](0x193)[_0x2237('0x31')]({'message':_0x2237('0x3e')});}}}else if(_0x390624[_0x2237('0x3f')]['apikey']){try{var _0x350bc1={'audience':hardwareConf['getUuid'](),'issuer':hardwareConf[_0x2237('0x40')]()};verifyJwt(_0x390624[_0x2237('0x3f')]['apikey'],_0x350bc1)[_0x2237('0x2e')](function(_0x3ad9aa){return db['User']['find']({'where':{'id':_0x3ad9aa[_0x2237('0x41')]}})[_0x2237('0x2e')](function(_0x5a0599){_0x320b88=_0x5a0599;return db[_0x2237('0x42')][_0x2237('0x2a')]({'where':{'id':0x1},'attributes':['allowedLoginAttempts',_0x2237('0x43')],'raw':!![]});})[_0x2237('0x2e')](function(_0x29302f){if(!_0x320b88||!_[_0x2237('0x44')](_0x320b88[_0x2237('0x45')],_0x3ad9aa[_0x2237('0x46')])){return _0x12c588[_0x2237('0x2f')](0x191)[_0x2237('0x31')]({'message':_0x2237('0x47')});}if(_0x320b88[_0x2237('0x48')]){return _0x12c588[_0x2237('0x2f')](0x191)['json']({'message':'Invalid\x20API\x20access\x20key'});}if(_0x320b88['blocked']){if(_0x29302f[_0x2237('0x43')]>0x0){if(moment(_0x320b88[_0x2237('0x49')])[_0x2237('0x4a')](_0x29302f[_0x2237('0x43')],_0x2237('0x4b'))>moment()){return _0x12c588[_0x2237('0x2f')](0x191)['json']({'message':_0x2237('0x4c')});}}else{return _0x12c588['status'](0x191)['json']({'message':_0x2237('0x4c')});}}_0x390624[_0x2237('0x28')]={'id':_0x320b88['id']};_0x427271();});})['catch'](function(){return _0x12c588[_0x2237('0x2f')](0x191)[_0x2237('0x31')]({'message':_0x2237('0x4c')});});}catch(_0x4daaf6){_0x427271(_0x4daaf6);}}else if(_0xaef43b){_0x427271();}else{return _0x12c588[_0x2237('0x2f')](0x193)[_0x2237('0x31')]({'message':'Unknown\x20authorization\x20format'});}})[_0x2237('0x27')](function(_0x113e58,_0x2e2a66,_0x4e6bfd){if(_0x113e58[_0x2237('0x28')]){db['User'][_0x2237('0x38')]({'where':{'id':_0x113e58[_0x2237('0x28')]['id']},'attributes':userAttributes})[_0x2237('0x2e')](function(_0x2bcbed){if(!_0x2bcbed){return _0x2e2a66[_0x2237('0x2f')](0x194)[_0x2237('0x31')]({'message':'User\x20not\x20found.'});}_0x113e58[_0x2237('0x28')]=_0x2bcbed;_0x4e6bfd();})[_0x2237('0x3c')](function(_0x5c37c0){_0x4e6bfd(_0x5c37c0);});}else if(_0xaef43b){_0x4e6bfd();}else{return _0x2e2a66[_0x2237('0x2f')](0x194)[_0x2237('0x31')]({'message':_0x2237('0x4d')});}});};exports['canUpdate']=function canUpdate(){return compose()[_0x2237('0x27')](function(_0x5b3239,_0x861dd9,_0x199bad){return licenseUtil['getLicense']()['then'](function(_0xdb6a6d){if(_0xdb6a6d[_0x2237('0x4e')]){_0x199bad();}else{return _0x861dd9['status'](0x193)[_0x2237('0x31')]({'message':'Forbidden'});}})[_0x2237('0x3c')](function(_0x1dde55){_0x199bad(_0x1dde55);});});};exports[_0x2237('0x4f')]=function(_0x1053a1,_0x28d33c,_0x2d2a55){_0x1053a1[_0x2237('0x4f')]=!![];return _0x2d2a55();};exports[_0x2237('0x50')]=function signToken(_0x578a17){return signJwt(_0x578a17);};exports[_0x2237('0x51')]=function(_0x585569,_0x3120f1){if(!_0x585569[_0x2237('0x28')]){return _0x3120f1[_0x2237('0x2f')](0x194)[_0x2237('0x31')]({'message':_0x2237('0x52')});}var _0x27a5c4={'payload':{'id':_0x585569[_0x2237('0x28')]['id'],'role':_0x585569['user'][_0x2237('0xa')]},'options':{'expiresIn':0x15180}};return signJwt(_0x27a5c4)[_0x2237('0x2e')](function(_0x269224){_0x3120f1['cookie'](_0x2237('0x53'),_0x269224);_0x3120f1['redirect']('/dashboards/general');})[_0x2237('0x3c')](function(_0x5312d5){return _0x3120f1[_0x2237('0x2f')](0x1f4)[_0x2237('0x54')](_0x5312d5);});};exports[_0x2237('0x55')]=function(_0x225f64){if(_['isNil'](_0x225f64['apiKeyNonce'])||_[_0x2237('0x56')](_0x225f64[_0x2237('0x57')])){return null;}else{return createJwt(_0x225f64);}};exports[_0x2237('0x58')]=function(_0x6ce79c){_0x6ce79c[_0x2237('0x45')]=generateNonce();_0x6ce79c[_0x2237('0x57')]=generateIssuedAt();return createJwt(_0x6ce79c);};exports[_0x2237('0x59')]=function(_0x4b09d0,_0x305300){var _0x5ba7ec=_0x4b09d0[_0x2237('0x3f')][_0x2237('0x5a')];if(_0x5ba7ec){var _0x56473b={'nonce':_0x305300[_0x2237('0x45')],'iat':_0x305300[_0x2237('0x57')],'audience':hardwareConf['getUuid'](),'issuer':hardwareConf[_0x2237('0x40')]()};return verifyJwt(_0x5ba7ec,_0x56473b)[_0x2237('0x2e')](function(){return generateApiKey(_0x305300);});}else{throw{'message':_0x2237('0x5b')};}};exports[_0x2237('0x5c')]=function(_0x1712ba){var _0x4ad820=new RegExp(/(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[?!@#\$%\^&\*~\-_=+[{\]\}])(?=.{8,})/);if(!_0x4ad820[_0x2237('0x5d')](_0x1712ba))throw new db[(_0x2237('0x5e'))][(_0x2237('0x5f'))](_0x2237('0x60'));return;};exports[_0x2237('0x61')]=function(_0x315fa4,_0xba1d,_0x1f46a6){var _0x39e90a=encryptor[_0x2237('0x62')](_0xba1d)[_0x2237('0x63')](',');for(var _0x500d1c=0x0;_0x500d1c<_0x1f46a6;_0x500d1c++){if(!_0x39e90a[_0x500d1c])break;if(_0x315fa4['toLowerCase']()===_0x39e90a[_0x500d1c]['toLowerCase']()){var _0x1af499=util[_0x2237('0x64')]('The\x20password\x20must\x20be\x20different\x20from\x20the\x20previous\x20%d\x20passwords.\x20Please\x20choose\x20another\x20one.',_0x1f46a6);if(_0x1f46a6===0x1){_0x1af499='The\x20password\x20must\x20be\x20different\x20from\x20the\x20last\x20one.\x20Please\x20choose\x20another\x20one.';}throw new db[(_0x2237('0x5e'))][(_0x2237('0x5f'))](_0x1af499);}}return;};exports[_0x2237('0x65')]=function(_0x3f7c79,_0x419a70){var _0x4726e1=_0x419a70?encryptor[_0x2237('0x62')](_0x419a70)[_0x2237('0x63')](','):[];if(_0x4726e1[_0x2237('0x66')]===0x5){_0x4726e1['splice'](-0x1,0x1);}_0x4726e1[_0x2237('0x67')](_0x3f7c79);return encryptor[_0x2237('0x68')](_0x4726e1[_0x2237('0x69')](','));};function signJwt(_0x24e9cf){var _0x36f7f8=BPromise[_0x2237('0x6a')](jwt[_0x2237('0x6b')],{'context':jwt});var _0x13ccf9=_0x24e9cf[_0x2237('0x6c')]||config[_0x2237('0x9')][_0x2237('0x6d')];return new BPromise(function(_0x5d1630,_0x32ea91){_0x36f7f8(_0x24e9cf[_0x2237('0x6e')],_0x13ccf9,_0x24e9cf[_0x2237('0x6f')])[_0x2237('0x2e')](function(_0x4c464b){_0x5d1630(_0x4c464b);})[_0x2237('0x3c')](function(_0x59f1d5){_0x32ea91(_0x59f1d5);});});}function verifyJwt(_0x140c20,_0x1d1c94,_0x278b87){var _0x1814a5=BPromise['promisify'](jwt[_0x2237('0x70')],{'context':jwt});var _0x279d25=_0x278b87||config[_0x2237('0x9')]['session'];return new BPromise(function(_0x31cbde,_0x4040e5){_0x1814a5(_0x140c20,_0x279d25,_0x1d1c94)[_0x2237('0x2e')](function(_0x57753f){_0x31cbde(_0x57753f);})[_0x2237('0x3c')](function(_0x6efdb6){_0x4040e5(_0x6efdb6);});});}function generateNonce(){return crypto['randomBytes'](0x10)[_0x2237('0x71')](_0x2237('0x72'));}function generateIssuedAt(){return Math[_0x2237('0x73')](Date[_0x2237('0x74')]()/0x3e8)[_0x2237('0x71')]();}function createJwt(_0x3c053b){var _0x52ad37={'payload':{'iat':_0x3c053b[_0x2237('0x57')],'nonce':_0x3c053b[_0x2237('0x45')]},'options':{'algorithm':_0x2237('0x75'),'subject':_0x3c053b['id'][_0x2237('0x71')](),'issuer':hardwareConf[_0x2237('0x40')](),'audience':hardwareConf[_0x2237('0x40')]()}};return signJwt(_0x52ad37)['then'](function(_0x2b4007){return{'iat':_0x3c053b[_0x2237('0x57')],'nonce':_0x3c053b[_0x2237('0x45')],'token':_0x2b4007};});}