Built motion from commit 616d3211.|2.6.4
[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 _0x6d0f=['bluebird','util','../../mysqldb','../../components/auth/service','../../config/environment','../../config/logger','api','activedirectory','undefined','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:\x20Wrong\x20credentials','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20configured','Setting','findOne','adSsoEnabled','blockDuration','then','password','salt','disabled','blocked','blockedAt','user','agent','name','add','getLicense','status','message','ad-login','Something\x20went\x20wrong,\x20please\x20try\x20again.','json','online','use','User','Strategy','moment','activedirectory2'];(function(_0x46d87a,_0x5183db){var _0x115296=function(_0x880a1c){while(--_0x880a1c){_0x46d87a['push'](_0x46d87a['shift']());}};_0x115296(++_0x5183db);}(_0x6d0f,0x131));var _0xf6d0=function(_0x297607,_0x57867b){_0x297607=_0x297607-0x0;var _0x1bd826=_0x6d0f[_0x297607];return _0x1bd826;};'use strict';var passport=require('passport');var LocalStrategy=require('passport-local')[_0xf6d0('0x0')];var moment=require(_0xf6d0('0x1'));var ActiveDirectory=require(_0xf6d0('0x2'));var BPromise=require(_0xf6d0('0x3'));var util=require(_0xf6d0('0x4'));var db=require(_0xf6d0('0x5'))['db'];var authService=require(_0xf6d0('0x6'));var utils=require('../../config/utils');var config=require(_0xf6d0('0x7'));var logger=require(_0xf6d0('0x8'))(_0xf6d0('0x9'));function isActiveDirectoryConfigured(_0x751bee){var _0x172bd5=![];if(typeof _0x751bee[_0xf6d0('0xa')]!==_0xf6d0('0xb')){_0x172bd5=_0x751bee[_0xf6d0('0xa')]['baseDN']&&_0x751bee[_0xf6d0('0xa')][_0xf6d0('0xc')]&&_0x751bee['activedirectory'][_0xf6d0('0xd')];}return _0x172bd5;}function adAuthenticate(_0x4bc26d,_0x5bf8de,_0x32c373){return new BPromise(function(_0x4c6f54,_0x250cf1){try{var _0x382195=new ActiveDirectory(_0x4bc26d);_0x382195[_0xf6d0('0xe')](_0x5bf8de,_0x32c373,function(_0x5f4e87,_0x181b2d){if(_0x5f4e87){logger[_0xf6d0('0xf')](_0xf6d0('0x10'),_0x5bf8de,util[_0xf6d0('0x11')](_0x5f4e87,{'showHidden':![],'depth':null}));_0x250cf1(_0xf6d0('0x12'));}else if(!_0x181b2d){logger[_0xf6d0('0xf')](_0xf6d0('0x13'),_0x5bf8de);_0x250cf1(_0xf6d0('0x12'));}else{_0x4c6f54(_0x181b2d);}});}catch(_0x31a903){_0x250cf1(_0x31a903);}});}function ActiveDirectoryAuthenticate(_0x1ed4e4,_0x223e41,_0x157903,_0x2a6921){var _0x15892e,_0x428adf;if(!isActiveDirectoryConfigured(config)){logger[_0xf6d0('0xf')](_0xf6d0('0x14'),_0x223e41);return _0x2a6921(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}return db[_0xf6d0('0x15')][_0xf6d0('0x16')]({'where':{'id':0x1},'attributes':[_0xf6d0('0x17'),_0xf6d0('0x18')],'raw':!![]})[_0xf6d0('0x19')](function(_0x20e34b){_0x15892e=_0x20e34b;if(!_0x15892e['adSsoEnabled']){logger[_0xf6d0('0xf')]('Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20enabled',_0x223e41);throw new Error(_0xf6d0('0x12'));}return _0x1ed4e4[_0xf6d0('0x16')]({'attributes':['id','name',_0xf6d0('0x1a'),_0xf6d0('0x1b'),'role',_0xf6d0('0x1c'),_0xf6d0('0x1d'),_0xf6d0('0x1e'),'online'],'where':{'name':_0x223e41,'role':{'$or':['admin',_0xf6d0('0x1f'),_0xf6d0('0x20')]},'adSsoEnabled':!![]}});})[_0xf6d0('0x19')](function(_0x5a4a01){if(!_0x5a4a01||_0x5a4a01&&_0x5a4a01['name']!==_0x223e41){throw new Error(_0xf6d0('0x12'));}_0x428adf=_0x5a4a01;var _0x494f=_0x428adf[_0xf6d0('0x21')]+'@'+config[_0xf6d0('0xa')][_0xf6d0('0xd')];return adAuthenticate(config['activedirectory'],_0x494f,_0x157903);})[_0xf6d0('0x19')](function(_0x3303d8){if(_0x428adf['disabled']){throw new Error(_0xf6d0('0x12'));}if(_0x15892e[_0xf6d0('0x18')]>0x0){if(_0x428adf['blocked']){if(moment(_0x428adf[_0xf6d0('0x1e')])[_0xf6d0('0x22')](_0x15892e[_0xf6d0('0x18')],'minutes')>moment()){throw new Error(_0xf6d0('0x12'));}return utils['clearBlockUser'](_0x428adf,0x0,![],null)[_0xf6d0('0x19')](function(){return utils['getLicense'](_0x428adf);})['then'](function(){return _0x2a6921(null,_0x428adf);});}else{return utils[_0xf6d0('0x23')](_0x428adf)[_0xf6d0('0x19')](function(){return _0x2a6921(null,_0x428adf);});}}else{return utils['getLicense'](_0x428adf)['then'](function(){return _0x2a6921(null,_0x428adf);});}})['catch'](function(_0x436e4b){return _0x2a6921(null,![],_0x436e4b);});}function handleError(_0x47a5ed,_0x96fd8d,_0x5a415c){return _0x47a5ed[_0xf6d0('0x24')](_0x96fd8d||0x1f4)['send']({'message':_0x5a415c?_0x5a415c[_0xf6d0('0x25')]||_0x5a415c:''});}exports['login']=function(_0x3b8fcc,_0x1b6980,_0x4bd8f6){passport[_0xf6d0('0xe')](_0xf6d0('0x26'),function(_0x26b80e,_0xf5edca,_0x28969b){var _0x3c5de6=_0x26b80e||_0x28969b;if(_0x3c5de6){return handleError(_0x1b6980,0x191,_0x3c5de6);}if(!_0xf5edca){return handleError(_0x1b6980,0x194,{'message':_0xf6d0('0x27')});}else{var _0x46d102={'payload':{'id':_0xf5edca['id'],'role':_0xf5edca['role']},'options':{'expiresIn':0x15180}};return authService['signToken'](_0x46d102)[_0xf6d0('0x19')](function(_0x484c1b){_0x1b6980[_0xf6d0('0x28')]({'id':_0xf5edca['id'],'token':_0x484c1b,'statusCall':'','isAlreadyLogged':_0xf5edca[_0xf6d0('0x29')]});});}})(_0x3b8fcc,_0x1b6980,_0x4bd8f6);};passport[_0xf6d0('0x2a')](_0xf6d0('0x26'),new LocalStrategy({'usernameField':'name','passwordField':_0xf6d0('0x1a')},function(_0x619c73,_0x15fe70,_0x273355){return ActiveDirectoryAuthenticate(db[_0xf6d0('0x2b')],_0x619c73,_0x15fe70,_0x273355);}));