Built motion from commit 753c950e.|2.5.42
[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 _0xfe62=['disabled','blocked','blockedAt','user','agent','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','authenticate','loginAttempts','add','clearBlockUser','allowedLoginAttempts','minutes','getLicense','catch','MailAccount','find','email','Unable\x20to\x20find\x20a\x20configured\x20SMTP\x20service!','Smtp','service','port','secure','authentication','auth','pass','merge','format','request','error','message','stringify','status','local-login','Something\x20went\x20wrong,\x20please\x20try\x20again.','signToken','json','online','forgot','body','Email\x20parameter\x20is\x20missing','toLowerCase','User','The\x20user\x20is\x20disabled.\x20Please\x20contact\x20the\x20administrator.','The\x20user\x20is\x20blocked.\x20Please\x20contact\x20the\x20administrator.','resetPasswordExpires','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','/reset/','If\x20you\x20did\x20not\x20request\x20this,\x20please\x20ignore\x20this\x20email\x20and\x20your\x20password\x20will\x20remain\x20unchanged.\x0a','send','Reset\x20Email\x20sent!','reset','params','token','securePassword','enforcePasswordHistory','passwordHistoryLimit','validatePasswordPattern','validatePasswordHistory','previousPasswords','Password\x20reset!','util','passport','passport-local','Strategy','jayson/promise','lodash','randomstring','moment','../../config/utils','client','http','Setting','findOne','blockDuration','then','name','password','role'];(function(_0x4dfcd4,_0x20c39c){var _0x4cc2a0=function(_0x4ea11b){while(--_0x4ea11b){_0x4dfcd4['push'](_0x4dfcd4['shift']());}};_0x4cc2a0(++_0x20c39c);}(_0xfe62,0x197));var _0x2fe6=function(_0x88f03f,_0x41dad0){_0x88f03f=_0x88f03f-0x0;var _0x3e2d02=_0xfe62[_0x88f03f];return _0x3e2d02;};'use strict';var util=require(_0x2fe6('0x0'));var passport=require(_0x2fe6('0x1'));var LocalStrategy=require(_0x2fe6('0x2'))[_0x2fe6('0x3')];var jayson=require(_0x2fe6('0x4'));var _=require(_0x2fe6('0x5'));var rs=require(_0x2fe6('0x6'));var moment=require(_0x2fe6('0x7'));var db=require('../../mysqldb')['db'];var authService=require('../../components/auth/service');var utils=require(_0x2fe6('0x8'));var client=jayson[_0x2fe6('0x9')][_0x2fe6('0xa')]({'port':0x232b});function localAuthenticate(_0x2fc5c5,_0x44355a,_0xa611d8,_0x5d3175){var _0x51628e,_0x31038b;return db[_0x2fe6('0xb')][_0x2fe6('0xc')]({'where':{'id':0x1},'attributes':['allowedLoginAttempts',_0x2fe6('0xd')],'raw':!![]})[_0x2fe6('0xe')](function(_0x5661d2){_0x51628e=_0x5661d2;return _0x2fc5c5[_0x2fe6('0xc')]({'attributes':['id',_0x2fe6('0xf'),_0x2fe6('0x10'),'salt',_0x2fe6('0x11'),_0x2fe6('0x12'),_0x2fe6('0x13'),_0x2fe6('0x14'),'loginAttempts'],'where':{'name':_0x44355a,'role':{'$or':['admin',_0x2fe6('0x15'),_0x2fe6('0x16')]}}});})[_0x2fe6('0xe')](function(_0x4c54ac){if(!_0x4c54ac||_0x4c54ac&&_0x4c54ac['name']!==_0x44355a){throw new Error(_0x2fe6('0x17'));}else{_0x31038b=_0x4c54ac;return _0x31038b;}})[_0x2fe6('0xe')](function(){_0x31038b[_0x2fe6('0x18')](_0xa611d8,function(_0x555b41,_0x42e89d){var _0x1f2436=_0x31038b[_0x2fe6('0x19')]+0x1;if(_0x555b41){return _0x5d3175(_0x555b41);}else if(!_0x42e89d){if(_0x31038b[_0x2fe6('0x12')]){return _0x5d3175(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}else if(_0x51628e['allowedLoginAttempts']>0x0){if(_0x31038b['blocked']){if(_0x51628e['blockDuration']>0x0){if(moment(_0x31038b[_0x2fe6('0x14')])[_0x2fe6('0x1a')](_0x51628e[_0x2fe6('0xd')],'minutes')>moment()){return _0x5d3175(null,![],{'message':_0x2fe6('0x17')});}else{return utils[_0x2fe6('0x1b')](_0x31038b,_0x1f2436,![],null)[_0x2fe6('0xe')](function(){return _0x5d3175(null,![],{'message':_0x2fe6('0x17')});});}}else{return _0x5d3175(null,![],{'message':_0x2fe6('0x17')});}}else{if(_0x1f2436>=_0x51628e[_0x2fe6('0x1c')]){return utils['clearBlockUser'](_0x31038b,0x0,!![],moment())[_0x2fe6('0xe')](function(){return _0x5d3175(null,![],{'message':_0x2fe6('0x17')});});}else{return utils[_0x2fe6('0x1b')](_0x31038b,_0x1f2436,![],null)[_0x2fe6('0xe')](function(){return _0x5d3175(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});});}}}else{return _0x5d3175(null,![],{'message':_0x2fe6('0x17')});}}else{if(_0x31038b['disabled']){return _0x5d3175(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}else if(_0x51628e['allowedLoginAttempts']>0x0){if(_0x31038b[_0x2fe6('0x13')]){if(_0x51628e[_0x2fe6('0xd')]>0x0){if(moment(_0x31038b['blockedAt'])['add'](_0x51628e['blockDuration'],_0x2fe6('0x1d'))>moment()){return _0x5d3175(null,![],{'message':_0x2fe6('0x17')});}else{return utils[_0x2fe6('0x1b')](_0x31038b,0x0,![],null)[_0x2fe6('0xe')](function(){return utils[_0x2fe6('0x1e')](_0x31038b);})[_0x2fe6('0xe')](function(){return _0x5d3175(null,_0x31038b);});}}else{return _0x5d3175(null,![],{'message':_0x2fe6('0x17')});}}else{return utils['clearBlockUser'](_0x31038b,0x0,![],null)[_0x2fe6('0xe')](function(){return utils[_0x2fe6('0x1e')](_0x31038b);})['then'](function(){return _0x5d3175(null,_0x31038b);})[_0x2fe6('0x1f')](function(_0x2f74ed){return _0x5d3175(null,![],_0x2f74ed);});}}else{if(_0x31038b[_0x2fe6('0x13')]){return _0x5d3175(null,![],{'message':_0x2fe6('0x17')});}else{return utils[_0x2fe6('0x1e')](_0x31038b)[_0x2fe6('0xe')](function(){return _0x5d3175(null,_0x31038b);});}}}});})['catch'](function(_0x496a9d){return _0x5d3175(null,![],_0x496a9d);});}function sendServiceMail(_0x4b1f1f){return db[_0x2fe6('0x20')][_0x2fe6('0x21')]({'where':{'service':!![]},'attributes':['id',_0x2fe6('0xf'),_0x2fe6('0x22')],'include':[{'model':db['MailServerOut'],'as':'Smtp'}]})[_0x2fe6('0xe')](function(_0x3c11c6){if(!_0x3c11c6){throw new Error(_0x2fe6('0x23'));}var _0x38c823={'tls':{'rejectUnauthorized':![]}};if(_0x3c11c6[_0x2fe6('0x24')][_0x2fe6('0x25')]){_0x38c823[_0x2fe6('0x25')]=_0x3c11c6[_0x2fe6('0x24')][_0x2fe6('0x25')];}else{_0x38c823['host']=_0x3c11c6['Smtp']['host'];_0x38c823[_0x2fe6('0x26')]=_0x3c11c6['Smtp']['port'];_0x38c823['secure']=_0x3c11c6[_0x2fe6('0x24')][_0x2fe6('0x27')];}if(_0x3c11c6[_0x2fe6('0x24')][_0x2fe6('0x28')]){_0x38c823[_0x2fe6('0x29')]={'user':_0x3c11c6[_0x2fe6('0x24')]['user'],'pass':_0x3c11c6[_0x2fe6('0x24')][_0x2fe6('0x2a')]};}var _0x4d1c09={'account':_0x38c823,'message':_[_0x2fe6('0x2b')]({'from':util[_0x2fe6('0x2c')]('\x22%s\x22\x20<%s>',_0x3c11c6[_0x2fe6('0xf')],_0x3c11c6[_0x2fe6('0x22')]||_0x3c11c6[_0x2fe6('0x24')]['user'])},_0x4b1f1f)};return client[_0x2fe6('0x2d')]('SendMail',_0x4d1c09);})[_0x2fe6('0xe')](function(_0x333312){if(_0x333312[_0x2fe6('0x2e')]){throw new Error(util[_0x2fe6('0x2c')]('SendMail\x20error:\x20%s.\x20Please\x20contact\x20your\x20Administrator!',_0x333312[_0x2fe6('0x2e')][_0x2fe6('0x2f')]||JSON[_0x2fe6('0x30')](_0x333312[_0x2fe6('0x2e')])));}return!![];});}function handleError(_0x35d5da,_0x5ed4e0,_0x2aff68){return _0x35d5da[_0x2fe6('0x31')](_0x5ed4e0||0x1f4)['send']({'message':_0x2aff68?_0x2aff68[_0x2fe6('0x2f')]||_0x2aff68:''});}exports['login']=function(_0x39f8d2,_0x2e165b,_0x333353){passport[_0x2fe6('0x18')](_0x2fe6('0x32'),function(_0x2c632f,_0x2b312b,_0x4201ae){var _0x586bbc=_0x2c632f||_0x4201ae;if(_0x586bbc){return handleError(_0x2e165b,0x191,_0x586bbc);}if(!_0x2b312b){return handleError(_0x2e165b,0x194,{'message':_0x2fe6('0x33')});}else{var _0x12bb10={'payload':{'id':_0x2b312b['id'],'role':_0x2b312b[_0x2fe6('0x11')]},'options':{'expiresIn':0x15180}};return authService[_0x2fe6('0x34')](_0x12bb10)[_0x2fe6('0xe')](function(_0x4c4a54){_0x2e165b[_0x2fe6('0x35')]({'id':_0x2b312b['id'],'token':_0x4c4a54,'statusCall':'','isAlreadyLogged':_0x2b312b[_0x2fe6('0x36')]});});}})(_0x39f8d2,_0x2e165b,_0x333353);};exports[_0x2fe6('0x37')]=function(_0xb12a44,_0x4ef036,_0x4a5a58){if(!_0xb12a44[_0x2fe6('0x38')][_0x2fe6('0x22')]){return handleError(_0x4ef036,0x1f4,{'message':_0x2fe6('0x39')});}var _0x385612=rs['generate']();_0xb12a44[_0x2fe6('0x38')][_0x2fe6('0x22')]=_0xb12a44[_0x2fe6('0x38')][_0x2fe6('0x22')]['trim']()[_0x2fe6('0x3a')]();var _0xfc195b;return db[_0x2fe6('0xb')][_0x2fe6('0xc')]({'where':{'id':0x1},'attributes':[_0x2fe6('0x1c'),_0x2fe6('0xd')],'raw':!![]})[_0x2fe6('0xe')](function(_0x5cab5d){_0xfc195b=_0x5cab5d;return db[_0x2fe6('0x3b')][_0x2fe6('0x21')]({'where':{'email':_0xb12a44[_0x2fe6('0x38')]['email']}});})['then'](function(_0x46d546){if(!_0x46d546){throw new Error('This\x20email\x20is\x20not\x20registered.');}if(_0x46d546[_0x2fe6('0x12')]){throw new Error(_0x2fe6('0x3c'));}if(_0x46d546[_0x2fe6('0x13')]){if(moment(_0x46d546[_0x2fe6('0x14')])[_0x2fe6('0x1a')](_0xfc195b['blockDuration'],_0x2fe6('0x1d'))>moment()){throw new Error(_0x2fe6('0x3d'));}else{_0x46d546['blocked']=![];_0x46d546[_0x2fe6('0x14')]=null;}}_0x46d546['resetPasswordToken']=_0x385612;_0x46d546[_0x2fe6('0x3e')]=Date[_0x2fe6('0x3f')]()+0x36ee80;return _0x46d546[_0x2fe6('0x40')]();})[_0x2fe6('0xe')](function(){return sendServiceMail({'to':_0xb12a44[_0x2fe6('0x38')]['email'],'subject':_0x2fe6('0x41'),'text':_0x2fe6('0x42')+_0x2fe6('0x43')+_0x2fe6('0x44')+_0xb12a44[_0x2fe6('0x45')][_0x2fe6('0x46')]+_0x2fe6('0x47')+_0x385612+'\x0a\x0a'+_0x2fe6('0x48')});})['then'](function(){_0x4ef036[_0x2fe6('0x31')](0xc8)[_0x2fe6('0x49')]({'message':_0x2fe6('0x4a')});})['catch'](function(_0x5dd4e4){return handleError(_0x4ef036,0x1f4,_0x5dd4e4);});};exports[_0x2fe6('0x4b')]=function(_0x502afc,_0x306f51,_0x152cec){return db['User'][_0x2fe6('0x21')]({'where':{'resetPasswordToken':_0x502afc[_0x2fe6('0x4c')][_0x2fe6('0x4d')],'resetPasswordExpires':{'$gt':Date['now']()}}})['then'](function(_0x4b6300){if(!_0x4b6300){throw new Error('Password\x20reset\x20token\x20is\x20invalid\x20or\x20has\x20expired.');}return db[_0x2fe6('0xb')][_0x2fe6('0xc')]({'attributes':['id',_0x2fe6('0x4e'),_0x2fe6('0x4f'),_0x2fe6('0x50')]})[_0x2fe6('0xe')](function(_0x523575){if(_0x523575[_0x2fe6('0x4e')])authService[_0x2fe6('0x51')](_0x502afc[_0x2fe6('0x38')][_0x2fe6('0x10')]);if(!_0x523575[_0x2fe6('0x4f')]||!_0x4b6300['previousPasswords'])return _0x4b6300;authService[_0x2fe6('0x52')](_0x502afc['body']['password'],_0x4b6300[_0x2fe6('0x53')],_0x523575[_0x2fe6('0x50')]);return _0x4b6300;})[_0x2fe6('0xe')](function(){_0x4b6300[_0x2fe6('0x10')]=_0x502afc[_0x2fe6('0x38')][_0x2fe6('0x10')];_0x4b6300['resetPasswordToken']=null;_0x4b6300[_0x2fe6('0x3e')]=null;return _0x4b6300[_0x2fe6('0x40')]();});})[_0x2fe6('0xe')](function(){_0x306f51[_0x2fe6('0x31')](0xc8)['send']({'message':_0x2fe6('0x54')});})[_0x2fe6('0x1f')](function(_0x1c7672){return handleError(_0x306f51,0x1f4,_0x1c7672);});};passport['use'](_0x2fe6('0x32'),new LocalStrategy({'usernameField':_0x2fe6('0xf'),'passwordField':_0x2fe6('0x10')},function(_0x31f965,_0x8b999a,_0x519c70){return localAuthenticate(db[_0x2fe6('0x3b')],_0x31f965,_0x8b999a,_0x519c70);}));