Built motion from commit 2b9af5da.|2.6.26
[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 _0x0312=['../../components/auth/service','../../config/utils','http','Setting','findOne','allowedLoginAttempts','blockDuration','then','name','password','salt','role','disabled','blocked','blockedAt','loginAttempts','user','agent','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','authenticate','add','clearBlockUser','minutes','getLicense','catch','find','email','Smtp','CloudProvider','Unable\x20to\x20find\x20a\x20configured\x20SMTP\x20service!','merge','\x22%s\x22\x20<%s>','request','SendMail','error','format','SendMail\x20error:\x20%s.\x20Please\x20contact\x20your\x20Administrator!','status','send','login','local-login','signToken','json','online','body','Email\x20parameter\x20is\x20missing','generate','toLowerCase','User','This\x20email\x20is\x20not\x20registered.','The\x20user\x20is\x20blocked.\x20Please\x20contact\x20the\x20administrator.','resetPasswordToken','resetPasswordExpires','now','You\x20are\x20receiving\x20this\x20because\x20you\x20(or\x20someone\x20else)\x20have\x20requested\x20the\x20reset\x20of\x20the\x20password\x20for\x20your\x20account.\x0a\x0a','https://','host','/reset/','If\x20you\x20did\x20not\x20request\x20this,\x20please\x20ignore\x20this\x20email\x20and\x20your\x20password\x20will\x20remain\x20unchanged.\x0a','Reset\x20Email\x20sent!','reset','params','token','enforcePasswordHistory','passwordHistoryLimit','securePassword','validatePasswordHistory','save','util','passport-local','Strategy','lodash'];(function(_0x5a788a,_0x2b2a5b){var _0x37d0e4=function(_0x135f60){while(--_0x135f60){_0x5a788a['push'](_0x5a788a['shift']());}};_0x37d0e4(++_0x2b2a5b);}(_0x0312,0xd4));var _0x2031=function(_0x351f60,_0x513731){_0x351f60=_0x351f60-0x0;var _0x557fac=_0x0312[_0x351f60];return _0x557fac;};'use strict';var util=require(_0x2031('0x0'));var passport=require('passport');var LocalStrategy=require(_0x2031('0x1'))[_0x2031('0x2')];var jayson=require('jayson/promise');var _=require(_0x2031('0x3'));var rs=require('randomstring');var moment=require('moment');var db=require('../../mysqldb')['db'];var authService=require(_0x2031('0x4'));var utils=require(_0x2031('0x5'));var client=jayson['client'][_0x2031('0x6')]({'port':0x232b});function localAuthenticate(_0x76470d,_0x1e490e,_0x3478ff,_0x54e30e){var _0x312e60,_0x3f8065;return db[_0x2031('0x7')][_0x2031('0x8')]({'where':{'id':0x1},'attributes':[_0x2031('0x9'),_0x2031('0xa')],'raw':!![]})[_0x2031('0xb')](function(_0xadc601){_0x312e60=_0xadc601;return _0x76470d['findOne']({'attributes':['id',_0x2031('0xc'),_0x2031('0xd'),_0x2031('0xe'),_0x2031('0xf'),_0x2031('0x10'),_0x2031('0x11'),_0x2031('0x12'),_0x2031('0x13')],'where':{'name':_0x1e490e,'role':{'$or':['admin',_0x2031('0x14'),_0x2031('0x15')]}}});})['then'](function(_0x19732a){if(!_0x19732a||_0x19732a&&_0x19732a['name']!==_0x1e490e){throw new Error(_0x2031('0x16'));}else{_0x3f8065=_0x19732a;return _0x3f8065;}})[_0x2031('0xb')](function(){_0x3f8065[_0x2031('0x17')](_0x3478ff,function(_0xd00923,_0x316d16){var _0x3612b2=_0x3f8065[_0x2031('0x13')]+0x1;if(_0xd00923){return _0x54e30e(_0xd00923);}else if(!_0x316d16){if(_0x3f8065[_0x2031('0x10')]){return _0x54e30e(null,![],{'message':_0x2031('0x16')});}else if(_0x312e60[_0x2031('0x9')]>0x0){if(_0x3f8065[_0x2031('0x11')]){if(_0x312e60[_0x2031('0xa')]>0x0){if(moment(_0x3f8065[_0x2031('0x12')])[_0x2031('0x18')](_0x312e60[_0x2031('0xa')],'minutes')>moment()){return _0x54e30e(null,![],{'message':_0x2031('0x16')});}else{return utils['clearBlockUser'](_0x3f8065,_0x3612b2,![],null)[_0x2031('0xb')](function(){return _0x54e30e(null,![],{'message':_0x2031('0x16')});});}}else{return _0x54e30e(null,![],{'message':_0x2031('0x16')});}}else{if(_0x3612b2>=_0x312e60[_0x2031('0x9')]){return utils['clearBlockUser'](_0x3f8065,0x0,!![],moment())[_0x2031('0xb')](function(){return _0x54e30e(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});});}else{return utils[_0x2031('0x19')](_0x3f8065,_0x3612b2,![],null)[_0x2031('0xb')](function(){return _0x54e30e(null,![],{'message':_0x2031('0x16')});});}}}else{return _0x54e30e(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}}else{if(_0x3f8065[_0x2031('0x10')]){return _0x54e30e(null,![],{'message':_0x2031('0x16')});}else if(_0x312e60['allowedLoginAttempts']>0x0){if(_0x3f8065[_0x2031('0x11')]){if(_0x312e60['blockDuration']>0x0){if(moment(_0x3f8065[_0x2031('0x12')])['add'](_0x312e60[_0x2031('0xa')],_0x2031('0x1a'))>moment()){return _0x54e30e(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}else{return utils[_0x2031('0x19')](_0x3f8065,0x0,![],null)[_0x2031('0xb')](function(){return utils[_0x2031('0x1b')](_0x3f8065);})[_0x2031('0xb')](function(){return _0x54e30e(null,_0x3f8065);});}}else{return _0x54e30e(null,![],{'message':_0x2031('0x16')});}}else{return utils[_0x2031('0x19')](_0x3f8065,0x0,![],null)[_0x2031('0xb')](function(){return utils[_0x2031('0x1b')](_0x3f8065);})[_0x2031('0xb')](function(){return _0x54e30e(null,_0x3f8065);})[_0x2031('0x1c')](function(_0x245a59){return _0x54e30e(null,![],_0x245a59);});}}else{if(_0x3f8065[_0x2031('0x11')]){return _0x54e30e(null,![],{'message':_0x2031('0x16')});}else{return utils[_0x2031('0x1b')](_0x3f8065)[_0x2031('0xb')](function(){return _0x54e30e(null,_0x3f8065);});}}}});})[_0x2031('0x1c')](function(_0x485878){return _0x54e30e(null,![],_0x485878);});}function sendServiceMail(_0x22ff8d){return db['MailAccount'][_0x2031('0x1d')]({'where':{'service':!![]},'attributes':['id',_0x2031('0xc'),_0x2031('0x1e')],'include':[{'model':db['MailServerOut'],'as':_0x2031('0x1f'),'include':[{'model':db[_0x2031('0x20')],'as':_0x2031('0x20')}]}]})[_0x2031('0xb')](function(_0x2f7743){if(!_0x2f7743){throw new Error(_0x2031('0x21'));}var _0x58a3a5=_0x2f7743['getSmtpOptions']();var _0x128b5f={'account':_0x58a3a5,'message':_[_0x2031('0x22')]({'from':util['format'](_0x2031('0x23'),_0x2f7743[_0x2031('0xc')],_0x2f7743[_0x2031('0x1e')]||_0x2f7743[_0x2031('0x1f')][_0x2031('0x14')])},_0x22ff8d)};return client[_0x2031('0x24')](_0x2031('0x25'),_0x128b5f);})['then'](function(_0x55d781){if(_0x55d781[_0x2031('0x26')]){throw new Error(util[_0x2031('0x27')](_0x2031('0x28'),_0x55d781[_0x2031('0x26')]['message']||JSON['stringify'](_0x55d781[_0x2031('0x26')])));}return!![];});}function handleError(_0x4b198c,_0x2159a5,_0x3f6c1b){return _0x4b198c[_0x2031('0x29')](_0x2159a5||0x1f4)[_0x2031('0x2a')]({'message':_0x3f6c1b?_0x3f6c1b['message']||_0x3f6c1b:''});}exports[_0x2031('0x2b')]=function(_0x29a7e5,_0x4e7dec,_0x56c0ab){passport['authenticate'](_0x2031('0x2c'),function(_0x395f22,_0x53470b,_0x4d5c0a){var _0x8a9027=_0x395f22||_0x4d5c0a;if(_0x8a9027){return handleError(_0x4e7dec,0x191,_0x8a9027);}if(!_0x53470b){return handleError(_0x4e7dec,0x194,{'message':'Something\x20went\x20wrong,\x20please\x20try\x20again.'});}else{var _0x1ec8be={'payload':{'id':_0x53470b['id'],'role':_0x53470b[_0x2031('0xf')]},'options':{'expiresIn':0x15180}};return authService[_0x2031('0x2d')](_0x1ec8be)[_0x2031('0xb')](function(_0xfb706c){_0x4e7dec[_0x2031('0x2e')]({'id':_0x53470b['id'],'token':_0xfb706c,'statusCall':'','isAlreadyLogged':_0x53470b[_0x2031('0x2f')]});});}})(_0x29a7e5,_0x4e7dec,_0x56c0ab);};exports['forgot']=function(_0x189bb5,_0x43976b){if(!_0x189bb5[_0x2031('0x30')]['email']){return handleError(_0x43976b,0x1f4,{'message':_0x2031('0x31')});}var _0x551cfc=rs[_0x2031('0x32')]();_0x189bb5[_0x2031('0x30')][_0x2031('0x1e')]=_0x189bb5[_0x2031('0x30')][_0x2031('0x1e')]['trim']()[_0x2031('0x33')]();var _0x21495f;return db[_0x2031('0x7')][_0x2031('0x8')]({'where':{'id':0x1},'attributes':[_0x2031('0x9'),_0x2031('0xa')],'raw':!![]})['then'](function(_0x421733){_0x21495f=_0x421733;return db[_0x2031('0x34')][_0x2031('0x1d')]({'where':{'email':_0x189bb5['body']['email']}});})[_0x2031('0xb')](function(_0x18888a){if(!_0x18888a){throw new Error(_0x2031('0x35'));}if(_0x18888a[_0x2031('0x10')]){throw new Error('The\x20user\x20is\x20disabled.\x20Please\x20contact\x20the\x20administrator.');}if(_0x18888a['blocked']){if(moment(_0x18888a[_0x2031('0x12')])[_0x2031('0x18')](_0x21495f[_0x2031('0xa')],'minutes')>moment()){throw new Error(_0x2031('0x36'));}else{_0x18888a[_0x2031('0x11')]=![];_0x18888a[_0x2031('0x12')]=null;}}_0x18888a[_0x2031('0x37')]=_0x551cfc;_0x18888a[_0x2031('0x38')]=Date[_0x2031('0x39')]()+0x36ee80;return _0x18888a['save']();})['then'](function(){return sendServiceMail({'to':_0x189bb5['body']['email'],'subject':'xCally\x20Motion\x20Password\x20Reset','text':_0x2031('0x3a')+'Please\x20click\x20on\x20the\x20following\x20link,\x20or\x20paste\x20this\x20into\x20your\x20browser\x20to\x20complete\x20the\x20process:\x0a\x0a'+_0x2031('0x3b')+_0x189bb5['headers'][_0x2031('0x3c')]+_0x2031('0x3d')+_0x551cfc+'\x0a\x0a'+_0x2031('0x3e')});})['then'](function(){_0x43976b[_0x2031('0x29')](0xc8)[_0x2031('0x2a')]({'message':_0x2031('0x3f')});})[_0x2031('0x1c')](function(_0x10be72){return handleError(_0x43976b,0x1f4,_0x10be72);});};exports[_0x2031('0x40')]=function(_0x17049d,_0x24734a){return db[_0x2031('0x34')][_0x2031('0x1d')]({'where':{'resetPasswordToken':_0x17049d[_0x2031('0x41')][_0x2031('0x42')],'resetPasswordExpires':{'$gt':Date[_0x2031('0x39')]()}}})[_0x2031('0xb')](function(_0xc607fa){if(!_0xc607fa){throw new Error('Password\x20reset\x20token\x20is\x20invalid\x20or\x20has\x20expired.');}return db['Setting'][_0x2031('0x8')]({'attributes':['id','securePassword',_0x2031('0x43'),_0x2031('0x44')]})[_0x2031('0xb')](function(_0x36e417){if(_0x36e417[_0x2031('0x45')])authService['validatePasswordPattern'](_0x17049d[_0x2031('0x30')][_0x2031('0xd')]);if(!_0x36e417[_0x2031('0x43')]||!_0xc607fa['previousPasswords'])return _0xc607fa;authService[_0x2031('0x46')](_0x17049d[_0x2031('0x30')][_0x2031('0xd')],_0xc607fa['previousPasswords'],_0x36e417[_0x2031('0x44')]);return _0xc607fa;})[_0x2031('0xb')](function(){_0xc607fa['password']=_0x17049d[_0x2031('0x30')][_0x2031('0xd')];_0xc607fa['resetPasswordToken']=null;_0xc607fa[_0x2031('0x38')]=null;return _0xc607fa[_0x2031('0x47')]();});})['then'](function(){_0x24734a['status'](0xc8)[_0x2031('0x2a')]({'message':'Password\x20reset!'});})[_0x2031('0x1c')](function(_0x45fc57){return handleError(_0x24734a,0x1f4,_0x45fc57);});};passport['use'](_0x2031('0x2c'),new LocalStrategy({'usernameField':_0x2031('0xc'),'passwordField':_0x2031('0xd')},function(_0xb6982c,_0x1ce38f,_0x4620c2){return localAuthenticate(db[_0x2031('0x34')],_0xb6982c,_0x1ce38f,_0x4620c2);}));