d630fcaf10d637444862f1af6d2de4b0b7e47973
[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 _0x1b21=['blockedAt','agent','domain','add','clearBlockUser','getLicense','login','ad-login','Something\x20went\x20wrong,\x20please\x20try\x20again.','role','json','use','password','User','passport','Strategy','activedirectory2','bluebird','util','../../components/auth/service','../../config/environment','api','activedirectory','undefined','baseDN','url','authenticate','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','Setting','adSsoEnabled','blockDuration','then','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20enabled','findOne','name','blocked'];(function(_0x17e319,_0x347c53){var _0xd6ba0f=function(_0x487e6b){while(--_0x487e6b){_0x17e319['push'](_0x17e319['shift']());}};_0xd6ba0f(++_0x347c53);}(_0x1b21,0x14e));var _0x11b2=function(_0xd6c062,_0x51fbea){_0xd6c062=_0xd6c062-0x0;var _0x38ce9c=_0x1b21[_0xd6c062];return _0x38ce9c;};'use strict';var passport=require(_0x11b2('0x0'));var LocalStrategy=require('passport-local')[_0x11b2('0x1')];var moment=require('moment');var ActiveDirectory=require(_0x11b2('0x2'));var BPromise=require(_0x11b2('0x3'));var util=require(_0x11b2('0x4'));var db=require('../../mysqldb')['db'];var authService=require(_0x11b2('0x5'));var utils=require('../../config/utils');var config=require(_0x11b2('0x6'));var logger=require('../../config/logger')(_0x11b2('0x7'));function isActiveDirectoryConfigured(_0x1d7810){var _0x4ab51d=![];if(typeof _0x1d7810[_0x11b2('0x8')]!==_0x11b2('0x9')){_0x4ab51d=_0x1d7810['activedirectory'][_0x11b2('0xa')]&&_0x1d7810[_0x11b2('0x8')][_0x11b2('0xb')]&&_0x1d7810[_0x11b2('0x8')]['domain'];}return _0x4ab51d;}function adAuthenticate(_0x2a9f1c,_0x3d3faa,_0x3cba72){return new BPromise(function(_0x572381,_0x5e312b){try{var _0x4799a5=new ActiveDirectory(_0x2a9f1c);_0x4799a5[_0x11b2('0xc')](_0x3d3faa,_0x3cba72,function(_0xcf2c7,_0xae2573){if(_0xcf2c7){logger[_0x11b2('0xd')](_0x11b2('0xe'),_0x3d3faa,util[_0x11b2('0xf')](_0xcf2c7,{'showHidden':![],'depth':null}));_0x5e312b(_0x11b2('0x10'));}else if(!_0xae2573){logger[_0x11b2('0xd')](_0x11b2('0x11'),_0x3d3faa);_0x5e312b('Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.');}else{_0x572381(_0xae2573);}});}catch(_0x2c4316){_0x5e312b(_0x2c4316);}});}function ActiveDirectoryAuthenticate(_0x4e2d36,_0x41a981,_0x3fd4d0,_0x571927){var _0x2153c8,_0x183e9b;if(!isActiveDirectoryConfigured(config)){logger['error']('Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20configured',_0x41a981);return _0x571927(null,![],{'message':_0x11b2('0x10')});}return db[_0x11b2('0x12')]['findOne']({'where':{'id':0x1},'attributes':[_0x11b2('0x13'),_0x11b2('0x14')],'raw':!![]})[_0x11b2('0x15')](function(_0x137f88){_0x2153c8=_0x137f88;if(!_0x2153c8[_0x11b2('0x13')]){logger[_0x11b2('0xd')](_0x11b2('0x16'),_0x41a981);throw new Error(_0x11b2('0x10'));}return _0x4e2d36[_0x11b2('0x17')]({'attributes':['id',_0x11b2('0x18'),'password','salt','role','disabled',_0x11b2('0x19'),_0x11b2('0x1a'),'online'],'where':{'name':_0x41a981,'role':{'$or':['admin','user',_0x11b2('0x1b')]},'adSsoEnabled':!![]}});})[_0x11b2('0x15')](function(_0x49ef99){if(!_0x49ef99||_0x49ef99&&_0x49ef99[_0x11b2('0x18')]!==_0x41a981){throw new Error(_0x11b2('0x10'));}_0x183e9b=_0x49ef99;var _0x59dd9d=_0x183e9b[_0x11b2('0x18')]+'@'+config[_0x11b2('0x8')][_0x11b2('0x1c')];return adAuthenticate(config[_0x11b2('0x8')],_0x59dd9d,_0x3fd4d0);})[_0x11b2('0x15')](function(_0x4321e2){if(_0x183e9b['disabled']){throw new Error(_0x11b2('0x10'));}if(_0x2153c8[_0x11b2('0x14')]>0x0){if(_0x183e9b[_0x11b2('0x19')]){if(moment(_0x183e9b['blockedAt'])[_0x11b2('0x1d')](_0x2153c8['blockDuration'],'minutes')>moment()){throw new Error(_0x11b2('0x10'));}return utils[_0x11b2('0x1e')](_0x183e9b,0x0,![],null)[_0x11b2('0x15')](function(){return utils[_0x11b2('0x1f')](_0x183e9b);})[_0x11b2('0x15')](function(){return _0x571927(null,_0x183e9b);});}else{return utils['getLicense'](_0x183e9b)[_0x11b2('0x15')](function(){return _0x571927(null,_0x183e9b);});}}else{return utils[_0x11b2('0x1f')](_0x183e9b)[_0x11b2('0x15')](function(){return _0x571927(null,_0x183e9b);});}})['catch'](function(_0x460703){return _0x571927(null,![],_0x460703);});}function handleError(_0x2458ee,_0x29d015,_0x40b55e){return _0x2458ee['status'](_0x29d015||0x1f4)['send']({'message':_0x40b55e?_0x40b55e['message']||_0x40b55e:''});}exports[_0x11b2('0x20')]=function(_0x2feb6c,_0x48136d,_0x4700aa){passport[_0x11b2('0xc')](_0x11b2('0x21'),function(_0xab7a0a,_0x544022,_0x2eea65){var _0x22e0e8=_0xab7a0a||_0x2eea65;if(_0x22e0e8){return handleError(_0x48136d,0x191,_0x22e0e8);}if(!_0x544022){return handleError(_0x48136d,0x194,{'message':_0x11b2('0x22')});}else{var _0x2f645e={'payload':{'id':_0x544022['id'],'role':_0x544022[_0x11b2('0x23')]},'options':{'expiresIn':0x15180}};return authService['signToken'](_0x2f645e)[_0x11b2('0x15')](function(_0x38b922){_0x48136d[_0x11b2('0x24')]({'id':_0x544022['id'],'token':_0x38b922,'statusCall':'','isAlreadyLogged':_0x544022['online']});});}})(_0x2feb6c,_0x48136d,_0x4700aa);};passport[_0x11b2('0x25')]('ad-login',new LocalStrategy({'usernameField':'name','passwordField':_0x11b2('0x26')},function(_0x2d566a,_0x242996,_0x3994ef){return ActiveDirectoryAuthenticate(db[_0x11b2('0x27')],_0x2d566a,_0x242996,_0x3994ef);}));