Built motion from commit c1aa34a0.|2.6.28
[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 _0x989a=['moment','activedirectory2','bluebird','util','../../mysqldb','../../components/auth/service','../../config/utils','../../config/environment','../../config/logger','activedirectory','undefined','url','domain','authenticate','error','inspect','Error\x20authenticate\x20%s:\x20Wrong\x20credentials','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20configured','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','Setting','findOne','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20enabled','password','salt','disabled','blocked','blockedAt','online','admin','user','then','name','blockDuration','add','clearBlockUser','getLicense','status','send','message','login','Something\x20went\x20wrong,\x20please\x20try\x20again.','role','signToken','use','ad-login','User','passport','passport-local'];(function(_0x5d6456,_0x58ecc7){var _0x4d66c1=function(_0x4335e3){while(--_0x4335e3){_0x5d6456['push'](_0x5d6456['shift']());}};_0x4d66c1(++_0x58ecc7);}(_0x989a,0x1ae));var _0xa989=function(_0x35031c,_0x400fac){_0x35031c=_0x35031c-0x0;var _0x2a7adf=_0x989a[_0x35031c];return _0x2a7adf;};'use strict';var passport=require(_0xa989('0x0'));var LocalStrategy=require(_0xa989('0x1'))['Strategy'];var moment=require(_0xa989('0x2'));var ActiveDirectory=require(_0xa989('0x3'));var BPromise=require(_0xa989('0x4'));var util=require(_0xa989('0x5'));var db=require(_0xa989('0x6'))['db'];var authService=require(_0xa989('0x7'));var utils=require(_0xa989('0x8'));var config=require(_0xa989('0x9'));var logger=require(_0xa989('0xa'))('api');function isActiveDirectoryConfigured(_0x5735be){var _0x3f39d6=![];if(typeof _0x5735be[_0xa989('0xb')]!==_0xa989('0xc')){_0x3f39d6=_0x5735be[_0xa989('0xb')]['baseDN']&&_0x5735be[_0xa989('0xb')][_0xa989('0xd')]&&_0x5735be['activedirectory'][_0xa989('0xe')];}return _0x3f39d6;}function adAuthenticate(_0xd6ebe,_0x3fa427,_0x333c82){return new BPromise(function(_0x22990b,_0x20c364){try{var _0x3c8569=new ActiveDirectory(_0xd6ebe);_0x3c8569[_0xa989('0xf')](_0x3fa427,_0x333c82,function(_0x1a409c,_0x7d75de){if(_0x1a409c){logger[_0xa989('0x10')]('Error\x20authenticate\x20%s:\x20%s',_0x3fa427,util[_0xa989('0x11')](_0x1a409c,{'showHidden':![],'depth':null}));_0x20c364('Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.');}else if(!_0x7d75de){logger['error'](_0xa989('0x12'),_0x3fa427);_0x20c364('Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.');}else{_0x22990b(_0x7d75de);}});}catch(_0x592cc5){_0x20c364(_0x592cc5);}});}function ActiveDirectoryAuthenticate(_0x44b408,_0x179d53,_0x44844a,_0x50be28){var _0xd3ae18,_0x13529f;if(!isActiveDirectoryConfigured(config)){logger[_0xa989('0x10')](_0xa989('0x13'),_0x179d53);return _0x50be28(null,![],{'message':_0xa989('0x14')});}return db[_0xa989('0x15')][_0xa989('0x16')]({'where':{'id':0x1},'attributes':['adSsoEnabled','blockDuration'],'raw':!![]})['then'](function(_0x304a9c){_0xd3ae18=_0x304a9c;if(!_0xd3ae18['adSsoEnabled']){logger['error'](_0xa989('0x17'),_0x179d53);throw new Error(_0xa989('0x14'));}return _0x44b408[_0xa989('0x16')]({'attributes':['id','name',_0xa989('0x18'),_0xa989('0x19'),'role',_0xa989('0x1a'),_0xa989('0x1b'),_0xa989('0x1c'),_0xa989('0x1d')],'where':{'name':_0x179d53,'role':{'$or':[_0xa989('0x1e'),_0xa989('0x1f'),'agent']},'adSsoEnabled':!![]}});})[_0xa989('0x20')](function(_0x25a025){if(!_0x25a025||_0x25a025&&_0x25a025[_0xa989('0x21')]!==_0x179d53){throw new Error(_0xa989('0x14'));}_0x13529f=_0x25a025;var _0x2aedfc=_0x13529f[_0xa989('0x21')]+'@'+config[_0xa989('0xb')][_0xa989('0xe')];return adAuthenticate(config['activedirectory'],_0x2aedfc,_0x44844a);})['then'](function(_0x4bfaa1){if(_0x13529f[_0xa989('0x1a')]){throw new Error(_0xa989('0x14'));}if(_0xd3ae18[_0xa989('0x22')]>0x0){if(_0x13529f['blocked']){if(moment(_0x13529f[_0xa989('0x1c')])[_0xa989('0x23')](_0xd3ae18['blockDuration'],'minutes')>moment()){throw new Error(_0xa989('0x14'));}return utils[_0xa989('0x24')](_0x13529f,0x0,![],null)['then'](function(){return utils[_0xa989('0x25')](_0x13529f);})['then'](function(){return _0x50be28(null,_0x13529f);});}else{return utils[_0xa989('0x25')](_0x13529f)[_0xa989('0x20')](function(){return _0x50be28(null,_0x13529f);});}}else{return utils[_0xa989('0x25')](_0x13529f)[_0xa989('0x20')](function(){return _0x50be28(null,_0x13529f);});}})['catch'](function(_0x483a5b){return _0x50be28(null,![],_0x483a5b);});}function handleError(_0x21897d,_0x34a73c,_0x33fcf4){return _0x21897d[_0xa989('0x26')](_0x34a73c||0x1f4)[_0xa989('0x27')]({'message':_0x33fcf4?_0x33fcf4[_0xa989('0x28')]||_0x33fcf4:''});}exports[_0xa989('0x29')]=function(_0x1af08c,_0x2118ed,_0x506f77){passport[_0xa989('0xf')]('ad-login',function(_0x33581a,_0x2d86ac,_0x16083c){var _0x11442e=_0x33581a||_0x16083c;if(_0x11442e){return handleError(_0x2118ed,0x191,_0x11442e);}if(!_0x2d86ac){return handleError(_0x2118ed,0x194,{'message':_0xa989('0x2a')});}else{var _0x1c1b98={'payload':{'id':_0x2d86ac['id'],'role':_0x2d86ac[_0xa989('0x2b')]},'options':{'expiresIn':0x15180}};return authService[_0xa989('0x2c')](_0x1c1b98)['then'](function(_0x3b0df3){_0x2118ed['json']({'id':_0x2d86ac['id'],'token':_0x3b0df3,'statusCall':'','isAlreadyLogged':_0x2d86ac[_0xa989('0x1d')]});});}})(_0x1af08c,_0x2118ed,_0x506f77);};passport[_0xa989('0x2d')](_0xa989('0x2e'),new LocalStrategy({'usernameField':_0xa989('0x21'),'passwordField':'password'},function(_0x17bd3a,_0x2c8435,_0x1cf0b9){return ActiveDirectoryAuthenticate(db[_0xa989('0x2f')],_0x17bd3a,_0x2c8435,_0x1cf0b9);}));