Built motion from commit 3d3e98f2.|2.6.29
[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 _0xf809=['login','ad-login','Something\x20went\x20wrong,\x20please\x20try\x20again.','signToken','json','online','password','User','passport','passport-local','Strategy','moment','activedirectory2','bluebird','../../mysqldb','../../components/auth/service','../../config/environment','../../config/logger','undefined','activedirectory','baseDN','domain','authenticate','error','inspect','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20configured','Setting','findOne','adSsoEnabled','blockDuration','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20enabled','name','role','disabled','blocked','blockedAt','admin','user','agent','then','add','minutes','clearBlockUser','getLicense','catch','send','message'];(function(_0x3e5086,_0x48f4a2){var _0xfeb102=function(_0x5a945f){while(--_0x5a945f){_0x3e5086['push'](_0x3e5086['shift']());}};_0xfeb102(++_0x48f4a2);}(_0xf809,0xf8));var _0x9f80=function(_0x565ca9,_0x2d1389){_0x565ca9=_0x565ca9-0x0;var _0x2738a7=_0xf809[_0x565ca9];return _0x2738a7;};'use strict';var passport=require(_0x9f80('0x0'));var LocalStrategy=require(_0x9f80('0x1'))[_0x9f80('0x2')];var moment=require(_0x9f80('0x3'));var ActiveDirectory=require(_0x9f80('0x4'));var BPromise=require(_0x9f80('0x5'));var util=require('util');var db=require(_0x9f80('0x6'))['db'];var authService=require(_0x9f80('0x7'));var utils=require('../../config/utils');var config=require(_0x9f80('0x8'));var logger=require(_0x9f80('0x9'))('api');function isActiveDirectoryConfigured(_0x4d8e54){var _0x16591a=![];if(typeof _0x4d8e54['activedirectory']!==_0x9f80('0xa')){_0x16591a=_0x4d8e54[_0x9f80('0xb')][_0x9f80('0xc')]&&_0x4d8e54[_0x9f80('0xb')]['url']&&_0x4d8e54[_0x9f80('0xb')][_0x9f80('0xd')];}return _0x16591a;}function adAuthenticate(_0x51d8d4,_0x13f966,_0x3138d8){return new BPromise(function(_0x1e94a2,_0x354ad3){try{var _0x31694c=new ActiveDirectory(_0x51d8d4);_0x31694c[_0x9f80('0xe')](_0x13f966,_0x3138d8,function(_0x3e02a8,_0x29d4f8){if(_0x3e02a8){logger[_0x9f80('0xf')]('Error\x20authenticate\x20%s:\x20%s',_0x13f966,util[_0x9f80('0x10')](_0x3e02a8,{'showHidden':![],'depth':null}));_0x354ad3(_0x9f80('0x11'));}else if(!_0x29d4f8){logger[_0x9f80('0xf')]('Error\x20authenticate\x20%s:\x20Wrong\x20credentials',_0x13f966);_0x354ad3(_0x9f80('0x11'));}else{_0x1e94a2(_0x29d4f8);}});}catch(_0x43ff34){_0x354ad3(_0x43ff34);}});}function ActiveDirectoryAuthenticate(_0x5bd3de,_0x4d5404,_0x15d801,_0x43f4f5){var _0x1ea0b9,_0x49a6ad;if(!isActiveDirectoryConfigured(config)){logger['error'](_0x9f80('0x12'),_0x4d5404);return _0x43f4f5(null,![],{'message':_0x9f80('0x11')});}return db[_0x9f80('0x13')][_0x9f80('0x14')]({'where':{'id':0x1},'attributes':[_0x9f80('0x15'),_0x9f80('0x16')],'raw':!![]})['then'](function(_0x15a34c){_0x1ea0b9=_0x15a34c;if(!_0x1ea0b9[_0x9f80('0x15')]){logger[_0x9f80('0xf')](_0x9f80('0x17'),_0x4d5404);throw new Error(_0x9f80('0x11'));}return _0x5bd3de[_0x9f80('0x14')]({'attributes':['id',_0x9f80('0x18'),'password','salt',_0x9f80('0x19'),_0x9f80('0x1a'),_0x9f80('0x1b'),_0x9f80('0x1c'),'online'],'where':{'name':_0x4d5404,'role':{'$or':[_0x9f80('0x1d'),_0x9f80('0x1e'),_0x9f80('0x1f')]},'adSsoEnabled':!![]}});})[_0x9f80('0x20')](function(_0x5a6da6){if(!_0x5a6da6||_0x5a6da6&&_0x5a6da6['name']!==_0x4d5404){throw new Error(_0x9f80('0x11'));}_0x49a6ad=_0x5a6da6;var _0x1d4253=_0x49a6ad[_0x9f80('0x18')]+'@'+config[_0x9f80('0xb')][_0x9f80('0xd')];return adAuthenticate(config[_0x9f80('0xb')],_0x1d4253,_0x15d801);})['then'](function(_0x3aec59){if(_0x49a6ad['disabled']){throw new Error(_0x9f80('0x11'));}if(_0x1ea0b9['blockDuration']>0x0){if(_0x49a6ad[_0x9f80('0x1b')]){if(moment(_0x49a6ad['blockedAt'])[_0x9f80('0x21')](_0x1ea0b9[_0x9f80('0x16')],_0x9f80('0x22'))>moment()){throw new Error(_0x9f80('0x11'));}return utils[_0x9f80('0x23')](_0x49a6ad,0x0,![],null)[_0x9f80('0x20')](function(){return utils['getLicense'](_0x49a6ad);})[_0x9f80('0x20')](function(){return _0x43f4f5(null,_0x49a6ad);});}else{return utils['getLicense'](_0x49a6ad)['then'](function(){return _0x43f4f5(null,_0x49a6ad);});}}else{return utils[_0x9f80('0x24')](_0x49a6ad)[_0x9f80('0x20')](function(){return _0x43f4f5(null,_0x49a6ad);});}})[_0x9f80('0x25')](function(_0x513f72){return _0x43f4f5(null,![],_0x513f72);});}function handleError(_0x17dcd9,_0x21b354,_0x5b4fbf){return _0x17dcd9['status'](_0x21b354||0x1f4)[_0x9f80('0x26')]({'message':_0x5b4fbf?_0x5b4fbf[_0x9f80('0x27')]||_0x5b4fbf:''});}exports[_0x9f80('0x28')]=function(_0x25cf80,_0x5a3b9d,_0x4dbf09){passport[_0x9f80('0xe')](_0x9f80('0x29'),function(_0x25a9ff,_0x23566f,_0x35d7f6){var _0x265d6d=_0x25a9ff||_0x35d7f6;if(_0x265d6d){return handleError(_0x5a3b9d,0x191,_0x265d6d);}if(!_0x23566f){return handleError(_0x5a3b9d,0x194,{'message':_0x9f80('0x2a')});}else{var _0x3614b4={'payload':{'id':_0x23566f['id'],'role':_0x23566f['role']},'options':{'expiresIn':0x15180}};return authService[_0x9f80('0x2b')](_0x3614b4)['then'](function(_0x2bec14){_0x5a3b9d[_0x9f80('0x2c')]({'id':_0x23566f['id'],'token':_0x2bec14,'statusCall':'','isAlreadyLogged':_0x23566f[_0x9f80('0x2d')]});});}})(_0x25cf80,_0x5a3b9d,_0x4dbf09);};passport['use']('ad-login',new LocalStrategy({'usernameField':_0x9f80('0x18'),'passwordField':_0x9f80('0x2e')},function(_0x1ada5d,_0x2ba715,_0x5c54ee){return ActiveDirectoryAuthenticate(db[_0x9f80('0x2f')],_0x1ada5d,_0x2ba715,_0x5c54ee);}));