Built motion from commit (unavailable).|2.1.0
[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 _0x3aff=['pauseType','phoneBarRemoteControl','interface','userProfileId','isChatInteractionAuthorized','use','user','ChatInteraction','params','closed','disposition','then','status','unmanaged','Unmanaged.','catch','authorization','startsWith','headers','Basic','User','find','authenticate','Wrong\x20credentials.','Bearer','json','User\x20not\x20found.','isAuthenticated','../../config/license/util','getLicense','update','Forbidden','canUpdate','sign','signToken','hasRole','Required\x20role\x20needs\x20to\x20be\x20set','userRoles','indexOf','It\x20looks\x20like\x20you\x20aren\x27t\x20logged\x20in,\x20please\x20try\x20again.','motion.token','pick','/dashboards/general','../../mysqldb','jsonwebtoken','express-jwt','composable-middleware','basic-auth','ipaddr.js','secrets','session','role','fullname','name','internal','email','userpic','permissions','voicePause','chatPause','mailPause','faxPause','smsPause','openchannelPause'];(function(_0x12ca16,_0x281fd9){var _0x56fb78=function(_0x3479b2){while(--_0x3479b2){_0x12ca16['push'](_0x12ca16['shift']());}};_0x56fb78(++_0x281fd9);}(_0x3aff,0x12b));var _0xf3af=function(_0x105ee6,_0x5f1768){_0x105ee6=_0x105ee6-0x0;var _0x3856e3=_0x3aff[_0x105ee6];return _0x3856e3;};'use strict';var db=require(_0xf3af('0x0'))['db'];var config=require('../../config/environment');var _=require('lodash');var jwt=require(_0xf3af('0x1'));var expressJwt=require(_0xf3af('0x2'));var compose=require(_0xf3af('0x3'));var basicAuth=require(_0xf3af('0x4'));var ipaddr=require(_0xf3af('0x5'));var validateJwt=expressJwt({'secret':config[_0xf3af('0x6')][_0xf3af('0x7')]});var userAttributes=['id',_0xf3af('0x8'),_0xf3af('0x9'),_0xf3af('0xa'),_0xf3af('0xb'),_0xf3af('0xc'),_0xf3af('0xd'),_0xf3af('0xe'),_0xf3af('0xf'),_0xf3af('0x10'),_0xf3af('0x11'),_0xf3af('0x12'),_0xf3af('0x13'),_0xf3af('0x14'),_0xf3af('0x15'),'showWebBar','phoneBarRemoteControlPort',_0xf3af('0x16'),_0xf3af('0x17'),_0xf3af('0x18')];exports[_0xf3af('0x19')]=function(){return isAuthenticated(!![])[_0xf3af('0x1a')](function(_0x48257a,_0x44c999,_0x57c1c4){if(_0x48257a[_0xf3af('0x1b')]){_0x57c1c4();}else{return db[_0xf3af('0x1c')]['findOne']({'where':{'id':_0x48257a[_0xf3af('0x1d')]['id']},'attributes':['id',_0xf3af('0x1e'),_0xf3af('0x1f')],'raw':!![]})[_0xf3af('0x20')](function(_0x30aaf0){if(_0x30aaf0&&_0x30aaf0[_0xf3af('0x1e')]){return _0x44c999[_0xf3af('0x21')](_0x30aaf0[_0xf3af('0x1f')]===_0xf3af('0x22')?0x195:0x193)['json']({'message':_0x30aaf0['disposition']===_0xf3af('0x22')?_0xf3af('0x23'):'Forbidden.'});}else{_0x57c1c4();}})[_0xf3af('0x24')](function(_0x11a862){_0x57c1c4(_0x11a862);});}});};function isAuthenticated(_0x4ddb22){return compose()[_0xf3af('0x1a')](function(_0x5611a3,_0x4b5c94,_0x2bc5a1){if(_0x5611a3['headers'][_0xf3af('0x25')]){if(_[_0xf3af('0x26')](_0x5611a3[_0xf3af('0x27')][_0xf3af('0x25')],_0xf3af('0x28'))){var _0x1db94f=basicAuth(_0x5611a3);db[_0xf3af('0x29')][_0xf3af('0x2a')]({'where':{'name':_0x1db94f[_0xf3af('0xa')]}})[_0xf3af('0x20')](function(_0x2910ff){if(!_0x2910ff||!_0x2910ff[_0xf3af('0x2b')](_0x1db94f['pass'])){return _0x4b5c94[_0xf3af('0x21')](0x191)['json']({'message':_0xf3af('0x2c')});}_0x5611a3[_0xf3af('0x1b')]={'id':_0x2910ff['id']};_0x2bc5a1();})[_0xf3af('0x24')](function(_0x517e0a){_0x2bc5a1(_0x517e0a);});}else if(_[_0xf3af('0x26')](_0x5611a3[_0xf3af('0x27')]['authorization'],_0xf3af('0x2d'))){validateJwt(_0x5611a3,_0x4b5c94,_0x2bc5a1);}}else{if(_0x4ddb22){_0x2bc5a1();}else{return _0x4b5c94[_0xf3af('0x21')](0x191)[_0xf3af('0x2e')]({'message':'Unknown\x20authorization\x20format.'});}}})[_0xf3af('0x1a')](function(_0x594fae,_0x5782d6,_0x50cfc4){if(_0x594fae[_0xf3af('0x1b')]){db[_0xf3af('0x29')][_0xf3af('0x2a')]({'where':{'id':_0x594fae[_0xf3af('0x1b')]['id']},'attributes':userAttributes})[_0xf3af('0x20')](function(_0x219dd6){if(!_0x219dd6){return _0x5782d6[_0xf3af('0x21')](0x194)[_0xf3af('0x2e')]({'message':_0xf3af('0x2f')});}_0x594fae[_0xf3af('0x1b')]=_0x219dd6;_0x50cfc4();})['catch'](function(_0x336c0e){_0x50cfc4(_0x336c0e);});}else if(_0x4ddb22){_0x50cfc4();}else{return _0x5782d6[_0xf3af('0x21')](0x194)[_0xf3af('0x2e')]({'message':'User\x20object\x20not\x20found.'});}});}exports[_0xf3af('0x30')]=isAuthenticated;function canUpdate(){return compose()[_0xf3af('0x1a')](function(_0x42ea82,_0x2ca0e7,_0x465448){return require(_0xf3af('0x31'))[_0xf3af('0x32')]()[_0xf3af('0x20')](function(_0x51ee23){if(_0x51ee23[_0xf3af('0x33')]){_0x465448();}else{return _0x2ca0e7[_0xf3af('0x21')](0x193)[_0xf3af('0x2e')]({'message':_0xf3af('0x34')});}})['catch'](function(_0x1eeb34){_0x465448(_0x1eeb34);});});}exports[_0xf3af('0x35')]=canUpdate;function signToken(_0xb84247,_0x57a7ca){return jwt[_0xf3af('0x36')]({'id':_0xb84247,'role':_0x57a7ca},config[_0xf3af('0x6')]['session'],{'expiresIn':0x15180});}exports[_0xf3af('0x37')]=signToken;exports[_0xf3af('0x38')]=function(_0x4618e7){if(!_0x4618e7){throw new Error(_0xf3af('0x39'));}return compose()[_0xf3af('0x1a')](isAuthenticated())[_0xf3af('0x1a')](function meetsRequirements(_0x365541,_0x754623,_0x1e235e){if(config[_0xf3af('0x3a')][_0xf3af('0x3b')](_0x365541[_0xf3af('0x1b')][_0xf3af('0x8')])>=config[_0xf3af('0x3a')][_0xf3af('0x3b')](_0x4618e7)){return _0x1e235e();}else{return _0x754623[_0xf3af('0x21')](0x193)[_0xf3af('0x2e')]({'message':_0xf3af('0x34')});}});};exports['setTokenCookie']=function(_0x30e539,_0x366cca){if(!_0x30e539[_0xf3af('0x1b')]){return _0x366cca['status'](0x194)[_0xf3af('0x2e')]({'message':_0xf3af('0x3c')});}var _0x45a9da=signToken(_0x30e539[_0xf3af('0x1b')]['id'],_0x30e539[_0xf3af('0x1b')]['role']);_0x366cca['cookie'](_0xf3af('0x3d'),_0x45a9da);_0x366cca['cookie']('motion.user',JSON['stringify'](_[_0xf3af('0x3e')](_0x30e539[_0xf3af('0x1b')],userAttributes)));_0x366cca['redirect'](_0xf3af('0x3f'));};