Built motion from commit ae138b72.|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 _0x051e=['passport','Strategy','moment','activedirectory2','bluebird','util','../../components/auth/service','../../config/utils','../../config/environment','../../config/logger','api','baseDN','activedirectory','url','domain','authenticate','error','Error\x20authenticate\x20%s:\x20%s','Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.','Error\x20authenticate\x20%s:\x20Wrong\x20credentials','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20configured','Setting','findOne','adSsoEnabled','blockDuration','Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20enabled','name','password','salt','role','disabled','blocked','blockedAt','online','admin','user','agent','minutes','then','getLicense','status','send','login','ad-login','signToken','User'];(function(_0x58c3a7,_0x1e0d71){var _0xafb522=function(_0x3c0472){while(--_0x3c0472){_0x58c3a7['push'](_0x58c3a7['shift']());}};_0xafb522(++_0x1e0d71);}(_0x051e,0xb8));var _0xe051=function(_0x5998f8,_0x2f8e15){_0x5998f8=_0x5998f8-0x0;var _0x1adb98=_0x051e[_0x5998f8];return _0x1adb98;};'use strict';var passport=require(_0xe051('0x0'));var LocalStrategy=require('passport-local')[_0xe051('0x1')];var moment=require(_0xe051('0x2'));var ActiveDirectory=require(_0xe051('0x3'));var BPromise=require(_0xe051('0x4'));var util=require(_0xe051('0x5'));var db=require('../../mysqldb')['db'];var authService=require(_0xe051('0x6'));var utils=require(_0xe051('0x7'));var config=require(_0xe051('0x8'));var logger=require(_0xe051('0x9'))(_0xe051('0xa'));function isActiveDirectoryConfigured(_0x3389e0){var _0x5a0b5e=![];if(typeof _0x3389e0['activedirectory']!=='undefined'){_0x5a0b5e=_0x3389e0['activedirectory'][_0xe051('0xb')]&&_0x3389e0[_0xe051('0xc')][_0xe051('0xd')]&&_0x3389e0['activedirectory'][_0xe051('0xe')];}return _0x5a0b5e;}function adAuthenticate(_0x5ebed8,_0x17bc49,_0x3f9295){return new BPromise(function(_0x26acd3,_0x4a7d64){try{var _0x2c1837=new ActiveDirectory(_0x5ebed8);_0x2c1837[_0xe051('0xf')](_0x17bc49,_0x3f9295,function(_0xd0483f,_0x412d8e){if(_0xd0483f){logger[_0xe051('0x10')](_0xe051('0x11'),_0x17bc49,util['inspect'](_0xd0483f,{'showHidden':![],'depth':null}));_0x4a7d64(_0xe051('0x12'));}else if(!_0x412d8e){logger[_0xe051('0x10')](_0xe051('0x13'),_0x17bc49);_0x4a7d64('Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.');}else{_0x26acd3(_0x412d8e);}});}catch(_0x170653){_0x4a7d64(_0x170653);}});}function ActiveDirectoryAuthenticate(_0x51b71d,_0x79fd59,_0x1f7ec3,_0x122dd7){var _0x22c68b,_0xba50f4;if(!isActiveDirectoryConfigured(config)){logger['error'](_0xe051('0x14'),_0x79fd59);return _0x122dd7(null,![],{'message':'Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.'});}return db[_0xe051('0x15')][_0xe051('0x16')]({'where':{'id':0x1},'attributes':[_0xe051('0x17'),_0xe051('0x18')],'raw':!![]})['then'](function(_0x3a2486){_0x22c68b=_0x3a2486;if(!_0x22c68b[_0xe051('0x17')]){logger['error'](_0xe051('0x19'),_0x79fd59);throw new Error('Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.');}return _0x51b71d[_0xe051('0x16')]({'attributes':['id',_0xe051('0x1a'),_0xe051('0x1b'),_0xe051('0x1c'),_0xe051('0x1d'),_0xe051('0x1e'),_0xe051('0x1f'),_0xe051('0x20'),_0xe051('0x21')],'where':{'name':_0x79fd59,'role':{'$or':[_0xe051('0x22'),_0xe051('0x23'),_0xe051('0x24')]},'adSsoEnabled':!![]}});})['then'](function(_0x3244e5){if(!_0x3244e5||_0x3244e5&&_0x3244e5[_0xe051('0x1a')]!==_0x79fd59){throw new Error(_0xe051('0x12'));}_0xba50f4=_0x3244e5;var _0x27eab8=_0xba50f4[_0xe051('0x1a')]+'@'+config[_0xe051('0xc')][_0xe051('0xe')];return adAuthenticate(config[_0xe051('0xc')],_0x27eab8,_0x1f7ec3);})['then'](function(_0x108c4b){if(_0xba50f4[_0xe051('0x1e')]){throw new Error(_0xe051('0x12'));}if(_0x22c68b['blockDuration']>0x0){if(_0xba50f4['blocked']){if(moment(_0xba50f4['blockedAt'])['add'](_0x22c68b[_0xe051('0x18')],_0xe051('0x25'))>moment()){throw new Error('Incorrect\x20username\x20and/or\x20password.\x20Please\x20retry\x20again\x20or\x20contact\x20the\x20administrator.');}return utils['clearBlockUser'](_0xba50f4,0x0,![],null)[_0xe051('0x26')](function(){return utils['getLicense'](_0xba50f4);})['then'](function(){return _0x122dd7(null,_0xba50f4);});}else{return utils[_0xe051('0x27')](_0xba50f4)[_0xe051('0x26')](function(){return _0x122dd7(null,_0xba50f4);});}}else{return utils['getLicense'](_0xba50f4)[_0xe051('0x26')](function(){return _0x122dd7(null,_0xba50f4);});}})['catch'](function(_0x1b9fc8){return _0x122dd7(null,![],_0x1b9fc8);});}function handleError(_0x5cf726,_0x54ddc9,_0x2337cc){return _0x5cf726[_0xe051('0x28')](_0x54ddc9||0x1f4)[_0xe051('0x29')]({'message':_0x2337cc?_0x2337cc['message']||_0x2337cc:''});}exports[_0xe051('0x2a')]=function(_0x185eb0,_0x52fb40,_0x42f1a0){passport[_0xe051('0xf')](_0xe051('0x2b'),function(_0x1bf2cf,_0x5dfbeb,_0x7ae8f2){var _0x133c38=_0x1bf2cf||_0x7ae8f2;if(_0x133c38){return handleError(_0x52fb40,0x191,_0x133c38);}if(!_0x5dfbeb){return handleError(_0x52fb40,0x194,{'message':'Something\x20went\x20wrong,\x20please\x20try\x20again.'});}else{var _0x492cc3={'payload':{'id':_0x5dfbeb['id'],'role':_0x5dfbeb[_0xe051('0x1d')]},'options':{'expiresIn':0x15180}};return authService[_0xe051('0x2c')](_0x492cc3)['then'](function(_0x5aa550){_0x52fb40['json']({'id':_0x5dfbeb['id'],'token':_0x5aa550,'statusCall':'','isAlreadyLogged':_0x5dfbeb[_0xe051('0x21')]});});}})(_0x185eb0,_0x52fb40,_0x42f1a0);};passport['use'](_0xe051('0x2b'),new LocalStrategy({'usernameField':_0xe051('0x1a'),'passwordField':'password'},function(_0x14e52a,_0x301bbd,_0x255726){return ActiveDirectoryAuthenticate(db[_0xe051('0x2d')],_0x14e52a,_0x301bbd,_0x255726);}));