Built motion from commit (unavailable).|2.5.1
[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 _0x1c59=['catch','MailAccount','find','email','MailServerOut','Smtp','Unable\x20to\x20find\x20a\x20configured\x20SMTP\x20service!','service','host','port','secure','authentication','auth','user','pass','merge','format','\x22%s\x22\x20<%s>','request','error','SendMail\x20error:\x20%s.\x20Please\x20contact\x20your\x20Administrator!','message','status','send','test','The\x20password\x20must\x20be\x20at\x20least\x208\x20characters\x20long\x20and\x20have\x201\x20lowercase\x20character,\x201\x20uppercase\x20character,\x201\x20number\x20and\x201\x20special\x20character\x20~!@#$%^&-_=+[{]}.','local','Something\x20went\x20wrong,\x20please\x20try\x20again.','role','json','online','forgot','Email\x20parameter\x20is\x20missing','generate','body','trim','toLowerCase','User','This\x20email\x20is\x20not\x20registered.','The\x20user\x20is\x20disabled.\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','http://','/reset/','If\x20you\x20did\x20not\x20request\x20this,\x20please\x20ignore\x20this\x20email\x20and\x20your\x20password\x20will\x20remain\x20unchanged.\x0a','Reset\x20Email\x20sent!','params','token','Password\x20reset\x20token\x20is\x20invalid\x20or\x20has\x20expired.','securePassword','password','Password\x20reset!','use','util','passport','passport-local','Strategy','jayson/promise','lodash','moment','client','Setting','allowedLoginAttempts','blockDuration','then','findOne','name','salt','blocked','blockedAt','admin','agent','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','authenticate','loginAttempts','disabled','minutes','clearBlockUser','add','getLicense'];(function(_0x4ddc58,_0x10912c){var _0x34de9f=function(_0x5e57fa){while(--_0x5e57fa){_0x4ddc58['push'](_0x4ddc58['shift']());}};_0x34de9f(++_0x10912c);}(_0x1c59,0x18e));var _0x91c5=function(_0x39ea5f,_0xad7c1c){_0x39ea5f=_0x39ea5f-0x0;var _0x41d0c4=_0x1c59[_0x39ea5f];return _0x41d0c4;};'use strict';var util=require(_0x91c5('0x0'));var passport=require(_0x91c5('0x1'));var LocalStrategy=require(_0x91c5('0x2'))[_0x91c5('0x3')];var jayson=require(_0x91c5('0x4'));var _=require(_0x91c5('0x5'));var rs=require('randomstring');var moment=require(_0x91c5('0x6'));var db=require('../../mysqldb')['db'];var authService=require('../../components/auth/service');var utils=require('../../config/utils');var client=jayson[_0x91c5('0x7')]['http']({'port':0x232b});function localAuthenticate(_0x135d6b,_0x3a9b48,_0x49f7f8,_0x580c32){var _0x1754b5,_0x1660a4;return db[_0x91c5('0x8')]['findOne']({'where':{'id':0x1},'attributes':[_0x91c5('0x9'),_0x91c5('0xa')],'raw':!![]})[_0x91c5('0xb')](function(_0x3b27d5){_0x1754b5=_0x3b27d5;return _0x135d6b[_0x91c5('0xc')]({'attributes':['id',_0x91c5('0xd'),'password',_0x91c5('0xe'),'role','disabled',_0x91c5('0xf'),_0x91c5('0x10'),'loginAttempts'],'where':{'name':_0x3a9b48,'role':{'$or':[_0x91c5('0x11'),'user',_0x91c5('0x12')]}}});})[_0x91c5('0xb')](function(_0x4db24d){if(!_0x4db24d||_0x4db24d&&_0x4db24d['name']!==_0x3a9b48){throw new Error(_0x91c5('0x13'));}else{_0x1660a4=_0x4db24d;return _0x1660a4;}})[_0x91c5('0xb')](function(){_0x1660a4[_0x91c5('0x14')](_0x49f7f8,function(_0x2b6747,_0x5f297b){var _0xf31b9e=_0x1660a4[_0x91c5('0x15')]+0x1;if(_0x2b6747){return _0x580c32(_0x2b6747);}else if(!_0x5f297b){if(_0x1660a4[_0x91c5('0x16')]){return _0x580c32(null,![],{'message':_0x91c5('0x13')});}else if(_0x1754b5[_0x91c5('0x9')]>0x0){if(_0x1660a4['blocked']){if(_0x1754b5[_0x91c5('0xa')]>0x0){if(moment(_0x1660a4['blockedAt'])['add'](_0x1754b5[_0x91c5('0xa')],_0x91c5('0x17'))>moment()){return _0x580c32(null,![],{'message':_0x91c5('0x13')});}else{return utils['clearBlockUser'](_0x1660a4,_0xf31b9e,![],null)['then'](function(){return _0x580c32(null,![],{'message':_0x91c5('0x13')});});}}else{return _0x580c32(null,![],{'message':_0x91c5('0x13')});}}else{if(_0xf31b9e>=_0x1754b5[_0x91c5('0x9')]){return utils[_0x91c5('0x18')](_0x1660a4,0x0,!![],moment())[_0x91c5('0xb')](function(){return _0x580c32(null,![],{'message':_0x91c5('0x13')});});}else{return utils[_0x91c5('0x18')](_0x1660a4,_0xf31b9e,![],null)[_0x91c5('0xb')](function(){return _0x580c32(null,![],{'message':_0x91c5('0x13')});});}}}else{return _0x580c32(null,![],{'message':_0x91c5('0x13')});}}else{if(_0x1660a4['disabled']){return _0x580c32(null,![],{'message':_0x91c5('0x13')});}else if(_0x1754b5[_0x91c5('0x9')]>0x0){if(_0x1660a4[_0x91c5('0xf')]){if(_0x1754b5[_0x91c5('0xa')]>0x0){if(moment(_0x1660a4[_0x91c5('0x10')])[_0x91c5('0x19')](_0x1754b5[_0x91c5('0xa')],_0x91c5('0x17'))>moment()){return _0x580c32(null,![],{'message':_0x91c5('0x13')});}else{return utils[_0x91c5('0x18')](_0x1660a4,0x0,![],null)[_0x91c5('0xb')](function(){return utils['getLicense'](_0x1660a4);})[_0x91c5('0xb')](function(){return _0x580c32(null,_0x1660a4);});}}else{return _0x580c32(null,![],{'message':_0x91c5('0x13')});}}else{return utils['clearBlockUser'](_0x1660a4,0x0,![],null)['then'](function(){return utils[_0x91c5('0x1a')](_0x1660a4);})['then'](function(){return _0x580c32(null,_0x1660a4);})[_0x91c5('0x1b')](function(_0x4a6655){return _0x580c32(null,![],_0x4a6655);});}}else{if(_0x1660a4[_0x91c5('0xf')]){return _0x580c32(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}else{return utils['getLicense'](_0x1660a4)[_0x91c5('0xb')](function(){return _0x580c32(null,_0x1660a4);});}}}});})['catch'](function(_0x9923f8){return _0x580c32(null,![],_0x9923f8);});}function sendServiceMail(_0x189ad5){return db[_0x91c5('0x1c')][_0x91c5('0x1d')]({'where':{'service':!![]},'attributes':['id',_0x91c5('0xd'),_0x91c5('0x1e')],'include':[{'model':db[_0x91c5('0x1f')],'as':_0x91c5('0x20')}]})['then'](function(_0x2843fb){if(!_0x2843fb){throw new Error(_0x91c5('0x21'));}var _0x48b721={'tls':{'rejectUnauthorized':![]}};if(_0x2843fb[_0x91c5('0x20')][_0x91c5('0x22')]){_0x48b721[_0x91c5('0x22')]=_0x2843fb[_0x91c5('0x20')][_0x91c5('0x22')];}else{_0x48b721[_0x91c5('0x23')]=_0x2843fb[_0x91c5('0x20')][_0x91c5('0x23')];_0x48b721[_0x91c5('0x24')]=_0x2843fb['Smtp'][_0x91c5('0x24')];_0x48b721[_0x91c5('0x25')]=_0x2843fb[_0x91c5('0x20')]['secure'];}if(_0x2843fb[_0x91c5('0x20')][_0x91c5('0x26')]){_0x48b721[_0x91c5('0x27')]={'user':_0x2843fb['Smtp'][_0x91c5('0x28')],'pass':_0x2843fb[_0x91c5('0x20')][_0x91c5('0x29')]};}var _0x5742c7={'account':_0x48b721,'message':_[_0x91c5('0x2a')]({'from':util[_0x91c5('0x2b')](_0x91c5('0x2c'),_0x2843fb['name'],_0x2843fb[_0x91c5('0x1e')]||_0x2843fb['Smtp'][_0x91c5('0x28')])},_0x189ad5)};return client[_0x91c5('0x2d')]('SendMail',_0x5742c7);})[_0x91c5('0xb')](function(_0x29521b){if(_0x29521b[_0x91c5('0x2e')]){throw new Error(util[_0x91c5('0x2b')](_0x91c5('0x2f'),_0x29521b[_0x91c5('0x2e')][_0x91c5('0x30')]||JSON['stringify'](_0x29521b[_0x91c5('0x2e')])));}return!![];});}function handleError(_0x21d115,_0xa8ad7c,_0x4b9908){return _0x21d115[_0x91c5('0x31')](_0xa8ad7c||0x1f4)[_0x91c5('0x32')]({'message':_0x4b9908?_0x4b9908[_0x91c5('0x30')]||_0x4b9908:''});}function checkPasswordPattern(_0xc82d7c,_0x4a6b48,_0x334f99){if(_0x334f99){var _0x51bc8e=new RegExp(/(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[?!@#\$%\^&\*~\-_=+[{\]\}])(?=.{8,})/);if(_0x51bc8e[_0x91c5('0x33')](_0x4a6b48)){return _0xc82d7c;}else{throw new Error(_0x91c5('0x34'));}}else{return _0xc82d7c;}}exports['login']=function(_0x522c31,_0x116db6,_0x529cfe){passport[_0x91c5('0x14')](_0x91c5('0x35'),function(_0x4ac3f2,_0xe4e63c,_0x5a1600){var _0x235ccd=_0x4ac3f2||_0x5a1600;if(_0x235ccd){return handleError(_0x116db6,0x191,_0x235ccd);}if(!_0xe4e63c){return handleError(_0x116db6,0x194,{'message':_0x91c5('0x36')});}else{var _0x16362a={'payload':{'id':_0xe4e63c['id'],'role':_0xe4e63c[_0x91c5('0x37')]},'options':{'expiresIn':0x15180}};return authService['signToken'](_0x16362a)[_0x91c5('0xb')](function(_0x36d5f9){_0x116db6[_0x91c5('0x38')]({'id':_0xe4e63c['id'],'token':_0x36d5f9,'statusCall':'','isAlreadyLogged':_0xe4e63c[_0x91c5('0x39')]});});}})(_0x522c31,_0x116db6,_0x529cfe);};exports[_0x91c5('0x3a')]=function(_0x480163,_0x12aea2,_0x22e8d5){if(!_0x480163['body'][_0x91c5('0x1e')]){return handleError(_0x12aea2,0x1f4,{'message':_0x91c5('0x3b')});}var _0x246b8e=rs[_0x91c5('0x3c')]();_0x480163[_0x91c5('0x3d')][_0x91c5('0x1e')]=_0x480163['body']['email'][_0x91c5('0x3e')]()[_0x91c5('0x3f')]();var _0x3f85be;return db[_0x91c5('0x8')]['findOne']({'where':{'id':0x1},'attributes':[_0x91c5('0x9'),_0x91c5('0xa')],'raw':!![]})[_0x91c5('0xb')](function(_0x1e648b){_0x3f85be=_0x1e648b;return db[_0x91c5('0x40')][_0x91c5('0x1d')]({'where':{'email':_0x480163['body'][_0x91c5('0x1e')]}});})['then'](function(_0x337ee2){if(!_0x337ee2){throw new Error(_0x91c5('0x41'));}if(_0x337ee2[_0x91c5('0x16')]){throw new Error(_0x91c5('0x42'));}if(_0x337ee2['blocked']){if(moment(_0x337ee2[_0x91c5('0x10')])[_0x91c5('0x19')](_0x3f85be['blockDuration'],_0x91c5('0x17'))>moment()){throw new Error('The\x20user\x20is\x20blocked.\x20Please\x20contact\x20the\x20administrator.');}else{_0x337ee2[_0x91c5('0xf')]=![];_0x337ee2['blockedAt']=null;}}_0x337ee2[_0x91c5('0x43')]=_0x246b8e;_0x337ee2[_0x91c5('0x44')]=Date[_0x91c5('0x45')]()+0x36ee80;return _0x337ee2[_0x91c5('0x46')]();})[_0x91c5('0xb')](function(){return sendServiceMail({'to':_0x480163[_0x91c5('0x3d')][_0x91c5('0x1e')],'subject':_0x91c5('0x47'),'text':_0x91c5('0x48')+_0x91c5('0x49')+_0x91c5('0x4a')+_0x480163['headers'][_0x91c5('0x23')]+_0x91c5('0x4b')+_0x246b8e+'\x0a\x0a'+_0x91c5('0x4c')});})[_0x91c5('0xb')](function(){_0x12aea2[_0x91c5('0x31')](0xc8)[_0x91c5('0x32')]({'message':_0x91c5('0x4d')});})[_0x91c5('0x1b')](function(_0x238124){return handleError(_0x12aea2,0x1f4,_0x238124);});};exports['reset']=function(_0x329734,_0x534901,_0x118d26){var _0x25eb75;return db[_0x91c5('0x40')]['find']({'where':{'resetPasswordToken':_0x329734[_0x91c5('0x4e')][_0x91c5('0x4f')],'resetPasswordExpires':{'$gt':Date[_0x91c5('0x45')]()}}})[_0x91c5('0xb')](function(_0x32bc2b){_0x25eb75=_0x32bc2b;if(!_0x25eb75){throw new Error(_0x91c5('0x50'));}return db[_0x91c5('0x8')][_0x91c5('0xc')]({'attributes':['id','securePassword']});})[_0x91c5('0xb')](function(_0x30ee81){return checkPasswordPattern(_0x30ee81,_0x329734[_0x91c5('0x3d')]['password'],_0x30ee81[_0x91c5('0x51')]);})['then'](function(){_0x25eb75[_0x91c5('0x52')]=_0x329734[_0x91c5('0x3d')]['password'];_0x25eb75[_0x91c5('0x43')]=null;_0x25eb75[_0x91c5('0x44')]=null;return _0x25eb75['save']();})[_0x91c5('0xb')](function(){_0x534901[_0x91c5('0x31')](0xc8)[_0x91c5('0x32')]({'message':_0x91c5('0x53')});})[_0x91c5('0x1b')](function(_0x9f929b){return handleError(_0x534901,0x1f4,_0x9f929b);});};passport[_0x91c5('0x54')](new LocalStrategy({'usernameField':_0x91c5('0xd'),'passwordField':'password'},function(_0x24a2c2,_0xa6453c,_0x7a5c1e){return localAuthenticate(db[_0x91c5('0x40')],_0x24a2c2,_0xa6453c,_0x7a5c1e);}));