Built motion from commit (unavailable).|2.5.13
[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 _0x4819=['status','login','Something\x20went\x20wrong,\x20please\x20try\x20again.','signToken','json','forgot','body','Email\x20parameter\x20is\x20missing','generate','trim','toLowerCase','This\x20email\x20is\x20not\x20registered.','The\x20user\x20is\x20blocked.\x20Please\x20contact\x20the\x20administrator.','resetPasswordToken','resetPasswordExpires','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','/reset/','Reset\x20Email\x20sent!','reset','User','now','Password\x20reset\x20token\x20is\x20invalid\x20or\x20has\x20expired.','securePassword','enforcePasswordHistory','validatePasswordPattern','previousPasswords','validatePasswordHistory','send','Password\x20reset!','use','passport','passport-local','Strategy','jayson/promise','lodash','randomstring','moment','../../mysqldb','../../components/auth/service','../../config/utils','http','Setting','allowedLoginAttempts','then','findOne','name','password','salt','role','disabled','blocked','blockedAt','loginAttempts','agent','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','authenticate','blockDuration','minutes','clearBlockUser','add','getLicense','catch','find','email','MailServerOut','Smtp','Unable\x20to\x20find\x20a\x20configured\x20SMTP\x20service!','service','host','secure','authentication','auth','pass','format','\x22%s\x22\x20<%s>','user','SendMail','error','SendMail\x20error:\x20%s.\x20Please\x20contact\x20your\x20Administrator!','message'];(function(_0x122ef9,_0x451d2a){var _0x42fba0=function(_0x17c9d3){while(--_0x17c9d3){_0x122ef9['push'](_0x122ef9['shift']());}};_0x42fba0(++_0x451d2a);}(_0x4819,0xcd));var _0x9481=function(_0x1a7276,_0x4d3e59){_0x1a7276=_0x1a7276-0x0;var _0x163cbb=_0x4819[_0x1a7276];return _0x163cbb;};'use strict';var util=require('util');var passport=require(_0x9481('0x0'));var LocalStrategy=require(_0x9481('0x1'))[_0x9481('0x2')];var jayson=require(_0x9481('0x3'));var _=require(_0x9481('0x4'));var rs=require(_0x9481('0x5'));var moment=require(_0x9481('0x6'));var db=require(_0x9481('0x7'))['db'];var authService=require(_0x9481('0x8'));var utils=require(_0x9481('0x9'));var client=jayson['client'][_0x9481('0xa')]({'port':0x232b});function localAuthenticate(_0x5def07,_0x54cc78,_0x14e05a,_0x6c6a08){var _0x27c033,_0x3278ff;return db[_0x9481('0xb')]['findOne']({'where':{'id':0x1},'attributes':[_0x9481('0xc'),'blockDuration'],'raw':!![]})[_0x9481('0xd')](function(_0x564d5b){_0x27c033=_0x564d5b;return _0x5def07[_0x9481('0xe')]({'attributes':['id',_0x9481('0xf'),_0x9481('0x10'),_0x9481('0x11'),_0x9481('0x12'),_0x9481('0x13'),_0x9481('0x14'),_0x9481('0x15'),_0x9481('0x16')],'where':{'name':_0x54cc78,'role':{'$or':['admin','user',_0x9481('0x17')]}}});})[_0x9481('0xd')](function(_0x2f0032){if(!_0x2f0032||_0x2f0032&&_0x2f0032[_0x9481('0xf')]!==_0x54cc78){throw new Error(_0x9481('0x18'));}else{_0x3278ff=_0x2f0032;return _0x3278ff;}})[_0x9481('0xd')](function(){_0x3278ff[_0x9481('0x19')](_0x14e05a,function(_0x495b30,_0x4220f1){var _0x7a7ee=_0x3278ff['loginAttempts']+0x1;if(_0x495b30){return _0x6c6a08(_0x495b30);}else if(!_0x4220f1){if(_0x3278ff[_0x9481('0x13')]){return _0x6c6a08(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}else if(_0x27c033[_0x9481('0xc')]>0x0){if(_0x3278ff[_0x9481('0x14')]){if(_0x27c033[_0x9481('0x1a')]>0x0){if(moment(_0x3278ff[_0x9481('0x15')])['add'](_0x27c033['blockDuration'],_0x9481('0x1b'))>moment()){return _0x6c6a08(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}else{return utils['clearBlockUser'](_0x3278ff,_0x7a7ee,![],null)[_0x9481('0xd')](function(){return _0x6c6a08(null,![],{'message':_0x9481('0x18')});});}}else{return _0x6c6a08(null,![],{'message':_0x9481('0x18')});}}else{if(_0x7a7ee>=_0x27c033[_0x9481('0xc')]){return utils[_0x9481('0x1c')](_0x3278ff,0x0,!![],moment())[_0x9481('0xd')](function(){return _0x6c6a08(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});});}else{return utils[_0x9481('0x1c')](_0x3278ff,_0x7a7ee,![],null)[_0x9481('0xd')](function(){return _0x6c6a08(null,![],{'message':_0x9481('0x18')});});}}}else{return _0x6c6a08(null,![],{'message':_0x9481('0x18')});}}else{if(_0x3278ff[_0x9481('0x13')]){return _0x6c6a08(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}else if(_0x27c033[_0x9481('0xc')]>0x0){if(_0x3278ff[_0x9481('0x14')]){if(_0x27c033['blockDuration']>0x0){if(moment(_0x3278ff[_0x9481('0x15')])[_0x9481('0x1d')](_0x27c033[_0x9481('0x1a')],_0x9481('0x1b'))>moment()){return _0x6c6a08(null,![],{'message':_0x9481('0x18')});}else{return utils['clearBlockUser'](_0x3278ff,0x0,![],null)[_0x9481('0xd')](function(){return utils[_0x9481('0x1e')](_0x3278ff);})['then'](function(){return _0x6c6a08(null,_0x3278ff);});}}else{return _0x6c6a08(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}}else{return utils[_0x9481('0x1c')](_0x3278ff,0x0,![],null)[_0x9481('0xd')](function(){return utils['getLicense'](_0x3278ff);})[_0x9481('0xd')](function(){return _0x6c6a08(null,_0x3278ff);})[_0x9481('0x1f')](function(_0xeff0a5){return _0x6c6a08(null,![],_0xeff0a5);});}}else{if(_0x3278ff[_0x9481('0x14')]){return _0x6c6a08(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}else{return utils[_0x9481('0x1e')](_0x3278ff)[_0x9481('0xd')](function(){return _0x6c6a08(null,_0x3278ff);});}}}});})[_0x9481('0x1f')](function(_0x587706){return _0x6c6a08(null,![],_0x587706);});}function sendServiceMail(_0x1791b2){return db['MailAccount'][_0x9481('0x20')]({'where':{'service':!![]},'attributes':['id',_0x9481('0xf'),_0x9481('0x21')],'include':[{'model':db[_0x9481('0x22')],'as':_0x9481('0x23')}]})['then'](function(_0x2aa9ff){if(!_0x2aa9ff){throw new Error(_0x9481('0x24'));}var _0x46e4ab={'tls':{'rejectUnauthorized':![]}};if(_0x2aa9ff['Smtp']['service']){_0x46e4ab[_0x9481('0x25')]=_0x2aa9ff[_0x9481('0x23')][_0x9481('0x25')];}else{_0x46e4ab[_0x9481('0x26')]=_0x2aa9ff['Smtp']['host'];_0x46e4ab['port']=_0x2aa9ff[_0x9481('0x23')]['port'];_0x46e4ab[_0x9481('0x27')]=_0x2aa9ff['Smtp'][_0x9481('0x27')];}if(_0x2aa9ff[_0x9481('0x23')][_0x9481('0x28')]){_0x46e4ab[_0x9481('0x29')]={'user':_0x2aa9ff[_0x9481('0x23')]['user'],'pass':_0x2aa9ff[_0x9481('0x23')][_0x9481('0x2a')]};}var _0x2323c7={'account':_0x46e4ab,'message':_['merge']({'from':util[_0x9481('0x2b')](_0x9481('0x2c'),_0x2aa9ff[_0x9481('0xf')],_0x2aa9ff[_0x9481('0x21')]||_0x2aa9ff[_0x9481('0x23')][_0x9481('0x2d')])},_0x1791b2)};return client['request'](_0x9481('0x2e'),_0x2323c7);})['then'](function(_0x203ef9){if(_0x203ef9[_0x9481('0x2f')]){throw new Error(util[_0x9481('0x2b')](_0x9481('0x30'),_0x203ef9[_0x9481('0x2f')][_0x9481('0x31')]||JSON['stringify'](_0x203ef9[_0x9481('0x2f')])));}return!![];});}function handleError(_0x15e27f,_0x4ed991,_0x5e00ad){return _0x15e27f[_0x9481('0x32')](_0x4ed991||0x1f4)['send']({'message':_0x5e00ad?_0x5e00ad[_0x9481('0x31')]||_0x5e00ad:''});}exports[_0x9481('0x33')]=function(_0x2c2a1e,_0x2a1bcf,_0x50667a){passport[_0x9481('0x19')]('local',function(_0x62b05b,_0x56f151,_0x2ce972){var _0x4d6a13=_0x62b05b||_0x2ce972;if(_0x4d6a13){return handleError(_0x2a1bcf,0x191,_0x4d6a13);}if(!_0x56f151){return handleError(_0x2a1bcf,0x194,{'message':_0x9481('0x34')});}else{var _0x22e916={'payload':{'id':_0x56f151['id'],'role':_0x56f151[_0x9481('0x12')]},'options':{'expiresIn':0x15180}};return authService[_0x9481('0x35')](_0x22e916)[_0x9481('0xd')](function(_0x3b7998){_0x2a1bcf[_0x9481('0x36')]({'id':_0x56f151['id'],'token':_0x3b7998,'statusCall':'','isAlreadyLogged':_0x56f151['online']});});}})(_0x2c2a1e,_0x2a1bcf,_0x50667a);};exports[_0x9481('0x37')]=function(_0x3857e8,_0x406e4a,_0x30168b){if(!_0x3857e8[_0x9481('0x38')][_0x9481('0x21')]){return handleError(_0x406e4a,0x1f4,{'message':_0x9481('0x39')});}var _0x4e081d=rs[_0x9481('0x3a')]();_0x3857e8[_0x9481('0x38')]['email']=_0x3857e8[_0x9481('0x38')][_0x9481('0x21')][_0x9481('0x3b')]()[_0x9481('0x3c')]();var _0x29a8e9;return db['Setting'][_0x9481('0xe')]({'where':{'id':0x1},'attributes':['allowedLoginAttempts',_0x9481('0x1a')],'raw':!![]})[_0x9481('0xd')](function(_0x4a42b8){_0x29a8e9=_0x4a42b8;return db['User'][_0x9481('0x20')]({'where':{'email':_0x3857e8[_0x9481('0x38')][_0x9481('0x21')]}});})['then'](function(_0x4402f6){if(!_0x4402f6){throw new Error(_0x9481('0x3d'));}if(_0x4402f6['disabled']){throw new Error('The\x20user\x20is\x20disabled.\x20Please\x20contact\x20the\x20administrator.');}if(_0x4402f6[_0x9481('0x14')]){if(moment(_0x4402f6['blockedAt'])[_0x9481('0x1d')](_0x29a8e9[_0x9481('0x1a')],_0x9481('0x1b'))>moment()){throw new Error(_0x9481('0x3e'));}else{_0x4402f6[_0x9481('0x14')]=![];_0x4402f6['blockedAt']=null;}}_0x4402f6[_0x9481('0x3f')]=_0x4e081d;_0x4402f6[_0x9481('0x40')]=Date['now']()+0x36ee80;return _0x4402f6[_0x9481('0x41')]();})[_0x9481('0xd')](function(){return sendServiceMail({'to':_0x3857e8[_0x9481('0x38')][_0x9481('0x21')],'subject':_0x9481('0x42'),'text':_0x9481('0x43')+_0x9481('0x44')+_0x9481('0x45')+_0x3857e8[_0x9481('0x46')]['host']+_0x9481('0x47')+_0x4e081d+'\x0a\x0a'+'If\x20you\x20did\x20not\x20request\x20this,\x20please\x20ignore\x20this\x20email\x20and\x20your\x20password\x20will\x20remain\x20unchanged.\x0a'});})['then'](function(){_0x406e4a[_0x9481('0x32')](0xc8)['send']({'message':_0x9481('0x48')});})[_0x9481('0x1f')](function(_0x47f8b2){return handleError(_0x406e4a,0x1f4,_0x47f8b2);});};exports[_0x9481('0x49')]=function(_0x1044a2,_0x2f3413,_0x13b674){return db[_0x9481('0x4a')][_0x9481('0x20')]({'where':{'resetPasswordToken':_0x1044a2['params']['token'],'resetPasswordExpires':{'$gt':Date[_0x9481('0x4b')]()}}})[_0x9481('0xd')](function(_0x728c42){if(!_0x728c42){throw new Error(_0x9481('0x4c'));}return db[_0x9481('0xb')][_0x9481('0xe')]({'attributes':['id',_0x9481('0x4d'),_0x9481('0x4e'),'passwordHistoryLimit']})[_0x9481('0xd')](function(_0x143c80){if(_0x143c80[_0x9481('0x4d')])authService[_0x9481('0x4f')](_0x1044a2[_0x9481('0x38')]['password']);if(!_0x143c80['enforcePasswordHistory']||!_0x728c42[_0x9481('0x50')])return _0x728c42;authService[_0x9481('0x51')](_0x1044a2[_0x9481('0x38')]['password'],_0x728c42['previousPasswords'],_0x143c80['passwordHistoryLimit']);return _0x728c42;})['then'](function(){_0x728c42['password']=_0x1044a2['body']['password'];_0x728c42[_0x9481('0x3f')]=null;_0x728c42[_0x9481('0x40')]=null;return _0x728c42['save']();});})[_0x9481('0xd')](function(){_0x2f3413['status'](0xc8)[_0x9481('0x52')]({'message':_0x9481('0x53')});})[_0x9481('0x1f')](function(_0x84ab39){return handleError(_0x2f3413,0x1f4,_0x84ab39);});};passport[_0x9481('0x54')](new LocalStrategy({'usernameField':_0x9481('0xf'),'passwordField':_0x9481('0x10')},function(_0x4c27e4,_0x26b41f,_0x1b51b7){return localAuthenticate(db['User'],_0x4c27e4,_0x26b41f,_0x1b51b7);}));