88948bf0a6b0f0866970d67223f79f60f6d5c5fe
[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 _0x5902=['signToken','json','online','body','Email\x20parameter\x20is\x20missing','trim','toLowerCase','User','This\x20email\x20is\x20not\x20registered.','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','Please\x20click\x20on\x20the\x20following\x20link,\x20or\x20paste\x20this\x20into\x20your\x20browser\x20to\x20complete\x20the\x20process:\x0a\x0a','https://','headers','host','If\x20you\x20did\x20not\x20request\x20this,\x20please\x20ignore\x20this\x20email\x20and\x20your\x20password\x20will\x20remain\x20unchanged.\x0a','send','Reset\x20Email\x20sent!','token','Password\x20reset\x20token\x20is\x20invalid\x20or\x20has\x20expired.','securePassword','enforcePasswordHistory','passwordHistoryLimit','validatePasswordPattern','previousPasswords','validatePasswordHistory','resetPasswordToken','resetPasswordExpires','status','Password\x20reset!','use','util','passport','Strategy','jayson/promise','lodash','../../mysqldb','../../components/auth/service','../../config/utils','client','http','Setting','findOne','allowedLoginAttempts','blockDuration','then','password','disabled','blocked','loginAttempts','admin','user','agent','name','authenticate','add','clearBlockUser','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','blockedAt','minutes','getLicense','catch','MailAccount','find','email','MailServerOut','Smtp','CloudProvider','Unable\x20to\x20find\x20a\x20configured\x20SMTP\x20service!','getSmtpOptions','merge','format','\x22%s\x22\x20<%s>','SendMail','error','SendMail\x20error:\x20%s.\x20Please\x20contact\x20your\x20Administrator!','message','stringify','local-login'];(function(_0x2c7618,_0x26be9c){var _0x5ce3f0=function(_0x1837b3){while(--_0x1837b3){_0x2c7618['push'](_0x2c7618['shift']());}};_0x5ce3f0(++_0x26be9c);}(_0x5902,0x11c));var _0x2590=function(_0x4b369d,_0x265eb8){_0x4b369d=_0x4b369d-0x0;var _0x571a98=_0x5902[_0x4b369d];return _0x571a98;};'use strict';var util=require(_0x2590('0x0'));var passport=require(_0x2590('0x1'));var LocalStrategy=require('passport-local')[_0x2590('0x2')];var jayson=require(_0x2590('0x3'));var _=require(_0x2590('0x4'));var rs=require('randomstring');var moment=require('moment');var db=require(_0x2590('0x5'))['db'];var authService=require(_0x2590('0x6'));var utils=require(_0x2590('0x7'));var client=jayson[_0x2590('0x8')][_0x2590('0x9')]({'port':0x232b});function localAuthenticate(_0x3e50e7,_0x4ecff7,_0x4c5746,_0x2d4a63){var _0x12ee06,_0x47ce3c;return db[_0x2590('0xa')][_0x2590('0xb')]({'where':{'id':0x1},'attributes':[_0x2590('0xc'),_0x2590('0xd')],'raw':!![]})[_0x2590('0xe')](function(_0x28851f){_0x12ee06=_0x28851f;return _0x3e50e7[_0x2590('0xb')]({'attributes':['id','name',_0x2590('0xf'),'salt','role',_0x2590('0x10'),_0x2590('0x11'),'blockedAt',_0x2590('0x12')],'where':{'name':_0x4ecff7,'role':{'$or':[_0x2590('0x13'),_0x2590('0x14'),_0x2590('0x15')]}}});})[_0x2590('0xe')](function(_0x3d110e){if(!_0x3d110e||_0x3d110e&&_0x3d110e[_0x2590('0x16')]!==_0x4ecff7){throw new Error('Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.');}else{_0x47ce3c=_0x3d110e;return _0x47ce3c;}})[_0x2590('0xe')](function(){_0x47ce3c[_0x2590('0x17')](_0x4c5746,function(_0x28bf70,_0x5054e4){var _0x1cd234=_0x47ce3c[_0x2590('0x12')]+0x1;if(_0x28bf70){return _0x2d4a63(_0x28bf70);}else if(!_0x5054e4){if(_0x47ce3c[_0x2590('0x10')]){return _0x2d4a63(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}else if(_0x12ee06[_0x2590('0xc')]>0x0){if(_0x47ce3c['blocked']){if(_0x12ee06[_0x2590('0xd')]>0x0){if(moment(_0x47ce3c['blockedAt'])[_0x2590('0x18')](_0x12ee06['blockDuration'],'minutes')>moment()){return _0x2d4a63(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}else{return utils[_0x2590('0x19')](_0x47ce3c,_0x1cd234,![],null)[_0x2590('0xe')](function(){return _0x2d4a63(null,![],{'message':_0x2590('0x1a')});});}}else{return _0x2d4a63(null,![],{'message':_0x2590('0x1a')});}}else{if(_0x1cd234>=_0x12ee06[_0x2590('0xc')]){return utils[_0x2590('0x19')](_0x47ce3c,0x0,!![],moment())[_0x2590('0xe')](function(){return _0x2d4a63(null,![],{'message':_0x2590('0x1a')});});}else{return utils[_0x2590('0x19')](_0x47ce3c,_0x1cd234,![],null)['then'](function(){return _0x2d4a63(null,![],{'message':_0x2590('0x1a')});});}}}else{return _0x2d4a63(null,![],{'message':_0x2590('0x1a')});}}else{if(_0x47ce3c['disabled']){return _0x2d4a63(null,![],{'message':_0x2590('0x1a')});}else if(_0x12ee06['allowedLoginAttempts']>0x0){if(_0x47ce3c[_0x2590('0x11')]){if(_0x12ee06[_0x2590('0xd')]>0x0){if(moment(_0x47ce3c[_0x2590('0x1b')])[_0x2590('0x18')](_0x12ee06[_0x2590('0xd')],_0x2590('0x1c'))>moment()){return _0x2d4a63(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}else{return utils[_0x2590('0x19')](_0x47ce3c,0x0,![],null)[_0x2590('0xe')](function(){return utils['getLicense'](_0x47ce3c);})[_0x2590('0xe')](function(){return _0x2d4a63(null,_0x47ce3c);});}}else{return _0x2d4a63(null,![],{'message':_0x2590('0x1a')});}}else{return utils['clearBlockUser'](_0x47ce3c,0x0,![],null)['then'](function(){return utils[_0x2590('0x1d')](_0x47ce3c);})['then'](function(){return _0x2d4a63(null,_0x47ce3c);})['catch'](function(_0x1e7003){return _0x2d4a63(null,![],_0x1e7003);});}}else{if(_0x47ce3c[_0x2590('0x11')]){return _0x2d4a63(null,![],{'message':_0x2590('0x1a')});}else{return utils[_0x2590('0x1d')](_0x47ce3c)['then'](function(){return _0x2d4a63(null,_0x47ce3c);});}}}});})[_0x2590('0x1e')](function(_0x42246d){return _0x2d4a63(null,![],_0x42246d);});}function sendServiceMail(_0x3affec){return db[_0x2590('0x1f')][_0x2590('0x20')]({'where':{'service':!![]},'attributes':['id','name',_0x2590('0x21')],'include':[{'model':db[_0x2590('0x22')],'as':_0x2590('0x23'),'include':[{'model':db[_0x2590('0x24')],'as':'CloudProvider'}]}]})['then'](function(_0x1c567f){if(!_0x1c567f){throw new Error(_0x2590('0x25'));}var _0x1fa20e=_0x1c567f[_0x2590('0x26')]();var _0x56a056={'account':_0x1fa20e,'message':_[_0x2590('0x27')]({'from':util[_0x2590('0x28')](_0x2590('0x29'),_0x1c567f[_0x2590('0x16')],_0x1c567f[_0x2590('0x21')]||_0x1c567f['Smtp'][_0x2590('0x14')])},_0x3affec)};return client['request'](_0x2590('0x2a'),_0x56a056);})['then'](function(_0x496235){if(_0x496235[_0x2590('0x2b')]){throw new Error(util[_0x2590('0x28')](_0x2590('0x2c'),_0x496235['error'][_0x2590('0x2d')]||JSON[_0x2590('0x2e')](_0x496235[_0x2590('0x2b')])));}return!![];});}function handleError(_0x1fa2c7,_0x16410c,_0x1ac72b){return _0x1fa2c7['status'](_0x16410c||0x1f4)['send']({'message':_0x1ac72b?_0x1ac72b[_0x2590('0x2d')]||_0x1ac72b:''});}exports['login']=function(_0x22fd50,_0x3211f0,_0x1e4678){passport['authenticate'](_0x2590('0x2f'),function(_0x44ca8d,_0x361eb5,_0x507484){var _0x5eedc3=_0x44ca8d||_0x507484;if(_0x5eedc3){return handleError(_0x3211f0,0x191,_0x5eedc3);}if(!_0x361eb5){return handleError(_0x3211f0,0x194,{'message':'Something\x20went\x20wrong,\x20please\x20try\x20again.'});}else{var _0x39416b={'payload':{'id':_0x361eb5['id'],'role':_0x361eb5['role']},'options':{'expiresIn':0x15180}};return authService[_0x2590('0x30')](_0x39416b)[_0x2590('0xe')](function(_0x4fba9e){_0x3211f0[_0x2590('0x31')]({'id':_0x361eb5['id'],'token':_0x4fba9e,'statusCall':'','isAlreadyLogged':_0x361eb5[_0x2590('0x32')]});});}})(_0x22fd50,_0x3211f0,_0x1e4678);};exports['forgot']=function(_0x446b84,_0x228a39){if(!_0x446b84[_0x2590('0x33')][_0x2590('0x21')]){return handleError(_0x228a39,0x1f4,{'message':_0x2590('0x34')});}var _0x1456aa=rs['generate']();_0x446b84[_0x2590('0x33')]['email']=_0x446b84[_0x2590('0x33')]['email'][_0x2590('0x35')]()[_0x2590('0x36')]();var _0x35fc58;return db[_0x2590('0xa')][_0x2590('0xb')]({'where':{'id':0x1},'attributes':[_0x2590('0xc'),_0x2590('0xd')],'raw':!![]})[_0x2590('0xe')](function(_0x20f597){_0x35fc58=_0x20f597;return db[_0x2590('0x37')]['find']({'where':{'email':_0x446b84['body'][_0x2590('0x21')]}});})['then'](function(_0x142f15){if(!_0x142f15){throw new Error(_0x2590('0x38'));}if(_0x142f15['disabled']){throw new Error(_0x2590('0x39'));}if(_0x142f15['blocked']){if(moment(_0x142f15[_0x2590('0x1b')])[_0x2590('0x18')](_0x35fc58[_0x2590('0xd')],_0x2590('0x1c'))>moment()){throw new Error(_0x2590('0x3a'));}else{_0x142f15['blocked']=![];_0x142f15['blockedAt']=null;}}_0x142f15['resetPasswordToken']=_0x1456aa;_0x142f15['resetPasswordExpires']=Date[_0x2590('0x3b')]()+0x36ee80;return _0x142f15[_0x2590('0x3c')]();})[_0x2590('0xe')](function(){return sendServiceMail({'to':_0x446b84[_0x2590('0x33')][_0x2590('0x21')],'subject':_0x2590('0x3d'),'text':_0x2590('0x3e')+_0x2590('0x3f')+_0x2590('0x40')+_0x446b84[_0x2590('0x41')][_0x2590('0x42')]+'/reset/'+_0x1456aa+'\x0a\x0a'+_0x2590('0x43')});})[_0x2590('0xe')](function(){_0x228a39['status'](0xc8)[_0x2590('0x44')]({'message':_0x2590('0x45')});})[_0x2590('0x1e')](function(_0x16c7c3){return handleError(_0x228a39,0x1f4,_0x16c7c3);});};exports['reset']=function(_0x338c99,_0x3c0c00){return db['User'][_0x2590('0x20')]({'where':{'resetPasswordToken':_0x338c99['params'][_0x2590('0x46')],'resetPasswordExpires':{'$gt':Date['now']()}}})['then'](function(_0x3f4bc5){if(!_0x3f4bc5){throw new Error(_0x2590('0x47'));}return db[_0x2590('0xa')][_0x2590('0xb')]({'attributes':['id',_0x2590('0x48'),_0x2590('0x49'),_0x2590('0x4a')]})[_0x2590('0xe')](function(_0x192daa){if(_0x192daa[_0x2590('0x48')])authService[_0x2590('0x4b')](_0x338c99[_0x2590('0x33')][_0x2590('0xf')]);if(!_0x192daa[_0x2590('0x49')]||!_0x3f4bc5[_0x2590('0x4c')])return _0x3f4bc5;authService[_0x2590('0x4d')](_0x338c99['body'][_0x2590('0xf')],_0x3f4bc5['previousPasswords'],_0x192daa[_0x2590('0x4a')]);return _0x3f4bc5;})[_0x2590('0xe')](function(){_0x3f4bc5['password']=_0x338c99['body'][_0x2590('0xf')];_0x3f4bc5[_0x2590('0x4e')]=null;_0x3f4bc5[_0x2590('0x4f')]=null;return _0x3f4bc5[_0x2590('0x3c')]();});})[_0x2590('0xe')](function(){_0x3c0c00[_0x2590('0x50')](0xc8)[_0x2590('0x44')]({'message':_0x2590('0x51')});})[_0x2590('0x1e')](function(_0x2860d3){return handleError(_0x3c0c00,0x1f4,_0x2860d3);});};passport[_0x2590('0x52')](_0x2590('0x2f'),new LocalStrategy({'usernameField':_0x2590('0x16'),'passwordField':_0x2590('0xf')},function(_0x2f7d65,_0x5c13a5,_0x5a8ada){return localAuthenticate(db[_0x2590('0x37')],_0x2f7d65,_0x5c13a5,_0x5a8ada);}));