Built motion from commit 00bf23f6.|2.6.16
[motion2.git] / server / api / cloudProvider / cloudProvider.oauth.js
index fbe51c8..6229b16 100644 (file)
@@ -15,4 +15,4 @@
 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                        *
 // *                                                                       *
 // *************************************************************************
-var _0x778f=['../../config/schedule/cloud-provider','redis','defaults','localhost','https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0','profile','email','offline_access','https://outlook.office365.com/IMAP.AccessAsUser.All','https://outlook.office.com/POP.AccessAsUser.All','https://outlook.office.com/SMTP.Send','openid','/authorize','/token','map','type','Dynamics365','push','data7','join','Outlook365','/.default','replace','{TENANT_ID}','data2','code','id_token','stringify','toString','base64','randomBytes','hex','client_id','data1','response_type','redirect_uri','data4','form_post','scope','state','nonce','prompt','key','value','oauth2Claims','oauth2/','set','payload','iss','aud','audience','isAfter','exp','data3','CloudProvider','findOne','then','refresh_token','data6','decryptString','data5','access_token','update','catch','authorization_code','intervals','bind','all','inspect','[CLOUD_PROVIDER]\x20Error\x20while\x20refreshing\x20the\x20tokens\x20after\x20service\x20restart\x20error:%s','exports','lodash','jsonwebtoken','moment','ioredis','request-promise','util','../../components/encryptor','../../config/environment'];(function(_0x438bd2,_0x22dddc){var _0x4922b0=function(_0x4e50c7){while(--_0x4e50c7){_0x438bd2['push'](_0x438bd2['shift']());}};_0x4922b0(++_0x22dddc);}(_0x778f,0x134));var _0xf778=function(_0x17928f,_0x4ad33f){_0x17928f=_0x17928f-0x0;var _0x500b63=_0x778f[_0x17928f];return _0x500b63;};'use strict';var _=require(_0xf778('0x0'));var crypto=require('crypto');var jwt=require(_0xf778('0x1'));var moment=require(_0xf778('0x2'));var Redis=require(_0xf778('0x3'));var rp=require(_0xf778('0x4'));var util=require(_0xf778('0x5'));var encryptor=require(_0xf778('0x6'));var config=require(_0xf778('0x7'));var logger=require('../../config/logger')('api');var schedule=require(_0xf778('0x8'));var db=require('../../mysqldb')['db'];config[_0xf778('0x9')]=_[_0xf778('0xa')](config['redis'],{'host':_0xf778('0xb'),'port':0x18eb});var redis=new Redis(config['redis']);var MICROSOFT_AUTH_URL=_0xf778('0xc');var AZURE_AUTH_SCOPES={'Outlook365':['openid',_0xf778('0xd'),_0xf778('0xe'),_0xf778('0xf'),_0xf778('0x10'),_0xf778('0x11'),_0xf778('0x12')],'Dynamics365':[_0xf778('0x13'),_0xf778('0xf')]};var MICROSOFT_AUTH_ENDPOINT=_0xf778('0x14');var MICROSOFT_TOKEN_ENDPOINT=_0xf778('0x15');var OAUTH_REFRESH_INTERVAL=0xfa;function getAuthorizationScopes(_0x3759ae){var _0xf7c0c5=_[_0xf778('0x16')](AZURE_AUTH_SCOPES[_0x3759ae['type']]);if(_0x3759ae[_0xf778('0x17')]===_0xf778('0x18'))_0xf7c0c5[_0xf778('0x19')](_0x3759ae[_0xf778('0x1a')]+'/.default');return _0xf7c0c5[_0xf778('0x1b')]('\x20');}function getAccessTokenScope(_0x36e350){if(_0x36e350[_0xf778('0x17')]===_0xf778('0x1c'))return _0xf778('0x10');if(_0x36e350['type']===_0xf778('0x18'))return _0x36e350[_0xf778('0x1a')]+_0xf778('0x1d');}function generateMicrosoftAuthorizationUrl(_0x579577,_0x5026a9){var _0x42f012=MICROSOFT_AUTH_URL[_0xf778('0x1e')](_0xf778('0x1f'),_0x579577[_0xf778('0x20')]);var _0x12a601=[_0xf778('0x21'),_0xf778('0x22')];var _0x3f0d51=Buffer['from'](JSON[_0xf778('0x23')]({'id':_0x5026a9}))[_0xf778('0x24')](_0xf778('0x25'));var _0x492fd8=crypto[_0xf778('0x26')](0x10)[_0xf778('0x24')](_0xf778('0x27'));var _0x599dac=getAuthorizationScopes(_0x579577);var _0x5b5402=[{'key':_0xf778('0x28'),'value':_0x579577[_0xf778('0x29')]},{'key':_0xf778('0x2a'),'value':encodeURIComponent(_0x12a601[_0xf778('0x1b')]('\x20'))},{'key':_0xf778('0x2b'),'value':_0x579577[_0xf778('0x2c')]},{'key':'response_mode','value':_0xf778('0x2d')},{'key':_0xf778('0x2e'),'value':encodeURIComponent(_0x599dac)},{'key':_0xf778('0x2f'),'value':_0x3f0d51},{'key':_0xf778('0x30'),'value':_0x492fd8},{'key':_0xf778('0x31'),'value':'login'}];var _0x4c9e17=_0x42f012+MICROSOFT_AUTH_ENDPOINT+'?'+_['map'](_0x5b5402,function(_0x1f527b){return _0x1f527b[_0xf778('0x32')]+'='+_0x1f527b[_0xf778('0x33')];})['join']('&');_0x579577[_0xf778('0x34')]={'issuer':_0x42f012[_0xf778('0x1e')](_0xf778('0x35'),''),'audience':_0x579577[_0xf778('0x29')],'state':_0x3f0d51,'nonce':_0x492fd8};redis[_0xf778('0x36')](_0x3f0d51,JSON[_0xf778('0x23')](_0x579577));return _0x4c9e17;}function isValidIdToken(_0x49fc2d,_0x197d4c){try{var _0x986268=jwt['decode'](_0x49fc2d,{'complete':!![]});var _0x3e63fd=_0x986268[_0xf778('0x37')];if(_0x3e63fd[_0xf778('0x38')]!==_0x197d4c['issuer'])return![];if(_0x3e63fd[_0xf778('0x39')]!==_0x197d4c[_0xf778('0x3a')])return![];if(_0x3e63fd[_0xf778('0x30')]!==_0x197d4c['nonce'])return![];if(moment()[_0xf778('0x3b')](moment['unix'](_0x3e63fd[_0xf778('0x3c')])))return![];return!![];}catch(_0x329b5b){throw _0x329b5b;}}function refreshOauth2MicrosoftAccessToken(_0x238d18){return Promise['resolve']()['then'](function(){if(_0x238d18[_0xf778('0x3d')])return _0x238d18;return db[_0xf778('0x3e')][_0xf778('0x3f')]({'where':{'id':_0x238d18['id']},'raw':!![]});})[_0xf778('0x40')](function(_0x1f94c6){var _0xe478d6={'grant_type':_0xf778('0x41'),'refresh_token':_0x1f94c6[_0xf778('0x42')],'scope':getAccessTokenScope(_0x1f94c6),'redirect_uri':_0x1f94c6[_0xf778('0x2c')],'client_id':_0x1f94c6[_0xf778('0x29')],'client_secret':encryptor[_0xf778('0x43')](_0x1f94c6[_0xf778('0x3d')])};var _0x3ab67d={'method':'POST','uri':MICROSOFT_AUTH_URL[_0xf778('0x1e')]('{TENANT_ID}',_0x1f94c6[_0xf778('0x20')])+MICROSOFT_TOKEN_ENDPOINT,'form':_0xe478d6,'json':!![]};return rp(_0x3ab67d);})[_0xf778('0x40')](function(_0x29b139){_0x238d18[_0xf778('0x44')]=_0x29b139[_0xf778('0x45')];_0x238d18[_0xf778('0x42')]=_0x29b139['refresh_token'];return db['CloudProvider'][_0xf778('0x46')]({'data5':_0x238d18[_0xf778('0x44')],'data6':_0x238d18[_0xf778('0x42')]},{'where':{'id':_0x238d18['id']}});})['then'](function(){return _0x238d18;})[_0xf778('0x47')](function(_0xbb8467){logger['error']('Failed\x20to\x20refresh\x20access\x20token\x20for\x20cloud\x20provider\x20#%s,\x20-\x20err:%s',_0x238d18['id'],_0xbb8467);});}function getOauth2MicrosoftAccessToken(_0x709647,_0x1c12a6){var _0x5ee8e1={'grant_type':_0xf778('0x48'),'code':_0x709647,'scope':getAccessTokenScope(_0x1c12a6),'redirect_uri':_0x1c12a6[_0xf778('0x2c')],'client_id':_0x1c12a6[_0xf778('0x29')],'client_secret':encryptor[_0xf778('0x43')](_0x1c12a6['data3'])};var _0x1881a1={'method':'POST','uri':MICROSOFT_AUTH_URL['replace']('{TENANT_ID}',_0x1c12a6[_0xf778('0x20')])+MICROSOFT_TOKEN_ENDPOINT,'form':_0x5ee8e1,'json':!![]};return rp(_0x1881a1);}function startRefreshInterval(_0x3464b0){var _0x11bcf1=schedule[_0xf778('0x49')];if(_0x11bcf1[_0x3464b0['id']])clearInterval(_0x11bcf1[_0x3464b0['id']]);_0x11bcf1[_0x3464b0['id']]=setInterval(refreshOauth2MicrosoftAccessToken[_0xf778('0x4a')](this,{'id':_0x3464b0['id']}),OAUTH_REFRESH_INTERVAL*0x3e8);schedule[_0xf778('0x49')]=_0x11bcf1;}function startAllRefreshIntervals(){return db['CloudProvider']['findAll']({'where':{'data6':{'$ne':null}},'raw':!![]})[_0xf778('0x40')](function(_0x3a3039){var _0x5881e9=_0x3a3039[_0xf778('0x16')](function(_0x2896b5){return refreshOauth2MicrosoftAccessToken(_0x2896b5)[_0xf778('0x40')](function(_0x55e59f){startRefreshInterval(_0x55e59f);});});return Promise[_0xf778('0x4b')](_0x5881e9);})['catch'](function(_0x3a6ad5){var _0x3d66f3=_0x3a6ad5?util[_0xf778('0x4c')](_0x3a6ad5,{'showHidden':![],'depth':null}):'';logger['error'](_0xf778('0x4d'),_0x3d66f3);});}module[_0xf778('0x4e')]={'generateMicrosoftAuthorizationUrl':generateMicrosoftAuthorizationUrl,'getOauth2MicrosoftAccessToken':getOauth2MicrosoftAccessToken,'isValidIdToken':isValidIdToken,'startAllRefreshIntervals':startAllRefreshIntervals,'refreshOauth2MicrosoftAccessToken':refreshOauth2MicrosoftAccessToken,'startRefreshInterval':startRefreshInterval};
\ No newline at end of file
+var _0xb796=['[CLOUD_PROVIDER]\x20Error\x20while\x20refreshing\x20the\x20tokens\x20after\x20service\x20restart\x20error:%s','exports','lodash','crypto','jsonwebtoken','request-promise','../../components/encryptor','../../config/logger','api','redis','defaults','localhost','https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0','openid','email','offline_access','https://outlook.office365.com/IMAP.AccessAsUser.All','https://outlook.office.com/POP.AccessAsUser.All','https://outlook.office.com/SMTP.Send','/authorize','/token','map','type','Dynamics365','push','data7','/.default','join','Outlook365','data2','code','id_token','from','stringify','toString','base64','randomBytes','client_id','redirect_uri','data4','scope','state','nonce','prompt','login','key','value','replace','oauth2/','data1','set','decode','payload','issuer','aud','audience','isAfter','exp','then','CloudProvider','findOne','refresh_token','data6','decryptString','data3','POST','{TENANT_ID}','data5','update','catch','error','Failed\x20to\x20refresh\x20access\x20token\x20for\x20cloud\x20provider\x20#%s,\x20-\x20err:%s','authorization_code','intervals','bind','findAll','all','inspect'];(function(_0x20b9fc,_0x5dab92){var _0x511a2b=function(_0x292cbd){while(--_0x292cbd){_0x20b9fc['push'](_0x20b9fc['shift']());}};_0x511a2b(++_0x5dab92);}(_0xb796,0x13a));var _0x6b79=function(_0x45c2f5,_0x586478){_0x45c2f5=_0x45c2f5-0x0;var _0xe27109=_0xb796[_0x45c2f5];return _0xe27109;};'use strict';var _=require(_0x6b79('0x0'));var crypto=require(_0x6b79('0x1'));var jwt=require(_0x6b79('0x2'));var moment=require('moment');var Redis=require('ioredis');var rp=require(_0x6b79('0x3'));var util=require('util');var encryptor=require(_0x6b79('0x4'));var config=require('../../config/environment');var logger=require(_0x6b79('0x5'))(_0x6b79('0x6'));var schedule=require('../../config/schedule/cloud-provider');var db=require('../../mysqldb')['db'];config[_0x6b79('0x7')]=_[_0x6b79('0x8')](config[_0x6b79('0x7')],{'host':_0x6b79('0x9'),'port':0x18eb});var redis=new Redis(config['redis']);var MICROSOFT_AUTH_URL=_0x6b79('0xa');var AZURE_AUTH_SCOPES={'Outlook365':[_0x6b79('0xb'),'profile',_0x6b79('0xc'),_0x6b79('0xd'),_0x6b79('0xe'),_0x6b79('0xf'),_0x6b79('0x10')],'Dynamics365':['openid',_0x6b79('0xd')]};var MICROSOFT_AUTH_ENDPOINT=_0x6b79('0x11');var MICROSOFT_TOKEN_ENDPOINT=_0x6b79('0x12');var OAUTH_REFRESH_INTERVAL=0xfa;function getAuthorizationScopes(_0x8abf93){var _0x167517=_[_0x6b79('0x13')](AZURE_AUTH_SCOPES[_0x8abf93[_0x6b79('0x14')]]);if(_0x8abf93['type']===_0x6b79('0x15'))_0x167517[_0x6b79('0x16')](_0x8abf93[_0x6b79('0x17')]+_0x6b79('0x18'));return _0x167517[_0x6b79('0x19')]('\x20');}function getAccessTokenScope(_0x540bb4){if(_0x540bb4[_0x6b79('0x14')]===_0x6b79('0x1a'))return _0x6b79('0xe');if(_0x540bb4[_0x6b79('0x14')]===_0x6b79('0x15'))return _0x540bb4[_0x6b79('0x17')]+_0x6b79('0x18');}function generateMicrosoftAuthorizationUrl(_0x23dded,_0x117259){var _0x2bf9cb=MICROSOFT_AUTH_URL['replace']('{TENANT_ID}',_0x23dded[_0x6b79('0x1b')]);var _0x59704a=[_0x6b79('0x1c'),_0x6b79('0x1d')];var _0x10fb64=Buffer[_0x6b79('0x1e')](JSON[_0x6b79('0x1f')]({'id':_0x117259}))[_0x6b79('0x20')](_0x6b79('0x21'));var _0x1f4f36=crypto[_0x6b79('0x22')](0x10)[_0x6b79('0x20')]('hex');var _0x209d68=getAuthorizationScopes(_0x23dded);var _0x3c30e5=[{'key':_0x6b79('0x23'),'value':_0x23dded['data1']},{'key':'response_type','value':encodeURIComponent(_0x59704a[_0x6b79('0x19')]('\x20'))},{'key':_0x6b79('0x24'),'value':_0x23dded[_0x6b79('0x25')]},{'key':'response_mode','value':'form_post'},{'key':_0x6b79('0x26'),'value':encodeURIComponent(_0x209d68)},{'key':_0x6b79('0x27'),'value':_0x10fb64},{'key':_0x6b79('0x28'),'value':_0x1f4f36},{'key':_0x6b79('0x29'),'value':_0x6b79('0x2a')}];var _0x5d1168=_0x2bf9cb+MICROSOFT_AUTH_ENDPOINT+'?'+_[_0x6b79('0x13')](_0x3c30e5,function(_0x2cee39){return _0x2cee39[_0x6b79('0x2b')]+'='+_0x2cee39[_0x6b79('0x2c')];})[_0x6b79('0x19')]('&');_0x23dded['oauth2Claims']={'issuer':_0x2bf9cb[_0x6b79('0x2d')](_0x6b79('0x2e'),''),'audience':_0x23dded[_0x6b79('0x2f')],'state':_0x10fb64,'nonce':_0x1f4f36};redis[_0x6b79('0x30')](_0x10fb64,JSON[_0x6b79('0x1f')](_0x23dded));return _0x5d1168;}function isValidIdToken(_0x1777a6,_0x1ae8dc){try{var _0x5c96ca=jwt[_0x6b79('0x31')](_0x1777a6,{'complete':!![]});var _0x171f7a=_0x5c96ca[_0x6b79('0x32')];if(_0x171f7a['iss']!==_0x1ae8dc[_0x6b79('0x33')])return![];if(_0x171f7a[_0x6b79('0x34')]!==_0x1ae8dc[_0x6b79('0x35')])return![];if(_0x171f7a['nonce']!==_0x1ae8dc[_0x6b79('0x28')])return![];if(moment()[_0x6b79('0x36')](moment['unix'](_0x171f7a[_0x6b79('0x37')])))return![];return!![];}catch(_0x44602f){throw _0x44602f;}}function refreshOauth2MicrosoftAccessToken(_0x2bfb52){return Promise['resolve']()[_0x6b79('0x38')](function(){if(_0x2bfb52['data3'])return _0x2bfb52;return db[_0x6b79('0x39')][_0x6b79('0x3a')]({'where':{'id':_0x2bfb52['id']},'raw':!![]});})[_0x6b79('0x38')](function(_0x5794ab){var _0x2189a6={'grant_type':_0x6b79('0x3b'),'refresh_token':_0x5794ab[_0x6b79('0x3c')],'scope':getAccessTokenScope(_0x5794ab),'redirect_uri':_0x5794ab[_0x6b79('0x25')],'client_id':_0x5794ab[_0x6b79('0x2f')],'client_secret':encryptor[_0x6b79('0x3d')](_0x5794ab[_0x6b79('0x3e')])};var _0x31d9c1={'method':_0x6b79('0x3f'),'uri':MICROSOFT_AUTH_URL['replace'](_0x6b79('0x40'),_0x5794ab[_0x6b79('0x1b')])+MICROSOFT_TOKEN_ENDPOINT,'form':_0x2189a6,'json':!![]};return rp(_0x31d9c1);})[_0x6b79('0x38')](function(_0x37c251){_0x2bfb52[_0x6b79('0x41')]=_0x37c251['access_token'];_0x2bfb52[_0x6b79('0x3c')]=_0x37c251[_0x6b79('0x3b')];return db['CloudProvider'][_0x6b79('0x42')]({'data5':_0x2bfb52['data5'],'data6':_0x2bfb52[_0x6b79('0x3c')]},{'where':{'id':_0x2bfb52['id']}});})[_0x6b79('0x38')](function(){return _0x2bfb52;})[_0x6b79('0x43')](function(_0x430676){logger[_0x6b79('0x44')](_0x6b79('0x45'),_0x2bfb52['id'],_0x430676);});}function getOauth2MicrosoftAccessToken(_0x526649,_0x35e0f6){var _0x5c831e={'grant_type':_0x6b79('0x46'),'code':_0x526649,'scope':getAccessTokenScope(_0x35e0f6),'redirect_uri':_0x35e0f6[_0x6b79('0x25')],'client_id':_0x35e0f6['data1'],'client_secret':encryptor[_0x6b79('0x3d')](_0x35e0f6['data3'])};var _0x458315={'method':_0x6b79('0x3f'),'uri':MICROSOFT_AUTH_URL['replace'](_0x6b79('0x40'),_0x35e0f6['data2'])+MICROSOFT_TOKEN_ENDPOINT,'form':_0x5c831e,'json':!![]};return rp(_0x458315);}function startRefreshInterval(_0x35062b){var _0x4ab8d8=schedule[_0x6b79('0x47')];if(_0x4ab8d8[_0x35062b['id']])clearInterval(_0x4ab8d8[_0x35062b['id']]);_0x4ab8d8[_0x35062b['id']]=setInterval(refreshOauth2MicrosoftAccessToken[_0x6b79('0x48')](this,{'id':_0x35062b['id']}),OAUTH_REFRESH_INTERVAL*0x3e8);schedule['intervals']=_0x4ab8d8;}function startAllRefreshIntervals(){return db[_0x6b79('0x39')][_0x6b79('0x49')]({'where':{'data6':{'$ne':null}},'raw':!![]})[_0x6b79('0x38')](function(_0x2dc6e0){var _0x90ad72=_0x2dc6e0[_0x6b79('0x13')](function(_0x20d334){return refreshOauth2MicrosoftAccessToken(_0x20d334)['then'](function(_0x16184d){startRefreshInterval(_0x16184d);});});return Promise[_0x6b79('0x4a')](_0x90ad72);})[_0x6b79('0x43')](function(_0x18a908){var _0x35860d=_0x18a908?util[_0x6b79('0x4b')](_0x18a908,{'showHidden':![],'depth':null}):'';logger[_0x6b79('0x44')](_0x6b79('0x4c'),_0x35860d);});}module[_0x6b79('0x4d')]={'generateMicrosoftAuthorizationUrl':generateMicrosoftAuthorizationUrl,'getOauth2MicrosoftAccessToken':getOauth2MicrosoftAccessToken,'isValidIdToken':isValidIdToken,'startAllRefreshIntervals':startAllRefreshIntervals,'refreshOauth2MicrosoftAccessToken':refreshOauth2MicrosoftAccessToken,'startRefreshInterval':startRefreshInterval};
\ No newline at end of file