0d8e8408ace911f9e89128480d41d6287d8e6fe9
[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 _0xf5a2=['error','SendMail\x20error:\x20%s.\x20Please\x20contact\x20your\x20Administrator!','message','status','send','login','local-login','signToken','online','forgot','body','Email\x20parameter\x20is\x20missing','generate','trim','toLowerCase','User','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','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','Reset\x20Email\x20sent!','catch','params','token','Password\x20reset\x20token\x20is\x20invalid\x20or\x20has\x20expired.','securePassword','enforcePasswordHistory','passwordHistoryLimit','validatePasswordPattern','password','previousPasswords','validatePasswordHistory','Password\x20reset!','use','util','passport','Strategy','jayson/promise','../../components/auth/service','../../config/utils','client','http','Setting','findOne','allowedLoginAttempts','then','salt','role','disabled','blockedAt','loginAttempts','admin','user','agent','name','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','authenticate','add','blockDuration','clearBlockUser','minutes','getLicense','blocked','MailAccount','find','email','MailServerOut','CloudProvider','Unable\x20to\x20find\x20a\x20configured\x20SMTP\x20service!','getSmtpOptions','merge','format','\x22%s\x22\x20<%s>','Smtp','request'];(function(_0xf73dcd,_0x15c363){var _0x44c1e2=function(_0x5f1996){while(--_0x5f1996){_0xf73dcd['push'](_0xf73dcd['shift']());}};_0x44c1e2(++_0x15c363);}(_0xf5a2,0x185));var _0x2f5a=function(_0x480c57,_0x5bba33){_0x480c57=_0x480c57-0x0;var _0x1109b7=_0xf5a2[_0x480c57];return _0x1109b7;};'use strict';var util=require(_0x2f5a('0x0'));var passport=require(_0x2f5a('0x1'));var LocalStrategy=require('passport-local')[_0x2f5a('0x2')];var jayson=require(_0x2f5a('0x3'));var _=require('lodash');var rs=require('randomstring');var moment=require('moment');var db=require('../../mysqldb')['db'];var authService=require(_0x2f5a('0x4'));var utils=require(_0x2f5a('0x5'));var client=jayson[_0x2f5a('0x6')][_0x2f5a('0x7')]({'port':0x232b});function localAuthenticate(_0x5ee553,_0x2cd5f2,_0xbb0bec,_0x1186a6){var _0x4adae1,_0x112fb9;return db[_0x2f5a('0x8')][_0x2f5a('0x9')]({'where':{'id':0x1},'attributes':[_0x2f5a('0xa'),'blockDuration'],'raw':!![]})[_0x2f5a('0xb')](function(_0x173089){_0x4adae1=_0x173089;return _0x5ee553['findOne']({'attributes':['id','name','password',_0x2f5a('0xc'),_0x2f5a('0xd'),_0x2f5a('0xe'),'blocked',_0x2f5a('0xf'),_0x2f5a('0x10')],'where':{'name':_0x2cd5f2,'role':{'$or':[_0x2f5a('0x11'),_0x2f5a('0x12'),_0x2f5a('0x13')]}}});})['then'](function(_0x2d4e15){if(!_0x2d4e15||_0x2d4e15&&_0x2d4e15[_0x2f5a('0x14')]!==_0x2cd5f2){throw new Error(_0x2f5a('0x15'));}else{_0x112fb9=_0x2d4e15;return _0x112fb9;}})[_0x2f5a('0xb')](function(){_0x112fb9[_0x2f5a('0x16')](_0xbb0bec,function(_0x5ca12f,_0x5cbe8b){var _0x3cdc3a=_0x112fb9[_0x2f5a('0x10')]+0x1;if(_0x5ca12f){return _0x1186a6(_0x5ca12f);}else if(!_0x5cbe8b){if(_0x112fb9['disabled']){return _0x1186a6(null,![],{'message':_0x2f5a('0x15')});}else if(_0x4adae1['allowedLoginAttempts']>0x0){if(_0x112fb9['blocked']){if(_0x4adae1['blockDuration']>0x0){if(moment(_0x112fb9[_0x2f5a('0xf')])[_0x2f5a('0x17')](_0x4adae1[_0x2f5a('0x18')],'minutes')>moment()){return _0x1186a6(null,![],{'message':_0x2f5a('0x15')});}else{return utils['clearBlockUser'](_0x112fb9,_0x3cdc3a,![],null)[_0x2f5a('0xb')](function(){return _0x1186a6(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});});}}else{return _0x1186a6(null,![],{'message':_0x2f5a('0x15')});}}else{if(_0x3cdc3a>=_0x4adae1['allowedLoginAttempts']){return utils[_0x2f5a('0x19')](_0x112fb9,0x0,!![],moment())[_0x2f5a('0xb')](function(){return _0x1186a6(null,![],{'message':_0x2f5a('0x15')});});}else{return utils['clearBlockUser'](_0x112fb9,_0x3cdc3a,![],null)[_0x2f5a('0xb')](function(){return _0x1186a6(null,![],{'message':_0x2f5a('0x15')});});}}}else{return _0x1186a6(null,![],{'message':_0x2f5a('0x15')});}}else{if(_0x112fb9['disabled']){return _0x1186a6(null,![],{'message':_0x2f5a('0x15')});}else if(_0x4adae1[_0x2f5a('0xa')]>0x0){if(_0x112fb9['blocked']){if(_0x4adae1[_0x2f5a('0x18')]>0x0){if(moment(_0x112fb9[_0x2f5a('0xf')])['add'](_0x4adae1[_0x2f5a('0x18')],_0x2f5a('0x1a'))>moment()){return _0x1186a6(null,![],{'message':_0x2f5a('0x15')});}else{return utils['clearBlockUser'](_0x112fb9,0x0,![],null)[_0x2f5a('0xb')](function(){return utils[_0x2f5a('0x1b')](_0x112fb9);})[_0x2f5a('0xb')](function(){return _0x1186a6(null,_0x112fb9);});}}else{return _0x1186a6(null,![],{'message':_0x2f5a('0x15')});}}else{return utils['clearBlockUser'](_0x112fb9,0x0,![],null)[_0x2f5a('0xb')](function(){return utils['getLicense'](_0x112fb9);})[_0x2f5a('0xb')](function(){return _0x1186a6(null,_0x112fb9);})['catch'](function(_0x4470f4){return _0x1186a6(null,![],_0x4470f4);});}}else{if(_0x112fb9[_0x2f5a('0x1c')]){return _0x1186a6(null,![],{'message':_0x2f5a('0x15')});}else{return utils['getLicense'](_0x112fb9)['then'](function(){return _0x1186a6(null,_0x112fb9);});}}}});})['catch'](function(_0x416c24){return _0x1186a6(null,![],_0x416c24);});}function sendServiceMail(_0x3f5900){return db[_0x2f5a('0x1d')][_0x2f5a('0x1e')]({'where':{'service':!![]},'attributes':['id',_0x2f5a('0x14'),_0x2f5a('0x1f')],'include':[{'model':db[_0x2f5a('0x20')],'as':'Smtp','include':[{'model':db[_0x2f5a('0x21')],'as':_0x2f5a('0x21')}]}]})[_0x2f5a('0xb')](function(_0x502342){if(!_0x502342){throw new Error(_0x2f5a('0x22'));}var _0xd082c=_0x502342[_0x2f5a('0x23')]();var _0x597904={'account':_0xd082c,'message':_[_0x2f5a('0x24')]({'from':util[_0x2f5a('0x25')](_0x2f5a('0x26'),_0x502342[_0x2f5a('0x14')],_0x502342[_0x2f5a('0x1f')]||_0x502342[_0x2f5a('0x27')][_0x2f5a('0x12')])},_0x3f5900)};return client[_0x2f5a('0x28')]('SendMail',_0x597904);})['then'](function(_0x6f51f9){if(_0x6f51f9[_0x2f5a('0x29')]){throw new Error(util[_0x2f5a('0x25')](_0x2f5a('0x2a'),_0x6f51f9[_0x2f5a('0x29')][_0x2f5a('0x2b')]||JSON['stringify'](_0x6f51f9['error'])));}return!![];});}function handleError(_0x454afd,_0x1a1446,_0x5ec2e7){return _0x454afd[_0x2f5a('0x2c')](_0x1a1446||0x1f4)[_0x2f5a('0x2d')]({'message':_0x5ec2e7?_0x5ec2e7['message']||_0x5ec2e7:''});}exports[_0x2f5a('0x2e')]=function(_0x453127,_0x3a057b,_0x1cff3b){passport[_0x2f5a('0x16')](_0x2f5a('0x2f'),function(_0xdafc3a,_0x2958d4,_0x5f2410){var _0x32640c=_0xdafc3a||_0x5f2410;if(_0x32640c){return handleError(_0x3a057b,0x191,_0x32640c);}if(!_0x2958d4){return handleError(_0x3a057b,0x194,{'message':'Something\x20went\x20wrong,\x20please\x20try\x20again.'});}else{var _0x55b2d4={'payload':{'id':_0x2958d4['id'],'role':_0x2958d4[_0x2f5a('0xd')]},'options':{'expiresIn':0x15180}};return authService[_0x2f5a('0x30')](_0x55b2d4)[_0x2f5a('0xb')](function(_0x593144){_0x3a057b['json']({'id':_0x2958d4['id'],'token':_0x593144,'statusCall':'','isAlreadyLogged':_0x2958d4[_0x2f5a('0x31')]});});}})(_0x453127,_0x3a057b,_0x1cff3b);};exports[_0x2f5a('0x32')]=function(_0xe517df,_0x1f57bc){if(!_0xe517df[_0x2f5a('0x33')]['email']){return handleError(_0x1f57bc,0x1f4,{'message':_0x2f5a('0x34')});}var _0x32e11d=rs[_0x2f5a('0x35')]();_0xe517df[_0x2f5a('0x33')][_0x2f5a('0x1f')]=_0xe517df[_0x2f5a('0x33')][_0x2f5a('0x1f')][_0x2f5a('0x36')]()[_0x2f5a('0x37')]();var _0x51b6d5;return db['Setting'][_0x2f5a('0x9')]({'where':{'id':0x1},'attributes':[_0x2f5a('0xa'),'blockDuration'],'raw':!![]})['then'](function(_0x339708){_0x51b6d5=_0x339708;return db[_0x2f5a('0x38')][_0x2f5a('0x1e')]({'where':{'email':_0xe517df['body']['email']}});})[_0x2f5a('0xb')](function(_0x5ea25f){if(!_0x5ea25f){throw new Error(_0x2f5a('0x39'));}if(_0x5ea25f[_0x2f5a('0xe')]){throw new Error(_0x2f5a('0x3a'));}if(_0x5ea25f[_0x2f5a('0x1c')]){if(moment(_0x5ea25f[_0x2f5a('0xf')])[_0x2f5a('0x17')](_0x51b6d5['blockDuration'],_0x2f5a('0x1a'))>moment()){throw new Error(_0x2f5a('0x3b'));}else{_0x5ea25f[_0x2f5a('0x1c')]=![];_0x5ea25f[_0x2f5a('0xf')]=null;}}_0x5ea25f[_0x2f5a('0x3c')]=_0x32e11d;_0x5ea25f[_0x2f5a('0x3d')]=Date[_0x2f5a('0x3e')]()+0x36ee80;return _0x5ea25f[_0x2f5a('0x3f')]();})[_0x2f5a('0xb')](function(){return sendServiceMail({'to':_0xe517df[_0x2f5a('0x33')][_0x2f5a('0x1f')],'subject':_0x2f5a('0x40'),'text':_0x2f5a('0x41')+_0x2f5a('0x42')+_0x2f5a('0x43')+_0xe517df[_0x2f5a('0x44')][_0x2f5a('0x45')]+_0x2f5a('0x46')+_0x32e11d+'\x0a\x0a'+_0x2f5a('0x47')});})[_0x2f5a('0xb')](function(){_0x1f57bc[_0x2f5a('0x2c')](0xc8)[_0x2f5a('0x2d')]({'message':_0x2f5a('0x48')});})[_0x2f5a('0x49')](function(_0x496a17){return handleError(_0x1f57bc,0x1f4,_0x496a17);});};exports['reset']=function(_0x222707,_0x4cbb84){return db['User']['find']({'where':{'resetPasswordToken':_0x222707[_0x2f5a('0x4a')][_0x2f5a('0x4b')],'resetPasswordExpires':{'$gt':Date[_0x2f5a('0x3e')]()}}})[_0x2f5a('0xb')](function(_0x2cd422){if(!_0x2cd422){throw new Error(_0x2f5a('0x4c'));}return db['Setting'][_0x2f5a('0x9')]({'attributes':['id',_0x2f5a('0x4d'),_0x2f5a('0x4e'),_0x2f5a('0x4f')]})[_0x2f5a('0xb')](function(_0x16127c){if(_0x16127c[_0x2f5a('0x4d')])authService[_0x2f5a('0x50')](_0x222707[_0x2f5a('0x33')][_0x2f5a('0x51')]);if(!_0x16127c[_0x2f5a('0x4e')]||!_0x2cd422[_0x2f5a('0x52')])return _0x2cd422;authService[_0x2f5a('0x53')](_0x222707['body'][_0x2f5a('0x51')],_0x2cd422['previousPasswords'],_0x16127c['passwordHistoryLimit']);return _0x2cd422;})[_0x2f5a('0xb')](function(){_0x2cd422[_0x2f5a('0x51')]=_0x222707['body']['password'];_0x2cd422['resetPasswordToken']=null;_0x2cd422[_0x2f5a('0x3d')]=null;return _0x2cd422[_0x2f5a('0x3f')]();});})[_0x2f5a('0xb')](function(){_0x4cbb84[_0x2f5a('0x2c')](0xc8)['send']({'message':_0x2f5a('0x54')});})[_0x2f5a('0x49')](function(_0x3ed5e0){return handleError(_0x4cbb84,0x1f4,_0x3ed5e0);});};passport[_0x2f5a('0x55')]('local-login',new LocalStrategy({'usernameField':_0x2f5a('0x14'),'passwordField':'password'},function(_0x410a1e,_0x19985f,_0x4f7d77){return localAuthenticate(db[_0x2f5a('0x38')],_0x410a1e,_0x19985f,_0x4f7d77);}));