Built motion from commit 25580385.|2.6.2
[motion2.git] / server / api / authLocal / authLocal.controller.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 _0xbce9=['getLicense','catch','MailAccount','find','email','MailServerOut','Smtp','CloudProvider','Unable\x20to\x20find\x20a\x20configured\x20SMTP\x20service!','getSmtpOptions','merge','format','\x22%s\x22\x20<%s>','request','error','stringify','status','send','message','login','authenticate','local-login','Something\x20went\x20wrong,\x20please\x20try\x20again.','role','signToken','json','forgot','body','Email\x20parameter\x20is\x20missing','User','This\x20email\x20is\x20not\x20registered.','The\x20user\x20is\x20blocked.\x20Please\x20contact\x20the\x20administrator.','resetPasswordToken','resetPasswordExpires','now','xCally\x20Motion\x20Password\x20Reset','host','If\x20you\x20did\x20not\x20request\x20this,\x20please\x20ignore\x20this\x20email\x20and\x20your\x20password\x20will\x20remain\x20unchanged.\x0a','Reset\x20Email\x20sent!','reset','params','token','Password\x20reset\x20token\x20is\x20invalid\x20or\x20has\x20expired.','securePassword','enforcePasswordHistory','passwordHistoryLimit','validatePasswordPattern','validatePasswordHistory','password','previousPasswords','save','Password\x20reset!','util','passport','passport-local','lodash','randomstring','moment','../../mysqldb','../../config/utils','client','http','Setting','findOne','blockDuration','then','name','disabled','blocked','blockedAt','admin','user','agent','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','loginAttempts','allowedLoginAttempts','minutes','clearBlockUser','add'];(function(_0x172747,_0x1732bd){var _0x509692=function(_0x448c56){while(--_0x448c56){_0x172747['push'](_0x172747['shift']());}};_0x509692(++_0x1732bd);}(_0xbce9,0x83));var _0x9bce=function(_0x1f9bcc,_0x400cb8){_0x1f9bcc=_0x1f9bcc-0x0;var _0x12831a=_0xbce9[_0x1f9bcc];return _0x12831a;};'use strict';var util=require(_0x9bce('0x0'));var passport=require(_0x9bce('0x1'));var LocalStrategy=require(_0x9bce('0x2'))['Strategy'];var jayson=require('jayson/promise');var _=require(_0x9bce('0x3'));var rs=require(_0x9bce('0x4'));var moment=require(_0x9bce('0x5'));var db=require(_0x9bce('0x6'))['db'];var authService=require('../../components/auth/service');var utils=require(_0x9bce('0x7'));var client=jayson[_0x9bce('0x8')][_0x9bce('0x9')]({'port':0x232b});function localAuthenticate(_0x299226,_0x44156e,_0x478f8c,_0x5661ef){var _0x3847fd,_0x41f56a;return db[_0x9bce('0xa')][_0x9bce('0xb')]({'where':{'id':0x1},'attributes':['allowedLoginAttempts',_0x9bce('0xc')],'raw':!![]})[_0x9bce('0xd')](function(_0x257e20){_0x3847fd=_0x257e20;return _0x299226['findOne']({'attributes':['id',_0x9bce('0xe'),'password','salt','role',_0x9bce('0xf'),_0x9bce('0x10'),_0x9bce('0x11'),'loginAttempts'],'where':{'name':_0x44156e,'role':{'$or':[_0x9bce('0x12'),_0x9bce('0x13'),_0x9bce('0x14')]}}});})[_0x9bce('0xd')](function(_0x5b65df){if(!_0x5b65df||_0x5b65df&&_0x5b65df[_0x9bce('0xe')]!==_0x44156e){throw new Error(_0x9bce('0x15'));}else{_0x41f56a=_0x5b65df;return _0x41f56a;}})['then'](function(){_0x41f56a['authenticate'](_0x478f8c,function(_0x26149f,_0x3fae2a){var _0x83d079=_0x41f56a[_0x9bce('0x16')]+0x1;if(_0x26149f){return _0x5661ef(_0x26149f);}else if(!_0x3fae2a){if(_0x41f56a['disabled']){return _0x5661ef(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}else if(_0x3847fd[_0x9bce('0x17')]>0x0){if(_0x41f56a['blocked']){if(_0x3847fd[_0x9bce('0xc')]>0x0){if(moment(_0x41f56a[_0x9bce('0x11')])['add'](_0x3847fd[_0x9bce('0xc')],_0x9bce('0x18'))>moment()){return _0x5661ef(null,![],{'message':_0x9bce('0x15')});}else{return utils['clearBlockUser'](_0x41f56a,_0x83d079,![],null)[_0x9bce('0xd')](function(){return _0x5661ef(null,![],{'message':_0x9bce('0x15')});});}}else{return _0x5661ef(null,![],{'message':_0x9bce('0x15')});}}else{if(_0x83d079>=_0x3847fd[_0x9bce('0x17')]){return utils[_0x9bce('0x19')](_0x41f56a,0x0,!![],moment())[_0x9bce('0xd')](function(){return _0x5661ef(null,![],{'message':_0x9bce('0x15')});});}else{return utils[_0x9bce('0x19')](_0x41f56a,_0x83d079,![],null)[_0x9bce('0xd')](function(){return _0x5661ef(null,![],{'message':_0x9bce('0x15')});});}}}else{return _0x5661ef(null,![],{'message':_0x9bce('0x15')});}}else{if(_0x41f56a['disabled']){return _0x5661ef(null,![],{'message':_0x9bce('0x15')});}else if(_0x3847fd[_0x9bce('0x17')]>0x0){if(_0x41f56a[_0x9bce('0x10')]){if(_0x3847fd[_0x9bce('0xc')]>0x0){if(moment(_0x41f56a['blockedAt'])[_0x9bce('0x1a')](_0x3847fd['blockDuration'],'minutes')>moment()){return _0x5661ef(null,![],{'message':_0x9bce('0x15')});}else{return utils[_0x9bce('0x19')](_0x41f56a,0x0,![],null)[_0x9bce('0xd')](function(){return utils[_0x9bce('0x1b')](_0x41f56a);})[_0x9bce('0xd')](function(){return _0x5661ef(null,_0x41f56a);});}}else{return _0x5661ef(null,![],{'message':_0x9bce('0x15')});}}else{return utils[_0x9bce('0x19')](_0x41f56a,0x0,![],null)[_0x9bce('0xd')](function(){return utils[_0x9bce('0x1b')](_0x41f56a);})[_0x9bce('0xd')](function(){return _0x5661ef(null,_0x41f56a);})[_0x9bce('0x1c')](function(_0x344921){return _0x5661ef(null,![],_0x344921);});}}else{if(_0x41f56a['blocked']){return _0x5661ef(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}else{return utils[_0x9bce('0x1b')](_0x41f56a)[_0x9bce('0xd')](function(){return _0x5661ef(null,_0x41f56a);});}}}});})[_0x9bce('0x1c')](function(_0x4ea0de){return _0x5661ef(null,![],_0x4ea0de);});}function sendServiceMail(_0x200d5c){return db[_0x9bce('0x1d')][_0x9bce('0x1e')]({'where':{'service':!![]},'attributes':['id',_0x9bce('0xe'),_0x9bce('0x1f')],'include':[{'model':db[_0x9bce('0x20')],'as':_0x9bce('0x21'),'include':[{'model':db[_0x9bce('0x22')],'as':_0x9bce('0x22')}]}]})[_0x9bce('0xd')](function(_0x2735b1){if(!_0x2735b1){throw new Error(_0x9bce('0x23'));}var _0x4ac4a6=_0x2735b1[_0x9bce('0x24')]();var _0x1c26b3={'account':_0x4ac4a6,'message':_[_0x9bce('0x25')]({'from':util[_0x9bce('0x26')](_0x9bce('0x27'),_0x2735b1[_0x9bce('0xe')],_0x2735b1[_0x9bce('0x1f')]||_0x2735b1['Smtp']['user'])},_0x200d5c)};return client[_0x9bce('0x28')]('SendMail',_0x1c26b3);})['then'](function(_0x19b02d){if(_0x19b02d[_0x9bce('0x29')]){throw new Error(util[_0x9bce('0x26')]('SendMail\x20error:\x20%s.\x20Please\x20contact\x20your\x20Administrator!',_0x19b02d[_0x9bce('0x29')]['message']||JSON[_0x9bce('0x2a')](_0x19b02d[_0x9bce('0x29')])));}return!![];});}function handleError(_0x163479,_0xadf1a2,_0x3b73a3){return _0x163479[_0x9bce('0x2b')](_0xadf1a2||0x1f4)[_0x9bce('0x2c')]({'message':_0x3b73a3?_0x3b73a3[_0x9bce('0x2d')]||_0x3b73a3:''});}exports[_0x9bce('0x2e')]=function(_0x14a7f1,_0x3a4623,_0x3a75ad){passport[_0x9bce('0x2f')](_0x9bce('0x30'),function(_0x309bd6,_0x56baef,_0x3e4d9d){var _0xe606c=_0x309bd6||_0x3e4d9d;if(_0xe606c){return handleError(_0x3a4623,0x191,_0xe606c);}if(!_0x56baef){return handleError(_0x3a4623,0x194,{'message':_0x9bce('0x31')});}else{var _0x9a0e2c={'payload':{'id':_0x56baef['id'],'role':_0x56baef[_0x9bce('0x32')]},'options':{'expiresIn':0x15180}};return authService[_0x9bce('0x33')](_0x9a0e2c)['then'](function(_0x12800b){_0x3a4623[_0x9bce('0x34')]({'id':_0x56baef['id'],'token':_0x12800b,'statusCall':'','isAlreadyLogged':_0x56baef['online']});});}})(_0x14a7f1,_0x3a4623,_0x3a75ad);};exports[_0x9bce('0x35')]=function(_0x49e26e,_0x2f7b51){if(!_0x49e26e[_0x9bce('0x36')][_0x9bce('0x1f')]){return handleError(_0x2f7b51,0x1f4,{'message':_0x9bce('0x37')});}var _0x123072=rs['generate']();_0x49e26e[_0x9bce('0x36')][_0x9bce('0x1f')]=_0x49e26e[_0x9bce('0x36')][_0x9bce('0x1f')]['trim']()['toLowerCase']();var _0x173e15;return db['Setting']['findOne']({'where':{'id':0x1},'attributes':[_0x9bce('0x17'),_0x9bce('0xc')],'raw':!![]})['then'](function(_0x2ca360){_0x173e15=_0x2ca360;return db[_0x9bce('0x38')][_0x9bce('0x1e')]({'where':{'email':_0x49e26e[_0x9bce('0x36')][_0x9bce('0x1f')]}});})['then'](function(_0xc716ab){if(!_0xc716ab){throw new Error(_0x9bce('0x39'));}if(_0xc716ab['disabled']){throw new Error('The\x20user\x20is\x20disabled.\x20Please\x20contact\x20the\x20administrator.');}if(_0xc716ab[_0x9bce('0x10')]){if(moment(_0xc716ab[_0x9bce('0x11')])[_0x9bce('0x1a')](_0x173e15[_0x9bce('0xc')],_0x9bce('0x18'))>moment()){throw new Error(_0x9bce('0x3a'));}else{_0xc716ab['blocked']=![];_0xc716ab['blockedAt']=null;}}_0xc716ab[_0x9bce('0x3b')]=_0x123072;_0xc716ab[_0x9bce('0x3c')]=Date[_0x9bce('0x3d')]()+0x36ee80;return _0xc716ab['save']();})[_0x9bce('0xd')](function(){return sendServiceMail({'to':_0x49e26e[_0x9bce('0x36')][_0x9bce('0x1f')],'subject':_0x9bce('0x3e'),'text':'You\x20are\x20receiving\x20this\x20because\x20you\x20(or\x20someone\x20else)\x20have\x20requested\x20the\x20reset\x20of\x20the\x20password\x20for\x20your\x20account.\x0a\x0a'+'Please\x20click\x20on\x20the\x20following\x20link,\x20or\x20paste\x20this\x20into\x20your\x20browser\x20to\x20complete\x20the\x20process:\x0a\x0a'+'https://'+_0x49e26e['headers'][_0x9bce('0x3f')]+'/reset/'+_0x123072+'\x0a\x0a'+_0x9bce('0x40')});})[_0x9bce('0xd')](function(){_0x2f7b51['status'](0xc8)['send']({'message':_0x9bce('0x41')});})[_0x9bce('0x1c')](function(_0xe79c82){return handleError(_0x2f7b51,0x1f4,_0xe79c82);});};exports[_0x9bce('0x42')]=function(_0x4fcbe5,_0x183f58){return db[_0x9bce('0x38')][_0x9bce('0x1e')]({'where':{'resetPasswordToken':_0x4fcbe5[_0x9bce('0x43')][_0x9bce('0x44')],'resetPasswordExpires':{'$gt':Date['now']()}}})['then'](function(_0xdc0384){if(!_0xdc0384){throw new Error(_0x9bce('0x45'));}return db['Setting'][_0x9bce('0xb')]({'attributes':['id',_0x9bce('0x46'),_0x9bce('0x47'),_0x9bce('0x48')]})[_0x9bce('0xd')](function(_0x11b1c9){if(_0x11b1c9[_0x9bce('0x46')])authService[_0x9bce('0x49')](_0x4fcbe5['body']['password']);if(!_0x11b1c9[_0x9bce('0x47')]||!_0xdc0384['previousPasswords'])return _0xdc0384;authService[_0x9bce('0x4a')](_0x4fcbe5[_0x9bce('0x36')][_0x9bce('0x4b')],_0xdc0384[_0x9bce('0x4c')],_0x11b1c9[_0x9bce('0x48')]);return _0xdc0384;})['then'](function(){_0xdc0384[_0x9bce('0x4b')]=_0x4fcbe5['body'][_0x9bce('0x4b')];_0xdc0384[_0x9bce('0x3b')]=null;_0xdc0384[_0x9bce('0x3c')]=null;return _0xdc0384[_0x9bce('0x4d')]();});})[_0x9bce('0xd')](function(){_0x183f58[_0x9bce('0x2b')](0xc8)[_0x9bce('0x2c')]({'message':_0x9bce('0x4e')});})[_0x9bce('0x1c')](function(_0x54d174){return handleError(_0x183f58,0x1f4,_0x54d174);});};passport['use'](_0x9bce('0x30'),new LocalStrategy({'usernameField':'name','passwordField':_0x9bce('0x4b')},function(_0x1ab069,_0xc334a8,_0x1c7ddc){return localAuthenticate(db[_0x9bce('0x38')],_0x1ab069,_0xc334a8,_0x1c7ddc);}));