c31b4d0783578bd6d2c5d1f2708cabb9133931be
[motion2.git] / server / api / authActiveDirectory / authActiveDirectory.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 _0xaaea=['getLicense','catch','send','ad-login','Something\x20went\x20wrong,\x20please\x20try\x20again.','signToken','json','password','User','passport','passport-local','Strategy','activedirectory2','bluebird','../../mysqldb','../../components/auth/service','../../config/utils','../../config/logger','api','activedirectory','baseDN','url','domain','error','Error\x20authenticate\x20%s:\x20%s','inspect','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','Error\x20authenticate\x20%s:\x20Wrong\x20credentials','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20configured','Setting','findOne','adSsoEnabled','then','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20enabled','name','salt','role','disabled','blocked','blockedAt','online','admin','user','blockDuration','minutes','clearBlockUser'];(function(_0x328090,_0x5a7215){var _0x114776=function(_0x4bc712){while(--_0x4bc712){_0x328090['push'](_0x328090['shift']());}};_0x114776(++_0x5a7215);}(_0xaaea,0xc1));var _0xaaae=function(_0x17d9e3,_0x1f969c){_0x17d9e3=_0x17d9e3-0x0;var _0x14192d=_0xaaea[_0x17d9e3];return _0x14192d;};'use strict';var passport=require(_0xaaae('0x0'));var LocalStrategy=require(_0xaaae('0x1'))[_0xaaae('0x2')];var moment=require('moment');var ActiveDirectory=require(_0xaaae('0x3'));var BPromise=require(_0xaaae('0x4'));var util=require('util');var db=require(_0xaaae('0x5'))['db'];var authService=require(_0xaaae('0x6'));var utils=require(_0xaaae('0x7'));var config=require('../../config/environment');var logger=require(_0xaaae('0x8'))(_0xaaae('0x9'));function isActiveDirectoryConfigured(_0x25e775){var _0x4c972a=![];if(typeof _0x25e775[_0xaaae('0xa')]!=='undefined'){_0x4c972a=_0x25e775['activedirectory'][_0xaaae('0xb')]&&_0x25e775[_0xaaae('0xa')][_0xaaae('0xc')]&&_0x25e775['activedirectory'][_0xaaae('0xd')];}return _0x4c972a;}function adAuthenticate(_0x367b14,_0x394441,_0x45b3e3){return new BPromise(function(_0x445670,_0x1a6476){try{var _0x2e9536=new ActiveDirectory(_0x367b14);_0x2e9536['authenticate'](_0x394441,_0x45b3e3,function(_0x23dfe3,_0x424b6f){if(_0x23dfe3){logger[_0xaaae('0xe')](_0xaaae('0xf'),_0x394441,util[_0xaaae('0x10')](_0x23dfe3,{'showHidden':![],'depth':null}));_0x1a6476(_0xaaae('0x11'));}else if(!_0x424b6f){logger[_0xaaae('0xe')](_0xaaae('0x12'),_0x394441);_0x1a6476(_0xaaae('0x11'));}else{_0x445670(_0x424b6f);}});}catch(_0x455081){_0x1a6476(_0x455081);}});}function ActiveDirectoryAuthenticate(_0x95fb1c,_0x53056c,_0x4d0bc7,_0x3f1476){var _0x483971,_0x298df7;if(!isActiveDirectoryConfigured(config)){logger[_0xaaae('0xe')](_0xaaae('0x13'),_0x53056c);return _0x3f1476(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}return db[_0xaaae('0x14')][_0xaaae('0x15')]({'where':{'id':0x1},'attributes':[_0xaaae('0x16'),'blockDuration'],'raw':!![]})[_0xaaae('0x17')](function(_0x58fd67){_0x483971=_0x58fd67;if(!_0x483971[_0xaaae('0x16')]){logger[_0xaaae('0xe')](_0xaaae('0x18'),_0x53056c);throw new Error('Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.');}return _0x95fb1c[_0xaaae('0x15')]({'attributes':['id',_0xaaae('0x19'),'password',_0xaaae('0x1a'),_0xaaae('0x1b'),_0xaaae('0x1c'),_0xaaae('0x1d'),_0xaaae('0x1e'),_0xaaae('0x1f')],'where':{'name':_0x53056c,'role':{'$or':[_0xaaae('0x20'),_0xaaae('0x21'),'agent']},'adSsoEnabled':!![]}});})[_0xaaae('0x17')](function(_0x649671){if(!_0x649671||_0x649671&&_0x649671[_0xaaae('0x19')]!==_0x53056c){throw new Error(_0xaaae('0x11'));}_0x298df7=_0x649671;var _0x46a5b4=_0x298df7[_0xaaae('0x19')]+'@'+config['activedirectory'][_0xaaae('0xd')];return adAuthenticate(config[_0xaaae('0xa')],_0x46a5b4,_0x4d0bc7);})[_0xaaae('0x17')](function(_0x4e7a24){if(_0x298df7['disabled']){throw new Error(_0xaaae('0x11'));}if(_0x483971[_0xaaae('0x22')]>0x0){if(_0x298df7[_0xaaae('0x1d')]){if(moment(_0x298df7[_0xaaae('0x1e')])['add'](_0x483971['blockDuration'],_0xaaae('0x23'))>moment()){throw new Error(_0xaaae('0x11'));}return utils[_0xaaae('0x24')](_0x298df7,0x0,![],null)['then'](function(){return utils['getLicense'](_0x298df7);})[_0xaaae('0x17')](function(){return _0x3f1476(null,_0x298df7);});}else{return utils[_0xaaae('0x25')](_0x298df7)[_0xaaae('0x17')](function(){return _0x3f1476(null,_0x298df7);});}}else{return utils[_0xaaae('0x25')](_0x298df7)['then'](function(){return _0x3f1476(null,_0x298df7);});}})[_0xaaae('0x26')](function(_0x2e38df){return _0x3f1476(null,![],_0x2e38df);});}function handleError(_0x333fd9,_0x1c5d9e,_0xdbb26e){return _0x333fd9['status'](_0x1c5d9e||0x1f4)[_0xaaae('0x27')]({'message':_0xdbb26e?_0xdbb26e['message']||_0xdbb26e:''});}exports['login']=function(_0x3b8454,_0x4be37f,_0x4f4dca){passport['authenticate'](_0xaaae('0x28'),function(_0x3ad94f,_0x516ce1,_0x188888){var _0x18e653=_0x3ad94f||_0x188888;if(_0x18e653){return handleError(_0x4be37f,0x191,_0x18e653);}if(!_0x516ce1){return handleError(_0x4be37f,0x194,{'message':_0xaaae('0x29')});}else{var _0x4a5fcd={'payload':{'id':_0x516ce1['id'],'role':_0x516ce1['role']},'options':{'expiresIn':0x15180}};return authService[_0xaaae('0x2a')](_0x4a5fcd)['then'](function(_0x1aa006){_0x4be37f[_0xaaae('0x2b')]({'id':_0x516ce1['id'],'token':_0x1aa006,'statusCall':'','isAlreadyLogged':_0x516ce1[_0xaaae('0x1f')]});});}})(_0x3b8454,_0x4be37f,_0x4f4dca);};passport['use'](_0xaaae('0x28'),new LocalStrategy({'usernameField':_0xaaae('0x19'),'passwordField':_0xaaae('0x2c')},function(_0x48ac6e,_0x37d3bd,_0x4cf8d9){return ActiveDirectoryAuthenticate(db[_0xaaae('0x2d')],_0x48ac6e,_0x37d3bd,_0x4cf8d9);}));