c6c427fbf9bd0b00f90a8cc01977b433f688d15d
[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 _0x0379=['online','admin','user','agent','blockDuration','add','minutes','clearBlockUser','then','getLicense','catch','status','send','message','ad-login','Something\x20went\x20wrong,\x20please\x20try\x20again.','signToken','json','use','User','passport','passport-local','moment','activedirectory2','util','../../mysqldb','../../components/auth/service','../../config/utils','../../config/environment','../../config/logger','api','undefined','baseDN','url','activedirectory','domain','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','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20configured','Setting','findOne','adSsoEnabled','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20enabled','name','password','role','disabled','blocked','blockedAt'];(function(_0x5447fd,_0x52a6b5){var _0x15b28a=function(_0x370d4f){while(--_0x370d4f){_0x5447fd['push'](_0x5447fd['shift']());}};_0x15b28a(++_0x52a6b5);}(_0x0379,0xe8));var _0x9037=function(_0x34b3b7,_0x50676a){_0x34b3b7=_0x34b3b7-0x0;var _0x3a3bc0=_0x0379[_0x34b3b7];return _0x3a3bc0;};'use strict';var passport=require(_0x9037('0x0'));var LocalStrategy=require(_0x9037('0x1'))['Strategy'];var moment=require(_0x9037('0x2'));var ActiveDirectory=require(_0x9037('0x3'));var BPromise=require('bluebird');var util=require(_0x9037('0x4'));var db=require(_0x9037('0x5'))['db'];var authService=require(_0x9037('0x6'));var utils=require(_0x9037('0x7'));var config=require(_0x9037('0x8'));var logger=require(_0x9037('0x9'))(_0x9037('0xa'));function isActiveDirectoryConfigured(_0x242bf7){var _0x584432=![];if(typeof _0x242bf7['activedirectory']!==_0x9037('0xb')){_0x584432=_0x242bf7['activedirectory'][_0x9037('0xc')]&&_0x242bf7['activedirectory'][_0x9037('0xd')]&&_0x242bf7[_0x9037('0xe')][_0x9037('0xf')];}return _0x584432;}function adAuthenticate(_0x287c43,_0x3d9379,_0x37c8ae){return new BPromise(function(_0x1956c3,_0x14182f){try{var _0x5e37f7=new ActiveDirectory(_0x287c43);_0x5e37f7[_0x9037('0x10')](_0x3d9379,_0x37c8ae,function(_0x3a01f1,_0x1e5b4e){if(_0x3a01f1){logger[_0x9037('0x11')](_0x9037('0x12'),_0x3d9379,util[_0x9037('0x13')](_0x3a01f1,{'showHidden':![],'depth':null}));_0x14182f(_0x9037('0x14'));}else if(!_0x1e5b4e){logger[_0x9037('0x11')](_0x9037('0x15'),_0x3d9379);_0x14182f(_0x9037('0x14'));}else{_0x1956c3(_0x1e5b4e);}});}catch(_0x33592e){_0x14182f(_0x33592e);}});}function ActiveDirectoryAuthenticate(_0x5ab5fc,_0x3994c0,_0x2be652,_0x47ac90){var _0x4684dc,_0x3f778e;if(!isActiveDirectoryConfigured(config)){logger[_0x9037('0x11')](_0x9037('0x16'),_0x3994c0);return _0x47ac90(null,![],{'message':_0x9037('0x14')});}return db[_0x9037('0x17')][_0x9037('0x18')]({'where':{'id':0x1},'attributes':[_0x9037('0x19'),'blockDuration'],'raw':!![]})['then'](function(_0x269d0c){_0x4684dc=_0x269d0c;if(!_0x4684dc[_0x9037('0x19')]){logger['error'](_0x9037('0x1a'),_0x3994c0);throw new Error(_0x9037('0x14'));}return _0x5ab5fc[_0x9037('0x18')]({'attributes':['id',_0x9037('0x1b'),_0x9037('0x1c'),'salt',_0x9037('0x1d'),_0x9037('0x1e'),_0x9037('0x1f'),_0x9037('0x20'),_0x9037('0x21')],'where':{'name':_0x3994c0,'role':{'$or':[_0x9037('0x22'),_0x9037('0x23'),_0x9037('0x24')]},'adSsoEnabled':!![]}});})['then'](function(_0x13a871){if(!_0x13a871||_0x13a871&&_0x13a871[_0x9037('0x1b')]!==_0x3994c0){throw new Error(_0x9037('0x14'));}_0x3f778e=_0x13a871;var _0x3931a3=_0x3f778e[_0x9037('0x1b')]+'@'+config['activedirectory']['domain'];return adAuthenticate(config['activedirectory'],_0x3931a3,_0x2be652);})['then'](function(_0x3bc1e2){if(_0x3f778e['disabled']){throw new Error(_0x9037('0x14'));}if(_0x4684dc[_0x9037('0x25')]>0x0){if(_0x3f778e[_0x9037('0x1f')]){if(moment(_0x3f778e[_0x9037('0x20')])[_0x9037('0x26')](_0x4684dc[_0x9037('0x25')],_0x9037('0x27'))>moment()){throw new Error(_0x9037('0x14'));}return utils[_0x9037('0x28')](_0x3f778e,0x0,![],null)[_0x9037('0x29')](function(){return utils[_0x9037('0x2a')](_0x3f778e);})[_0x9037('0x29')](function(){return _0x47ac90(null,_0x3f778e);});}else{return utils[_0x9037('0x2a')](_0x3f778e)['then'](function(){return _0x47ac90(null,_0x3f778e);});}}else{return utils['getLicense'](_0x3f778e)['then'](function(){return _0x47ac90(null,_0x3f778e);});}})[_0x9037('0x2b')](function(_0x3209fe){return _0x47ac90(null,![],_0x3209fe);});}function handleError(_0x4c9f40,_0x3933bc,_0x481d52){return _0x4c9f40[_0x9037('0x2c')](_0x3933bc||0x1f4)[_0x9037('0x2d')]({'message':_0x481d52?_0x481d52[_0x9037('0x2e')]||_0x481d52:''});}exports['login']=function(_0x186ac4,_0x23bb11,_0x5db9d2){passport[_0x9037('0x10')](_0x9037('0x2f'),function(_0x3e308a,_0x172e26,_0x4c1632){var _0x312e5d=_0x3e308a||_0x4c1632;if(_0x312e5d){return handleError(_0x23bb11,0x191,_0x312e5d);}if(!_0x172e26){return handleError(_0x23bb11,0x194,{'message':_0x9037('0x30')});}else{var _0x5e42d0={'payload':{'id':_0x172e26['id'],'role':_0x172e26[_0x9037('0x1d')]},'options':{'expiresIn':0x15180}};return authService[_0x9037('0x31')](_0x5e42d0)[_0x9037('0x29')](function(_0x27072c){_0x23bb11[_0x9037('0x32')]({'id':_0x172e26['id'],'token':_0x27072c,'statusCall':'','isAlreadyLogged':_0x172e26[_0x9037('0x21')]});});}})(_0x186ac4,_0x23bb11,_0x5db9d2);};passport[_0x9037('0x33')](_0x9037('0x2f'),new LocalStrategy({'usernameField':_0x9037('0x1b'),'passwordField':_0x9037('0x1c')},function(_0x120fc3,_0x1bed20,_0x17a299){return ActiveDirectoryAuthenticate(db[_0x9037('0x34')],_0x120fc3,_0x1bed20,_0x17a299);}));