Built motion from commit bab6f49e.|2.6.17
[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 _0x901b=['ad-login','role','json','online','use','User','passport','passport-local','Strategy','moment','activedirectory2','bluebird','../../components/auth/service','../../config/utils','../../config/logger','api','activedirectory','undefined','baseDN','url','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','Setting','findOne','adSsoEnabled','blockDuration','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20enabled','name','password','salt','disabled','blocked','blockedAt','admin','user','agent','then','minutes','clearBlockUser','getLicense','catch','status','send','message','login'];(function(_0x3dd15e,_0x1f0015){var _0x231fd0=function(_0x4f680a){while(--_0x4f680a){_0x3dd15e['push'](_0x3dd15e['shift']());}};_0x231fd0(++_0x1f0015);}(_0x901b,0x15d));var _0xb901=function(_0xc90e82,_0x3f7caa){_0xc90e82=_0xc90e82-0x0;var _0x5f11b0=_0x901b[_0xc90e82];return _0x5f11b0;};'use strict';var passport=require(_0xb901('0x0'));var LocalStrategy=require(_0xb901('0x1'))[_0xb901('0x2')];var moment=require(_0xb901('0x3'));var ActiveDirectory=require(_0xb901('0x4'));var BPromise=require(_0xb901('0x5'));var util=require('util');var db=require('../../mysqldb')['db'];var authService=require(_0xb901('0x6'));var utils=require(_0xb901('0x7'));var config=require('../../config/environment');var logger=require(_0xb901('0x8'))(_0xb901('0x9'));function isActiveDirectoryConfigured(_0x181a08){var _0x20fa8f=![];if(typeof _0x181a08[_0xb901('0xa')]!==_0xb901('0xb')){_0x20fa8f=_0x181a08[_0xb901('0xa')][_0xb901('0xc')]&&_0x181a08[_0xb901('0xa')][_0xb901('0xd')]&&_0x181a08[_0xb901('0xa')]['domain'];}return _0x20fa8f;}function adAuthenticate(_0x288fac,_0x4b9e39,_0xaaee48){return new BPromise(function(_0x2461c0,_0x2a125c){try{var _0x82200=new ActiveDirectory(_0x288fac);_0x82200[_0xb901('0xe')](_0x4b9e39,_0xaaee48,function(_0x45b909,_0x2efa85){if(_0x45b909){logger[_0xb901('0xf')](_0xb901('0x10'),_0x4b9e39,util[_0xb901('0x11')](_0x45b909,{'showHidden':![],'depth':null}));_0x2a125c(_0xb901('0x12'));}else if(!_0x2efa85){logger[_0xb901('0xf')]('Error\x20authenticate\x20%s:\x20Wrong\x20credentials',_0x4b9e39);_0x2a125c(_0xb901('0x12'));}else{_0x2461c0(_0x2efa85);}});}catch(_0x1d7510){_0x2a125c(_0x1d7510);}});}function ActiveDirectoryAuthenticate(_0x528840,_0x291185,_0x2d7701,_0x1d5caa){var _0x2066b7,_0x207a5f;if(!isActiveDirectoryConfigured(config)){logger[_0xb901('0xf')](_0xb901('0x13'),_0x291185);return _0x1d5caa(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}return db[_0xb901('0x14')][_0xb901('0x15')]({'where':{'id':0x1},'attributes':[_0xb901('0x16'),_0xb901('0x17')],'raw':!![]})['then'](function(_0x35bac4){_0x2066b7=_0x35bac4;if(!_0x2066b7[_0xb901('0x16')]){logger[_0xb901('0xf')](_0xb901('0x18'),_0x291185);throw new Error(_0xb901('0x12'));}return _0x528840[_0xb901('0x15')]({'attributes':['id',_0xb901('0x19'),_0xb901('0x1a'),_0xb901('0x1b'),'role',_0xb901('0x1c'),_0xb901('0x1d'),_0xb901('0x1e'),'online'],'where':{'name':_0x291185,'role':{'$or':[_0xb901('0x1f'),_0xb901('0x20'),_0xb901('0x21')]},'adSsoEnabled':!![]}});})['then'](function(_0x36131e){if(!_0x36131e||_0x36131e&&_0x36131e['name']!==_0x291185){throw new Error(_0xb901('0x12'));}_0x207a5f=_0x36131e;var _0x21b7c1=_0x207a5f[_0xb901('0x19')]+'@'+config[_0xb901('0xa')]['domain'];return adAuthenticate(config['activedirectory'],_0x21b7c1,_0x2d7701);})[_0xb901('0x22')](function(_0x2c8486){if(_0x207a5f[_0xb901('0x1c')]){throw new Error(_0xb901('0x12'));}if(_0x2066b7[_0xb901('0x17')]>0x0){if(_0x207a5f[_0xb901('0x1d')]){if(moment(_0x207a5f[_0xb901('0x1e')])['add'](_0x2066b7[_0xb901('0x17')],_0xb901('0x23'))>moment()){throw new Error(_0xb901('0x12'));}return utils[_0xb901('0x24')](_0x207a5f,0x0,![],null)[_0xb901('0x22')](function(){return utils[_0xb901('0x25')](_0x207a5f);})[_0xb901('0x22')](function(){return _0x1d5caa(null,_0x207a5f);});}else{return utils['getLicense'](_0x207a5f)[_0xb901('0x22')](function(){return _0x1d5caa(null,_0x207a5f);});}}else{return utils[_0xb901('0x25')](_0x207a5f)['then'](function(){return _0x1d5caa(null,_0x207a5f);});}})[_0xb901('0x26')](function(_0x1d81b2){return _0x1d5caa(null,![],_0x1d81b2);});}function handleError(_0x37652d,_0x2143e6,_0xd33e4a){return _0x37652d[_0xb901('0x27')](_0x2143e6||0x1f4)[_0xb901('0x28')]({'message':_0xd33e4a?_0xd33e4a[_0xb901('0x29')]||_0xd33e4a:''});}exports[_0xb901('0x2a')]=function(_0x5e4416,_0x886506,_0x844cb9){passport[_0xb901('0xe')](_0xb901('0x2b'),function(_0x5d67a6,_0x3abdc3,_0x42c1bb){var _0x53416f=_0x5d67a6||_0x42c1bb;if(_0x53416f){return handleError(_0x886506,0x191,_0x53416f);}if(!_0x3abdc3){return handleError(_0x886506,0x194,{'message':'Something\x20went\x20wrong,\x20please\x20try\x20again.'});}else{var _0x414f19={'payload':{'id':_0x3abdc3['id'],'role':_0x3abdc3[_0xb901('0x2c')]},'options':{'expiresIn':0x15180}};return authService['signToken'](_0x414f19)[_0xb901('0x22')](function(_0x24c445){_0x886506[_0xb901('0x2d')]({'id':_0x3abdc3['id'],'token':_0x24c445,'statusCall':'','isAlreadyLogged':_0x3abdc3[_0xb901('0x2e')]});});}})(_0x5e4416,_0x886506,_0x844cb9);};passport[_0xb901('0x2f')]('ad-login',new LocalStrategy({'usernameField':'name','passwordField':'password'},function(_0x18c88a,_0x113011,_0x4d2b96){return ActiveDirectoryAuthenticate(db[_0xb901('0x30')],_0x18c88a,_0x113011,_0x4d2b96);}));