Built motion from commit f861bcb808219e66da88ba48b61bddb35224a65e. Version 3.0.0...
[motion-next.git] / server / components / auth / service.js
1 'use strict';const a989_0x1de5=['337qZMhQq','nonce','allowmessenger','authenticate','exports','1rWDqHq','iat','startsWith','1BMBGBb','verify','chatPause','some','userProfileId','HS512','retrieveApiKey','pass','openchannelPause','Forbidden','1276168RRpdgN','1145206HWCtMg','Wrong\x20credentials.','@config/license/hardware','phoneBarRemoteControl','Unknown\x20authorization\x20format','sign','toLowerCase','Unmanaged.','Setting','params','faxPause','1673thcuzk','disabled','sequelize','1022331wPjzog','redirect','Bearer','lastPauseAt','headers','unshift','moment','\x20passwords.\x20Please\x20choose\x20another\x20one.','phoneBarEnableSettings','Forbidden.','blockDuration','685073lhubxq','User','phoneBarPrefixRequired','name','authorization','API\x20access\x20key\x20is\x20not\x20valid\x20anymore','allowedLoginAttempts','cookie','email','@config/license/util','privacyEnabled','hotdesk','crypto','/dashboards/general','phoneBarEnableDtmfTone','HS256','mailPause','sessionSecret','phoneBarAutoAnswerDelay','send','pauseType','canUpdate','splice','isAuthenticated','minutes','The\x20password\x20must\x20be\x20different\x20from\x20the\x20previous\x20','secret','phoneBarRemoteControlPort','number','apiKeyNonce','floor','@components/encryptor','60399RygrNo','getLicense','disposition','ChatInteraction','length','permissions','generateApiKey','lastLoginAt','updatePasswordsHistory','The\x20password\x20must\x20be\x20different\x20from\x20the\x20last\x20one.\x20Please\x20choose\x20another\x20one.','getUuid','role','status','isMiddleware','use','json','catch','phoneBarDnd','promisify','fullname','User\x20not\x20found.','md5secret','findOne','validatePasswordPattern','decryptString','user','isNil','slice','The\x20password\x20must\x20be\x20at\x20least\x208\x20characters\x20long\x20and\x20have\x201\x20lowercase\x20character,\x201\x20uppercase\x20character,\x201\x20number\x20and\x201\x20special\x20character\x20~!@#$%^&-_=+[{]}.','now','Invalid\x20API\x20access\x20key','phoneBarEnableRecording','basic-auth','composable-middleware','hex','837857IeZvEo','closed','split','blocked','5Mdtqgl','apiKeyIat','signToken','interface','setTokenCookie','unmanaged','util','payload','toString','update','query','then','2kyNdtP','blockedAt'];const a989_0xf479=function(_0x2279e3,_0x3f5306){_0x2279e3=_0x2279e3-0x10c;let _0x1de569=a989_0x1de5[_0x2279e3];return _0x1de569;};const a989_0x112afc=a989_0xf479;(function(_0x337dc4,_0x14245d){const _0x494647=a989_0xf479;while(!![]){try{const _0x2013f6=-parseInt(_0x494647(0x17b))*-parseInt(_0x494647(0x15d))+-parseInt(_0x494647(0x170))+parseInt(_0x494647(0x165))*-parseInt(_0x494647(0x189))+-parseInt(_0x494647(0x16f))+parseInt(_0x494647(0x128))*parseInt(_0x494647(0x15b))+parseInt(_0x494647(0x17e))*-parseInt(_0x494647(0x162))+parseInt(_0x494647(0x14b))*parseInt(_0x494647(0x14f));if(_0x2013f6===_0x14245d)break;else _0x337dc4['push'](_0x337dc4['shift']());}catch(_0x3315b7){_0x337dc4['push'](_0x337dc4['shift']());}}}(a989_0x1de5,0xb5e92));const {db}=require('@mysqldb'),{ValidationError}=require(a989_0x112afc(0x17d)),{getConfig}=require('@config/get-config'),hardwareConf=require(a989_0x112afc(0x172)),licenseUtil=require(a989_0x112afc(0x111)),encryptor=require(a989_0x112afc(0x127)),util=require(a989_0x112afc(0x155)),_=require('lodash'),jwt=require('jsonwebtoken'),expressJwt=require('express-jwt'),compose=require(a989_0x112afc(0x149)),basicAuth=require(a989_0x112afc(0x148)),crypto=require(a989_0x112afc(0x114)),moment=require(a989_0x112afc(0x184)),config=getConfig(),validateJwt=expressJwt({'secret':config[a989_0x112afc(0x119)],'algorithms':[a989_0x112afc(0x117)]}),userAttributes=['id',a989_0x112afc(0x133),a989_0x112afc(0x13b),'name','internal',a989_0x112afc(0x110),'userpic',a989_0x112afc(0x12d),a989_0x112afc(0x13d),'voicePause',a989_0x112afc(0x167),a989_0x112afc(0x118),a989_0x112afc(0x17a),'smsPause',a989_0x112afc(0x16d),a989_0x112afc(0x11c),'showWebBar',a989_0x112afc(0x12f),a989_0x112afc(0x181),'crudPermissions',a989_0x112afc(0x15f),'passwordResetAt','alias','phoneBarAutoAnswer',a989_0x112afc(0x11a),a989_0x112afc(0x139),a989_0x112afc(0x147),a989_0x112afc(0x116),a989_0x112afc(0x186),'phoneBarExpires',a989_0x112afc(0x18b),a989_0x112afc(0x173),a989_0x112afc(0x123),a989_0x112afc(0x113),a989_0x112afc(0x152),a989_0x112afc(0x169),a989_0x112afc(0x112),'settingsEnabled'];function isChatInteractionAuthorized(){const _0x48e021=a989_0x112afc;return this[_0x48e021(0x11f)](!![])['use'](function(_0x24712a,_0x493c06,_0x32364f){const _0x45ee17=_0x48e021;if(_0x24712a[_0x45ee17(0x141)])_0x32364f();else return db[_0x45ee17(0x12b)][_0x45ee17(0x13e)]({'where':{'id':_0x24712a[_0x45ee17(0x179)]['id']},'attributes':['id',_0x45ee17(0x14c),_0x45ee17(0x12a)],'raw':!![]})['then'](function(_0x30b4df){const _0x4db2fe=_0x45ee17;if(_0x30b4df&&_0x30b4df[_0x4db2fe(0x14c)])return _0x493c06[_0x4db2fe(0x134)](_0x30b4df[_0x4db2fe(0x12a)]===_0x4db2fe(0x154)?0x195:0x193)[_0x4db2fe(0x137)]({'message':_0x30b4df[_0x4db2fe(0x12a)]===_0x4db2fe(0x154)?_0x4db2fe(0x177):_0x4db2fe(0x187)});else _0x32364f();})[_0x45ee17(0x138)](function(_0x553bad){_0x32364f(_0x553bad);});});}function isAuthenticated(_0x4170af){const _0x5b51b9=a989_0x112afc;return compose()[_0x5b51b9(0x136)](function(_0x3e310e,_0x3d9976,_0xb42fd1){const _0x41c6ee=_0x5b51b9;let _0xfda3b9;if(_0x3e310e['headers'][_0x41c6ee(0x10c)]){if(_[_0x41c6ee(0x164)](_0x3e310e[_0x41c6ee(0x182)][_0x41c6ee(0x10c)],'Basic')){const _0x26ccdb=basicAuth(_0x3e310e);db['User']['findOne']({'where':{'name':_0x26ccdb[_0x41c6ee(0x18c)]}})['then'](function(_0x5796d4){const _0x348f59=_0x41c6ee;if(!_0x5796d4||!_0x5796d4[_0x348f59(0x160)](_0x26ccdb[_0x348f59(0x16c)]))return _0x3d9976[_0x348f59(0x134)](0x191)[_0x348f59(0x137)]({'message':_0x348f59(0x171)});_0x3e310e[_0x348f59(0x141)]={'id':_0x5796d4['id']},_0xb42fd1();})[_0x41c6ee(0x138)](function(_0x394b05){_0xb42fd1(_0x394b05);});}else{if(_[_0x41c6ee(0x164)](_0x3e310e[_0x41c6ee(0x182)][_0x41c6ee(0x10c)],_0x41c6ee(0x180)))validateJwt(_0x3e310e,_0x3d9976,_0xb42fd1);else{if(_0x4170af)_0xb42fd1();else return _0x3d9976[_0x41c6ee(0x134)](0x193)['json']({'message':_0x41c6ee(0x174)});}}}else{if(_0x3e310e[_0x41c6ee(0x159)]['apikey'])try{const _0x131e1c={'audience':hardwareConf['getUuid'](),'issuer':hardwareConf[_0x41c6ee(0x132)]()};verifyJwt(_0x3e310e[_0x41c6ee(0x159)]['apikey'],_0x131e1c)[_0x41c6ee(0x15a)](function(_0x4dbac3){const _0x2c0aa0=_0x41c6ee;return db[_0x2c0aa0(0x18a)][_0x2c0aa0(0x13e)]({'where':{'id':_0x4dbac3['sub']}})[_0x2c0aa0(0x15a)](function(_0x1d94d5){const _0x4c625e=_0x2c0aa0;return _0xfda3b9=_0x1d94d5,db[_0x4c625e(0x178)][_0x4c625e(0x13e)]({'where':{'id':0x1},'attributes':[_0x4c625e(0x10e),'blockDuration'],'raw':!![]});})[_0x2c0aa0(0x15a)](function(_0x2140fb){const _0xfb1ea5=_0x2c0aa0;if(!_0xfda3b9||!_['isEqual'](_0xfda3b9[_0xfb1ea5(0x125)],_0x4dbac3[_0xfb1ea5(0x15e)]))return _0x3d9976['status'](0x191)[_0xfb1ea5(0x137)]({'message':_0xfb1ea5(0x10d)});if(_0xfda3b9[_0xfb1ea5(0x17c)])return _0x3d9976['status'](0x191)[_0xfb1ea5(0x137)]({'message':'Invalid\x20API\x20access\x20key'});if(_0xfda3b9[_0xfb1ea5(0x14e)]){if(_0x2140fb[_0xfb1ea5(0x188)]>0x0){if(moment(_0xfda3b9[_0xfb1ea5(0x15c)])['add'](_0x2140fb[_0xfb1ea5(0x188)],_0xfb1ea5(0x120))>moment())return _0x3d9976[_0xfb1ea5(0x134)](0x191)[_0xfb1ea5(0x137)]({'message':_0xfb1ea5(0x146)});}else return _0x3d9976[_0xfb1ea5(0x134)](0x191)['json']({'message':_0xfb1ea5(0x146)});}_0x3e310e[_0xfb1ea5(0x141)]={'id':_0xfda3b9['id']},_0xb42fd1();});})[_0x41c6ee(0x138)](function(){const _0x16b4a4=_0x41c6ee;return _0x3d9976[_0x16b4a4(0x134)](0x191)[_0x16b4a4(0x137)]({'message':'Invalid\x20API\x20access\x20key'});});}catch(_0x2d0ecf){_0xb42fd1(_0x2d0ecf);}else{if(_0x4170af)_0xb42fd1();else return _0x3d9976[_0x41c6ee(0x134)](0x193)[_0x41c6ee(0x137)]({'message':_0x41c6ee(0x174)});}}})[_0x5b51b9(0x136)](function(_0x573f80,_0xb0b9c5,_0x4b5353){const _0x5709f3=_0x5b51b9;if(_0x573f80['user'])db[_0x5709f3(0x18a)][_0x5709f3(0x13e)]({'where':{'id':_0x573f80[_0x5709f3(0x141)]['id']},'attributes':userAttributes})[_0x5709f3(0x15a)](function(_0x232128){const _0x22120e=_0x5709f3;if(!_0x232128)return _0xb0b9c5[_0x22120e(0x134)](0x194)[_0x22120e(0x137)]({'message':_0x22120e(0x13c)});_0x573f80[_0x22120e(0x141)]=_0x232128,_0x4b5353();})['catch'](function(_0x1cb641){_0x4b5353(_0x1cb641);});else{if(_0x4170af)_0x4b5353();else return _0xb0b9c5[_0x5709f3(0x134)](0x194)[_0x5709f3(0x137)]({'message':'User\x20object\x20not\x20found.'});}});}function canUpdate(){return compose()['use'](function(_0x2befce,_0x1ac369,_0x275326){const _0x7a68aa=a989_0xf479;return licenseUtil[_0x7a68aa(0x129)]()[_0x7a68aa(0x15a)](function(_0x264099){const _0x566d87=_0x7a68aa;if(_0x264099[_0x566d87(0x158)])_0x275326();else return _0x1ac369[_0x566d87(0x134)](0x193)[_0x566d87(0x137)]({'message':_0x566d87(0x16e)});})[_0x7a68aa(0x138)](function(_0x3153c5){_0x275326(_0x3153c5);});});}function isMiddleware(_0x9bb453,_0xe98c6c,_0x414f8b){return _0x9bb453['isMiddleware']=!![],_0x414f8b();}function signToken(_0x57dd7e){return signJwt(_0x57dd7e);}function setTokenCookie(_0x33dffd,_0x1f7719){const _0x2b9604=a989_0x112afc;if(!_0x33dffd['user'])return _0x1f7719['status'](0x194)['json']({'message':'It\x20looks\x20like\x20you\x20aren\x27t\x20logged\x20in,\x20please\x20try\x20again.'});const _0x5959ed={'payload':{'id':_0x33dffd[_0x2b9604(0x141)]['id'],'role':_0x33dffd[_0x2b9604(0x141)][_0x2b9604(0x133)]},'options':{'expiresIn':0x15180}};return signJwt(_0x5959ed)[_0x2b9604(0x15a)](function(_0x16da8d){const _0x3db6b3=_0x2b9604;_0x1f7719[_0x3db6b3(0x10f)]('motion.token',_0x16da8d),_0x1f7719[_0x3db6b3(0x17f)](_0x3db6b3(0x115));})[_0x2b9604(0x138)](function(_0x646d14){const _0x3ff472=_0x2b9604;return _0x1f7719[_0x3ff472(0x134)](0x1f4)[_0x3ff472(0x11b)](_0x646d14);});}function retrieveApiKey(_0x172893){const _0x4025f8=a989_0x112afc;return _[_0x4025f8(0x142)](_0x172893['apiKeyNonce'])||_[_0x4025f8(0x142)](_0x172893[_0x4025f8(0x150)])?null:createJwt(_0x172893);}function generateApiKey(_0x51752e){const _0x4e63e8=a989_0x112afc;return _0x51752e[_0x4e63e8(0x125)]=generateNonce(),_0x51752e[_0x4e63e8(0x150)]=generateIssuedAt(),createJwt(_0x51752e);}function regenerateApiKey(_0x22bd80,_0x2d6025){const _0x587956=a989_0x112afc,_0x45cfc9=_0x22bd80[_0x587956(0x159)]['apikey'];if(_0x45cfc9){const _0x525acd={'nonce':_0x2d6025[_0x587956(0x125)],'iat':_0x2d6025['apiKeyIat'],'audience':hardwareConf[_0x587956(0x132)](),'issuer':hardwareConf[_0x587956(0x132)]()};return verifyJwt(_0x45cfc9,_0x525acd)[_0x587956(0x15a)](function(){return generateApiKey(_0x2d6025);});}else throw{'message':'Please\x20use\x20the\x20previous\x20API\x20access\x20key\x20to\x20generate\x20a\x20new\x20one'};}function validatePasswordPattern(_0x2d4cf6){const _0x34ccd1=a989_0x112afc,_0x53f268=new RegExp(/(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[?!@#$%^&*~\-_=+[{\]}])(?=.{8,})/);if(!_0x53f268['test'](_0x2d4cf6))throw new ValidationError(_0x34ccd1(0x144));return;}function validatePasswordHistory(_0x3929bd,_0x52604f,_0x48e7b4){const _0x193ddb=a989_0x112afc,_0x214c87=encryptor[_0x193ddb(0x140)](_0x52604f)[_0x193ddb(0x14d)](','),_0x41fb3a=_0x214c87[_0x193ddb(0x143)](0x0,_0x48e7b4)[_0x193ddb(0x168)](_0xf7988a=>_0xf7988a['toLowerCase']()===_0x3929bd[_0x193ddb(0x176)]());if(_0x41fb3a){const _0x170827=_0x48e7b4===0x1?_0x193ddb(0x131):_0x193ddb(0x121)+_0x48e7b4+_0x193ddb(0x185);throw new ValidationError(_0x170827);}}function updatePasswordsHistory(_0x389c67,_0x2f2bd5){const _0x47ba18=a989_0x112afc,_0x593f62=_0x2f2bd5?encryptor[_0x47ba18(0x140)](_0x2f2bd5)[_0x47ba18(0x14d)](','):[];return _0x593f62[_0x47ba18(0x12c)]===0x5&&_0x593f62[_0x47ba18(0x11e)](-0x1,0x1),_0x593f62[_0x47ba18(0x183)](_0x389c67),encryptor['encryptString'](_0x593f62['join'](','));}function signJwt(_0x29ec4b){const _0xd3e29=a989_0x112afc,_0xc1c6b9=util[_0xd3e29(0x13a)](jwt[_0xd3e29(0x175)],{'context':jwt}),_0x8d13cb=_0x29ec4b[_0xd3e29(0x122)]||config['sessionSecret'];return new Promise(function(_0x5ba2f1,_0xfc72ea){const _0x432365=_0xd3e29,_0x3faf83=typeof _0x29ec4b[_0x432365(0x156)][_0x432365(0x163)]===_0x432365(0x124)?_0x29ec4b[_0x432365(0x156)][_0x432365(0x163)]:Number(_0x29ec4b['payload'][_0x432365(0x163)]),_0x4363bb={..._0x29ec4b[_0x432365(0x156)],'iat':_0x3faf83};_0xc1c6b9(_0x4363bb,_0x8d13cb,_0x29ec4b['options'])['then'](function(_0x1ec195){_0x5ba2f1(_0x1ec195);})[_0x432365(0x138)](function(_0x2739ce){_0xfc72ea(_0x2739ce);});});}function verifyJwt(_0x4dcd41,_0x20856e,_0x11a7b7){const _0x454911=a989_0x112afc,_0x3d1f7f=util[_0x454911(0x13a)](jwt[_0x454911(0x166)],{'context':jwt}),_0x1fd549=_0x11a7b7||config['sessionSecret'];return new Promise(function(_0x59210e,_0x5ce9ff){const _0x23bc3d=_0x454911;_0x3d1f7f(_0x4dcd41,_0x1fd549,_0x20856e)[_0x23bc3d(0x15a)](function(_0x51ccd8){_0x59210e(_0x51ccd8);})[_0x23bc3d(0x138)](function(_0x1ce674){_0x5ce9ff(_0x1ce674);});});}function generateNonce(){const _0x5a1314=a989_0x112afc;return crypto['randomBytes'](0x10)['toString'](_0x5a1314(0x14a));}function generateIssuedAt(){const _0x3d199b=a989_0x112afc;return Math[_0x3d199b(0x126)](Date[_0x3d199b(0x145)]()/0x3e8)[_0x3d199b(0x157)]();}function createJwt(_0x3d18b0){const _0x59f051=a989_0x112afc,_0x5117ca={'payload':{'iat':_0x3d18b0[_0x59f051(0x150)],'nonce':_0x3d18b0[_0x59f051(0x125)]},'options':{'algorithm':_0x59f051(0x16a),'subject':_0x3d18b0['id'][_0x59f051(0x157)](),'issuer':hardwareConf[_0x59f051(0x132)](),'audience':hardwareConf[_0x59f051(0x132)]()}};return signJwt(_0x5117ca)[_0x59f051(0x15a)](function(_0x343c40){const _0x3b76d9=_0x59f051;return{'iat':_0x3d18b0[_0x3b76d9(0x150)],'nonce':_0x3d18b0[_0x3b76d9(0x125)],'token':_0x343c40};});}module[a989_0x112afc(0x161)]['isChatInteractionAuthorized']=isChatInteractionAuthorized,module[a989_0x112afc(0x161)][a989_0x112afc(0x11f)]=isAuthenticated,module[a989_0x112afc(0x161)][a989_0x112afc(0x11d)]=canUpdate,module[a989_0x112afc(0x161)][a989_0x112afc(0x135)]=isMiddleware,module[a989_0x112afc(0x161)][a989_0x112afc(0x151)]=signToken,module[a989_0x112afc(0x161)][a989_0x112afc(0x153)]=setTokenCookie,module[a989_0x112afc(0x161)][a989_0x112afc(0x16b)]=retrieveApiKey,module[a989_0x112afc(0x161)][a989_0x112afc(0x12e)]=generateApiKey,module[a989_0x112afc(0x161)]['regenerateApiKey']=regenerateApiKey,module['exports'][a989_0x112afc(0x13f)]=validatePasswordPattern,module[a989_0x112afc(0x161)]['validatePasswordHistory']=validatePasswordHistory,module[a989_0x112afc(0x161)][a989_0x112afc(0x130)]=updatePasswordsHistory;