d3fd2be8f94f28aa38495bbbdb4ea80939572c9c
[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 _0x8648=['blocked','blockedAt','online','admin','user','agent','then','disabled','add','clearBlockUser','getLicense','status','send','message','login','ad-login','Something\x20went\x20wrong,\x20please\x20try\x20again.','role','User','passport','passport-local','Strategy','moment','activedirectory2','bluebird','util','../../mysqldb','../../components/auth/service','../../config/utils','../../config/environment','../../config/logger','api','activedirectory','undefined','baseDN','url','domain','authenticate','inspect','error','Error\x20authenticate\x20%s:\x20Wrong\x20credentials','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20configured','Setting','findOne','blockDuration','adSsoEnabled','name','salt'];(function(_0x300c19,_0x709d87){var _0x52ccab=function(_0x27bf6e){while(--_0x27bf6e){_0x300c19['push'](_0x300c19['shift']());}};_0x52ccab(++_0x709d87);}(_0x8648,0x16a));var _0x8864=function(_0x2da9eb,_0x43b26e){_0x2da9eb=_0x2da9eb-0x0;var _0x3f8166=_0x8648[_0x2da9eb];return _0x3f8166;};'use strict';var passport=require(_0x8864('0x0'));var LocalStrategy=require(_0x8864('0x1'))[_0x8864('0x2')];var moment=require(_0x8864('0x3'));var ActiveDirectory=require(_0x8864('0x4'));var BPromise=require(_0x8864('0x5'));var util=require(_0x8864('0x6'));var db=require(_0x8864('0x7'))['db'];var authService=require(_0x8864('0x8'));var utils=require(_0x8864('0x9'));var config=require(_0x8864('0xa'));var logger=require(_0x8864('0xb'))(_0x8864('0xc'));function isActiveDirectoryConfigured(_0x427a54){var _0x958085=![];if(typeof _0x427a54[_0x8864('0xd')]!==_0x8864('0xe')){_0x958085=_0x427a54[_0x8864('0xd')][_0x8864('0xf')]&&_0x427a54[_0x8864('0xd')][_0x8864('0x10')]&&_0x427a54[_0x8864('0xd')][_0x8864('0x11')];}return _0x958085;}function adAuthenticate(_0x297d8c,_0x1170ff,_0x4651ab){return new BPromise(function(_0x54b670,_0x166849){try{var _0x139a85=new ActiveDirectory(_0x297d8c);_0x139a85[_0x8864('0x12')](_0x1170ff,_0x4651ab,function(_0x218e13,_0xa45a9d){if(_0x218e13){logger['error']('Error\x20authenticate\x20%s:\x20%s',_0x1170ff,util[_0x8864('0x13')](_0x218e13,{'showHidden':![],'depth':null}));_0x166849('Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.');}else if(!_0xa45a9d){logger[_0x8864('0x14')](_0x8864('0x15'),_0x1170ff);_0x166849(_0x8864('0x16'));}else{_0x54b670(_0xa45a9d);}});}catch(_0x1f87df){_0x166849(_0x1f87df);}});}function ActiveDirectoryAuthenticate(_0x2245a5,_0x140969,_0x16c847,_0x4868ea){var _0x25f5ca,_0x579cad;if(!isActiveDirectoryConfigured(config)){logger[_0x8864('0x14')](_0x8864('0x17'),_0x140969);return _0x4868ea(null,![],{'message':_0x8864('0x16')});}return db[_0x8864('0x18')][_0x8864('0x19')]({'where':{'id':0x1},'attributes':['adSsoEnabled',_0x8864('0x1a')],'raw':!![]})['then'](function(_0x496458){_0x25f5ca=_0x496458;if(!_0x25f5ca[_0x8864('0x1b')]){logger[_0x8864('0x14')]('Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20enabled',_0x140969);throw new Error(_0x8864('0x16'));}return _0x2245a5[_0x8864('0x19')]({'attributes':['id',_0x8864('0x1c'),'password',_0x8864('0x1d'),'role','disabled',_0x8864('0x1e'),_0x8864('0x1f'),_0x8864('0x20')],'where':{'name':_0x140969,'role':{'$or':[_0x8864('0x21'),_0x8864('0x22'),_0x8864('0x23')]},'adSsoEnabled':!![]}});})[_0x8864('0x24')](function(_0x53b6d9){if(!_0x53b6d9||_0x53b6d9&&_0x53b6d9[_0x8864('0x1c')]!==_0x140969){throw new Error(_0x8864('0x16'));}_0x579cad=_0x53b6d9;var _0x58b69a=_0x579cad['name']+'@'+config[_0x8864('0xd')][_0x8864('0x11')];return adAuthenticate(config[_0x8864('0xd')],_0x58b69a,_0x16c847);})['then'](function(_0x4746fd){if(_0x579cad[_0x8864('0x25')]){throw new Error(_0x8864('0x16'));}if(_0x25f5ca[_0x8864('0x1a')]>0x0){if(_0x579cad[_0x8864('0x1e')]){if(moment(_0x579cad[_0x8864('0x1f')])[_0x8864('0x26')](_0x25f5ca[_0x8864('0x1a')],'minutes')>moment()){throw new Error(_0x8864('0x16'));}return utils[_0x8864('0x27')](_0x579cad,0x0,![],null)[_0x8864('0x24')](function(){return utils[_0x8864('0x28')](_0x579cad);})['then'](function(){return _0x4868ea(null,_0x579cad);});}else{return utils[_0x8864('0x28')](_0x579cad)['then'](function(){return _0x4868ea(null,_0x579cad);});}}else{return utils[_0x8864('0x28')](_0x579cad)[_0x8864('0x24')](function(){return _0x4868ea(null,_0x579cad);});}})['catch'](function(_0x2fd841){return _0x4868ea(null,![],_0x2fd841);});}function handleError(_0x31d42f,_0xfa468c,_0x291bc2){return _0x31d42f[_0x8864('0x29')](_0xfa468c||0x1f4)[_0x8864('0x2a')]({'message':_0x291bc2?_0x291bc2[_0x8864('0x2b')]||_0x291bc2:''});}exports[_0x8864('0x2c')]=function(_0x341744,_0x326e22,_0x1966c8){passport[_0x8864('0x12')](_0x8864('0x2d'),function(_0x485c7f,_0x37d016,_0x275ce5){var _0x19149a=_0x485c7f||_0x275ce5;if(_0x19149a){return handleError(_0x326e22,0x191,_0x19149a);}if(!_0x37d016){return handleError(_0x326e22,0x194,{'message':_0x8864('0x2e')});}else{var _0x333ddb={'payload':{'id':_0x37d016['id'],'role':_0x37d016[_0x8864('0x2f')]},'options':{'expiresIn':0x15180}};return authService['signToken'](_0x333ddb)['then'](function(_0x20eca4){_0x326e22['json']({'id':_0x37d016['id'],'token':_0x20eca4,'statusCall':'','isAlreadyLogged':_0x37d016['online']});});}})(_0x341744,_0x326e22,_0x1966c8);};passport['use'](_0x8864('0x2d'),new LocalStrategy({'usernameField':_0x8864('0x1c'),'passwordField':'password'},function(_0x40cec6,_0x3b0ee1,_0x48170e){return ActiveDirectoryAuthenticate(db[_0x8864('0x30')],_0x40cec6,_0x3b0ee1,_0x48170e);}));