9edd4cea0893f4f42d95c67b0a4440420ff14f24
[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 _0xf925=['randomstring','moment','../../mysqldb','../../config/utils','client','http','Setting','blockDuration','findOne','name','salt','role','blocked','loginAttempts','admin','user','agent','then','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','authenticate','disabled','allowedLoginAttempts','blockedAt','add','minutes','clearBlockUser','getLicense','catch','MailAccount','find','email','Smtp','service','host','port','secure','authentication','auth','pass','merge','format','\x22%s\x22\x20<%s>','request','SendMail','error','SendMail\x20error:\x20%s.\x20Please\x20contact\x20your\x20Administrator!','message','stringify','status','send','login','Something\x20went\x20wrong,\x20please\x20try\x20again.','signToken','json','forgot','body','generate','The\x20user\x20is\x20disabled.\x20Please\x20contact\x20the\x20administrator.','The\x20user\x20is\x20blocked.\x20Please\x20contact\x20the\x20administrator.','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','https://','headers','/reset/','If\x20you\x20did\x20not\x20request\x20this,\x20please\x20ignore\x20this\x20email\x20and\x20your\x20password\x20will\x20remain\x20unchanged.\x0a','Reset\x20Email\x20sent!','User','params','token','Password\x20reset\x20token\x20is\x20invalid\x20or\x20has\x20expired.','securePassword','enforcePasswordHistory','passwordHistoryLimit','validatePasswordPattern','previousPasswords','validatePasswordHistory','password','resetPasswordToken','resetPasswordExpires','Password\x20reset!','use','util','passport'];(function(_0x3aaf7d,_0x110885){var _0x364c9e=function(_0x5b0aca){while(--_0x5b0aca){_0x3aaf7d['push'](_0x3aaf7d['shift']());}};_0x364c9e(++_0x110885);}(_0xf925,0x1a7));var _0x5f92=function(_0x3b1106,_0x4bd523){_0x3b1106=_0x3b1106-0x0;var _0x1fbeef=_0xf925[_0x3b1106];return _0x1fbeef;};'use strict';var util=require(_0x5f92('0x0'));var passport=require(_0x5f92('0x1'));var LocalStrategy=require('passport-local')['Strategy'];var jayson=require('jayson/promise');var _=require('lodash');var rs=require(_0x5f92('0x2'));var moment=require(_0x5f92('0x3'));var db=require(_0x5f92('0x4'))['db'];var authService=require('../../components/auth/service');var utils=require(_0x5f92('0x5'));var client=jayson[_0x5f92('0x6')][_0x5f92('0x7')]({'port':0x232b});function localAuthenticate(_0x484788,_0x9cf95e,_0xd5b7f4,_0x28c227){var _0x40e627,_0x2277b2;return db[_0x5f92('0x8')]['findOne']({'where':{'id':0x1},'attributes':['allowedLoginAttempts',_0x5f92('0x9')],'raw':!![]})['then'](function(_0x21b51a){_0x40e627=_0x21b51a;return _0x484788[_0x5f92('0xa')]({'attributes':['id',_0x5f92('0xb'),'password',_0x5f92('0xc'),_0x5f92('0xd'),'disabled',_0x5f92('0xe'),'blockedAt',_0x5f92('0xf')],'where':{'name':_0x9cf95e,'role':{'$or':[_0x5f92('0x10'),_0x5f92('0x11'),_0x5f92('0x12')]}}});})[_0x5f92('0x13')](function(_0x1c94d2){if(!_0x1c94d2||_0x1c94d2&&_0x1c94d2[_0x5f92('0xb')]!==_0x9cf95e){throw new Error(_0x5f92('0x14'));}else{_0x2277b2=_0x1c94d2;return _0x2277b2;}})['then'](function(){_0x2277b2[_0x5f92('0x15')](_0xd5b7f4,function(_0xc1d590,_0x285f96){var _0x32ccbb=_0x2277b2['loginAttempts']+0x1;if(_0xc1d590){return _0x28c227(_0xc1d590);}else if(!_0x285f96){if(_0x2277b2[_0x5f92('0x16')]){return _0x28c227(null,![],{'message':_0x5f92('0x14')});}else if(_0x40e627[_0x5f92('0x17')]>0x0){if(_0x2277b2[_0x5f92('0xe')]){if(_0x40e627['blockDuration']>0x0){if(moment(_0x2277b2[_0x5f92('0x18')])[_0x5f92('0x19')](_0x40e627[_0x5f92('0x9')],_0x5f92('0x1a'))>moment()){return _0x28c227(null,![],{'message':_0x5f92('0x14')});}else{return utils[_0x5f92('0x1b')](_0x2277b2,_0x32ccbb,![],null)['then'](function(){return _0x28c227(null,![],{'message':_0x5f92('0x14')});});}}else{return _0x28c227(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}}else{if(_0x32ccbb>=_0x40e627[_0x5f92('0x17')]){return utils['clearBlockUser'](_0x2277b2,0x0,!![],moment())[_0x5f92('0x13')](function(){return _0x28c227(null,![],{'message':_0x5f92('0x14')});});}else{return utils[_0x5f92('0x1b')](_0x2277b2,_0x32ccbb,![],null)[_0x5f92('0x13')](function(){return _0x28c227(null,![],{'message':_0x5f92('0x14')});});}}}else{return _0x28c227(null,![],{'message':_0x5f92('0x14')});}}else{if(_0x2277b2[_0x5f92('0x16')]){return _0x28c227(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}else if(_0x40e627[_0x5f92('0x17')]>0x0){if(_0x2277b2['blocked']){if(_0x40e627[_0x5f92('0x9')]>0x0){if(moment(_0x2277b2[_0x5f92('0x18')])[_0x5f92('0x19')](_0x40e627['blockDuration'],_0x5f92('0x1a'))>moment()){return _0x28c227(null,![],{'message':_0x5f92('0x14')});}else{return utils[_0x5f92('0x1b')](_0x2277b2,0x0,![],null)['then'](function(){return utils[_0x5f92('0x1c')](_0x2277b2);})[_0x5f92('0x13')](function(){return _0x28c227(null,_0x2277b2);});}}else{return _0x28c227(null,![],{'message':_0x5f92('0x14')});}}else{return utils[_0x5f92('0x1b')](_0x2277b2,0x0,![],null)['then'](function(){return utils[_0x5f92('0x1c')](_0x2277b2);})[_0x5f92('0x13')](function(){return _0x28c227(null,_0x2277b2);})['catch'](function(_0x273ccd){return _0x28c227(null,![],_0x273ccd);});}}else{if(_0x2277b2[_0x5f92('0xe')]){return _0x28c227(null,![],{'message':_0x5f92('0x14')});}else{return utils[_0x5f92('0x1c')](_0x2277b2)['then'](function(){return _0x28c227(null,_0x2277b2);});}}}});})[_0x5f92('0x1d')](function(_0x5210fb){return _0x28c227(null,![],_0x5210fb);});}function sendServiceMail(_0x23f5b8){return db[_0x5f92('0x1e')][_0x5f92('0x1f')]({'where':{'service':!![]},'attributes':['id',_0x5f92('0xb'),_0x5f92('0x20')],'include':[{'model':db['MailServerOut'],'as':_0x5f92('0x21')}]})['then'](function(_0x1b42c3){if(!_0x1b42c3){throw new Error('Unable\x20to\x20find\x20a\x20configured\x20SMTP\x20service!');}var _0x150b97={'tls':{'rejectUnauthorized':![]}};if(_0x1b42c3[_0x5f92('0x21')][_0x5f92('0x22')]){_0x150b97[_0x5f92('0x22')]=_0x1b42c3['Smtp'][_0x5f92('0x22')];}else{_0x150b97[_0x5f92('0x23')]=_0x1b42c3['Smtp'][_0x5f92('0x23')];_0x150b97['port']=_0x1b42c3[_0x5f92('0x21')][_0x5f92('0x24')];_0x150b97[_0x5f92('0x25')]=_0x1b42c3[_0x5f92('0x21')][_0x5f92('0x25')];}if(_0x1b42c3[_0x5f92('0x21')][_0x5f92('0x26')]){_0x150b97[_0x5f92('0x27')]={'user':_0x1b42c3[_0x5f92('0x21')][_0x5f92('0x11')],'pass':_0x1b42c3['Smtp'][_0x5f92('0x28')]};}var _0x973d95={'account':_0x150b97,'message':_[_0x5f92('0x29')]({'from':util[_0x5f92('0x2a')](_0x5f92('0x2b'),_0x1b42c3[_0x5f92('0xb')],_0x1b42c3[_0x5f92('0x20')]||_0x1b42c3[_0x5f92('0x21')][_0x5f92('0x11')])},_0x23f5b8)};return client[_0x5f92('0x2c')](_0x5f92('0x2d'),_0x973d95);})[_0x5f92('0x13')](function(_0xe4f5ad){if(_0xe4f5ad[_0x5f92('0x2e')]){throw new Error(util['format'](_0x5f92('0x2f'),_0xe4f5ad[_0x5f92('0x2e')][_0x5f92('0x30')]||JSON[_0x5f92('0x31')](_0xe4f5ad[_0x5f92('0x2e')])));}return!![];});}function handleError(_0x54ff4e,_0xda5613,_0x4fb864){return _0x54ff4e[_0x5f92('0x32')](_0xda5613||0x1f4)[_0x5f92('0x33')]({'message':_0x4fb864?_0x4fb864['message']||_0x4fb864:''});}exports[_0x5f92('0x34')]=function(_0x4fa6fb,_0x4ae1a8,_0x48301a){passport[_0x5f92('0x15')]('local',function(_0x1f5a2e,_0x2c9cb1,_0x217512){var _0x22d2cf=_0x1f5a2e||_0x217512;if(_0x22d2cf){return handleError(_0x4ae1a8,0x191,_0x22d2cf);}if(!_0x2c9cb1){return handleError(_0x4ae1a8,0x194,{'message':_0x5f92('0x35')});}else{var _0x4a2cfd={'payload':{'id':_0x2c9cb1['id'],'role':_0x2c9cb1[_0x5f92('0xd')]},'options':{'expiresIn':0x15180}};return authService[_0x5f92('0x36')](_0x4a2cfd)['then'](function(_0x16fe6a){_0x4ae1a8[_0x5f92('0x37')]({'id':_0x2c9cb1['id'],'token':_0x16fe6a,'statusCall':'','isAlreadyLogged':_0x2c9cb1['online']});});}})(_0x4fa6fb,_0x4ae1a8,_0x48301a);};exports[_0x5f92('0x38')]=function(_0xbdb8ac,_0x36d63a,_0x3f4020){if(!_0xbdb8ac[_0x5f92('0x39')]['email']){return handleError(_0x36d63a,0x1f4,{'message':'Email\x20parameter\x20is\x20missing'});}var _0x3a07dc=rs[_0x5f92('0x3a')]();_0xbdb8ac[_0x5f92('0x39')][_0x5f92('0x20')]=_0xbdb8ac[_0x5f92('0x39')][_0x5f92('0x20')]['trim']()['toLowerCase']();var _0x43e45f;return db[_0x5f92('0x8')]['findOne']({'where':{'id':0x1},'attributes':[_0x5f92('0x17'),_0x5f92('0x9')],'raw':!![]})[_0x5f92('0x13')](function(_0x1cbacf){_0x43e45f=_0x1cbacf;return db['User'][_0x5f92('0x1f')]({'where':{'email':_0xbdb8ac[_0x5f92('0x39')][_0x5f92('0x20')]}});})[_0x5f92('0x13')](function(_0x1fbf69){if(!_0x1fbf69){throw new Error('This\x20email\x20is\x20not\x20registered.');}if(_0x1fbf69[_0x5f92('0x16')]){throw new Error(_0x5f92('0x3b'));}if(_0x1fbf69[_0x5f92('0xe')]){if(moment(_0x1fbf69['blockedAt'])[_0x5f92('0x19')](_0x43e45f[_0x5f92('0x9')],_0x5f92('0x1a'))>moment()){throw new Error(_0x5f92('0x3c'));}else{_0x1fbf69[_0x5f92('0xe')]=![];_0x1fbf69['blockedAt']=null;}}_0x1fbf69['resetPasswordToken']=_0x3a07dc;_0x1fbf69['resetPasswordExpires']=Date[_0x5f92('0x3d')]()+0x36ee80;return _0x1fbf69[_0x5f92('0x3e')]();})[_0x5f92('0x13')](function(){return sendServiceMail({'to':_0xbdb8ac[_0x5f92('0x39')][_0x5f92('0x20')],'subject':_0x5f92('0x3f'),'text':_0x5f92('0x40')+'Please\x20click\x20on\x20the\x20following\x20link,\x20or\x20paste\x20this\x20into\x20your\x20browser\x20to\x20complete\x20the\x20process:\x0a\x0a'+_0x5f92('0x41')+_0xbdb8ac[_0x5f92('0x42')][_0x5f92('0x23')]+_0x5f92('0x43')+_0x3a07dc+'\x0a\x0a'+_0x5f92('0x44')});})['then'](function(){_0x36d63a['status'](0xc8)[_0x5f92('0x33')]({'message':_0x5f92('0x45')});})['catch'](function(_0x2d4d4b){return handleError(_0x36d63a,0x1f4,_0x2d4d4b);});};exports['reset']=function(_0x5cdbff,_0x476a07,_0x2c71fc){return db[_0x5f92('0x46')][_0x5f92('0x1f')]({'where':{'resetPasswordToken':_0x5cdbff[_0x5f92('0x47')][_0x5f92('0x48')],'resetPasswordExpires':{'$gt':Date[_0x5f92('0x3d')]()}}})[_0x5f92('0x13')](function(_0x498bd5){if(!_0x498bd5){throw new Error(_0x5f92('0x49'));}return db[_0x5f92('0x8')]['findOne']({'attributes':['id',_0x5f92('0x4a'),_0x5f92('0x4b'),_0x5f92('0x4c')]})[_0x5f92('0x13')](function(_0xca9229){if(_0xca9229[_0x5f92('0x4a')])authService[_0x5f92('0x4d')](_0x5cdbff[_0x5f92('0x39')]['password']);if(!_0xca9229[_0x5f92('0x4b')]||!_0x498bd5[_0x5f92('0x4e')])return _0x498bd5;authService[_0x5f92('0x4f')](_0x5cdbff['body'][_0x5f92('0x50')],_0x498bd5[_0x5f92('0x4e')],_0xca9229[_0x5f92('0x4c')]);return _0x498bd5;})['then'](function(){_0x498bd5[_0x5f92('0x50')]=_0x5cdbff[_0x5f92('0x39')][_0x5f92('0x50')];_0x498bd5[_0x5f92('0x51')]=null;_0x498bd5[_0x5f92('0x52')]=null;return _0x498bd5[_0x5f92('0x3e')]();});})[_0x5f92('0x13')](function(){_0x476a07['status'](0xc8)[_0x5f92('0x33')]({'message':_0x5f92('0x53')});})['catch'](function(_0x404c18){return handleError(_0x476a07,0x1f4,_0x404c18);});};passport[_0x5f92('0x54')](new LocalStrategy({'usernameField':'name','passwordField':_0x5f92('0x50')},function(_0x2d3168,_0x1a82d9,_0x3c3cbf){return localAuthenticate(db[_0x5f92('0x46')],_0x2d3168,_0x1a82d9,_0x3c3cbf);}));