Built motion from commit 9e1399f3.|2.5.37
[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 _0x298c=['passwordHistoryLimit','Password\x20reset!','use','User','util','passport','passport-local','jayson/promise','lodash','randomstring','moment','../../mysqldb','../../components/auth/service','client','http','Setting','findOne','allowedLoginAttempts','blockDuration','then','password','salt','role','disabled','blocked','blockedAt','loginAttempts','agent','name','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','add','minutes','clearBlockUser','getLicense','catch','MailAccount','find','email','Unable\x20to\x20find\x20a\x20configured\x20SMTP\x20service!','Smtp','service','host','port','secure','authentication','auth','user','merge','format','error','SendMail\x20error:\x20%s.\x20Please\x20contact\x20your\x20Administrator!','message','stringify','status','send','login','authenticate','local','Something\x20went\x20wrong,\x20please\x20try\x20again.','json','online','forgot','body','Email\x20parameter\x20is\x20missing','generate','trim','toLowerCase','This\x20email\x20is\x20not\x20registered.','The\x20user\x20is\x20disabled.\x20Please\x20contact\x20the\x20administrator.','The\x20user\x20is\x20blocked.\x20Please\x20contact\x20the\x20administrator.','resetPasswordToken','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://','If\x20you\x20did\x20not\x20request\x20this,\x20please\x20ignore\x20this\x20email\x20and\x20your\x20password\x20will\x20remain\x20unchanged.\x0a','reset','params','Password\x20reset\x20token\x20is\x20invalid\x20or\x20has\x20expired.','securePassword','enforcePasswordHistory','validatePasswordPattern','validatePasswordHistory','previousPasswords'];(function(_0x4b7d92,_0x588b43){var _0xe931a=function(_0x5591ef){while(--_0x5591ef){_0x4b7d92['push'](_0x4b7d92['shift']());}};_0xe931a(++_0x588b43);}(_0x298c,0x106));var _0xc298=function(_0x4e09fd,_0x4b8809){_0x4e09fd=_0x4e09fd-0x0;var _0xe8e808=_0x298c[_0x4e09fd];return _0xe8e808;};'use strict';var util=require(_0xc298('0x0'));var passport=require(_0xc298('0x1'));var LocalStrategy=require(_0xc298('0x2'))['Strategy'];var jayson=require(_0xc298('0x3'));var _=require(_0xc298('0x4'));var rs=require(_0xc298('0x5'));var moment=require(_0xc298('0x6'));var db=require(_0xc298('0x7'))['db'];var authService=require(_0xc298('0x8'));var utils=require('../../config/utils');var client=jayson[_0xc298('0x9')][_0xc298('0xa')]({'port':0x232b});function localAuthenticate(_0xeef138,_0x27c69e,_0x1f5866,_0x13f0cd){var _0x4af34d,_0x46de8b;return db[_0xc298('0xb')][_0xc298('0xc')]({'where':{'id':0x1},'attributes':[_0xc298('0xd'),_0xc298('0xe')],'raw':!![]})[_0xc298('0xf')](function(_0x2fe39b){_0x4af34d=_0x2fe39b;return _0xeef138['findOne']({'attributes':['id','name',_0xc298('0x10'),_0xc298('0x11'),_0xc298('0x12'),_0xc298('0x13'),_0xc298('0x14'),_0xc298('0x15'),_0xc298('0x16')],'where':{'name':_0x27c69e,'role':{'$or':['admin','user',_0xc298('0x17')]}}});})[_0xc298('0xf')](function(_0x56d029){if(!_0x56d029||_0x56d029&&_0x56d029[_0xc298('0x18')]!==_0x27c69e){throw new Error(_0xc298('0x19'));}else{_0x46de8b=_0x56d029;return _0x46de8b;}})['then'](function(){_0x46de8b['authenticate'](_0x1f5866,function(_0x4a565d,_0x292c39){var _0x2f3560=_0x46de8b[_0xc298('0x16')]+0x1;if(_0x4a565d){return _0x13f0cd(_0x4a565d);}else if(!_0x292c39){if(_0x46de8b[_0xc298('0x13')]){return _0x13f0cd(null,![],{'message':_0xc298('0x19')});}else if(_0x4af34d[_0xc298('0xd')]>0x0){if(_0x46de8b[_0xc298('0x14')]){if(_0x4af34d[_0xc298('0xe')]>0x0){if(moment(_0x46de8b[_0xc298('0x15')])[_0xc298('0x1a')](_0x4af34d[_0xc298('0xe')],_0xc298('0x1b'))>moment()){return _0x13f0cd(null,![],{'message':_0xc298('0x19')});}else{return utils[_0xc298('0x1c')](_0x46de8b,_0x2f3560,![],null)['then'](function(){return _0x13f0cd(null,![],{'message':_0xc298('0x19')});});}}else{return _0x13f0cd(null,![],{'message':_0xc298('0x19')});}}else{if(_0x2f3560>=_0x4af34d[_0xc298('0xd')]){return utils[_0xc298('0x1c')](_0x46de8b,0x0,!![],moment())[_0xc298('0xf')](function(){return _0x13f0cd(null,![],{'message':_0xc298('0x19')});});}else{return utils[_0xc298('0x1c')](_0x46de8b,_0x2f3560,![],null)['then'](function(){return _0x13f0cd(null,![],{'message':_0xc298('0x19')});});}}}else{return _0x13f0cd(null,![],{'message':_0xc298('0x19')});}}else{if(_0x46de8b['disabled']){return _0x13f0cd(null,![],{'message':_0xc298('0x19')});}else if(_0x4af34d['allowedLoginAttempts']>0x0){if(_0x46de8b[_0xc298('0x14')]){if(_0x4af34d[_0xc298('0xe')]>0x0){if(moment(_0x46de8b[_0xc298('0x15')])[_0xc298('0x1a')](_0x4af34d[_0xc298('0xe')],_0xc298('0x1b'))>moment()){return _0x13f0cd(null,![],{'message':_0xc298('0x19')});}else{return utils['clearBlockUser'](_0x46de8b,0x0,![],null)[_0xc298('0xf')](function(){return utils[_0xc298('0x1d')](_0x46de8b);})[_0xc298('0xf')](function(){return _0x13f0cd(null,_0x46de8b);});}}else{return _0x13f0cd(null,![],{'message':_0xc298('0x19')});}}else{return utils[_0xc298('0x1c')](_0x46de8b,0x0,![],null)[_0xc298('0xf')](function(){return utils[_0xc298('0x1d')](_0x46de8b);})['then'](function(){return _0x13f0cd(null,_0x46de8b);})['catch'](function(_0x15c214){return _0x13f0cd(null,![],_0x15c214);});}}else{if(_0x46de8b[_0xc298('0x14')]){return _0x13f0cd(null,![],{'message':_0xc298('0x19')});}else{return utils[_0xc298('0x1d')](_0x46de8b)[_0xc298('0xf')](function(){return _0x13f0cd(null,_0x46de8b);});}}}});})[_0xc298('0x1e')](function(_0x391864){return _0x13f0cd(null,![],_0x391864);});}function sendServiceMail(_0x184e05){return db[_0xc298('0x1f')][_0xc298('0x20')]({'where':{'service':!![]},'attributes':['id',_0xc298('0x18'),_0xc298('0x21')],'include':[{'model':db['MailServerOut'],'as':'Smtp'}]})['then'](function(_0x4c4ddd){if(!_0x4c4ddd){throw new Error(_0xc298('0x22'));}var _0x3e5ffe={'tls':{'rejectUnauthorized':![]}};if(_0x4c4ddd[_0xc298('0x23')][_0xc298('0x24')]){_0x3e5ffe['service']=_0x4c4ddd['Smtp'][_0xc298('0x24')];}else{_0x3e5ffe[_0xc298('0x25')]=_0x4c4ddd['Smtp']['host'];_0x3e5ffe[_0xc298('0x26')]=_0x4c4ddd[_0xc298('0x23')]['port'];_0x3e5ffe[_0xc298('0x27')]=_0x4c4ddd[_0xc298('0x23')][_0xc298('0x27')];}if(_0x4c4ddd[_0xc298('0x23')][_0xc298('0x28')]){_0x3e5ffe[_0xc298('0x29')]={'user':_0x4c4ddd[_0xc298('0x23')][_0xc298('0x2a')],'pass':_0x4c4ddd['Smtp']['pass']};}var _0x2fafe2={'account':_0x3e5ffe,'message':_[_0xc298('0x2b')]({'from':util[_0xc298('0x2c')]('\x22%s\x22\x20<%s>',_0x4c4ddd['name'],_0x4c4ddd[_0xc298('0x21')]||_0x4c4ddd[_0xc298('0x23')][_0xc298('0x2a')])},_0x184e05)};return client['request']('SendMail',_0x2fafe2);})[_0xc298('0xf')](function(_0x2a327b){if(_0x2a327b[_0xc298('0x2d')]){throw new Error(util[_0xc298('0x2c')](_0xc298('0x2e'),_0x2a327b['error'][_0xc298('0x2f')]||JSON[_0xc298('0x30')](_0x2a327b['error'])));}return!![];});}function handleError(_0x272290,_0x1998e6,_0x2d4eeb){return _0x272290[_0xc298('0x31')](_0x1998e6||0x1f4)[_0xc298('0x32')]({'message':_0x2d4eeb?_0x2d4eeb[_0xc298('0x2f')]||_0x2d4eeb:''});}exports[_0xc298('0x33')]=function(_0x501786,_0x3cf349,_0x707740){passport[_0xc298('0x34')](_0xc298('0x35'),function(_0x25df3c,_0x33c862,_0x1e9e0f){var _0x50c358=_0x25df3c||_0x1e9e0f;if(_0x50c358){return handleError(_0x3cf349,0x191,_0x50c358);}if(!_0x33c862){return handleError(_0x3cf349,0x194,{'message':_0xc298('0x36')});}else{var _0x4d0a6d={'payload':{'id':_0x33c862['id'],'role':_0x33c862[_0xc298('0x12')]},'options':{'expiresIn':0x15180}};return authService['signToken'](_0x4d0a6d)[_0xc298('0xf')](function(_0x4aeef6){_0x3cf349[_0xc298('0x37')]({'id':_0x33c862['id'],'token':_0x4aeef6,'statusCall':'','isAlreadyLogged':_0x33c862[_0xc298('0x38')]});});}})(_0x501786,_0x3cf349,_0x707740);};exports[_0xc298('0x39')]=function(_0x666d72,_0x5c34e7,_0x3502a7){if(!_0x666d72[_0xc298('0x3a')]['email']){return handleError(_0x5c34e7,0x1f4,{'message':_0xc298('0x3b')});}var _0x23a4d4=rs[_0xc298('0x3c')]();_0x666d72[_0xc298('0x3a')][_0xc298('0x21')]=_0x666d72[_0xc298('0x3a')]['email'][_0xc298('0x3d')]()[_0xc298('0x3e')]();var _0x20fcbe;return db[_0xc298('0xb')][_0xc298('0xc')]({'where':{'id':0x1},'attributes':[_0xc298('0xd'),_0xc298('0xe')],'raw':!![]})[_0xc298('0xf')](function(_0x578a22){_0x20fcbe=_0x578a22;return db['User'][_0xc298('0x20')]({'where':{'email':_0x666d72[_0xc298('0x3a')][_0xc298('0x21')]}});})[_0xc298('0xf')](function(_0x37c885){if(!_0x37c885){throw new Error(_0xc298('0x3f'));}if(_0x37c885[_0xc298('0x13')]){throw new Error(_0xc298('0x40'));}if(_0x37c885[_0xc298('0x14')]){if(moment(_0x37c885[_0xc298('0x15')])[_0xc298('0x1a')](_0x20fcbe['blockDuration'],_0xc298('0x1b'))>moment()){throw new Error(_0xc298('0x41'));}else{_0x37c885[_0xc298('0x14')]=![];_0x37c885[_0xc298('0x15')]=null;}}_0x37c885[_0xc298('0x42')]=_0x23a4d4;_0x37c885[_0xc298('0x43')]=Date[_0xc298('0x44')]()+0x36ee80;return _0x37c885[_0xc298('0x45')]();})[_0xc298('0xf')](function(){return sendServiceMail({'to':_0x666d72[_0xc298('0x3a')][_0xc298('0x21')],'subject':'xCally\x20Motion\x20Password\x20Reset','text':_0xc298('0x46')+_0xc298('0x47')+_0xc298('0x48')+_0x666d72['headers'][_0xc298('0x25')]+'/reset/'+_0x23a4d4+'\x0a\x0a'+_0xc298('0x49')});})[_0xc298('0xf')](function(){_0x5c34e7[_0xc298('0x31')](0xc8)[_0xc298('0x32')]({'message':'Reset\x20Email\x20sent!'});})[_0xc298('0x1e')](function(_0x5c06ac){return handleError(_0x5c34e7,0x1f4,_0x5c06ac);});};exports[_0xc298('0x4a')]=function(_0x3b3464,_0x3ae7d4,_0x1e4051){return db['User']['find']({'where':{'resetPasswordToken':_0x3b3464[_0xc298('0x4b')]['token'],'resetPasswordExpires':{'$gt':Date[_0xc298('0x44')]()}}})[_0xc298('0xf')](function(_0x116b1b){if(!_0x116b1b){throw new Error(_0xc298('0x4c'));}return db[_0xc298('0xb')][_0xc298('0xc')]({'attributes':['id',_0xc298('0x4d'),_0xc298('0x4e'),'passwordHistoryLimit']})[_0xc298('0xf')](function(_0x44f046){if(_0x44f046['securePassword'])authService[_0xc298('0x4f')](_0x3b3464[_0xc298('0x3a')][_0xc298('0x10')]);if(!_0x44f046[_0xc298('0x4e')]||!_0x116b1b['previousPasswords'])return _0x116b1b;authService[_0xc298('0x50')](_0x3b3464['body'][_0xc298('0x10')],_0x116b1b[_0xc298('0x51')],_0x44f046[_0xc298('0x52')]);return _0x116b1b;})[_0xc298('0xf')](function(){_0x116b1b['password']=_0x3b3464[_0xc298('0x3a')][_0xc298('0x10')];_0x116b1b['resetPasswordToken']=null;_0x116b1b[_0xc298('0x43')]=null;return _0x116b1b[_0xc298('0x45')]();});})[_0xc298('0xf')](function(){_0x3ae7d4[_0xc298('0x31')](0xc8)[_0xc298('0x32')]({'message':_0xc298('0x53')});})[_0xc298('0x1e')](function(_0x177cc6){return handleError(_0x3ae7d4,0x1f4,_0x177cc6);});};passport[_0xc298('0x54')](new LocalStrategy({'usernameField':_0xc298('0x18'),'passwordField':_0xc298('0x10')},function(_0x2c007b,_0x2c61c4,_0x5c1163){return localAuthenticate(db[_0xc298('0x55')],_0x2c007b,_0x2c61c4,_0x5c1163);}));