Built motion from commit a61a4a20.|2.5.46
[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 _0x1e5c=['use','ad-login','password','User','passport-local','Strategy','moment','activedirectory2','util','../../mysqldb','../../components/auth/service','../../config/environment','../../config/logger','api','activedirectory','undefined','url','domain','error','inspect','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','Setting','findOne','then','adSsoEnabled','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20enabled','name','role','disabled','blocked','blockedAt','online','admin','user','agent','blockDuration','add','minutes','getLicense','catch','status','send','message','login','authenticate','Something\x20went\x20wrong,\x20please\x20try\x20again.','signToken'];(function(_0x3f082b,_0x30f37a){var _0x163d64=function(_0x41ba13){while(--_0x41ba13){_0x3f082b['push'](_0x3f082b['shift']());}};_0x163d64(++_0x30f37a);}(_0x1e5c,0x1da));var _0xc1e5=function(_0x9699f0,_0x354c17){_0x9699f0=_0x9699f0-0x0;var _0x17e53a=_0x1e5c[_0x9699f0];return _0x17e53a;};'use strict';var passport=require('passport');var LocalStrategy=require(_0xc1e5('0x0'))[_0xc1e5('0x1')];var moment=require(_0xc1e5('0x2'));var ActiveDirectory=require(_0xc1e5('0x3'));var BPromise=require('bluebird');var util=require(_0xc1e5('0x4'));var db=require(_0xc1e5('0x5'))['db'];var authService=require(_0xc1e5('0x6'));var utils=require('../../config/utils');var config=require(_0xc1e5('0x7'));var logger=require(_0xc1e5('0x8'))(_0xc1e5('0x9'));function isActiveDirectoryConfigured(_0x41c824){var _0x2e9dcd=![];if(typeof _0x41c824[_0xc1e5('0xa')]!==_0xc1e5('0xb')){_0x2e9dcd=_0x41c824[_0xc1e5('0xa')]['baseDN']&&_0x41c824['activedirectory'][_0xc1e5('0xc')]&&_0x41c824[_0xc1e5('0xa')][_0xc1e5('0xd')];}return _0x2e9dcd;}function adAuthenticate(_0x19c5e9,_0x4c0cb0,_0x2a56e4){return new BPromise(function(_0x482c28,_0x354c41){try{var _0x5593b9=new ActiveDirectory(_0x19c5e9);_0x5593b9['authenticate'](_0x4c0cb0,_0x2a56e4,function(_0x58c9b3,_0x56e3f9){if(_0x58c9b3){logger[_0xc1e5('0xe')]('Error\x20authenticate\x20%s:\x20%s',_0x4c0cb0,util[_0xc1e5('0xf')](_0x58c9b3,{'showHidden':![],'depth':null}));_0x354c41(_0xc1e5('0x10'));}else if(!_0x56e3f9){logger[_0xc1e5('0xe')]('Error\x20authenticate\x20%s:\x20Wrong\x20credentials',_0x4c0cb0);_0x354c41(_0xc1e5('0x10'));}else{_0x482c28(_0x56e3f9);}});}catch(_0x1e091a){_0x354c41(_0x1e091a);}});}function ActiveDirectoryAuthenticate(_0x43d9a1,_0x2d6042,_0x29f471,_0x335320){var _0xfb9dc5,_0x5a6a1f;if(!isActiveDirectoryConfigured(config)){logger[_0xc1e5('0xe')]('Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20configured',_0x2d6042);return _0x335320(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}return db[_0xc1e5('0x11')][_0xc1e5('0x12')]({'where':{'id':0x1},'attributes':['adSsoEnabled','blockDuration'],'raw':!![]})[_0xc1e5('0x13')](function(_0x6b420a){_0xfb9dc5=_0x6b420a;if(!_0xfb9dc5[_0xc1e5('0x14')]){logger['error'](_0xc1e5('0x15'),_0x2d6042);throw new Error(_0xc1e5('0x10'));}return _0x43d9a1[_0xc1e5('0x12')]({'logging':!![],'attributes':['id',_0xc1e5('0x16'),'password','salt',_0xc1e5('0x17'),_0xc1e5('0x18'),_0xc1e5('0x19'),_0xc1e5('0x1a'),_0xc1e5('0x1b')],'where':{'name':_0x2d6042,'role':{'$or':[_0xc1e5('0x1c'),_0xc1e5('0x1d'),_0xc1e5('0x1e')]},'adSsoEnabled':!![]}});})[_0xc1e5('0x13')](function(_0x2450e6){if(!_0x2450e6||_0x2450e6&&_0x2450e6['name']!==_0x2d6042){throw new Error('Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.');}_0x5a6a1f=_0x2450e6;var _0x5782c6=_0x5a6a1f[_0xc1e5('0x16')]+'@'+config[_0xc1e5('0xa')]['domain'];return adAuthenticate(config[_0xc1e5('0xa')],_0x5782c6,_0x29f471);})[_0xc1e5('0x13')](function(_0x3541ce){if(_0x5a6a1f[_0xc1e5('0x18')]){throw new Error(_0xc1e5('0x10'));}if(_0xfb9dc5[_0xc1e5('0x1f')]>0x0){if(_0x5a6a1f[_0xc1e5('0x19')]){if(moment(_0x5a6a1f['blockedAt'])[_0xc1e5('0x20')](_0xfb9dc5['blockDuration'],_0xc1e5('0x21'))>moment()){throw new Error(_0xc1e5('0x10'));}return utils['clearBlockUser'](_0x5a6a1f,0x0,![],null)[_0xc1e5('0x13')](function(){return utils[_0xc1e5('0x22')](_0x5a6a1f);})[_0xc1e5('0x13')](function(){return _0x335320(null,_0x5a6a1f);});}else{return utils[_0xc1e5('0x22')](_0x5a6a1f)['then'](function(){return _0x335320(null,_0x5a6a1f);});}}else{return utils['getLicense'](_0x5a6a1f)[_0xc1e5('0x13')](function(){return _0x335320(null,_0x5a6a1f);});}})[_0xc1e5('0x23')](function(_0x225f7e){return _0x335320(null,![],_0x225f7e);});}function handleError(_0x4263cf,_0x1f955c,_0x2da45f){return _0x4263cf[_0xc1e5('0x24')](_0x1f955c||0x1f4)[_0xc1e5('0x25')]({'message':_0x2da45f?_0x2da45f[_0xc1e5('0x26')]||_0x2da45f:''});}exports[_0xc1e5('0x27')]=function(_0x692244,_0x1152c4,_0x2cacce){passport[_0xc1e5('0x28')]('ad-login',function(_0x4cf4b0,_0x230800,_0x4bf266){var _0x380e75=_0x4cf4b0||_0x4bf266;if(_0x380e75){return handleError(_0x1152c4,0x191,_0x380e75);}if(!_0x230800){return handleError(_0x1152c4,0x194,{'message':_0xc1e5('0x29')});}else{var _0x327317={'payload':{'id':_0x230800['id'],'role':_0x230800[_0xc1e5('0x17')]},'options':{'expiresIn':0x15180}};return authService[_0xc1e5('0x2a')](_0x327317)['then'](function(_0x41e48b){_0x1152c4['json']({'id':_0x230800['id'],'token':_0x41e48b,'statusCall':'','isAlreadyLogged':_0x230800[_0xc1e5('0x1b')]});});}})(_0x692244,_0x1152c4,_0x2cacce);};passport[_0xc1e5('0x2b')](_0xc1e5('0x2c'),new LocalStrategy({'usernameField':'name','passwordField':_0xc1e5('0x2d')},function(_0x20f3d6,_0x1793d3,_0x5ce911){return ActiveDirectoryAuthenticate(db[_0xc1e5('0x2e')],_0x20f3d6,_0x1793d3,_0x5ce911);}));