Built motion from commit 52c8a02d.|2.6.29
[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 _0x0229=['now','save','xCally\x20Motion\x20Password\x20Reset','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','headers','host','/reset/','If\x20you\x20did\x20not\x20request\x20this,\x20please\x20ignore\x20this\x20email\x20and\x20your\x20password\x20will\x20remain\x20unchanged.\x0a','reset','params','Password\x20reset\x20token\x20is\x20invalid\x20or\x20has\x20expired.','securePassword','enforcePasswordHistory','passwordHistoryLimit','validatePasswordPattern','previousPasswords','Password\x20reset!','use','local-login','util','passport','passport-local','Strategy','jayson/promise','lodash','randomstring','moment','../../mysqldb','../../components/auth/service','http','Setting','findOne','allowedLoginAttempts','blockDuration','name','password','salt','role','disabled','blocked','blockedAt','loginAttempts','admin','user','agent','then','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','add','clearBlockUser','minutes','getLicense','catch','MailAccount','find','email','Smtp','CloudProvider','Unable\x20to\x20find\x20a\x20configured\x20SMTP\x20service!','getSmtpOptions','merge','format','\x22%s\x22\x20<%s>','request','SendMail','error','SendMail\x20error:\x20%s.\x20Please\x20contact\x20your\x20Administrator!','status','send','message','login','Something\x20went\x20wrong,\x20please\x20try\x20again.','json','forgot','body','Email\x20parameter\x20is\x20missing','generate','trim','toLowerCase','User','This\x20email\x20is\x20not\x20registered.','The\x20user\x20is\x20disabled.\x20Please\x20contact\x20the\x20administrator.','The\x20user\x20is\x20blocked.\x20Please\x20contact\x20the\x20administrator.','resetPasswordToken','resetPasswordExpires'];(function(_0x3481bd,_0xc9debe){var _0x59fab8=function(_0x568b03){while(--_0x568b03){_0x3481bd['push'](_0x3481bd['shift']());}};_0x59fab8(++_0xc9debe);}(_0x0229,0xbe));var _0x9022=function(_0x10f5b9,_0x5a8cbe){_0x10f5b9=_0x10f5b9-0x0;var _0x10c8ba=_0x0229[_0x10f5b9];return _0x10c8ba;};'use strict';var util=require(_0x9022('0x0'));var passport=require(_0x9022('0x1'));var LocalStrategy=require(_0x9022('0x2'))[_0x9022('0x3')];var jayson=require(_0x9022('0x4'));var _=require(_0x9022('0x5'));var rs=require(_0x9022('0x6'));var moment=require(_0x9022('0x7'));var db=require(_0x9022('0x8'))['db'];var authService=require(_0x9022('0x9'));var utils=require('../../config/utils');var client=jayson['client'][_0x9022('0xa')]({'port':0x232b});function localAuthenticate(_0x381972,_0x1612e6,_0x789662,_0x4f89cf){var _0x38b29f,_0x4269d5;return db[_0x9022('0xb')][_0x9022('0xc')]({'where':{'id':0x1},'attributes':[_0x9022('0xd'),_0x9022('0xe')],'raw':!![]})['then'](function(_0x12ec62){_0x38b29f=_0x12ec62;return _0x381972[_0x9022('0xc')]({'attributes':['id',_0x9022('0xf'),_0x9022('0x10'),_0x9022('0x11'),_0x9022('0x12'),_0x9022('0x13'),_0x9022('0x14'),_0x9022('0x15'),_0x9022('0x16')],'where':{'name':_0x1612e6,'role':{'$or':[_0x9022('0x17'),_0x9022('0x18'),_0x9022('0x19')]}}});})[_0x9022('0x1a')](function(_0x185a52){if(!_0x185a52||_0x185a52&&_0x185a52['name']!==_0x1612e6){throw new Error(_0x9022('0x1b'));}else{_0x4269d5=_0x185a52;return _0x4269d5;}})[_0x9022('0x1a')](function(){_0x4269d5['authenticate'](_0x789662,function(_0x1a1490,_0x3d489e){var _0x4b1be6=_0x4269d5[_0x9022('0x16')]+0x1;if(_0x1a1490){return _0x4f89cf(_0x1a1490);}else if(!_0x3d489e){if(_0x4269d5[_0x9022('0x13')]){return _0x4f89cf(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}else if(_0x38b29f[_0x9022('0xd')]>0x0){if(_0x4269d5['blocked']){if(_0x38b29f['blockDuration']>0x0){if(moment(_0x4269d5[_0x9022('0x15')])[_0x9022('0x1c')](_0x38b29f[_0x9022('0xe')],'minutes')>moment()){return _0x4f89cf(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}else{return utils[_0x9022('0x1d')](_0x4269d5,_0x4b1be6,![],null)[_0x9022('0x1a')](function(){return _0x4f89cf(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});});}}else{return _0x4f89cf(null,![],{'message':_0x9022('0x1b')});}}else{if(_0x4b1be6>=_0x38b29f[_0x9022('0xd')]){return utils[_0x9022('0x1d')](_0x4269d5,0x0,!![],moment())[_0x9022('0x1a')](function(){return _0x4f89cf(null,![],{'message':_0x9022('0x1b')});});}else{return utils['clearBlockUser'](_0x4269d5,_0x4b1be6,![],null)[_0x9022('0x1a')](function(){return _0x4f89cf(null,![],{'message':_0x9022('0x1b')});});}}}else{return _0x4f89cf(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}}else{if(_0x4269d5['disabled']){return _0x4f89cf(null,![],{'message':_0x9022('0x1b')});}else if(_0x38b29f['allowedLoginAttempts']>0x0){if(_0x4269d5[_0x9022('0x14')]){if(_0x38b29f[_0x9022('0xe')]>0x0){if(moment(_0x4269d5[_0x9022('0x15')])[_0x9022('0x1c')](_0x38b29f[_0x9022('0xe')],_0x9022('0x1e'))>moment()){return _0x4f89cf(null,![],{'message':_0x9022('0x1b')});}else{return utils[_0x9022('0x1d')](_0x4269d5,0x0,![],null)[_0x9022('0x1a')](function(){return utils['getLicense'](_0x4269d5);})['then'](function(){return _0x4f89cf(null,_0x4269d5);});}}else{return _0x4f89cf(null,![],{'message':_0x9022('0x1b')});}}else{return utils[_0x9022('0x1d')](_0x4269d5,0x0,![],null)[_0x9022('0x1a')](function(){return utils[_0x9022('0x1f')](_0x4269d5);})[_0x9022('0x1a')](function(){return _0x4f89cf(null,_0x4269d5);})[_0x9022('0x20')](function(_0x29bdde){return _0x4f89cf(null,![],_0x29bdde);});}}else{if(_0x4269d5['blocked']){return _0x4f89cf(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}else{return utils[_0x9022('0x1f')](_0x4269d5)['then'](function(){return _0x4f89cf(null,_0x4269d5);});}}}});})[_0x9022('0x20')](function(_0xc79d43){return _0x4f89cf(null,![],_0xc79d43);});}function sendServiceMail(_0x7d514f){return db[_0x9022('0x21')][_0x9022('0x22')]({'where':{'service':!![]},'attributes':['id',_0x9022('0xf'),_0x9022('0x23')],'include':[{'model':db['MailServerOut'],'as':_0x9022('0x24'),'include':[{'model':db[_0x9022('0x25')],'as':_0x9022('0x25')}]}]})[_0x9022('0x1a')](function(_0x5a3b98){if(!_0x5a3b98){throw new Error(_0x9022('0x26'));}var _0x294345=_0x5a3b98[_0x9022('0x27')]();var _0x19a02b={'account':_0x294345,'message':_[_0x9022('0x28')]({'from':util[_0x9022('0x29')](_0x9022('0x2a'),_0x5a3b98[_0x9022('0xf')],_0x5a3b98['email']||_0x5a3b98['Smtp']['user'])},_0x7d514f)};return client[_0x9022('0x2b')](_0x9022('0x2c'),_0x19a02b);})[_0x9022('0x1a')](function(_0x14f72c){if(_0x14f72c[_0x9022('0x2d')]){throw new Error(util[_0x9022('0x29')](_0x9022('0x2e'),_0x14f72c['error']['message']||JSON['stringify'](_0x14f72c[_0x9022('0x2d')])));}return!![];});}function handleError(_0xd81b9b,_0x2e80f,_0x4990a9){return _0xd81b9b[_0x9022('0x2f')](_0x2e80f||0x1f4)[_0x9022('0x30')]({'message':_0x4990a9?_0x4990a9[_0x9022('0x31')]||_0x4990a9:''});}exports[_0x9022('0x32')]=function(_0x1c2c88,_0x4ad37c,_0x3117e9){passport['authenticate']('local-login',function(_0x4624f4,_0x270af9,_0x20c4a9){var _0x2400db=_0x4624f4||_0x20c4a9;if(_0x2400db){return handleError(_0x4ad37c,0x191,_0x2400db);}if(!_0x270af9){return handleError(_0x4ad37c,0x194,{'message':_0x9022('0x33')});}else{var _0xb637b6={'payload':{'id':_0x270af9['id'],'role':_0x270af9[_0x9022('0x12')]},'options':{'expiresIn':0x15180}};return authService['signToken'](_0xb637b6)['then'](function(_0x298201){_0x4ad37c[_0x9022('0x34')]({'id':_0x270af9['id'],'token':_0x298201,'statusCall':'','isAlreadyLogged':_0x270af9['online']});});}})(_0x1c2c88,_0x4ad37c,_0x3117e9);};exports[_0x9022('0x35')]=function(_0x32463c,_0x2153d7){if(!_0x32463c[_0x9022('0x36')][_0x9022('0x23')]){return handleError(_0x2153d7,0x1f4,{'message':_0x9022('0x37')});}var _0x19bfd6=rs[_0x9022('0x38')]();_0x32463c[_0x9022('0x36')]['email']=_0x32463c[_0x9022('0x36')][_0x9022('0x23')][_0x9022('0x39')]()[_0x9022('0x3a')]();var _0x21a29d;return db[_0x9022('0xb')][_0x9022('0xc')]({'where':{'id':0x1},'attributes':[_0x9022('0xd'),_0x9022('0xe')],'raw':!![]})[_0x9022('0x1a')](function(_0x588e11){_0x21a29d=_0x588e11;return db[_0x9022('0x3b')][_0x9022('0x22')]({'where':{'email':_0x32463c[_0x9022('0x36')][_0x9022('0x23')]}});})[_0x9022('0x1a')](function(_0x55a477){if(!_0x55a477){throw new Error(_0x9022('0x3c'));}if(_0x55a477[_0x9022('0x13')]){throw new Error(_0x9022('0x3d'));}if(_0x55a477['blocked']){if(moment(_0x55a477['blockedAt'])[_0x9022('0x1c')](_0x21a29d[_0x9022('0xe')],'minutes')>moment()){throw new Error(_0x9022('0x3e'));}else{_0x55a477['blocked']=![];_0x55a477[_0x9022('0x15')]=null;}}_0x55a477[_0x9022('0x3f')]=_0x19bfd6;_0x55a477[_0x9022('0x40')]=Date[_0x9022('0x41')]()+0x36ee80;return _0x55a477[_0x9022('0x42')]();})[_0x9022('0x1a')](function(){return sendServiceMail({'to':_0x32463c['body'][_0x9022('0x23')],'subject':_0x9022('0x43'),'text':_0x9022('0x44')+_0x9022('0x45')+'https://'+_0x32463c[_0x9022('0x46')][_0x9022('0x47')]+_0x9022('0x48')+_0x19bfd6+'\x0a\x0a'+_0x9022('0x49')});})[_0x9022('0x1a')](function(){_0x2153d7[_0x9022('0x2f')](0xc8)[_0x9022('0x30')]({'message':'Reset\x20Email\x20sent!'});})[_0x9022('0x20')](function(_0x4d67f8){return handleError(_0x2153d7,0x1f4,_0x4d67f8);});};exports[_0x9022('0x4a')]=function(_0x229a5e,_0x4d145f){return db['User'][_0x9022('0x22')]({'where':{'resetPasswordToken':_0x229a5e[_0x9022('0x4b')]['token'],'resetPasswordExpires':{'$gt':Date[_0x9022('0x41')]()}}})['then'](function(_0x48fe94){if(!_0x48fe94){throw new Error(_0x9022('0x4c'));}return db[_0x9022('0xb')][_0x9022('0xc')]({'attributes':['id',_0x9022('0x4d'),_0x9022('0x4e'),_0x9022('0x4f')]})[_0x9022('0x1a')](function(_0x53cff1){if(_0x53cff1[_0x9022('0x4d')])authService[_0x9022('0x50')](_0x229a5e[_0x9022('0x36')][_0x9022('0x10')]);if(!_0x53cff1[_0x9022('0x4e')]||!_0x48fe94[_0x9022('0x51')])return _0x48fe94;authService['validatePasswordHistory'](_0x229a5e[_0x9022('0x36')][_0x9022('0x10')],_0x48fe94[_0x9022('0x51')],_0x53cff1[_0x9022('0x4f')]);return _0x48fe94;})['then'](function(){_0x48fe94[_0x9022('0x10')]=_0x229a5e['body'][_0x9022('0x10')];_0x48fe94[_0x9022('0x3f')]=null;_0x48fe94[_0x9022('0x40')]=null;return _0x48fe94[_0x9022('0x42')]();});})[_0x9022('0x1a')](function(){_0x4d145f[_0x9022('0x2f')](0xc8)[_0x9022('0x30')]({'message':_0x9022('0x52')});})[_0x9022('0x20')](function(_0x435a23){return handleError(_0x4d145f,0x1f4,_0x435a23);});};passport[_0x9022('0x53')](_0x9022('0x54'),new LocalStrategy({'usernameField':_0x9022('0xf'),'passwordField':'password'},function(_0x369055,_0x177479,_0x56de6){return localAuthenticate(db[_0x9022('0x3b')],_0x369055,_0x177479,_0x56de6);}));