Built motion from commit 9d5c653a.|2.5.44
[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 _0x2207=['../../components/auth/service','../../config/utils','../../config/environment','../../config/logger','api','undefined','activedirectory','url','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:\x20Active\x20Directory\x20not\x20configured','adSsoEnabled','then','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20enabled','findOne','name','password','salt','disabled','blockedAt','online','admin','blockDuration','blocked','minutes','clearBlockUser','getLicense','catch','status','send','message','login','ad-login','Something\x20went\x20wrong,\x20please\x20try\x20again.','passport','passport-local','Strategy','activedirectory2','bluebird','util'];(function(_0x56a3a8,_0x356ae0){var _0x4d9814=function(_0x4aae00){while(--_0x4aae00){_0x56a3a8['push'](_0x56a3a8['shift']());}};_0x4d9814(++_0x356ae0);}(_0x2207,0x15a));var _0x7220=function(_0xc95853,_0x165cfa){_0xc95853=_0xc95853-0x0;var _0x23d7e1=_0x2207[_0xc95853];return _0x23d7e1;};'use strict';var passport=require(_0x7220('0x0'));var LocalStrategy=require(_0x7220('0x1'))[_0x7220('0x2')];var moment=require('moment');var ActiveDirectory=require(_0x7220('0x3'));var BPromise=require(_0x7220('0x4'));var util=require(_0x7220('0x5'));var db=require('../../mysqldb')['db'];var authService=require(_0x7220('0x6'));var utils=require(_0x7220('0x7'));var config=require(_0x7220('0x8'));var logger=require(_0x7220('0x9'))(_0x7220('0xa'));function isActiveDirectoryConfigured(_0xb53e89){var _0x17e21d=![];if(typeof _0xb53e89['activedirectory']!==_0x7220('0xb')){_0x17e21d=_0xb53e89[_0x7220('0xc')]['baseDN']&&_0xb53e89['activedirectory'][_0x7220('0xd')]&&_0xb53e89[_0x7220('0xc')][_0x7220('0xe')];}return _0x17e21d;}function adAuthenticate(_0x6ce05a,_0x426893,_0x1e5be3){return new BPromise(function(_0x57457c,_0x4db8fb){try{var _0x531747=new ActiveDirectory(_0x6ce05a);_0x531747[_0x7220('0xf')](_0x426893,_0x1e5be3,function(_0x556de5,_0x3711d9){if(_0x556de5){logger[_0x7220('0x10')](_0x7220('0x11'),_0x426893,util[_0x7220('0x12')](_0x556de5,{'showHidden':![],'depth':null}));_0x4db8fb(_0x7220('0x13'));}else if(!_0x3711d9){logger[_0x7220('0x10')]('Error\x20authenticate\x20%s:\x20Wrong\x20credentials',_0x426893);_0x4db8fb(_0x7220('0x13'));}else{_0x57457c(_0x3711d9);}});}catch(_0x4d60fe){_0x4db8fb(_0x4d60fe);}});}function ActiveDirectoryAuthenticate(_0x1bc60a,_0xece26f,_0x466f05,_0x5c53f3){var _0x58e94f,_0x332223;if(!isActiveDirectoryConfigured(config)){logger[_0x7220('0x10')](_0x7220('0x14'),_0xece26f);return _0x5c53f3(null,![],{'message':_0x7220('0x13')});}return db['Setting']['findOne']({'where':{'id':0x1},'attributes':[_0x7220('0x15'),'blockDuration'],'raw':!![]})[_0x7220('0x16')](function(_0x4bd026){_0x58e94f=_0x4bd026;if(!_0x58e94f['adSsoEnabled']){logger['error'](_0x7220('0x17'),_0xece26f);throw new Error('Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.');}return _0x1bc60a[_0x7220('0x18')]({'logging':!![],'attributes':['id',_0x7220('0x19'),_0x7220('0x1a'),_0x7220('0x1b'),'role',_0x7220('0x1c'),'blocked',_0x7220('0x1d'),_0x7220('0x1e')],'where':{'name':_0xece26f,'role':{'$or':[_0x7220('0x1f'),'user','agent']},'adSsoEnabled':!![]}});})['then'](function(_0x37408d){if(!_0x37408d||_0x37408d&&_0x37408d[_0x7220('0x19')]!==_0xece26f){throw new Error(_0x7220('0x13'));}_0x332223=_0x37408d;var _0x3c5e1e=_0x332223['name']+'@'+config[_0x7220('0xc')]['domain'];return adAuthenticate(config[_0x7220('0xc')],_0x3c5e1e,_0x466f05);})['then'](function(_0x1a3741){if(_0x332223[_0x7220('0x1c')]){throw new Error(_0x7220('0x13'));}if(_0x58e94f[_0x7220('0x20')]>0x0){if(_0x332223[_0x7220('0x21')]){if(moment(_0x332223[_0x7220('0x1d')])['add'](_0x58e94f[_0x7220('0x20')],_0x7220('0x22'))>moment()){throw new Error(_0x7220('0x13'));}return utils[_0x7220('0x23')](_0x332223,0x0,![],null)[_0x7220('0x16')](function(){return utils['getLicense'](_0x332223);})[_0x7220('0x16')](function(){return _0x5c53f3(null,_0x332223);});}else{return utils[_0x7220('0x24')](_0x332223)[_0x7220('0x16')](function(){return _0x5c53f3(null,_0x332223);});}}else{return utils[_0x7220('0x24')](_0x332223)[_0x7220('0x16')](function(){return _0x5c53f3(null,_0x332223);});}})[_0x7220('0x25')](function(_0x2d7d99){return _0x5c53f3(null,![],_0x2d7d99);});}function handleError(_0x3fe4eb,_0x2cd026,_0x683625){return _0x3fe4eb[_0x7220('0x26')](_0x2cd026||0x1f4)[_0x7220('0x27')]({'message':_0x683625?_0x683625[_0x7220('0x28')]||_0x683625:''});}exports[_0x7220('0x29')]=function(_0x52a4a5,_0x48a43b,_0x23d3aa){passport[_0x7220('0xf')](_0x7220('0x2a'),function(_0x14c56b,_0x14380b,_0x3ce6b9){var _0x2ce307=_0x14c56b||_0x3ce6b9;if(_0x2ce307){return handleError(_0x48a43b,0x191,_0x2ce307);}if(!_0x14380b){return handleError(_0x48a43b,0x194,{'message':_0x7220('0x2b')});}else{var _0xa5e8ab={'payload':{'id':_0x14380b['id'],'role':_0x14380b['role']},'options':{'expiresIn':0x15180}};return authService['signToken'](_0xa5e8ab)[_0x7220('0x16')](function(_0x1aab84){_0x48a43b['json']({'id':_0x14380b['id'],'token':_0x1aab84,'statusCall':'','isAlreadyLogged':_0x14380b['online']});});}})(_0x52a4a5,_0x48a43b,_0x23d3aa);};passport['use']('ad-login',new LocalStrategy({'usernameField':'name','passwordField':'password'},function(_0x1642bd,_0x3c1d1e,_0x56fe11){return ActiveDirectoryAuthenticate(db['User'],_0x1642bd,_0x3c1d1e,_0x56fe11);}));