Built motion from commit d5e4af8c.|2.6.23
[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 _0x8c5f=['name','password','salt','role','disabled','blocked','blockedAt','loginAttempts','agent','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','authenticate','allowedLoginAttempts','blockDuration','add','minutes','clearBlockUser','getLicense','catch','MailAccount','find','email','MailServerOut','Smtp','CloudProvider','getSmtpOptions','merge','format','\x22%s\x22\x20<%s>','SendMail','error','message','stringify','status','send','login','local-login','Something\x20went\x20wrong,\x20please\x20try\x20again.','signToken','json','online','body','generate','trim','toLowerCase','findOne','This\x20email\x20is\x20not\x20registered.','The\x20user\x20is\x20disabled.\x20Please\x20contact\x20the\x20administrator.','The\x20user\x20is\x20blocked.\x20Please\x20contact\x20the\x20administrator.','resetPasswordToken','resetPasswordExpires','now','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','headers','host','/reset/','If\x20you\x20did\x20not\x20request\x20this,\x20please\x20ignore\x20this\x20email\x20and\x20your\x20password\x20will\x20remain\x20unchanged.\x0a','Reset\x20Email\x20sent!','reset','token','Password\x20reset\x20token\x20is\x20invalid\x20or\x20has\x20expired.','enforcePasswordHistory','passwordHistoryLimit','securePassword','previousPasswords','validatePasswordHistory','save','Password\x20reset!','use','User','util','passport','passport-local','Strategy','jayson/promise','randomstring','../../mysqldb','../../config/utils','client','http','Setting','then'];(function(_0x389698,_0x1486e4){var _0x2e5391=function(_0x224c56){while(--_0x224c56){_0x389698['push'](_0x389698['shift']());}};_0x2e5391(++_0x1486e4);}(_0x8c5f,0x98));var _0xf8c5=function(_0x2e0af0,_0xace130){_0x2e0af0=_0x2e0af0-0x0;var _0x3d8f9e=_0x8c5f[_0x2e0af0];return _0x3d8f9e;};'use strict';var util=require(_0xf8c5('0x0'));var passport=require(_0xf8c5('0x1'));var LocalStrategy=require(_0xf8c5('0x2'))[_0xf8c5('0x3')];var jayson=require(_0xf8c5('0x4'));var _=require('lodash');var rs=require(_0xf8c5('0x5'));var moment=require('moment');var db=require(_0xf8c5('0x6'))['db'];var authService=require('../../components/auth/service');var utils=require(_0xf8c5('0x7'));var client=jayson[_0xf8c5('0x8')][_0xf8c5('0x9')]({'port':0x232b});function localAuthenticate(_0x34a3ec,_0x9ddd63,_0x204a89,_0x2c4673){var _0x11d94d,_0x1d8e71;return db[_0xf8c5('0xa')]['findOne']({'where':{'id':0x1},'attributes':['allowedLoginAttempts','blockDuration'],'raw':!![]})[_0xf8c5('0xb')](function(_0x4e1cb9){_0x11d94d=_0x4e1cb9;return _0x34a3ec['findOne']({'attributes':['id',_0xf8c5('0xc'),_0xf8c5('0xd'),_0xf8c5('0xe'),_0xf8c5('0xf'),_0xf8c5('0x10'),_0xf8c5('0x11'),_0xf8c5('0x12'),_0xf8c5('0x13')],'where':{'name':_0x9ddd63,'role':{'$or':['admin','user',_0xf8c5('0x14')]}}});})['then'](function(_0xbdb452){if(!_0xbdb452||_0xbdb452&&_0xbdb452[_0xf8c5('0xc')]!==_0x9ddd63){throw new Error(_0xf8c5('0x15'));}else{_0x1d8e71=_0xbdb452;return _0x1d8e71;}})[_0xf8c5('0xb')](function(){_0x1d8e71[_0xf8c5('0x16')](_0x204a89,function(_0xd63683,_0x4b9e10){var _0xb8d15e=_0x1d8e71[_0xf8c5('0x13')]+0x1;if(_0xd63683){return _0x2c4673(_0xd63683);}else if(!_0x4b9e10){if(_0x1d8e71[_0xf8c5('0x10')]){return _0x2c4673(null,![],{'message':_0xf8c5('0x15')});}else if(_0x11d94d[_0xf8c5('0x17')]>0x0){if(_0x1d8e71['blocked']){if(_0x11d94d[_0xf8c5('0x18')]>0x0){if(moment(_0x1d8e71['blockedAt'])[_0xf8c5('0x19')](_0x11d94d[_0xf8c5('0x18')],_0xf8c5('0x1a'))>moment()){return _0x2c4673(null,![],{'message':_0xf8c5('0x15')});}else{return utils['clearBlockUser'](_0x1d8e71,_0xb8d15e,![],null)[_0xf8c5('0xb')](function(){return _0x2c4673(null,![],{'message':_0xf8c5('0x15')});});}}else{return _0x2c4673(null,![],{'message':_0xf8c5('0x15')});}}else{if(_0xb8d15e>=_0x11d94d[_0xf8c5('0x17')]){return utils['clearBlockUser'](_0x1d8e71,0x0,!![],moment())[_0xf8c5('0xb')](function(){return _0x2c4673(null,![],{'message':_0xf8c5('0x15')});});}else{return utils[_0xf8c5('0x1b')](_0x1d8e71,_0xb8d15e,![],null)[_0xf8c5('0xb')](function(){return _0x2c4673(null,![],{'message':_0xf8c5('0x15')});});}}}else{return _0x2c4673(null,![],{'message':_0xf8c5('0x15')});}}else{if(_0x1d8e71[_0xf8c5('0x10')]){return _0x2c4673(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}else if(_0x11d94d[_0xf8c5('0x17')]>0x0){if(_0x1d8e71[_0xf8c5('0x11')]){if(_0x11d94d['blockDuration']>0x0){if(moment(_0x1d8e71[_0xf8c5('0x12')])[_0xf8c5('0x19')](_0x11d94d['blockDuration'],_0xf8c5('0x1a'))>moment()){return _0x2c4673(null,![],{'message':_0xf8c5('0x15')});}else{return utils[_0xf8c5('0x1b')](_0x1d8e71,0x0,![],null)[_0xf8c5('0xb')](function(){return utils['getLicense'](_0x1d8e71);})[_0xf8c5('0xb')](function(){return _0x2c4673(null,_0x1d8e71);});}}else{return _0x2c4673(null,![],{'message':_0xf8c5('0x15')});}}else{return utils[_0xf8c5('0x1b')](_0x1d8e71,0x0,![],null)[_0xf8c5('0xb')](function(){return utils[_0xf8c5('0x1c')](_0x1d8e71);})['then'](function(){return _0x2c4673(null,_0x1d8e71);})[_0xf8c5('0x1d')](function(_0x24f07a){return _0x2c4673(null,![],_0x24f07a);});}}else{if(_0x1d8e71[_0xf8c5('0x11')]){return _0x2c4673(null,![],{'message':_0xf8c5('0x15')});}else{return utils['getLicense'](_0x1d8e71)[_0xf8c5('0xb')](function(){return _0x2c4673(null,_0x1d8e71);});}}}});})[_0xf8c5('0x1d')](function(_0x135697){return _0x2c4673(null,![],_0x135697);});}function sendServiceMail(_0x4df27f){return db[_0xf8c5('0x1e')][_0xf8c5('0x1f')]({'where':{'service':!![]},'attributes':['id',_0xf8c5('0xc'),_0xf8c5('0x20')],'include':[{'model':db[_0xf8c5('0x21')],'as':_0xf8c5('0x22'),'include':[{'model':db[_0xf8c5('0x23')],'as':_0xf8c5('0x23')}]}]})[_0xf8c5('0xb')](function(_0x282c31){if(!_0x282c31){throw new Error('Unable\x20to\x20find\x20a\x20configured\x20SMTP\x20service!');}var _0x2243cb=_0x282c31[_0xf8c5('0x24')]();var _0x14d6ca={'account':_0x2243cb,'message':_[_0xf8c5('0x25')]({'from':util[_0xf8c5('0x26')](_0xf8c5('0x27'),_0x282c31[_0xf8c5('0xc')],_0x282c31[_0xf8c5('0x20')]||_0x282c31[_0xf8c5('0x22')]['user'])},_0x4df27f)};return client['request'](_0xf8c5('0x28'),_0x14d6ca);})[_0xf8c5('0xb')](function(_0x3f4650){if(_0x3f4650['error']){throw new Error(util['format']('SendMail\x20error:\x20%s.\x20Please\x20contact\x20your\x20Administrator!',_0x3f4650[_0xf8c5('0x29')][_0xf8c5('0x2a')]||JSON[_0xf8c5('0x2b')](_0x3f4650[_0xf8c5('0x29')])));}return!![];});}function handleError(_0x412274,_0x2efa6e,_0x27d5ff){return _0x412274[_0xf8c5('0x2c')](_0x2efa6e||0x1f4)[_0xf8c5('0x2d')]({'message':_0x27d5ff?_0x27d5ff['message']||_0x27d5ff:''});}exports[_0xf8c5('0x2e')]=function(_0x33bbab,_0x1c499a,_0x3cadca){passport[_0xf8c5('0x16')](_0xf8c5('0x2f'),function(_0x175e23,_0x6337cc,_0x50c739){var _0x360495=_0x175e23||_0x50c739;if(_0x360495){return handleError(_0x1c499a,0x191,_0x360495);}if(!_0x6337cc){return handleError(_0x1c499a,0x194,{'message':_0xf8c5('0x30')});}else{var _0x2bf96b={'payload':{'id':_0x6337cc['id'],'role':_0x6337cc['role']},'options':{'expiresIn':0x15180}};return authService[_0xf8c5('0x31')](_0x2bf96b)[_0xf8c5('0xb')](function(_0x27be54){_0x1c499a[_0xf8c5('0x32')]({'id':_0x6337cc['id'],'token':_0x27be54,'statusCall':'','isAlreadyLogged':_0x6337cc[_0xf8c5('0x33')]});});}})(_0x33bbab,_0x1c499a,_0x3cadca);};exports['forgot']=function(_0xcae1ec,_0x2a4a74){if(!_0xcae1ec[_0xf8c5('0x34')][_0xf8c5('0x20')]){return handleError(_0x2a4a74,0x1f4,{'message':'Email\x20parameter\x20is\x20missing'});}var _0x2ec69c=rs[_0xf8c5('0x35')]();_0xcae1ec[_0xf8c5('0x34')][_0xf8c5('0x20')]=_0xcae1ec[_0xf8c5('0x34')][_0xf8c5('0x20')][_0xf8c5('0x36')]()[_0xf8c5('0x37')]();var _0x4b765b;return db['Setting'][_0xf8c5('0x38')]({'where':{'id':0x1},'attributes':['allowedLoginAttempts','blockDuration'],'raw':!![]})[_0xf8c5('0xb')](function(_0x1c6ed4){_0x4b765b=_0x1c6ed4;return db['User'][_0xf8c5('0x1f')]({'where':{'email':_0xcae1ec[_0xf8c5('0x34')][_0xf8c5('0x20')]}});})[_0xf8c5('0xb')](function(_0x508096){if(!_0x508096){throw new Error(_0xf8c5('0x39'));}if(_0x508096[_0xf8c5('0x10')]){throw new Error(_0xf8c5('0x3a'));}if(_0x508096[_0xf8c5('0x11')]){if(moment(_0x508096['blockedAt'])[_0xf8c5('0x19')](_0x4b765b['blockDuration'],'minutes')>moment()){throw new Error(_0xf8c5('0x3b'));}else{_0x508096[_0xf8c5('0x11')]=![];_0x508096[_0xf8c5('0x12')]=null;}}_0x508096[_0xf8c5('0x3c')]=_0x2ec69c;_0x508096[_0xf8c5('0x3d')]=Date[_0xf8c5('0x3e')]()+0x36ee80;return _0x508096['save']();})[_0xf8c5('0xb')](function(){return sendServiceMail({'to':_0xcae1ec['body']['email'],'subject':_0xf8c5('0x3f'),'text':_0xf8c5('0x40')+'Please\x20click\x20on\x20the\x20following\x20link,\x20or\x20paste\x20this\x20into\x20your\x20browser\x20to\x20complete\x20the\x20process:\x0a\x0a'+'https://'+_0xcae1ec[_0xf8c5('0x41')][_0xf8c5('0x42')]+_0xf8c5('0x43')+_0x2ec69c+'\x0a\x0a'+_0xf8c5('0x44')});})[_0xf8c5('0xb')](function(){_0x2a4a74[_0xf8c5('0x2c')](0xc8)[_0xf8c5('0x2d')]({'message':_0xf8c5('0x45')});})['catch'](function(_0x100c84){return handleError(_0x2a4a74,0x1f4,_0x100c84);});};exports[_0xf8c5('0x46')]=function(_0x2d539b,_0x3d226f){return db['User'][_0xf8c5('0x1f')]({'where':{'resetPasswordToken':_0x2d539b['params'][_0xf8c5('0x47')],'resetPasswordExpires':{'$gt':Date[_0xf8c5('0x3e')]()}}})['then'](function(_0x2b375d){if(!_0x2b375d){throw new Error(_0xf8c5('0x48'));}return db[_0xf8c5('0xa')][_0xf8c5('0x38')]({'attributes':['id','securePassword',_0xf8c5('0x49'),_0xf8c5('0x4a')]})[_0xf8c5('0xb')](function(_0x793e3b){if(_0x793e3b[_0xf8c5('0x4b')])authService['validatePasswordPattern'](_0x2d539b[_0xf8c5('0x34')][_0xf8c5('0xd')]);if(!_0x793e3b[_0xf8c5('0x49')]||!_0x2b375d[_0xf8c5('0x4c')])return _0x2b375d;authService[_0xf8c5('0x4d')](_0x2d539b[_0xf8c5('0x34')]['password'],_0x2b375d[_0xf8c5('0x4c')],_0x793e3b[_0xf8c5('0x4a')]);return _0x2b375d;})[_0xf8c5('0xb')](function(){_0x2b375d[_0xf8c5('0xd')]=_0x2d539b[_0xf8c5('0x34')][_0xf8c5('0xd')];_0x2b375d[_0xf8c5('0x3c')]=null;_0x2b375d['resetPasswordExpires']=null;return _0x2b375d[_0xf8c5('0x4e')]();});})[_0xf8c5('0xb')](function(){_0x3d226f[_0xf8c5('0x2c')](0xc8)[_0xf8c5('0x2d')]({'message':_0xf8c5('0x4f')});})[_0xf8c5('0x1d')](function(_0x42e548){return handleError(_0x3d226f,0x1f4,_0x42e548);});};passport[_0xf8c5('0x50')]('local-login',new LocalStrategy({'usernameField':'name','passwordField':_0xf8c5('0xd')},function(_0x1c6666,_0x595177,_0x3da426){return localAuthenticate(db[_0xf8c5('0x51')],_0x1c6666,_0x595177,_0x3da426);}));