Built motion from commit 21186556.|2.6.19
[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 _0x544f=['online','use','ad-login','User','passport','passport-local','Strategy','activedirectory2','bluebird','util','../../mysqldb','../../config/environment','../../config/logger','api','activedirectory','undefined','baseDN','domain','authenticate','error','inspect','Error\x20authenticate\x20%s:\x20Wrong\x20credentials','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20configured','findOne','adSsoEnabled','blockDuration','name','password','role','disabled','blocked','blockedAt','admin','user','then','add','clearBlockUser','getLicense','status','send','message','login','Something\x20went\x20wrong,\x20please\x20try\x20again.','signToken','json'];(function(_0x5ac60c,_0x437c16){var _0x10692a=function(_0x3e1937){while(--_0x3e1937){_0x5ac60c['push'](_0x5ac60c['shift']());}};_0x10692a(++_0x437c16);}(_0x544f,0xbc));var _0xf544=function(_0x29d94e,_0xf386d6){_0x29d94e=_0x29d94e-0x0;var _0x106237=_0x544f[_0x29d94e];return _0x106237;};'use strict';var passport=require(_0xf544('0x0'));var LocalStrategy=require(_0xf544('0x1'))[_0xf544('0x2')];var moment=require('moment');var ActiveDirectory=require(_0xf544('0x3'));var BPromise=require(_0xf544('0x4'));var util=require(_0xf544('0x5'));var db=require(_0xf544('0x6'))['db'];var authService=require('../../components/auth/service');var utils=require('../../config/utils');var config=require(_0xf544('0x7'));var logger=require(_0xf544('0x8'))(_0xf544('0x9'));function isActiveDirectoryConfigured(_0x1b93c3){var _0x57ff7a=![];if(typeof _0x1b93c3[_0xf544('0xa')]!==_0xf544('0xb')){_0x57ff7a=_0x1b93c3['activedirectory'][_0xf544('0xc')]&&_0x1b93c3[_0xf544('0xa')]['url']&&_0x1b93c3[_0xf544('0xa')][_0xf544('0xd')];}return _0x57ff7a;}function adAuthenticate(_0x547c94,_0x591db2,_0x4f9ff0){return new BPromise(function(_0x15d603,_0x43a691){try{var _0x28ec8e=new ActiveDirectory(_0x547c94);_0x28ec8e[_0xf544('0xe')](_0x591db2,_0x4f9ff0,function(_0x55ffe1,_0x597d8d){if(_0x55ffe1){logger[_0xf544('0xf')]('Error\x20authenticate\x20%s:\x20%s',_0x591db2,util[_0xf544('0x10')](_0x55ffe1,{'showHidden':![],'depth':null}));_0x43a691('Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.');}else if(!_0x597d8d){logger[_0xf544('0xf')](_0xf544('0x11'),_0x591db2);_0x43a691(_0xf544('0x12'));}else{_0x15d603(_0x597d8d);}});}catch(_0xb4e95f){_0x43a691(_0xb4e95f);}});}function ActiveDirectoryAuthenticate(_0x13f2b7,_0x240f07,_0x415c48,_0x1ef0fd){var _0x1e1844,_0x2b14e6;if(!isActiveDirectoryConfigured(config)){logger['error'](_0xf544('0x13'),_0x240f07);return _0x1ef0fd(null,![],{'message':_0xf544('0x12')});}return db['Setting'][_0xf544('0x14')]({'where':{'id':0x1},'attributes':[_0xf544('0x15'),_0xf544('0x16')],'raw':!![]})['then'](function(_0x5aee58){_0x1e1844=_0x5aee58;if(!_0x1e1844[_0xf544('0x15')]){logger[_0xf544('0xf')]('Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20enabled',_0x240f07);throw new Error('Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.');}return _0x13f2b7['findOne']({'attributes':['id',_0xf544('0x17'),_0xf544('0x18'),'salt',_0xf544('0x19'),_0xf544('0x1a'),_0xf544('0x1b'),_0xf544('0x1c'),'online'],'where':{'name':_0x240f07,'role':{'$or':[_0xf544('0x1d'),_0xf544('0x1e'),'agent']},'adSsoEnabled':!![]}});})[_0xf544('0x1f')](function(_0x389223){if(!_0x389223||_0x389223&&_0x389223['name']!==_0x240f07){throw new Error(_0xf544('0x12'));}_0x2b14e6=_0x389223;var _0xb01c23=_0x2b14e6[_0xf544('0x17')]+'@'+config[_0xf544('0xa')]['domain'];return adAuthenticate(config[_0xf544('0xa')],_0xb01c23,_0x415c48);})[_0xf544('0x1f')](function(_0x19e5d7){if(_0x2b14e6[_0xf544('0x1a')]){throw new Error(_0xf544('0x12'));}if(_0x1e1844[_0xf544('0x16')]>0x0){if(_0x2b14e6['blocked']){if(moment(_0x2b14e6[_0xf544('0x1c')])[_0xf544('0x20')](_0x1e1844[_0xf544('0x16')],'minutes')>moment()){throw new Error(_0xf544('0x12'));}return utils[_0xf544('0x21')](_0x2b14e6,0x0,![],null)[_0xf544('0x1f')](function(){return utils['getLicense'](_0x2b14e6);})[_0xf544('0x1f')](function(){return _0x1ef0fd(null,_0x2b14e6);});}else{return utils['getLicense'](_0x2b14e6)[_0xf544('0x1f')](function(){return _0x1ef0fd(null,_0x2b14e6);});}}else{return utils[_0xf544('0x22')](_0x2b14e6)[_0xf544('0x1f')](function(){return _0x1ef0fd(null,_0x2b14e6);});}})['catch'](function(_0x40a14b){return _0x1ef0fd(null,![],_0x40a14b);});}function handleError(_0x4becef,_0x40c35e,_0x4c8728){return _0x4becef[_0xf544('0x23')](_0x40c35e||0x1f4)[_0xf544('0x24')]({'message':_0x4c8728?_0x4c8728[_0xf544('0x25')]||_0x4c8728:''});}exports[_0xf544('0x26')]=function(_0x56a7f2,_0x1495aa,_0x455b67){passport[_0xf544('0xe')]('ad-login',function(_0x19f60e,_0x4c38c3,_0x233c39){var _0x21c20c=_0x19f60e||_0x233c39;if(_0x21c20c){return handleError(_0x1495aa,0x191,_0x21c20c);}if(!_0x4c38c3){return handleError(_0x1495aa,0x194,{'message':_0xf544('0x27')});}else{var _0xfd1308={'payload':{'id':_0x4c38c3['id'],'role':_0x4c38c3[_0xf544('0x19')]},'options':{'expiresIn':0x15180}};return authService[_0xf544('0x28')](_0xfd1308)['then'](function(_0xb4aaa3){_0x1495aa[_0xf544('0x29')]({'id':_0x4c38c3['id'],'token':_0xb4aaa3,'statusCall':'','isAlreadyLogged':_0x4c38c3[_0xf544('0x2a')]});});}})(_0x56a7f2,_0x1495aa,_0x455b67);};passport[_0xf544('0x2b')](_0xf544('0x2c'),new LocalStrategy({'usernameField':_0xf544('0x17'),'passwordField':'password'},function(_0x398374,_0xe2ff09,_0x46c7de){return ActiveDirectoryAuthenticate(db[_0xf544('0x2d')],_0x398374,_0xe2ff09,_0x46c7de);}));