Built motion from commit 0d14f64c.|2.6.34
[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 _0xf324=['blocked','online','user','agent','blockDuration','blockedAt','add','minutes','clearBlockUser','getLicense','catch','status','send','message','login','ad-login','Something\x20went\x20wrong,\x20please\x20try\x20again.','json','User','passport','passport-local','Strategy','moment','bluebird','util','../../mysqldb','../../components/auth/service','../../config/utils','../../config/environment','../../config/logger','activedirectory','undefined','url','domain','Error\x20authenticate\x20%s:\x20%s','inspect','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','error','Error\x20authenticate\x20%s:\x20Wrong\x20credentials','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20configured','Setting','findOne','adSsoEnabled','then','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20enabled','name','salt','role','disabled'];(function(_0x206971,_0x49148c){var _0x39fce2=function(_0x4776a9){while(--_0x4776a9){_0x206971['push'](_0x206971['shift']());}};_0x39fce2(++_0x49148c);}(_0xf324,0xa6));var _0x4f32=function(_0x769fd9,_0x1d699e){_0x769fd9=_0x769fd9-0x0;var _0x1202b6=_0xf324[_0x769fd9];return _0x1202b6;};'use strict';var passport=require(_0x4f32('0x0'));var LocalStrategy=require(_0x4f32('0x1'))[_0x4f32('0x2')];var moment=require(_0x4f32('0x3'));var ActiveDirectory=require('activedirectory2');var BPromise=require(_0x4f32('0x4'));var util=require(_0x4f32('0x5'));var db=require(_0x4f32('0x6'))['db'];var authService=require(_0x4f32('0x7'));var utils=require(_0x4f32('0x8'));var config=require(_0x4f32('0x9'));var logger=require(_0x4f32('0xa'))('api');function isActiveDirectoryConfigured(_0x5051e2){var _0x238d8a=![];if(typeof _0x5051e2[_0x4f32('0xb')]!==_0x4f32('0xc')){_0x238d8a=_0x5051e2[_0x4f32('0xb')]['baseDN']&&_0x5051e2[_0x4f32('0xb')][_0x4f32('0xd')]&&_0x5051e2[_0x4f32('0xb')][_0x4f32('0xe')];}return _0x238d8a;}function adAuthenticate(_0x1c1faf,_0x27c803,_0x13dc39){return new BPromise(function(_0x24d01c,_0x2a3e88){try{var _0x491cf0=new ActiveDirectory(_0x1c1faf);_0x491cf0['authenticate'](_0x27c803,_0x13dc39,function(_0x44f4d3,_0xaf3917){if(_0x44f4d3){logger['error'](_0x4f32('0xf'),_0x27c803,util[_0x4f32('0x10')](_0x44f4d3,{'showHidden':![],'depth':null}));_0x2a3e88(_0x4f32('0x11'));}else if(!_0xaf3917){logger[_0x4f32('0x12')](_0x4f32('0x13'),_0x27c803);_0x2a3e88(_0x4f32('0x11'));}else{_0x24d01c(_0xaf3917);}});}catch(_0x3c6daf){_0x2a3e88(_0x3c6daf);}});}function ActiveDirectoryAuthenticate(_0x25aad3,_0xda352a,_0x48a827,_0x194ad4){var _0x1bc483,_0x192461;if(!isActiveDirectoryConfigured(config)){logger['error'](_0x4f32('0x14'),_0xda352a);return _0x194ad4(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}return db[_0x4f32('0x15')][_0x4f32('0x16')]({'where':{'id':0x1},'attributes':[_0x4f32('0x17'),'blockDuration'],'raw':!![]})[_0x4f32('0x18')](function(_0x5ceba9){_0x1bc483=_0x5ceba9;if(!_0x1bc483[_0x4f32('0x17')]){logger[_0x4f32('0x12')](_0x4f32('0x19'),_0xda352a);throw new Error('Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.');}return _0x25aad3[_0x4f32('0x16')]({'attributes':['id',_0x4f32('0x1a'),'password',_0x4f32('0x1b'),_0x4f32('0x1c'),_0x4f32('0x1d'),_0x4f32('0x1e'),'blockedAt',_0x4f32('0x1f')],'where':{'name':_0xda352a,'role':{'$or':['admin',_0x4f32('0x20'),_0x4f32('0x21')]},'adSsoEnabled':!![]}});})[_0x4f32('0x18')](function(_0x4ce898){if(!_0x4ce898||_0x4ce898&&_0x4ce898[_0x4f32('0x1a')]!==_0xda352a){throw new Error(_0x4f32('0x11'));}_0x192461=_0x4ce898;var _0x62be60=_0x192461['name']+'@'+config[_0x4f32('0xb')][_0x4f32('0xe')];return adAuthenticate(config[_0x4f32('0xb')],_0x62be60,_0x48a827);})['then'](function(_0x21b023){if(_0x192461[_0x4f32('0x1d')]){throw new Error(_0x4f32('0x11'));}if(_0x1bc483[_0x4f32('0x22')]>0x0){if(_0x192461[_0x4f32('0x1e')]){if(moment(_0x192461[_0x4f32('0x23')])[_0x4f32('0x24')](_0x1bc483[_0x4f32('0x22')],_0x4f32('0x25'))>moment()){throw new Error(_0x4f32('0x11'));}return utils[_0x4f32('0x26')](_0x192461,0x0,![],null)[_0x4f32('0x18')](function(){return utils['getLicense'](_0x192461);})['then'](function(){return _0x194ad4(null,_0x192461);});}else{return utils[_0x4f32('0x27')](_0x192461)[_0x4f32('0x18')](function(){return _0x194ad4(null,_0x192461);});}}else{return utils[_0x4f32('0x27')](_0x192461)[_0x4f32('0x18')](function(){return _0x194ad4(null,_0x192461);});}})[_0x4f32('0x28')](function(_0x302017){return _0x194ad4(null,![],_0x302017);});}function handleError(_0x31fcb5,_0x2fc04a,_0x357c25){return _0x31fcb5[_0x4f32('0x29')](_0x2fc04a||0x1f4)[_0x4f32('0x2a')]({'message':_0x357c25?_0x357c25[_0x4f32('0x2b')]||_0x357c25:''});}exports[_0x4f32('0x2c')]=function(_0x490698,_0x2c9666,_0x4f8242){passport['authenticate'](_0x4f32('0x2d'),function(_0x82c280,_0x16e0f1,_0x420c80){var _0x5c1669=_0x82c280||_0x420c80;if(_0x5c1669){return handleError(_0x2c9666,0x191,_0x5c1669);}if(!_0x16e0f1){return handleError(_0x2c9666,0x194,{'message':_0x4f32('0x2e')});}else{var _0x37af02={'payload':{'id':_0x16e0f1['id'],'role':_0x16e0f1[_0x4f32('0x1c')]},'options':{'expiresIn':0x15180}};return authService['signToken'](_0x37af02)['then'](function(_0x30da38){_0x2c9666[_0x4f32('0x2f')]({'id':_0x16e0f1['id'],'token':_0x30da38,'statusCall':'','isAlreadyLogged':_0x16e0f1['online']});});}})(_0x490698,_0x2c9666,_0x4f8242);};passport['use'](_0x4f32('0x2d'),new LocalStrategy({'usernameField':_0x4f32('0x1a'),'passwordField':'password'},function(_0x2ca8e2,_0x17b0ec,_0x3d657e){return ActiveDirectoryAuthenticate(db[_0x4f32('0x30')],_0x2ca8e2,_0x17b0ec,_0x3d657e);}));