46cc883c74ef2b97ca279f3cf7a3e94e7fd4e72e
[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 _0xf812=['auth','merge','format','\x22%s\x22\x20<%s>','SendMail','SendMail\x20error:\x20%s.\x20Please\x20contact\x20your\x20Administrator!','message','stringify','error','status','send','login','authenticate','local-login','signToken','online','body','Email\x20parameter\x20is\x20missing','generate','trim','User','This\x20email\x20is\x20not\x20registered.','The\x20user\x20is\x20blocked.\x20Please\x20contact\x20the\x20administrator.','resetPasswordExpires','now','save','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://','headers','/reset/','Reset\x20Email\x20sent!','reset','find','params','token','Password\x20reset\x20token\x20is\x20invalid\x20or\x20has\x20expired.','enforcePasswordHistory','securePassword','validatePasswordHistory','passwordHistoryLimit','resetPasswordToken','use','util','Strategy','jayson/promise','lodash','randomstring','../../mysqldb','../../components/auth/service','../../config/utils','http','Setting','findOne','allowedLoginAttempts','then','password','salt','role','disabled','blocked','blockedAt','loginAttempts','admin','user','name','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','blockDuration','add','minutes','clearBlockUser','getLicense','catch','MailAccount','email','MailServerOut','service','Smtp','host','port','secure','authentication'];(function(_0x576faa,_0x40825d){var _0x455ed3=function(_0x50adbd){while(--_0x50adbd){_0x576faa['push'](_0x576faa['shift']());}};_0x455ed3(++_0x40825d);}(_0xf812,0x121));var _0x2f81=function(_0x4104df,_0x313661){_0x4104df=_0x4104df-0x0;var _0x91f27c=_0xf812[_0x4104df];return _0x91f27c;};'use strict';var util=require(_0x2f81('0x0'));var passport=require('passport');var LocalStrategy=require('passport-local')[_0x2f81('0x1')];var jayson=require(_0x2f81('0x2'));var _=require(_0x2f81('0x3'));var rs=require(_0x2f81('0x4'));var moment=require('moment');var db=require(_0x2f81('0x5'))['db'];var authService=require(_0x2f81('0x6'));var utils=require(_0x2f81('0x7'));var client=jayson['client'][_0x2f81('0x8')]({'port':0x232b});function localAuthenticate(_0x36d3a4,_0x12f288,_0x5dde33,_0x54a8a1){var _0x24df73,_0x33a11d;return db[_0x2f81('0x9')][_0x2f81('0xa')]({'where':{'id':0x1},'attributes':[_0x2f81('0xb'),'blockDuration'],'raw':!![]})[_0x2f81('0xc')](function(_0x2d88f8){_0x24df73=_0x2d88f8;return _0x36d3a4[_0x2f81('0xa')]({'attributes':['id','name',_0x2f81('0xd'),_0x2f81('0xe'),_0x2f81('0xf'),_0x2f81('0x10'),_0x2f81('0x11'),_0x2f81('0x12'),_0x2f81('0x13')],'where':{'name':_0x12f288,'role':{'$or':[_0x2f81('0x14'),_0x2f81('0x15'),'agent']}}});})[_0x2f81('0xc')](function(_0x52ee2a){if(!_0x52ee2a||_0x52ee2a&&_0x52ee2a[_0x2f81('0x16')]!==_0x12f288){throw new Error('Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.');}else{_0x33a11d=_0x52ee2a;return _0x33a11d;}})['then'](function(){_0x33a11d['authenticate'](_0x5dde33,function(_0x5a4a6f,_0x79417c){var _0x22e48a=_0x33a11d['loginAttempts']+0x1;if(_0x5a4a6f){return _0x54a8a1(_0x5a4a6f);}else if(!_0x79417c){if(_0x33a11d['disabled']){return _0x54a8a1(null,![],{'message':_0x2f81('0x17')});}else if(_0x24df73[_0x2f81('0xb')]>0x0){if(_0x33a11d[_0x2f81('0x11')]){if(_0x24df73[_0x2f81('0x18')]>0x0){if(moment(_0x33a11d['blockedAt'])[_0x2f81('0x19')](_0x24df73['blockDuration'],_0x2f81('0x1a'))>moment()){return _0x54a8a1(null,![],{'message':_0x2f81('0x17')});}else{return utils[_0x2f81('0x1b')](_0x33a11d,_0x22e48a,![],null)['then'](function(){return _0x54a8a1(null,![],{'message':_0x2f81('0x17')});});}}else{return _0x54a8a1(null,![],{'message':_0x2f81('0x17')});}}else{if(_0x22e48a>=_0x24df73[_0x2f81('0xb')]){return utils[_0x2f81('0x1b')](_0x33a11d,0x0,!![],moment())[_0x2f81('0xc')](function(){return _0x54a8a1(null,![],{'message':_0x2f81('0x17')});});}else{return utils[_0x2f81('0x1b')](_0x33a11d,_0x22e48a,![],null)[_0x2f81('0xc')](function(){return _0x54a8a1(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});});}}}else{return _0x54a8a1(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}}else{if(_0x33a11d[_0x2f81('0x10')]){return _0x54a8a1(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}else if(_0x24df73[_0x2f81('0xb')]>0x0){if(_0x33a11d['blocked']){if(_0x24df73[_0x2f81('0x18')]>0x0){if(moment(_0x33a11d['blockedAt'])[_0x2f81('0x19')](_0x24df73[_0x2f81('0x18')],'minutes')>moment()){return _0x54a8a1(null,![],{'message':_0x2f81('0x17')});}else{return utils[_0x2f81('0x1b')](_0x33a11d,0x0,![],null)[_0x2f81('0xc')](function(){return utils[_0x2f81('0x1c')](_0x33a11d);})[_0x2f81('0xc')](function(){return _0x54a8a1(null,_0x33a11d);});}}else{return _0x54a8a1(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}}else{return utils[_0x2f81('0x1b')](_0x33a11d,0x0,![],null)[_0x2f81('0xc')](function(){return utils[_0x2f81('0x1c')](_0x33a11d);})[_0x2f81('0xc')](function(){return _0x54a8a1(null,_0x33a11d);})[_0x2f81('0x1d')](function(_0x32aa2c){return _0x54a8a1(null,![],_0x32aa2c);});}}else{if(_0x33a11d[_0x2f81('0x11')]){return _0x54a8a1(null,![],{'message':_0x2f81('0x17')});}else{return utils[_0x2f81('0x1c')](_0x33a11d)['then'](function(){return _0x54a8a1(null,_0x33a11d);});}}}});})[_0x2f81('0x1d')](function(_0x197c56){return _0x54a8a1(null,![],_0x197c56);});}function sendServiceMail(_0x485037){return db[_0x2f81('0x1e')]['find']({'where':{'service':!![]},'attributes':['id',_0x2f81('0x16'),_0x2f81('0x1f')],'include':[{'model':db[_0x2f81('0x20')],'as':'Smtp'}]})[_0x2f81('0xc')](function(_0xb845dc){if(!_0xb845dc){throw new Error('Unable\x20to\x20find\x20a\x20configured\x20SMTP\x20service!');}var _0x56a868={'tls':{'rejectUnauthorized':![]}};if(_0xb845dc['Smtp'][_0x2f81('0x21')]){_0x56a868['service']=_0xb845dc[_0x2f81('0x22')][_0x2f81('0x21')];}else{_0x56a868['host']=_0xb845dc[_0x2f81('0x22')][_0x2f81('0x23')];_0x56a868['port']=_0xb845dc[_0x2f81('0x22')][_0x2f81('0x24')];_0x56a868[_0x2f81('0x25')]=_0xb845dc['Smtp'][_0x2f81('0x25')];}if(_0xb845dc[_0x2f81('0x22')][_0x2f81('0x26')]){_0x56a868[_0x2f81('0x27')]={'user':_0xb845dc['Smtp']['user'],'pass':_0xb845dc[_0x2f81('0x22')]['pass']};}var _0x15cee9={'account':_0x56a868,'message':_[_0x2f81('0x28')]({'from':util[_0x2f81('0x29')](_0x2f81('0x2a'),_0xb845dc[_0x2f81('0x16')],_0xb845dc['email']||_0xb845dc[_0x2f81('0x22')][_0x2f81('0x15')])},_0x485037)};return client['request'](_0x2f81('0x2b'),_0x15cee9);})[_0x2f81('0xc')](function(_0x12e2c5){if(_0x12e2c5['error']){throw new Error(util['format'](_0x2f81('0x2c'),_0x12e2c5['error'][_0x2f81('0x2d')]||JSON[_0x2f81('0x2e')](_0x12e2c5[_0x2f81('0x2f')])));}return!![];});}function handleError(_0x44e7f9,_0x372509,_0x320453){return _0x44e7f9[_0x2f81('0x30')](_0x372509||0x1f4)[_0x2f81('0x31')]({'message':_0x320453?_0x320453['message']||_0x320453:''});}exports[_0x2f81('0x32')]=function(_0x44a4f7,_0x420b73,_0x4ee169){passport[_0x2f81('0x33')](_0x2f81('0x34'),function(_0x44b166,_0x44b221,_0x46b366){var _0x461f14=_0x44b166||_0x46b366;if(_0x461f14){return handleError(_0x420b73,0x191,_0x461f14);}if(!_0x44b221){return handleError(_0x420b73,0x194,{'message':'Something\x20went\x20wrong,\x20please\x20try\x20again.'});}else{var _0x5576bb={'payload':{'id':_0x44b221['id'],'role':_0x44b221[_0x2f81('0xf')]},'options':{'expiresIn':0x15180}};return authService[_0x2f81('0x35')](_0x5576bb)[_0x2f81('0xc')](function(_0x17b447){_0x420b73['json']({'id':_0x44b221['id'],'token':_0x17b447,'statusCall':'','isAlreadyLogged':_0x44b221[_0x2f81('0x36')]});});}})(_0x44a4f7,_0x420b73,_0x4ee169);};exports['forgot']=function(_0x133107,_0x2d5fba,_0xf867da){if(!_0x133107[_0x2f81('0x37')]['email']){return handleError(_0x2d5fba,0x1f4,{'message':_0x2f81('0x38')});}var _0x1b7dbf=rs[_0x2f81('0x39')]();_0x133107[_0x2f81('0x37')][_0x2f81('0x1f')]=_0x133107[_0x2f81('0x37')][_0x2f81('0x1f')][_0x2f81('0x3a')]()['toLowerCase']();var _0x58154e;return db[_0x2f81('0x9')][_0x2f81('0xa')]({'where':{'id':0x1},'attributes':[_0x2f81('0xb'),'blockDuration'],'raw':!![]})[_0x2f81('0xc')](function(_0x1d3f84){_0x58154e=_0x1d3f84;return db[_0x2f81('0x3b')]['find']({'where':{'email':_0x133107[_0x2f81('0x37')][_0x2f81('0x1f')]}});})[_0x2f81('0xc')](function(_0xb6121c){if(!_0xb6121c){throw new Error(_0x2f81('0x3c'));}if(_0xb6121c[_0x2f81('0x10')]){throw new Error('The\x20user\x20is\x20disabled.\x20Please\x20contact\x20the\x20administrator.');}if(_0xb6121c[_0x2f81('0x11')]){if(moment(_0xb6121c['blockedAt'])['add'](_0x58154e[_0x2f81('0x18')],_0x2f81('0x1a'))>moment()){throw new Error(_0x2f81('0x3d'));}else{_0xb6121c[_0x2f81('0x11')]=![];_0xb6121c[_0x2f81('0x12')]=null;}}_0xb6121c['resetPasswordToken']=_0x1b7dbf;_0xb6121c[_0x2f81('0x3e')]=Date[_0x2f81('0x3f')]()+0x36ee80;return _0xb6121c[_0x2f81('0x40')]();})['then'](function(){return sendServiceMail({'to':_0x133107[_0x2f81('0x37')]['email'],'subject':'xCally\x20Motion\x20Password\x20Reset','text':_0x2f81('0x41')+_0x2f81('0x42')+_0x2f81('0x43')+_0x133107[_0x2f81('0x44')][_0x2f81('0x23')]+_0x2f81('0x45')+_0x1b7dbf+'\x0a\x0a'+'If\x20you\x20did\x20not\x20request\x20this,\x20please\x20ignore\x20this\x20email\x20and\x20your\x20password\x20will\x20remain\x20unchanged.\x0a'});})['then'](function(){_0x2d5fba[_0x2f81('0x30')](0xc8)[_0x2f81('0x31')]({'message':_0x2f81('0x46')});})[_0x2f81('0x1d')](function(_0x32e59f){return handleError(_0x2d5fba,0x1f4,_0x32e59f);});};exports[_0x2f81('0x47')]=function(_0x3cf3f7,_0x39039f,_0x25a58e){return db[_0x2f81('0x3b')][_0x2f81('0x48')]({'where':{'resetPasswordToken':_0x3cf3f7[_0x2f81('0x49')][_0x2f81('0x4a')],'resetPasswordExpires':{'$gt':Date[_0x2f81('0x3f')]()}}})[_0x2f81('0xc')](function(_0x3631ac){if(!_0x3631ac){throw new Error(_0x2f81('0x4b'));}return db[_0x2f81('0x9')][_0x2f81('0xa')]({'attributes':['id','securePassword',_0x2f81('0x4c'),'passwordHistoryLimit']})[_0x2f81('0xc')](function(_0x4b402f){if(_0x4b402f[_0x2f81('0x4d')])authService['validatePasswordPattern'](_0x3cf3f7[_0x2f81('0x37')][_0x2f81('0xd')]);if(!_0x4b402f[_0x2f81('0x4c')]||!_0x3631ac['previousPasswords'])return _0x3631ac;authService[_0x2f81('0x4e')](_0x3cf3f7[_0x2f81('0x37')]['password'],_0x3631ac['previousPasswords'],_0x4b402f[_0x2f81('0x4f')]);return _0x3631ac;})[_0x2f81('0xc')](function(){_0x3631ac[_0x2f81('0xd')]=_0x3cf3f7[_0x2f81('0x37')][_0x2f81('0xd')];_0x3631ac[_0x2f81('0x50')]=null;_0x3631ac[_0x2f81('0x3e')]=null;return _0x3631ac[_0x2f81('0x40')]();});})[_0x2f81('0xc')](function(){_0x39039f['status'](0xc8)[_0x2f81('0x31')]({'message':'Password\x20reset!'});})[_0x2f81('0x1d')](function(_0xe6787f){return handleError(_0x39039f,0x1f4,_0xe6787f);});};passport[_0x2f81('0x51')](_0x2f81('0x34'),new LocalStrategy({'usernameField':_0x2f81('0x16'),'passwordField':_0x2f81('0xd')},function(_0x46eee7,_0x37b78f,_0x2a72af){return localAuthenticate(db[_0x2f81('0x3b')],_0x46eee7,_0x37b78f,_0x2a72af);}));