Built motion from commit (unavailable).|2.4.20
[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 _0x573f=['hex','floor','now','HS512','../../mysqldb','../../config/environment','jsonwebtoken','express-jwt','composable-middleware','basic-auth','crypto','bluebird','role','fullname','name','internal','email','userpic','permissions','md5secret','voicePause','chatPause','mailPause','smsPause','openchannelPause','lastPauseAt','crudPermissions','allowmessenger','alias','phoneBarAutoAnswer','phoneBarAutoAnswerDelay','phoneBarEnableDtmfTone','phoneBarEnableSettings','phoneBarExpires','phoneBarPrefixRequired','phoneBarRemoteControlPort','hotdesk','interface','userProfileId','isChatInteractionAuthorized','use','user','ChatInteraction','params','closed','disposition','then','unmanaged','json','Unmanaged.','Forbidden.','catch','isAuthenticated','headers','authorization','startsWith','find','authenticate','pass','Wrong\x20credentials.','Bearer','status','Unknown\x20authorization\x20format','query','apikey','getUuid','User','sub','isEqual','apiKeyNonce','API\x20access\x20key\x20is\x20not\x20valid\x20anymore','User\x20not\x20found.','User\x20object\x20not\x20found.','../../config/license/util','getLicense','update','Forbidden','isMiddleware','signToken','setTokenCookie','It\x20looks\x20like\x20you\x20aren\x27t\x20logged\x20in,\x20please\x20try\x20again.','redirect','/dashboards/general','isNil','apiKeyIat','regenerateApiKey','Please\x20use\x20the\x20previous\x20API\x20access\x20key\x20to\x20generate\x20a\x20new\x20one','secret','secrets','payload','options','promisify','verify','session','randomBytes','toString'];(function(_0x3dd15e,_0x1f0015){var _0x231fd0=function(_0x4f680a){while(--_0x4f680a){_0x3dd15e['push'](_0x3dd15e['shift']());}};_0x231fd0(++_0x1f0015);}(_0x573f,0x64));var _0xf573=function(_0x1b1e6a,_0x2856a3){_0x1b1e6a=_0x1b1e6a-0x0;var _0x5eff52=_0x573f[_0x1b1e6a];return _0x5eff52;};'use strict';var db=require(_0xf573('0x0'))['db'];var config=require(_0xf573('0x1'));var hardwareConf=require('../../config/license/hardware');var _=require('lodash');var jwt=require(_0xf573('0x2'));var expressJwt=require(_0xf573('0x3'));var compose=require(_0xf573('0x4'));var basicAuth=require(_0xf573('0x5'));var crypto=require(_0xf573('0x6'));var BPromise=require(_0xf573('0x7'));var validateJwt=expressJwt({'secret':config['secrets']['session']});var userAttributes=['id',_0xf573('0x8'),_0xf573('0x9'),_0xf573('0xa'),_0xf573('0xb'),_0xf573('0xc'),_0xf573('0xd'),_0xf573('0xe'),_0xf573('0xf'),_0xf573('0x10'),_0xf573('0x11'),_0xf573('0x12'),'faxPause',_0xf573('0x13'),_0xf573('0x14'),'pauseType','showWebBar','lastLoginAt',_0xf573('0x15'),_0xf573('0x16'),_0xf573('0x17'),'passwordResetAt',_0xf573('0x18'),_0xf573('0x19'),_0xf573('0x1a'),'phoneBarDnd','phoneBarEnableRecording',_0xf573('0x1b'),_0xf573('0x1c'),_0xf573('0x1d'),_0xf573('0x1e'),'phoneBarRemoteControl',_0xf573('0x1f'),_0xf573('0x20'),_0xf573('0x21'),_0xf573('0x22')];exports[_0xf573('0x23')]=function(){return this['isAuthenticated'](!![])[_0xf573('0x24')](function(_0x46fefd,_0x34c59a,_0xbcd159){if(_0x46fefd[_0xf573('0x25')]){_0xbcd159();}else{return db[_0xf573('0x26')]['findOne']({'where':{'id':_0x46fefd[_0xf573('0x27')]['id']},'attributes':['id',_0xf573('0x28'),_0xf573('0x29')],'raw':!![]})[_0xf573('0x2a')](function(_0x183e1d){if(_0x183e1d&&_0x183e1d[_0xf573('0x28')]){return _0x34c59a['status'](_0x183e1d[_0xf573('0x29')]===_0xf573('0x2b')?0x195:0x193)[_0xf573('0x2c')]({'message':_0x183e1d[_0xf573('0x29')]===_0xf573('0x2b')?_0xf573('0x2d'):_0xf573('0x2e')});}else{_0xbcd159();}})[_0xf573('0x2f')](function(_0xbed0a6){_0xbcd159(_0xbed0a6);});}});};exports[_0xf573('0x30')]=function isAuthenticated(_0x299d9c){return compose()['use'](function(_0xb42a35,_0x4f27e4,_0xf7cc65){if(_0xb42a35[_0xf573('0x31')][_0xf573('0x32')]){if(_[_0xf573('0x33')](_0xb42a35[_0xf573('0x31')]['authorization'],'Basic')){var _0x490b67=basicAuth(_0xb42a35);db['User'][_0xf573('0x34')]({'where':{'name':_0x490b67[_0xf573('0xa')]}})[_0xf573('0x2a')](function(_0xf59eef){if(!_0xf59eef||!_0xf59eef[_0xf573('0x35')](_0x490b67[_0xf573('0x36')])){return _0x4f27e4['status'](0x191)[_0xf573('0x2c')]({'message':_0xf573('0x37')});}_0xb42a35[_0xf573('0x25')]={'id':_0xf59eef['id']};_0xf7cc65();})[_0xf573('0x2f')](function(_0x284330){_0xf7cc65(_0x284330);});}else if(_[_0xf573('0x33')](_0xb42a35[_0xf573('0x31')][_0xf573('0x32')],_0xf573('0x38'))){validateJwt(_0xb42a35,_0x4f27e4,_0xf7cc65);}else{if(_0x299d9c){_0xf7cc65();}else{return _0x4f27e4[_0xf573('0x39')](0x193)['json']({'message':_0xf573('0x3a')});}}}else if(_0xb42a35[_0xf573('0x3b')][_0xf573('0x3c')]){try{var _0x54e6fc={'audience':hardwareConf[_0xf573('0x3d')](),'issuer':hardwareConf[_0xf573('0x3d')]()};verifyJwt(_0xb42a35[_0xf573('0x3b')][_0xf573('0x3c')],_0x54e6fc)[_0xf573('0x2a')](function(_0x51b7a7){return db[_0xf573('0x3e')]['find']({'where':{'id':_0x51b7a7[_0xf573('0x3f')]}})[_0xf573('0x2a')](function(_0x1abf7f){if(!_0x1abf7f||!_[_0xf573('0x40')](_0x1abf7f[_0xf573('0x41')],_0x51b7a7['nonce'])){return _0x4f27e4[_0xf573('0x39')](0x191)[_0xf573('0x2c')]({'message':_0xf573('0x42')});}_0xb42a35[_0xf573('0x25')]={'id':_0x1abf7f['id']};_0xf7cc65();});})[_0xf573('0x2f')](function(){return _0x4f27e4[_0xf573('0x39')](0x191)[_0xf573('0x2c')]({'message':'Invalid\x20API\x20access\x20key'});});}catch(_0x367c8c){_0xf7cc65(_0x367c8c);}}else if(_0x299d9c){_0xf7cc65();}else{return _0x4f27e4[_0xf573('0x39')](0x193)[_0xf573('0x2c')]({'message':'Unknown\x20authorization\x20format'});}})['use'](function(_0x303c87,_0x2653c4,_0x55d1b7){if(_0x303c87['user']){db[_0xf573('0x3e')][_0xf573('0x34')]({'where':{'id':_0x303c87[_0xf573('0x25')]['id']},'attributes':userAttributes})[_0xf573('0x2a')](function(_0x288539){if(!_0x288539){return _0x2653c4[_0xf573('0x39')](0x194)[_0xf573('0x2c')]({'message':_0xf573('0x43')});}_0x303c87['user']=_0x288539;_0x55d1b7();})['catch'](function(_0x38fd2f){_0x55d1b7(_0x38fd2f);});}else if(_0x299d9c){_0x55d1b7();}else{return _0x2653c4[_0xf573('0x39')](0x194)[_0xf573('0x2c')]({'message':_0xf573('0x44')});}});};exports['canUpdate']=function canUpdate(){return compose()[_0xf573('0x24')](function(_0x36c262,_0x2e77ba,_0x146476){return require(_0xf573('0x45'))[_0xf573('0x46')]()['then'](function(_0x32320e){if(_0x32320e[_0xf573('0x47')]){_0x146476();}else{return _0x2e77ba['status'](0x193)[_0xf573('0x2c')]({'message':_0xf573('0x48')});}})['catch'](function(_0x378c89){_0x146476(_0x378c89);});});};exports['isMiddleware']=function(_0x355578,_0x2aec97,_0xd28e96){_0x355578[_0xf573('0x49')]=!![];return _0xd28e96();};exports[_0xf573('0x4a')]=function signToken(_0x1e2dfe){return signJwt(_0x1e2dfe);};exports[_0xf573('0x4b')]=function(_0x58f069,_0x12dd48){if(!_0x58f069[_0xf573('0x25')]){return _0x12dd48[_0xf573('0x39')](0x194)['json']({'message':_0xf573('0x4c')});}var _0x1fff35={'payload':{'id':_0x58f069['user']['id'],'role':_0x58f069['user'][_0xf573('0x8')]},'options':{'expiresIn':0x15180}};return signJwt(_0x1fff35)[_0xf573('0x2a')](function(_0xf8ea30){_0x12dd48['cookie']('motion.token',_0xf8ea30);_0x12dd48[_0xf573('0x4d')](_0xf573('0x4e'));})[_0xf573('0x2f')](function(_0xbb9402){return _0x12dd48['status'](0x1f4)['send'](_0xbb9402);});};exports['retrieveApiKey']=function(_0x739224){if(_['isNil'](_0x739224[_0xf573('0x41')])||_[_0xf573('0x4f')](_0x739224[_0xf573('0x50')])){return null;}else{return createJwt(_0x739224);}};exports['generateApiKey']=function(_0x535ebc){_0x535ebc[_0xf573('0x41')]=generateNonce();_0x535ebc[_0xf573('0x50')]=generateIssuedAt();return createJwt(_0x535ebc);};exports[_0xf573('0x51')]=function(_0x27358d,_0x4e5229){var _0x9f93b5=_0x27358d[_0xf573('0x3b')][_0xf573('0x3c')];if(_0x9f93b5){var _0x40c65a={'nonce':_0x4e5229[_0xf573('0x41')],'iat':_0x4e5229[_0xf573('0x50')],'audience':hardwareConf[_0xf573('0x3d')](),'issuer':hardwareConf[_0xf573('0x3d')]()};return verifyJwt(_0x9f93b5,_0x40c65a)[_0xf573('0x2a')](function(){return generateApiKey(_0x4e5229);});}else{throw{'message':_0xf573('0x52')};}};function signJwt(_0x1226d6){var _0x5e3c6f=BPromise['promisify'](jwt['sign'],{'context':jwt});var _0x30abde=_0x1226d6[_0xf573('0x53')]||config[_0xf573('0x54')]['session'];return new BPromise(function(_0x2fbb66,_0x171375){_0x5e3c6f(_0x1226d6[_0xf573('0x55')],_0x30abde,_0x1226d6[_0xf573('0x56')])[_0xf573('0x2a')](function(_0x482f22){_0x2fbb66(_0x482f22);})[_0xf573('0x2f')](function(_0x16f2e7){_0x171375(_0x16f2e7);});});}function verifyJwt(_0x24b389,_0x59a194,_0x12904d){var _0x3c0021=BPromise[_0xf573('0x57')](jwt[_0xf573('0x58')],{'context':jwt});var _0x582cab=_0x12904d||config[_0xf573('0x54')][_0xf573('0x59')];return new BPromise(function(_0x54df7b,_0x5f2a15){_0x3c0021(_0x24b389,_0x582cab,_0x59a194)[_0xf573('0x2a')](function(_0x2f816f){_0x54df7b(_0x2f816f);})[_0xf573('0x2f')](function(_0x3adf63){_0x5f2a15(_0x3adf63);});});}function generateNonce(){return crypto[_0xf573('0x5a')](0x10)[_0xf573('0x5b')](_0xf573('0x5c'));}function generateIssuedAt(){return Math[_0xf573('0x5d')](Date[_0xf573('0x5e')]()/0x3e8)['toString']();}function createJwt(_0x20637c){var _0x368e76={'payload':{'iat':_0x20637c[_0xf573('0x50')],'nonce':_0x20637c[_0xf573('0x41')]},'options':{'algorithm':_0xf573('0x5f'),'subject':_0x20637c['id'][_0xf573('0x5b')](),'issuer':hardwareConf[_0xf573('0x3d')](),'audience':hardwareConf[_0xf573('0x3d')]()}};return signJwt(_0x368e76)[_0xf573('0x2a')](function(_0x3ccbca){return{'iat':_0x20637c[_0xf573('0x50')],'nonce':_0x20637c[_0xf573('0x41')],'token':_0x3ccbca};});}