Built motion from commit (unavailable).|2.0.84
[motion2.git] / server / components / auth / service.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 _0x7295=['Basic','User','find','name','authenticate','pass','Wrong\x20credentials.','user','authorization','Bearer','Unknown\x20authorization\x20format.','User\x20not\x20found.','getLicense','update','Forbidden','canUpdate','sign','session','signToken','hasRole','Required\x20role\x20needs\x20to\x20be\x20set','userRoles','indexOf','It\x20looks\x20like\x20you\x20aren\x27t\x20logged\x20in,\x20please\x20try\x20again.','cookie','motion.user','stringify','pick','redirect','/dashboards/general','../../mysqldb','../../config/environment','express-jwt','composable-middleware','basic-auth','secrets','role','fullname','internal','email','userpic','permissions','voicePause','chatPause','faxPause','openchannelPause','pauseType','showWebBar','phoneBarRemoteControlPort','phoneBarRemoteControl','interface','use','findOne','params','closed','then','status','disposition','json','unmanaged','Unmanaged.','Forbidden.','catch','startsWith','headers'];(function(_0x407302,_0x56d041){var _0x250792=function(_0x5e3d0f){while(--_0x5e3d0f){_0x407302['push'](_0x407302['shift']());}};_0x250792(++_0x56d041);}(_0x7295,0xe1));var _0x5729=function(_0x2d9535,_0x4bc592){_0x2d9535=_0x2d9535-0x0;var _0xb4e710=_0x7295[_0x2d9535];return _0xb4e710;};'use strict';var db=require(_0x5729('0x0'))['db'];var config=require(_0x5729('0x1'));var _=require('lodash');var jwt=require('jsonwebtoken');var expressJwt=require(_0x5729('0x2'));var compose=require(_0x5729('0x3'));var basicAuth=require(_0x5729('0x4'));var ipaddr=require('ipaddr.js');var validateJwt=expressJwt({'secret':config[_0x5729('0x5')]['session']});var userAttributes=['id',_0x5729('0x6'),_0x5729('0x7'),'name',_0x5729('0x8'),_0x5729('0x9'),_0x5729('0xa'),_0x5729('0xb'),_0x5729('0xc'),_0x5729('0xd'),'mailPause',_0x5729('0xe'),'smsPause',_0x5729('0xf'),_0x5729('0x10'),_0x5729('0x11'),_0x5729('0x12'),_0x5729('0x13'),_0x5729('0x14')];exports['isChatInteractionAuthorized']=function(){return isAuthenticated(!![])[_0x5729('0x15')](function(_0x2fb0cd,_0x475446,_0x30bc0a){if(_0x2fb0cd['user']){_0x30bc0a();}else{return db['ChatInteraction'][_0x5729('0x16')]({'where':{'id':_0x2fb0cd[_0x5729('0x17')]['id']},'attributes':['id',_0x5729('0x18'),'disposition'],'raw':!![]})[_0x5729('0x19')](function(_0x8ad636){if(_0x8ad636&&_0x8ad636['closed']){return _0x475446[_0x5729('0x1a')](_0x8ad636[_0x5729('0x1b')]==='unmanaged'?0x195:0x193)[_0x5729('0x1c')]({'message':_0x8ad636[_0x5729('0x1b')]===_0x5729('0x1d')?_0x5729('0x1e'):_0x5729('0x1f')});}else{_0x30bc0a();}})[_0x5729('0x20')](function(_0x57b238){_0x30bc0a(_0x57b238);});}});};function isAuthenticated(_0x3ac58b){return compose()[_0x5729('0x15')](function(_0x3c70c6,_0x43f79f,_0x1a3607){if(_0x3c70c6['headers']['authorization']){if(_[_0x5729('0x21')](_0x3c70c6[_0x5729('0x22')]['authorization'],_0x5729('0x23'))){var _0x5dc592=basicAuth(_0x3c70c6);db[_0x5729('0x24')][_0x5729('0x25')]({'where':{'name':_0x5dc592[_0x5729('0x26')]}})[_0x5729('0x19')](function(_0x3a8a96){if(!_0x3a8a96||!_0x3a8a96[_0x5729('0x27')](_0x5dc592[_0x5729('0x28')])){return _0x43f79f[_0x5729('0x1a')](0x191)[_0x5729('0x1c')]({'message':_0x5729('0x29')});}_0x3c70c6[_0x5729('0x2a')]={'id':_0x3a8a96['id']};_0x1a3607();})['catch'](function(_0x2af2ec){_0x1a3607(_0x2af2ec);});}else if(_[_0x5729('0x21')](_0x3c70c6[_0x5729('0x22')][_0x5729('0x2b')],_0x5729('0x2c'))){validateJwt(_0x3c70c6,_0x43f79f,_0x1a3607);}}else{if(_0x3ac58b){_0x1a3607();}else{return _0x43f79f[_0x5729('0x1a')](0x191)[_0x5729('0x1c')]({'message':_0x5729('0x2d')});}}})[_0x5729('0x15')](function(_0x2d78d7,_0x3ad304,_0x2ccf7b){if(_0x2d78d7[_0x5729('0x2a')]){db[_0x5729('0x24')]['find']({'where':{'id':_0x2d78d7[_0x5729('0x2a')]['id']},'attributes':userAttributes})[_0x5729('0x19')](function(_0x1cfeec){if(!_0x1cfeec){return _0x3ad304['status'](0x194)['json']({'message':_0x5729('0x2e')});}_0x2d78d7[_0x5729('0x2a')]=_0x1cfeec;_0x2ccf7b();})[_0x5729('0x20')](function(_0x28fa8e){_0x2ccf7b(_0x28fa8e);});}else if(_0x3ac58b){_0x2ccf7b();}else{return _0x3ad304['status'](0x194)[_0x5729('0x1c')]({'message':'User\x20object\x20not\x20found.'});}});}exports['isAuthenticated']=isAuthenticated;function canUpdate(){return compose()[_0x5729('0x15')](function(_0x38cca0,_0x51a963,_0x12fcf2){return require('../../config/license/util')[_0x5729('0x2f')]()[_0x5729('0x19')](function(_0x4458fa){if(_0x4458fa[_0x5729('0x30')]){_0x12fcf2();}else{return _0x51a963['status'](0x193)[_0x5729('0x1c')]({'message':_0x5729('0x31')});}})[_0x5729('0x20')](function(_0xa463c){_0x12fcf2(_0xa463c);});});}exports[_0x5729('0x32')]=canUpdate;function signToken(_0x733792,_0x51adb9){return jwt[_0x5729('0x33')]({'id':_0x733792,'role':_0x51adb9},config[_0x5729('0x5')][_0x5729('0x34')],{'expiresIn':0x15180});}exports[_0x5729('0x35')]=signToken;exports[_0x5729('0x36')]=function(_0x1fd004){if(!_0x1fd004){throw new Error(_0x5729('0x37'));}return compose()['use'](isAuthenticated())[_0x5729('0x15')](function meetsRequirements(_0x19334b,_0x3e5850,_0x492151){if(config['userRoles']['indexOf'](_0x19334b[_0x5729('0x2a')]['role'])>=config[_0x5729('0x38')][_0x5729('0x39')](_0x1fd004)){return _0x492151();}else{return _0x3e5850[_0x5729('0x1a')](0x193)[_0x5729('0x1c')]({'message':_0x5729('0x31')});}});};exports['setTokenCookie']=function(_0x369a1c,_0x419fd3){if(!_0x369a1c['user']){return _0x419fd3[_0x5729('0x1a')](0x194)['json']({'message':_0x5729('0x3a')});}var _0x599107=signToken(_0x369a1c['user']['id'],_0x369a1c['user'][_0x5729('0x6')]);_0x419fd3['cookie']('motion.token',_0x599107);_0x419fd3[_0x5729('0x3b')](_0x5729('0x3c'),JSON[_0x5729('0x3d')](_[_0x5729('0x3e')](_0x369a1c['user'],userAttributes)));_0x419fd3[_0x5729('0x3f')](_0x5729('0x40'));};