b3ebfed327953514d5be119e51096ddb797e6d57
[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 _0x13f5=['authenticate','ad-login','signToken','json','online','use','User','passport','passport-local','Strategy','moment','bluebird','../../components/auth/service','../../config/utils','../../config/environment','../../config/logger','api','activedirectory','undefined','baseDN','domain','error','Error\x20authenticate\x20%s:\x20%s','inspect','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','then','password','salt','role','disabled','blocked','blockedAt','admin','user','name','blockDuration','add','clearBlockUser','getLicense','status','message','login'];(function(_0x566da9,_0x487a9f){var _0x5e8ac7=function(_0x5db53a){while(--_0x5db53a){_0x566da9['push'](_0x566da9['shift']());}};_0x5e8ac7(++_0x487a9f);}(_0x13f5,0xc3));var _0x513f=function(_0x20a584,_0xe384a5){_0x20a584=_0x20a584-0x0;var _0x4b41a8=_0x13f5[_0x20a584];return _0x4b41a8;};'use strict';var passport=require(_0x513f('0x0'));var LocalStrategy=require(_0x513f('0x1'))[_0x513f('0x2')];var moment=require(_0x513f('0x3'));var ActiveDirectory=require('activedirectory2');var BPromise=require(_0x513f('0x4'));var util=require('util');var db=require('../../mysqldb')['db'];var authService=require(_0x513f('0x5'));var utils=require(_0x513f('0x6'));var config=require(_0x513f('0x7'));var logger=require(_0x513f('0x8'))(_0x513f('0x9'));function isActiveDirectoryConfigured(_0x579681){var _0x2be368=![];if(typeof _0x579681[_0x513f('0xa')]!==_0x513f('0xb')){_0x2be368=_0x579681[_0x513f('0xa')][_0x513f('0xc')]&&_0x579681[_0x513f('0xa')]['url']&&_0x579681[_0x513f('0xa')][_0x513f('0xd')];}return _0x2be368;}function adAuthenticate(_0x443bde,_0x4efd8b,_0x88288b){return new BPromise(function(_0x4aa878,_0x3a0c5e){try{var _0x821fe3=new ActiveDirectory(_0x443bde);_0x821fe3['authenticate'](_0x4efd8b,_0x88288b,function(_0x166a1a,_0x243f48){if(_0x166a1a){logger[_0x513f('0xe')](_0x513f('0xf'),_0x4efd8b,util[_0x513f('0x10')](_0x166a1a,{'showHidden':![],'depth':null}));_0x3a0c5e(_0x513f('0x11'));}else if(!_0x243f48){logger[_0x513f('0xe')](_0x513f('0x12'),_0x4efd8b);_0x3a0c5e(_0x513f('0x11'));}else{_0x4aa878(_0x243f48);}});}catch(_0x2356ab){_0x3a0c5e(_0x2356ab);}});}function ActiveDirectoryAuthenticate(_0x3958ab,_0x359478,_0x3874d1,_0x979578){var _0x57fd65,_0x369b25;if(!isActiveDirectoryConfigured(config)){logger['error'](_0x513f('0x13'),_0x359478);return _0x979578(null,![],{'message':_0x513f('0x11')});}return db[_0x513f('0x14')][_0x513f('0x15')]({'where':{'id':0x1},'attributes':[_0x513f('0x16'),'blockDuration'],'raw':!![]})[_0x513f('0x17')](function(_0x73ac7d){_0x57fd65=_0x73ac7d;if(!_0x57fd65['adSsoEnabled']){logger[_0x513f('0xe')]('Error\x20authenticate\x20%s:\x20Active\x20Directory\x20not\x20enabled',_0x359478);throw new Error(_0x513f('0x11'));}return _0x3958ab[_0x513f('0x15')]({'attributes':['id','name',_0x513f('0x18'),_0x513f('0x19'),_0x513f('0x1a'),_0x513f('0x1b'),_0x513f('0x1c'),_0x513f('0x1d'),'online'],'where':{'name':_0x359478,'role':{'$or':[_0x513f('0x1e'),_0x513f('0x1f'),'agent']},'adSsoEnabled':!![]}});})[_0x513f('0x17')](function(_0x3314cf){if(!_0x3314cf||_0x3314cf&&_0x3314cf[_0x513f('0x20')]!==_0x359478){throw new Error(_0x513f('0x11'));}_0x369b25=_0x3314cf;var _0x3d85f7=_0x369b25[_0x513f('0x20')]+'@'+config['activedirectory']['domain'];return adAuthenticate(config['activedirectory'],_0x3d85f7,_0x3874d1);})[_0x513f('0x17')](function(_0x4309cb){if(_0x369b25[_0x513f('0x1b')]){throw new Error(_0x513f('0x11'));}if(_0x57fd65[_0x513f('0x21')]>0x0){if(_0x369b25[_0x513f('0x1c')]){if(moment(_0x369b25[_0x513f('0x1d')])[_0x513f('0x22')](_0x57fd65[_0x513f('0x21')],'minutes')>moment()){throw new Error(_0x513f('0x11'));}return utils[_0x513f('0x23')](_0x369b25,0x0,![],null)[_0x513f('0x17')](function(){return utils[_0x513f('0x24')](_0x369b25);})['then'](function(){return _0x979578(null,_0x369b25);});}else{return utils[_0x513f('0x24')](_0x369b25)[_0x513f('0x17')](function(){return _0x979578(null,_0x369b25);});}}else{return utils['getLicense'](_0x369b25)[_0x513f('0x17')](function(){return _0x979578(null,_0x369b25);});}})['catch'](function(_0x47a5c4){return _0x979578(null,![],_0x47a5c4);});}function handleError(_0x24dc28,_0xd6c20c,_0x419810){return _0x24dc28[_0x513f('0x25')](_0xd6c20c||0x1f4)['send']({'message':_0x419810?_0x419810[_0x513f('0x26')]||_0x419810:''});}exports[_0x513f('0x27')]=function(_0xd8023d,_0x201cee,_0x495a01){passport[_0x513f('0x28')](_0x513f('0x29'),function(_0x315e3a,_0xcaf68d,_0x4324d3){var _0x2bff56=_0x315e3a||_0x4324d3;if(_0x2bff56){return handleError(_0x201cee,0x191,_0x2bff56);}if(!_0xcaf68d){return handleError(_0x201cee,0x194,{'message':'Something\x20went\x20wrong,\x20please\x20try\x20again.'});}else{var _0x586a09={'payload':{'id':_0xcaf68d['id'],'role':_0xcaf68d[_0x513f('0x1a')]},'options':{'expiresIn':0x15180}};return authService[_0x513f('0x2a')](_0x586a09)['then'](function(_0x4a11f7){_0x201cee[_0x513f('0x2b')]({'id':_0xcaf68d['id'],'token':_0x4a11f7,'statusCall':'','isAlreadyLogged':_0xcaf68d[_0x513f('0x2c')]});});}})(_0xd8023d,_0x201cee,_0x495a01);};passport[_0x513f('0x2d')]('ad-login',new LocalStrategy({'usernameField':_0x513f('0x20'),'passwordField':_0x513f('0x18')},function(_0x5a22cd,_0x45d134,_0x34a7b7){return ActiveDirectoryAuthenticate(db[_0x513f('0x2e')],_0x5a22cd,_0x45d134,_0x34a7b7);}));