Built motion from commit dc14b02d.|2.6.27
[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 _0x46b1=['User','passport','passport-local','Strategy','activedirectory2','bluebird','util','../../mysqldb','../../components/auth/service','../../config/utils','api','activedirectory','baseDN','url','domain','error','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','blockDuration','then','adSsoEnabled','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20enabled','name','password','salt','role','disabled','blocked','online','admin','user','agent','add','clearBlockUser','getLicense','message','login','authenticate','ad-login','signToken','json','use'];(function(_0xa4bd21,_0x232b23){var _0x34f8da=function(_0x5642ce){while(--_0x5642ce){_0xa4bd21['push'](_0xa4bd21['shift']());}};_0x34f8da(++_0x232b23);}(_0x46b1,0x143));var _0x146b=function(_0x46a3ff,_0x470b9a){_0x46a3ff=_0x46a3ff-0x0;var _0x404bc2=_0x46b1[_0x46a3ff];return _0x404bc2;};'use strict';var passport=require(_0x146b('0x0'));var LocalStrategy=require(_0x146b('0x1'))[_0x146b('0x2')];var moment=require('moment');var ActiveDirectory=require(_0x146b('0x3'));var BPromise=require(_0x146b('0x4'));var util=require(_0x146b('0x5'));var db=require(_0x146b('0x6'))['db'];var authService=require(_0x146b('0x7'));var utils=require(_0x146b('0x8'));var config=require('../../config/environment');var logger=require('../../config/logger')(_0x146b('0x9'));function isActiveDirectoryConfigured(_0x2622ec){var _0x4bb974=![];if(typeof _0x2622ec[_0x146b('0xa')]!=='undefined'){_0x4bb974=_0x2622ec[_0x146b('0xa')][_0x146b('0xb')]&&_0x2622ec[_0x146b('0xa')][_0x146b('0xc')]&&_0x2622ec[_0x146b('0xa')][_0x146b('0xd')];}return _0x4bb974;}function adAuthenticate(_0x22f636,_0x570709,_0x15526a){return new BPromise(function(_0x5d5f4b,_0x484dfb){try{var _0x270c94=new ActiveDirectory(_0x22f636);_0x270c94['authenticate'](_0x570709,_0x15526a,function(_0x368902,_0x96af3d){if(_0x368902){logger[_0x146b('0xe')]('Error\x20authenticate\x20%s:\x20%s',_0x570709,util[_0x146b('0xf')](_0x368902,{'showHidden':![],'depth':null}));_0x484dfb(_0x146b('0x10'));}else if(!_0x96af3d){logger['error'](_0x146b('0x11'),_0x570709);_0x484dfb('Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.');}else{_0x5d5f4b(_0x96af3d);}});}catch(_0xbe67db){_0x484dfb(_0xbe67db);}});}function ActiveDirectoryAuthenticate(_0x4fd47c,_0x3a0930,_0x5b480f,_0x532d77){var _0xd68a18,_0x3bed78;if(!isActiveDirectoryConfigured(config)){logger[_0x146b('0xe')](_0x146b('0x12'),_0x3a0930);return _0x532d77(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}return db[_0x146b('0x13')][_0x146b('0x14')]({'where':{'id':0x1},'attributes':['adSsoEnabled',_0x146b('0x15')],'raw':!![]})[_0x146b('0x16')](function(_0x235785){_0xd68a18=_0x235785;if(!_0xd68a18[_0x146b('0x17')]){logger[_0x146b('0xe')](_0x146b('0x18'),_0x3a0930);throw new Error('Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.');}return _0x4fd47c[_0x146b('0x14')]({'attributes':['id',_0x146b('0x19'),_0x146b('0x1a'),_0x146b('0x1b'),_0x146b('0x1c'),_0x146b('0x1d'),_0x146b('0x1e'),'blockedAt',_0x146b('0x1f')],'where':{'name':_0x3a0930,'role':{'$or':[_0x146b('0x20'),_0x146b('0x21'),_0x146b('0x22')]},'adSsoEnabled':!![]}});})[_0x146b('0x16')](function(_0x432045){if(!_0x432045||_0x432045&&_0x432045[_0x146b('0x19')]!==_0x3a0930){throw new Error('Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.');}_0x3bed78=_0x432045;var _0x5ba207=_0x3bed78[_0x146b('0x19')]+'@'+config['activedirectory'][_0x146b('0xd')];return adAuthenticate(config['activedirectory'],_0x5ba207,_0x5b480f);})[_0x146b('0x16')](function(_0x5156ad){if(_0x3bed78[_0x146b('0x1d')]){throw new Error(_0x146b('0x10'));}if(_0xd68a18[_0x146b('0x15')]>0x0){if(_0x3bed78[_0x146b('0x1e')]){if(moment(_0x3bed78['blockedAt'])[_0x146b('0x23')](_0xd68a18[_0x146b('0x15')],'minutes')>moment()){throw new Error(_0x146b('0x10'));}return utils[_0x146b('0x24')](_0x3bed78,0x0,![],null)[_0x146b('0x16')](function(){return utils[_0x146b('0x25')](_0x3bed78);})[_0x146b('0x16')](function(){return _0x532d77(null,_0x3bed78);});}else{return utils[_0x146b('0x25')](_0x3bed78)[_0x146b('0x16')](function(){return _0x532d77(null,_0x3bed78);});}}else{return utils[_0x146b('0x25')](_0x3bed78)[_0x146b('0x16')](function(){return _0x532d77(null,_0x3bed78);});}})['catch'](function(_0x1e8b03){return _0x532d77(null,![],_0x1e8b03);});}function handleError(_0x3107b7,_0x2e6f12,_0x1c7bf2){return _0x3107b7['status'](_0x2e6f12||0x1f4)['send']({'message':_0x1c7bf2?_0x1c7bf2[_0x146b('0x26')]||_0x1c7bf2:''});}exports[_0x146b('0x27')]=function(_0x1886f7,_0x1d9823,_0xb98687){passport[_0x146b('0x28')](_0x146b('0x29'),function(_0x195c63,_0xec8ea1,_0x17ce46){var _0x3e6a6e=_0x195c63||_0x17ce46;if(_0x3e6a6e){return handleError(_0x1d9823,0x191,_0x3e6a6e);}if(!_0xec8ea1){return handleError(_0x1d9823,0x194,{'message':'Something\x20went\x20wrong,\x20please\x20try\x20again.'});}else{var _0x538c98={'payload':{'id':_0xec8ea1['id'],'role':_0xec8ea1[_0x146b('0x1c')]},'options':{'expiresIn':0x15180}};return authService[_0x146b('0x2a')](_0x538c98)['then'](function(_0x4aadec){_0x1d9823[_0x146b('0x2b')]({'id':_0xec8ea1['id'],'token':_0x4aadec,'statusCall':'','isAlreadyLogged':_0xec8ea1[_0x146b('0x1f')]});});}})(_0x1886f7,_0x1d9823,_0xb98687);};passport[_0x146b('0x2c')](_0x146b('0x29'),new LocalStrategy({'usernameField':_0x146b('0x19'),'passwordField':_0x146b('0x1a')},function(_0x5928e8,_0x514abe,_0x2f07ce){return ActiveDirectoryAuthenticate(db[_0x146b('0x2d')],_0x5928e8,_0x514abe,_0x2f07ce);}));