1d66383fe8062036b6803cdfde1dd45bc2f49c5f
[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 _0x3f6b=['catch','status','message','login','ad-login','Something\x20went\x20wrong,\x20please\x20try\x20again.','role','signToken','json','online','password','User','passport','passport-local','Strategy','moment','activedirectory2','bluebird','util','../../mysqldb','../../components/auth/service','../../config/utils','../../config/environment','../../config/logger','activedirectory','baseDN','url','domain','authenticate','Error\x20authenticate\x20%s:\x20%s','inspect','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','error','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20configured','Setting','findOne','adSsoEnabled','then','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20enabled','name','salt','disabled','blocked','blockedAt','admin','agent','blockDuration','add','minutes','clearBlockUser','getLicense'];(function(_0xf08acb,_0x510eb2){var _0x1034da=function(_0x4e1c20){while(--_0x4e1c20){_0xf08acb['push'](_0xf08acb['shift']());}};_0x1034da(++_0x510eb2);}(_0x3f6b,0x10b));var _0xb3f6=function(_0x454830,_0x10fa3d){_0x454830=_0x454830-0x0;var _0x36fcf8=_0x3f6b[_0x454830];return _0x36fcf8;};'use strict';var passport=require(_0xb3f6('0x0'));var LocalStrategy=require(_0xb3f6('0x1'))[_0xb3f6('0x2')];var moment=require(_0xb3f6('0x3'));var ActiveDirectory=require(_0xb3f6('0x4'));var BPromise=require(_0xb3f6('0x5'));var util=require(_0xb3f6('0x6'));var db=require(_0xb3f6('0x7'))['db'];var authService=require(_0xb3f6('0x8'));var utils=require(_0xb3f6('0x9'));var config=require(_0xb3f6('0xa'));var logger=require(_0xb3f6('0xb'))('api');function isActiveDirectoryConfigured(_0x1c7401){var _0x5b869b=![];if(typeof _0x1c7401['activedirectory']!=='undefined'){_0x5b869b=_0x1c7401[_0xb3f6('0xc')][_0xb3f6('0xd')]&&_0x1c7401[_0xb3f6('0xc')][_0xb3f6('0xe')]&&_0x1c7401[_0xb3f6('0xc')][_0xb3f6('0xf')];}return _0x5b869b;}function adAuthenticate(_0xb0e34f,_0x216527,_0xe599a7){return new BPromise(function(_0x5b2981,_0x1d6500){try{var _0x4e7519=new ActiveDirectory(_0xb0e34f);_0x4e7519[_0xb3f6('0x10')](_0x216527,_0xe599a7,function(_0x3548d7,_0x847335){if(_0x3548d7){logger['error'](_0xb3f6('0x11'),_0x216527,util[_0xb3f6('0x12')](_0x3548d7,{'showHidden':![],'depth':null}));_0x1d6500(_0xb3f6('0x13'));}else if(!_0x847335){logger[_0xb3f6('0x14')]('Error\x20authenticate\x20%s:\x20Wrong\x20credentials',_0x216527);_0x1d6500(_0xb3f6('0x13'));}else{_0x5b2981(_0x847335);}});}catch(_0x8be1a5){_0x1d6500(_0x8be1a5);}});}function ActiveDirectoryAuthenticate(_0x1372b5,_0x17bd47,_0x5c1da1,_0x48d7dc){var _0x1d7205,_0x20d35b;if(!isActiveDirectoryConfigured(config)){logger['error'](_0xb3f6('0x15'),_0x17bd47);return _0x48d7dc(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}return db[_0xb3f6('0x16')][_0xb3f6('0x17')]({'where':{'id':0x1},'attributes':[_0xb3f6('0x18'),'blockDuration'],'raw':!![]})[_0xb3f6('0x19')](function(_0x3a94d3){_0x1d7205=_0x3a94d3;if(!_0x1d7205[_0xb3f6('0x18')]){logger['error'](_0xb3f6('0x1a'),_0x17bd47);throw new Error(_0xb3f6('0x13'));}return _0x1372b5['findOne']({'attributes':['id',_0xb3f6('0x1b'),'password',_0xb3f6('0x1c'),'role',_0xb3f6('0x1d'),_0xb3f6('0x1e'),_0xb3f6('0x1f'),'online'],'where':{'name':_0x17bd47,'role':{'$or':[_0xb3f6('0x20'),'user',_0xb3f6('0x21')]},'adSsoEnabled':!![]}});})['then'](function(_0x353ca8){if(!_0x353ca8||_0x353ca8&&_0x353ca8['name']!==_0x17bd47){throw new Error(_0xb3f6('0x13'));}_0x20d35b=_0x353ca8;var _0x22619b=_0x20d35b['name']+'@'+config['activedirectory'][_0xb3f6('0xf')];return adAuthenticate(config[_0xb3f6('0xc')],_0x22619b,_0x5c1da1);})[_0xb3f6('0x19')](function(_0x516988){if(_0x20d35b[_0xb3f6('0x1d')]){throw new Error(_0xb3f6('0x13'));}if(_0x1d7205[_0xb3f6('0x22')]>0x0){if(_0x20d35b[_0xb3f6('0x1e')]){if(moment(_0x20d35b[_0xb3f6('0x1f')])[_0xb3f6('0x23')](_0x1d7205[_0xb3f6('0x22')],_0xb3f6('0x24'))>moment()){throw new Error(_0xb3f6('0x13'));}return utils[_0xb3f6('0x25')](_0x20d35b,0x0,![],null)[_0xb3f6('0x19')](function(){return utils['getLicense'](_0x20d35b);})['then'](function(){return _0x48d7dc(null,_0x20d35b);});}else{return utils[_0xb3f6('0x26')](_0x20d35b)[_0xb3f6('0x19')](function(){return _0x48d7dc(null,_0x20d35b);});}}else{return utils[_0xb3f6('0x26')](_0x20d35b)[_0xb3f6('0x19')](function(){return _0x48d7dc(null,_0x20d35b);});}})[_0xb3f6('0x27')](function(_0x5b908d){return _0x48d7dc(null,![],_0x5b908d);});}function handleError(_0x19c8e3,_0x2d41c3,_0x23e2dc){return _0x19c8e3[_0xb3f6('0x28')](_0x2d41c3||0x1f4)['send']({'message':_0x23e2dc?_0x23e2dc[_0xb3f6('0x29')]||_0x23e2dc:''});}exports[_0xb3f6('0x2a')]=function(_0x257ed8,_0x5ca8b4,_0x538fa7){passport[_0xb3f6('0x10')](_0xb3f6('0x2b'),function(_0x5359bf,_0x3f2502,_0x1b9461){var _0x3754e8=_0x5359bf||_0x1b9461;if(_0x3754e8){return handleError(_0x5ca8b4,0x191,_0x3754e8);}if(!_0x3f2502){return handleError(_0x5ca8b4,0x194,{'message':_0xb3f6('0x2c')});}else{var _0x1e4521={'payload':{'id':_0x3f2502['id'],'role':_0x3f2502[_0xb3f6('0x2d')]},'options':{'expiresIn':0x15180}};return authService[_0xb3f6('0x2e')](_0x1e4521)[_0xb3f6('0x19')](function(_0x4c6efd){_0x5ca8b4[_0xb3f6('0x2f')]({'id':_0x3f2502['id'],'token':_0x4c6efd,'statusCall':'','isAlreadyLogged':_0x3f2502[_0xb3f6('0x30')]});});}})(_0x257ed8,_0x5ca8b4,_0x538fa7);};passport['use'](_0xb3f6('0x2b'),new LocalStrategy({'usernameField':_0xb3f6('0x1b'),'passwordField':_0xb3f6('0x31')},function(_0x57e027,_0x42599c,_0x5b0af5){return ActiveDirectoryAuthenticate(db[_0xb3f6('0x32')],_0x57e027,_0x42599c,_0x5b0af5);}));