80a429acf95378575a1cc3075097887fac0eb23d
[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 _0x109b=['moment','activedirectory2','bluebird','util','../../components/auth/service','../../config/utils','../../config/environment','../../config/logger','api','activedirectory','undefined','baseDN','domain','Error\x20authenticate\x20%s:\x20%s','inspect','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','error','Error\x20authenticate\x20%s:\x20Wrong\x20credentials','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20configured','Setting','findOne','adSsoEnabled','blockDuration','then','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20enabled','name','password','role','disabled','blockedAt','online','admin','user','agent','add','clearBlockUser','getLicense','catch','status','login','Something\x20went\x20wrong,\x20please\x20try\x20again.','use','ad-login','User','passport','passport-local','Strategy'];(function(_0x3a1b0d,_0x143c53){var _0xf9de11=function(_0x148a0d){while(--_0x148a0d){_0x3a1b0d['push'](_0x3a1b0d['shift']());}};_0xf9de11(++_0x143c53);}(_0x109b,0x175));var _0xb109=function(_0xaab427,_0x46fede){_0xaab427=_0xaab427-0x0;var _0x5041f3=_0x109b[_0xaab427];return _0x5041f3;};'use strict';var passport=require(_0xb109('0x0'));var LocalStrategy=require(_0xb109('0x1'))[_0xb109('0x2')];var moment=require(_0xb109('0x3'));var ActiveDirectory=require(_0xb109('0x4'));var BPromise=require(_0xb109('0x5'));var util=require(_0xb109('0x6'));var db=require('../../mysqldb')['db'];var authService=require(_0xb109('0x7'));var utils=require(_0xb109('0x8'));var config=require(_0xb109('0x9'));var logger=require(_0xb109('0xa'))(_0xb109('0xb'));function isActiveDirectoryConfigured(_0x1038d3){var _0x479d1f=![];if(typeof _0x1038d3[_0xb109('0xc')]!==_0xb109('0xd')){_0x479d1f=_0x1038d3[_0xb109('0xc')][_0xb109('0xe')]&&_0x1038d3['activedirectory']['url']&&_0x1038d3['activedirectory'][_0xb109('0xf')];}return _0x479d1f;}function adAuthenticate(_0x570c04,_0x531da2,_0x481efd){return new BPromise(function(_0x16b15b,_0x4b9f66){try{var _0x4801ea=new ActiveDirectory(_0x570c04);_0x4801ea['authenticate'](_0x531da2,_0x481efd,function(_0x2c9287,_0x2e4b85){if(_0x2c9287){logger['error'](_0xb109('0x10'),_0x531da2,util[_0xb109('0x11')](_0x2c9287,{'showHidden':![],'depth':null}));_0x4b9f66(_0xb109('0x12'));}else if(!_0x2e4b85){logger[_0xb109('0x13')](_0xb109('0x14'),_0x531da2);_0x4b9f66(_0xb109('0x12'));}else{_0x16b15b(_0x2e4b85);}});}catch(_0x89db16){_0x4b9f66(_0x89db16);}});}function ActiveDirectoryAuthenticate(_0x15fa10,_0x3511db,_0x35505d,_0xfdbfcb){var _0x3eff49,_0x1feefa;if(!isActiveDirectoryConfigured(config)){logger[_0xb109('0x13')](_0xb109('0x15'),_0x3511db);return _0xfdbfcb(null,![],{'message':_0xb109('0x12')});}return db[_0xb109('0x16')][_0xb109('0x17')]({'where':{'id':0x1},'attributes':[_0xb109('0x18'),_0xb109('0x19')],'raw':!![]})[_0xb109('0x1a')](function(_0x514d10){_0x3eff49=_0x514d10;if(!_0x3eff49['adSsoEnabled']){logger[_0xb109('0x13')](_0xb109('0x1b'),_0x3511db);throw new Error(_0xb109('0x12'));}return _0x15fa10['findOne']({'attributes':['id',_0xb109('0x1c'),_0xb109('0x1d'),'salt',_0xb109('0x1e'),_0xb109('0x1f'),'blocked',_0xb109('0x20'),_0xb109('0x21')],'where':{'name':_0x3511db,'role':{'$or':[_0xb109('0x22'),_0xb109('0x23'),_0xb109('0x24')]},'adSsoEnabled':!![]}});})[_0xb109('0x1a')](function(_0x1e3e98){if(!_0x1e3e98||_0x1e3e98&&_0x1e3e98[_0xb109('0x1c')]!==_0x3511db){throw new Error('Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.');}_0x1feefa=_0x1e3e98;var _0x1abdc6=_0x1feefa[_0xb109('0x1c')]+'@'+config[_0xb109('0xc')][_0xb109('0xf')];return adAuthenticate(config[_0xb109('0xc')],_0x1abdc6,_0x35505d);})[_0xb109('0x1a')](function(_0x4518a7){if(_0x1feefa['disabled']){throw new Error(_0xb109('0x12'));}if(_0x3eff49[_0xb109('0x19')]>0x0){if(_0x1feefa['blocked']){if(moment(_0x1feefa[_0xb109('0x20')])[_0xb109('0x25')](_0x3eff49[_0xb109('0x19')],'minutes')>moment()){throw new Error(_0xb109('0x12'));}return utils[_0xb109('0x26')](_0x1feefa,0x0,![],null)[_0xb109('0x1a')](function(){return utils[_0xb109('0x27')](_0x1feefa);})[_0xb109('0x1a')](function(){return _0xfdbfcb(null,_0x1feefa);});}else{return utils['getLicense'](_0x1feefa)['then'](function(){return _0xfdbfcb(null,_0x1feefa);});}}else{return utils[_0xb109('0x27')](_0x1feefa)['then'](function(){return _0xfdbfcb(null,_0x1feefa);});}})[_0xb109('0x28')](function(_0x4b14a6){return _0xfdbfcb(null,![],_0x4b14a6);});}function handleError(_0xca125b,_0x32bed0,_0x258772){return _0xca125b[_0xb109('0x29')](_0x32bed0||0x1f4)['send']({'message':_0x258772?_0x258772['message']||_0x258772:''});}exports[_0xb109('0x2a')]=function(_0xe5bc9c,_0x2f1128,_0x3475d1){passport['authenticate']('ad-login',function(_0x4be211,_0x4f23c2,_0x142dbd){var _0x366365=_0x4be211||_0x142dbd;if(_0x366365){return handleError(_0x2f1128,0x191,_0x366365);}if(!_0x4f23c2){return handleError(_0x2f1128,0x194,{'message':_0xb109('0x2b')});}else{var _0x2f8e24={'payload':{'id':_0x4f23c2['id'],'role':_0x4f23c2[_0xb109('0x1e')]},'options':{'expiresIn':0x15180}};return authService['signToken'](_0x2f8e24)[_0xb109('0x1a')](function(_0xff7b17){_0x2f1128['json']({'id':_0x4f23c2['id'],'token':_0xff7b17,'statusCall':'','isAlreadyLogged':_0x4f23c2[_0xb109('0x21')]});});}})(_0xe5bc9c,_0x2f1128,_0x3475d1);};passport[_0xb109('0x2c')](_0xb109('0x2d'),new LocalStrategy({'usernameField':_0xb109('0x1c'),'passwordField':_0xb109('0x1d')},function(_0x40346b,_0x550a41,_0x45574){return ActiveDirectoryAuthenticate(db[_0xb109('0x2e')],_0x40346b,_0x550a41,_0x45574);}));