Built motion from commit 092f8824.|2.5.49
[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 _0x794c=['token','Password\x20reset\x20token\x20is\x20invalid\x20or\x20has\x20expired.','enforcePasswordHistory','passwordHistoryLimit','securePassword','validatePasswordPattern','previousPasswords','validatePasswordHistory','Password\x20reset!','util','passport','passport-local','Strategy','jayson/promise','lodash','moment','../../components/auth/service','../../config/utils','http','Setting','findOne','allowedLoginAttempts','blockDuration','then','name','password','blocked','blockedAt','loginAttempts','user','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','authenticate','disabled','add','minutes','clearBlockUser','getLicense','catch','MailAccount','email','MailServerOut','CloudProvider','Unable\x20to\x20find\x20a\x20configured\x20SMTP\x20service!','getSmtpOptions','merge','format','Smtp','request','SendMail','error','SendMail\x20error:\x20%s.\x20Please\x20contact\x20your\x20Administrator!','message','stringify','status','send','local-login','Something\x20went\x20wrong,\x20please\x20try\x20again.','role','signToken','json','online','forgot','generate','body','trim','toLowerCase','User','find','This\x20email\x20is\x20not\x20registered.','The\x20user\x20is\x20disabled.\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://','host','/reset/','If\x20you\x20did\x20not\x20request\x20this,\x20please\x20ignore\x20this\x20email\x20and\x20your\x20password\x20will\x20remain\x20unchanged.\x0a','params'];(function(_0x5816d9,_0x307dea){var _0x8eaafc=function(_0x5feee2){while(--_0x5feee2){_0x5816d9['push'](_0x5816d9['shift']());}};_0x8eaafc(++_0x307dea);}(_0x794c,0xab));var _0xc794=function(_0x2ab365,_0xa3812e){_0x2ab365=_0x2ab365-0x0;var _0x49fef3=_0x794c[_0x2ab365];return _0x49fef3;};'use strict';var util=require(_0xc794('0x0'));var passport=require(_0xc794('0x1'));var LocalStrategy=require(_0xc794('0x2'))[_0xc794('0x3')];var jayson=require(_0xc794('0x4'));var _=require(_0xc794('0x5'));var rs=require('randomstring');var moment=require(_0xc794('0x6'));var db=require('../../mysqldb')['db'];var authService=require(_0xc794('0x7'));var utils=require(_0xc794('0x8'));var client=jayson['client'][_0xc794('0x9')]({'port':0x232b});function localAuthenticate(_0x19eb3d,_0x1e7a86,_0x41cc76,_0xe8da0b){var _0x7d256f,_0x1e9461;return db[_0xc794('0xa')][_0xc794('0xb')]({'where':{'id':0x1},'attributes':[_0xc794('0xc'),_0xc794('0xd')],'raw':!![]})[_0xc794('0xe')](function(_0x246fe9){_0x7d256f=_0x246fe9;return _0x19eb3d['findOne']({'attributes':['id',_0xc794('0xf'),_0xc794('0x10'),'salt','role','disabled',_0xc794('0x11'),_0xc794('0x12'),_0xc794('0x13')],'where':{'name':_0x1e7a86,'role':{'$or':['admin',_0xc794('0x14'),'agent']}}});})[_0xc794('0xe')](function(_0x49c825){if(!_0x49c825||_0x49c825&&_0x49c825['name']!==_0x1e7a86){throw new Error(_0xc794('0x15'));}else{_0x1e9461=_0x49c825;return _0x1e9461;}})[_0xc794('0xe')](function(){_0x1e9461[_0xc794('0x16')](_0x41cc76,function(_0x1627c9,_0xce2db2){var _0x2c7bc1=_0x1e9461[_0xc794('0x13')]+0x1;if(_0x1627c9){return _0xe8da0b(_0x1627c9);}else if(!_0xce2db2){if(_0x1e9461[_0xc794('0x17')]){return _0xe8da0b(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}else if(_0x7d256f[_0xc794('0xc')]>0x0){if(_0x1e9461[_0xc794('0x11')]){if(_0x7d256f[_0xc794('0xd')]>0x0){if(moment(_0x1e9461[_0xc794('0x12')])[_0xc794('0x18')](_0x7d256f[_0xc794('0xd')],_0xc794('0x19'))>moment()){return _0xe8da0b(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}else{return utils[_0xc794('0x1a')](_0x1e9461,_0x2c7bc1,![],null)['then'](function(){return _0xe8da0b(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});});}}else{return _0xe8da0b(null,![],{'message':_0xc794('0x15')});}}else{if(_0x2c7bc1>=_0x7d256f[_0xc794('0xc')]){return utils[_0xc794('0x1a')](_0x1e9461,0x0,!![],moment())[_0xc794('0xe')](function(){return _0xe8da0b(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});});}else{return utils[_0xc794('0x1a')](_0x1e9461,_0x2c7bc1,![],null)[_0xc794('0xe')](function(){return _0xe8da0b(null,![],{'message':_0xc794('0x15')});});}}}else{return _0xe8da0b(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}}else{if(_0x1e9461[_0xc794('0x17')]){return _0xe8da0b(null,![],{'message':_0xc794('0x15')});}else if(_0x7d256f[_0xc794('0xc')]>0x0){if(_0x1e9461[_0xc794('0x11')]){if(_0x7d256f['blockDuration']>0x0){if(moment(_0x1e9461['blockedAt'])[_0xc794('0x18')](_0x7d256f[_0xc794('0xd')],_0xc794('0x19'))>moment()){return _0xe8da0b(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}else{return utils[_0xc794('0x1a')](_0x1e9461,0x0,![],null)[_0xc794('0xe')](function(){return utils[_0xc794('0x1b')](_0x1e9461);})[_0xc794('0xe')](function(){return _0xe8da0b(null,_0x1e9461);});}}else{return _0xe8da0b(null,![],{'message':_0xc794('0x15')});}}else{return utils[_0xc794('0x1a')](_0x1e9461,0x0,![],null)[_0xc794('0xe')](function(){return utils[_0xc794('0x1b')](_0x1e9461);})[_0xc794('0xe')](function(){return _0xe8da0b(null,_0x1e9461);})['catch'](function(_0x104463){return _0xe8da0b(null,![],_0x104463);});}}else{if(_0x1e9461[_0xc794('0x11')]){return _0xe8da0b(null,![],{'message':_0xc794('0x15')});}else{return utils[_0xc794('0x1b')](_0x1e9461)[_0xc794('0xe')](function(){return _0xe8da0b(null,_0x1e9461);});}}}});})[_0xc794('0x1c')](function(_0x4e4731){return _0xe8da0b(null,![],_0x4e4731);});}function sendServiceMail(_0x158aab){return db[_0xc794('0x1d')]['find']({'where':{'service':!![]},'attributes':['id',_0xc794('0xf'),_0xc794('0x1e')],'include':[{'model':db[_0xc794('0x1f')],'as':'Smtp','include':[{'model':db[_0xc794('0x20')],'as':'CloudProvider'}]}]})[_0xc794('0xe')](function(_0x4ce5ab){if(!_0x4ce5ab){throw new Error(_0xc794('0x21'));}var _0xd878b1=_0x4ce5ab[_0xc794('0x22')]();var _0x518ad7={'account':_0xd878b1,'message':_[_0xc794('0x23')]({'from':util[_0xc794('0x24')]('\x22%s\x22\x20<%s>',_0x4ce5ab['name'],_0x4ce5ab[_0xc794('0x1e')]||_0x4ce5ab[_0xc794('0x25')][_0xc794('0x14')])},_0x158aab)};return client[_0xc794('0x26')](_0xc794('0x27'),_0x518ad7);})['then'](function(_0x90210d){if(_0x90210d[_0xc794('0x28')]){throw new Error(util[_0xc794('0x24')](_0xc794('0x29'),_0x90210d[_0xc794('0x28')][_0xc794('0x2a')]||JSON[_0xc794('0x2b')](_0x90210d['error'])));}return!![];});}function handleError(_0x4512d8,_0x5dd986,_0x417fe9){return _0x4512d8[_0xc794('0x2c')](_0x5dd986||0x1f4)[_0xc794('0x2d')]({'message':_0x417fe9?_0x417fe9[_0xc794('0x2a')]||_0x417fe9:''});}exports['login']=function(_0x6ce095,_0x249672,_0x3b43db){passport['authenticate'](_0xc794('0x2e'),function(_0xf61bf7,_0x5419c3,_0x366236){var _0x421ec9=_0xf61bf7||_0x366236;if(_0x421ec9){return handleError(_0x249672,0x191,_0x421ec9);}if(!_0x5419c3){return handleError(_0x249672,0x194,{'message':_0xc794('0x2f')});}else{var _0x1bcd76={'payload':{'id':_0x5419c3['id'],'role':_0x5419c3[_0xc794('0x30')]},'options':{'expiresIn':0x15180}};return authService[_0xc794('0x31')](_0x1bcd76)[_0xc794('0xe')](function(_0x5874cb){_0x249672[_0xc794('0x32')]({'id':_0x5419c3['id'],'token':_0x5874cb,'statusCall':'','isAlreadyLogged':_0x5419c3[_0xc794('0x33')]});});}})(_0x6ce095,_0x249672,_0x3b43db);};exports[_0xc794('0x34')]=function(_0x58429f,_0x1cf51a){if(!_0x58429f['body'][_0xc794('0x1e')]){return handleError(_0x1cf51a,0x1f4,{'message':'Email\x20parameter\x20is\x20missing'});}var _0x2bb277=rs[_0xc794('0x35')]();_0x58429f[_0xc794('0x36')][_0xc794('0x1e')]=_0x58429f['body'][_0xc794('0x1e')][_0xc794('0x37')]()[_0xc794('0x38')]();var _0x2d80f8;return db[_0xc794('0xa')][_0xc794('0xb')]({'where':{'id':0x1},'attributes':[_0xc794('0xc'),_0xc794('0xd')],'raw':!![]})[_0xc794('0xe')](function(_0x53c911){_0x2d80f8=_0x53c911;return db[_0xc794('0x39')][_0xc794('0x3a')]({'where':{'email':_0x58429f[_0xc794('0x36')][_0xc794('0x1e')]}});})[_0xc794('0xe')](function(_0x2c75e8){if(!_0x2c75e8){throw new Error(_0xc794('0x3b'));}if(_0x2c75e8[_0xc794('0x17')]){throw new Error(_0xc794('0x3c'));}if(_0x2c75e8[_0xc794('0x11')]){if(moment(_0x2c75e8['blockedAt'])[_0xc794('0x18')](_0x2d80f8['blockDuration'],'minutes')>moment()){throw new Error('The\x20user\x20is\x20blocked.\x20Please\x20contact\x20the\x20administrator.');}else{_0x2c75e8['blocked']=![];_0x2c75e8[_0xc794('0x12')]=null;}}_0x2c75e8[_0xc794('0x3d')]=_0x2bb277;_0x2c75e8[_0xc794('0x3e')]=Date[_0xc794('0x3f')]()+0x36ee80;return _0x2c75e8[_0xc794('0x40')]();})['then'](function(){return sendServiceMail({'to':_0x58429f['body']['email'],'subject':'xCally\x20Motion\x20Password\x20Reset','text':_0xc794('0x41')+_0xc794('0x42')+_0xc794('0x43')+_0x58429f['headers'][_0xc794('0x44')]+_0xc794('0x45')+_0x2bb277+'\x0a\x0a'+_0xc794('0x46')});})[_0xc794('0xe')](function(){_0x1cf51a[_0xc794('0x2c')](0xc8)['send']({'message':'Reset\x20Email\x20sent!'});})[_0xc794('0x1c')](function(_0x39bb0a){return handleError(_0x1cf51a,0x1f4,_0x39bb0a);});};exports['reset']=function(_0xfd73e0,_0x57935d){return db[_0xc794('0x39')][_0xc794('0x3a')]({'where':{'resetPasswordToken':_0xfd73e0[_0xc794('0x47')][_0xc794('0x48')],'resetPasswordExpires':{'$gt':Date[_0xc794('0x3f')]()}}})[_0xc794('0xe')](function(_0x18956e){if(!_0x18956e){throw new Error(_0xc794('0x49'));}return db['Setting'][_0xc794('0xb')]({'attributes':['id','securePassword',_0xc794('0x4a'),_0xc794('0x4b')]})[_0xc794('0xe')](function(_0x416d38){if(_0x416d38[_0xc794('0x4c')])authService[_0xc794('0x4d')](_0xfd73e0[_0xc794('0x36')][_0xc794('0x10')]);if(!_0x416d38[_0xc794('0x4a')]||!_0x18956e[_0xc794('0x4e')])return _0x18956e;authService[_0xc794('0x4f')](_0xfd73e0[_0xc794('0x36')]['password'],_0x18956e['previousPasswords'],_0x416d38[_0xc794('0x4b')]);return _0x18956e;})[_0xc794('0xe')](function(){_0x18956e[_0xc794('0x10')]=_0xfd73e0[_0xc794('0x36')][_0xc794('0x10')];_0x18956e[_0xc794('0x3d')]=null;_0x18956e[_0xc794('0x3e')]=null;return _0x18956e[_0xc794('0x40')]();});})[_0xc794('0xe')](function(){_0x57935d['status'](0xc8)[_0xc794('0x2d')]({'message':_0xc794('0x50')});})[_0xc794('0x1c')](function(_0x2bd45c){return handleError(_0x57935d,0x1f4,_0x2bd45c);});};passport['use'](_0xc794('0x2e'),new LocalStrategy({'usernameField':_0xc794('0xf'),'passwordField':_0xc794('0x10')},function(_0x2b6980,_0x28b44d,_0x83d369){return localAuthenticate(db[_0xc794('0x39')],_0x2b6980,_0x28b44d,_0x83d369);}));