f96b06525412b1af54b23908a9e71b557abf17a5
[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 _0x1c3d=['Strategy','moment','activedirectory2','bluebird','../../mysqldb','../../config/environment','activedirectory','undefined','baseDN','url','domain','error','inspect','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20configured','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','findOne','adSsoEnabled','blockDuration','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20enabled','password','salt','role','disabled','blocked','online','admin','user','then','name','blockedAt','add','minutes','clearBlockUser','getLicense','send','message','login','ad-login','Something\x20went\x20wrong,\x20please\x20try\x20again.','signToken','json','use','User','passport','passport-local'];(function(_0x1352f0,_0x4cd565){var _0x1c2a82=function(_0x2a9b63){while(--_0x2a9b63){_0x1352f0['push'](_0x1352f0['shift']());}};_0x1c2a82(++_0x4cd565);}(_0x1c3d,0xdf));var _0xd1c3=function(_0x19362a,_0x126dfe){_0x19362a=_0x19362a-0x0;var _0x39c212=_0x1c3d[_0x19362a];return _0x39c212;};'use strict';var passport=require(_0xd1c3('0x0'));var LocalStrategy=require(_0xd1c3('0x1'))[_0xd1c3('0x2')];var moment=require(_0xd1c3('0x3'));var ActiveDirectory=require(_0xd1c3('0x4'));var BPromise=require(_0xd1c3('0x5'));var util=require('util');var db=require(_0xd1c3('0x6'))['db'];var authService=require('../../components/auth/service');var utils=require('../../config/utils');var config=require(_0xd1c3('0x7'));var logger=require('../../config/logger')('api');function isActiveDirectoryConfigured(_0x36b279){var _0x1b1808=![];if(typeof _0x36b279[_0xd1c3('0x8')]!==_0xd1c3('0x9')){_0x1b1808=_0x36b279[_0xd1c3('0x8')][_0xd1c3('0xa')]&&_0x36b279['activedirectory'][_0xd1c3('0xb')]&&_0x36b279[_0xd1c3('0x8')][_0xd1c3('0xc')];}return _0x1b1808;}function adAuthenticate(_0x566dbe,_0x2ffe31,_0x5472f0){return new BPromise(function(_0xbcb6ca,_0x2f9f21){try{var _0x1990a4=new ActiveDirectory(_0x566dbe);_0x1990a4['authenticate'](_0x2ffe31,_0x5472f0,function(_0x1a6dcd,_0x461e38){if(_0x1a6dcd){logger[_0xd1c3('0xd')]('Error\x20authenticate\x20%s:\x20%s',_0x2ffe31,util[_0xd1c3('0xe')](_0x1a6dcd,{'showHidden':![],'depth':null}));_0x2f9f21('Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.');}else if(!_0x461e38){logger['error']('Error\x20authenticate\x20%s:\x20Wrong\x20credentials',_0x2ffe31);_0x2f9f21('Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.');}else{_0xbcb6ca(_0x461e38);}});}catch(_0x442400){_0x2f9f21(_0x442400);}});}function ActiveDirectoryAuthenticate(_0x27b987,_0x4d1f9f,_0x2a1991,_0x1cc142){var _0x2462bf,_0x9c985b;if(!isActiveDirectoryConfigured(config)){logger[_0xd1c3('0xd')](_0xd1c3('0xf'),_0x4d1f9f);return _0x1cc142(null,![],{'message':_0xd1c3('0x10')});}return db['Setting'][_0xd1c3('0x11')]({'where':{'id':0x1},'attributes':[_0xd1c3('0x12'),_0xd1c3('0x13')],'raw':!![]})['then'](function(_0x5105b1){_0x2462bf=_0x5105b1;if(!_0x2462bf['adSsoEnabled']){logger[_0xd1c3('0xd')](_0xd1c3('0x14'),_0x4d1f9f);throw new Error('Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.');}return _0x27b987['findOne']({'attributes':['id','name',_0xd1c3('0x15'),_0xd1c3('0x16'),_0xd1c3('0x17'),_0xd1c3('0x18'),_0xd1c3('0x19'),'blockedAt',_0xd1c3('0x1a')],'where':{'name':_0x4d1f9f,'role':{'$or':[_0xd1c3('0x1b'),_0xd1c3('0x1c'),'agent']},'adSsoEnabled':!![]}});})[_0xd1c3('0x1d')](function(_0x43023e){if(!_0x43023e||_0x43023e&&_0x43023e[_0xd1c3('0x1e')]!==_0x4d1f9f){throw new Error(_0xd1c3('0x10'));}_0x9c985b=_0x43023e;var _0x26a097=_0x9c985b[_0xd1c3('0x1e')]+'@'+config[_0xd1c3('0x8')]['domain'];return adAuthenticate(config[_0xd1c3('0x8')],_0x26a097,_0x2a1991);})[_0xd1c3('0x1d')](function(_0x35abbb){if(_0x9c985b[_0xd1c3('0x18')]){throw new Error(_0xd1c3('0x10'));}if(_0x2462bf[_0xd1c3('0x13')]>0x0){if(_0x9c985b['blocked']){if(moment(_0x9c985b[_0xd1c3('0x1f')])[_0xd1c3('0x20')](_0x2462bf[_0xd1c3('0x13')],_0xd1c3('0x21'))>moment()){throw new Error(_0xd1c3('0x10'));}return utils[_0xd1c3('0x22')](_0x9c985b,0x0,![],null)[_0xd1c3('0x1d')](function(){return utils['getLicense'](_0x9c985b);})[_0xd1c3('0x1d')](function(){return _0x1cc142(null,_0x9c985b);});}else{return utils[_0xd1c3('0x23')](_0x9c985b)[_0xd1c3('0x1d')](function(){return _0x1cc142(null,_0x9c985b);});}}else{return utils['getLicense'](_0x9c985b)[_0xd1c3('0x1d')](function(){return _0x1cc142(null,_0x9c985b);});}})['catch'](function(_0x49ac0f){return _0x1cc142(null,![],_0x49ac0f);});}function handleError(_0x16aaec,_0x47ad3d,_0x44181d){return _0x16aaec['status'](_0x47ad3d||0x1f4)[_0xd1c3('0x24')]({'message':_0x44181d?_0x44181d[_0xd1c3('0x25')]||_0x44181d:''});}exports[_0xd1c3('0x26')]=function(_0x7ae715,_0x592aa0,_0x15be67){passport['authenticate'](_0xd1c3('0x27'),function(_0x35e795,_0x468857,_0x70b52b){var _0x4d3762=_0x35e795||_0x70b52b;if(_0x4d3762){return handleError(_0x592aa0,0x191,_0x4d3762);}if(!_0x468857){return handleError(_0x592aa0,0x194,{'message':_0xd1c3('0x28')});}else{var _0x1f6dbe={'payload':{'id':_0x468857['id'],'role':_0x468857['role']},'options':{'expiresIn':0x15180}};return authService[_0xd1c3('0x29')](_0x1f6dbe)['then'](function(_0x5924e2){_0x592aa0[_0xd1c3('0x2a')]({'id':_0x468857['id'],'token':_0x5924e2,'statusCall':'','isAlreadyLogged':_0x468857['online']});});}})(_0x7ae715,_0x592aa0,_0x15be67);};passport[_0xd1c3('0x2b')](_0xd1c3('0x27'),new LocalStrategy({'usernameField':_0xd1c3('0x1e'),'passwordField':'password'},function(_0x12d4ca,_0x3b8ad9,_0x5bca3c){return ActiveDirectoryAuthenticate(db[_0xd1c3('0x2c')],_0x12d4ca,_0x3b8ad9,_0x5bca3c);}));