Built motion from commit c738b9ac.|2.6.25
[motion2.git] / server / api / cloudProvider / cloudProvider.oauth.js
index 2d3e032..d76cdfa 100644 (file)
@@ -15,4 +15,4 @@
 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                        *
 // *                                                                       *
 // *************************************************************************
-var _0x77e3=['response_mode','form_post','scope','state','prompt','login','key','value','oauth2Claims','oauth2/','set','decode','payload','iss','issuer','aud','nonce','isAfter','unix','exp','resolve','then','data3','CloudProvider','findOne','refresh_token','data6','decryptString','{TENANT_ID}','data5','access_token','catch','error','Failed\x20to\x20refresh\x20access\x20token\x20for\x20cloud\x20provider\x20#%s,\x20-\x20err:%s','authorization_code','POST','intervals','bind','findAll','all','inspect','[CLOUD_PROVIDER]\x20Error\x20while\x20refreshing\x20the\x20tokens\x20after\x20service\x20restart\x20error:%s','crypto','jsonwebtoken','ioredis','util','../../components/encryptor','../../config/environment','../../config/logger','api','../../config/schedule/cloud-provider','../../mysqldb','redis','defaults','https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0','openid','profile','email','offline_access','https://outlook.office365.com/IMAP.AccessAsUser.All','https://outlook.office.com/SMTP.Send','/authorize','/token','map','type','Dynamics365','/.default','join','Outlook365','replace','data2','code','id_token','from','stringify','toString','randomBytes','hex','client_id','data1','response_type','redirect_uri','data4'];(function(_0x3d8128,_0x137d87){var _0x7495b=function(_0x10cf91){while(--_0x10cf91){_0x3d8128['push'](_0x3d8128['shift']());}};_0x7495b(++_0x137d87);}(_0x77e3,0x176));var _0x377e=function(_0x402950,_0x52d699){_0x402950=_0x402950-0x0;var _0x643b00=_0x77e3[_0x402950];return _0x643b00;};'use strict';var _=require('lodash');var crypto=require(_0x377e('0x0'));var jwt=require(_0x377e('0x1'));var moment=require('moment');var Redis=require(_0x377e('0x2'));var rp=require('request-promise');var util=require(_0x377e('0x3'));var encryptor=require(_0x377e('0x4'));var config=require(_0x377e('0x5'));var logger=require(_0x377e('0x6'))(_0x377e('0x7'));var schedule=require(_0x377e('0x8'));var db=require(_0x377e('0x9'))['db'];config[_0x377e('0xa')]=_[_0x377e('0xb')](config['redis'],{'host':'localhost','port':0x18eb});var redis=new Redis(config[_0x377e('0xa')]);var MICROSOFT_AUTH_URL=_0x377e('0xc');var AZURE_AUTH_SCOPES={'Outlook365':[_0x377e('0xd'),_0x377e('0xe'),_0x377e('0xf'),_0x377e('0x10'),_0x377e('0x11'),'https://outlook.office.com/POP.AccessAsUser.All',_0x377e('0x12')],'Dynamics365':[_0x377e('0xd'),_0x377e('0x10')]};var MICROSOFT_AUTH_ENDPOINT=_0x377e('0x13');var MICROSOFT_TOKEN_ENDPOINT=_0x377e('0x14');var OAUTH_REFRESH_INTERVAL=0xfa;function getAuthorizationScopes(_0x1979b3){var _0x832956=_[_0x377e('0x15')](AZURE_AUTH_SCOPES[_0x1979b3[_0x377e('0x16')]]);if(_0x1979b3[_0x377e('0x16')]===_0x377e('0x17'))_0x832956['push'](_0x1979b3['data7']+_0x377e('0x18'));return _0x832956[_0x377e('0x19')]('\x20');}function getAccessTokenScope(_0x16f11f){if(_0x16f11f[_0x377e('0x16')]===_0x377e('0x1a'))return _0x377e('0x11');if(_0x16f11f[_0x377e('0x16')]===_0x377e('0x17'))return _0x16f11f['data7']+_0x377e('0x18');}function generateMicrosoftAuthorizationUrl(_0x4e6b12,_0x392f5a){var _0x2794a4=MICROSOFT_AUTH_URL[_0x377e('0x1b')]('{TENANT_ID}',_0x4e6b12[_0x377e('0x1c')]);var _0x150e18=[_0x377e('0x1d'),_0x377e('0x1e')];var _0x45d496=Buffer[_0x377e('0x1f')](JSON[_0x377e('0x20')]({'id':_0x392f5a}))[_0x377e('0x21')]('base64');var _0x306176=crypto[_0x377e('0x22')](0x10)[_0x377e('0x21')](_0x377e('0x23'));var _0xe15cc4=getAuthorizationScopes(_0x4e6b12);var _0x456a9f=[{'key':_0x377e('0x24'),'value':_0x4e6b12[_0x377e('0x25')]},{'key':_0x377e('0x26'),'value':encodeURIComponent(_0x150e18[_0x377e('0x19')]('\x20'))},{'key':_0x377e('0x27'),'value':_0x4e6b12[_0x377e('0x28')]},{'key':_0x377e('0x29'),'value':_0x377e('0x2a')},{'key':_0x377e('0x2b'),'value':encodeURIComponent(_0xe15cc4)},{'key':_0x377e('0x2c'),'value':_0x45d496},{'key':'nonce','value':_0x306176},{'key':_0x377e('0x2d'),'value':_0x377e('0x2e')}];var _0x27f380=_0x2794a4+MICROSOFT_AUTH_ENDPOINT+'?'+_[_0x377e('0x15')](_0x456a9f,function(_0x4305c5){return _0x4305c5[_0x377e('0x2f')]+'='+_0x4305c5[_0x377e('0x30')];})['join']('&');_0x4e6b12[_0x377e('0x31')]={'issuer':_0x2794a4[_0x377e('0x1b')](_0x377e('0x32'),''),'audience':_0x4e6b12[_0x377e('0x25')],'state':_0x45d496,'nonce':_0x306176};redis[_0x377e('0x33')](_0x45d496,JSON[_0x377e('0x20')](_0x4e6b12));return _0x27f380;}function isValidIdToken(_0x2fc3a7,_0xa37d1b){try{var _0x1b864c=jwt[_0x377e('0x34')](_0x2fc3a7,{'complete':!![]});var _0x33b461=_0x1b864c[_0x377e('0x35')];if(_0x33b461[_0x377e('0x36')]!==_0xa37d1b[_0x377e('0x37')])return![];if(_0x33b461[_0x377e('0x38')]!==_0xa37d1b['audience'])return![];if(_0x33b461['nonce']!==_0xa37d1b[_0x377e('0x39')])return![];if(moment()[_0x377e('0x3a')](moment[_0x377e('0x3b')](_0x33b461[_0x377e('0x3c')])))return![];return!![];}catch(_0x2e6ee2){throw _0x2e6ee2;}}function refreshOauth2MicrosoftAccessToken(_0x1408b7){return Promise[_0x377e('0x3d')]()[_0x377e('0x3e')](function(){if(_0x1408b7[_0x377e('0x3f')])return _0x1408b7;return db[_0x377e('0x40')][_0x377e('0x41')]({'where':{'id':_0x1408b7['id']},'raw':!![]});})[_0x377e('0x3e')](function(_0x301ce5){var _0x7bb6e8={'grant_type':_0x377e('0x42'),'refresh_token':_0x301ce5[_0x377e('0x43')],'scope':getAccessTokenScope(_0x301ce5),'redirect_uri':_0x301ce5['data4'],'client_id':_0x301ce5[_0x377e('0x25')],'client_secret':encryptor[_0x377e('0x44')](_0x301ce5[_0x377e('0x3f')])};var _0x2a31da={'method':'POST','uri':MICROSOFT_AUTH_URL['replace'](_0x377e('0x45'),_0x301ce5['data2'])+MICROSOFT_TOKEN_ENDPOINT,'form':_0x7bb6e8,'json':!![]};return rp(_0x2a31da);})[_0x377e('0x3e')](function(_0x614b6){_0x1408b7[_0x377e('0x46')]=_0x614b6[_0x377e('0x47')];_0x1408b7[_0x377e('0x43')]=_0x614b6[_0x377e('0x42')];return db[_0x377e('0x40')]['update']({'data5':_0x1408b7['data5'],'data6':_0x1408b7[_0x377e('0x43')]},{'where':{'id':_0x1408b7['id']}});})[_0x377e('0x3e')](function(){return _0x1408b7;})[_0x377e('0x48')](function(_0x444f35){logger[_0x377e('0x49')](_0x377e('0x4a'),_0x1408b7['id'],_0x444f35);});}function getOauth2MicrosoftAccessToken(_0x2ebdc2,_0x4de7c2){var _0x3dc314={'grant_type':_0x377e('0x4b'),'code':_0x2ebdc2,'scope':getAccessTokenScope(_0x4de7c2),'redirect_uri':_0x4de7c2[_0x377e('0x28')],'client_id':_0x4de7c2[_0x377e('0x25')],'client_secret':encryptor['decryptString'](_0x4de7c2[_0x377e('0x3f')])};var _0xfd8731={'method':_0x377e('0x4c'),'uri':MICROSOFT_AUTH_URL['replace'](_0x377e('0x45'),_0x4de7c2[_0x377e('0x1c')])+MICROSOFT_TOKEN_ENDPOINT,'form':_0x3dc314,'json':!![]};return rp(_0xfd8731);}function startRefreshInterval(_0x3e20b7){var _0xf04b94=schedule[_0x377e('0x4d')];if(_0xf04b94[_0x3e20b7['id']])clearInterval(_0xf04b94[_0x3e20b7['id']]);_0xf04b94[_0x3e20b7['id']]=setInterval(refreshOauth2MicrosoftAccessToken[_0x377e('0x4e')](this,{'id':_0x3e20b7['id']}),OAUTH_REFRESH_INTERVAL*0x3e8);schedule['intervals']=_0xf04b94;}function startAllRefreshIntervals(){return db[_0x377e('0x40')][_0x377e('0x4f')]({'where':{'data6':{'$ne':null}},'raw':!![]})['then'](function(_0x4aaab0){var _0x4b2e4a=_0x4aaab0[_0x377e('0x15')](function(_0x2b419a){return refreshOauth2MicrosoftAccessToken(_0x2b419a)[_0x377e('0x3e')](function(_0x57d03e){startRefreshInterval(_0x57d03e);});});return Promise[_0x377e('0x50')](_0x4b2e4a);})[_0x377e('0x48')](function(_0x36a326){var _0x15fc17=_0x36a326?util[_0x377e('0x51')](_0x36a326,{'showHidden':![],'depth':null}):'';logger['error'](_0x377e('0x52'),_0x15fc17);});}module['exports']={'generateMicrosoftAuthorizationUrl':generateMicrosoftAuthorizationUrl,'getOauth2MicrosoftAccessToken':getOauth2MicrosoftAccessToken,'isValidIdToken':isValidIdToken,'startAllRefreshIntervals':startAllRefreshIntervals,'refreshOauth2MicrosoftAccessToken':refreshOauth2MicrosoftAccessToken,'startRefreshInterval':startRefreshInterval};
\ No newline at end of file
+var _0xf426=['data7','/.default','join','Outlook365','Dynamics365','replace','data2','id_token','from','stringify','toString','base64','randomBytes','client_id','data1','redirect_uri','response_mode','scope','state','nonce','login','key','oauth2Claims','set','decode','payload','iss','aud','audience','isAfter','unix','exp','resolve','then','data3','CloudProvider','refresh_token','data6','data4','decryptString','{TENANT_ID}','data5','access_token','error','Failed\x20to\x20refresh\x20access\x20token\x20for\x20cloud\x20provider\x20#%s,\x20-\x20err:%s','POST','intervals','findAll','all','catch','[CLOUD_PROVIDER]\x20Error\x20while\x20refreshing\x20the\x20tokens\x20after\x20service\x20restart\x20error:%s','exports','lodash','request-promise','../../components/encryptor','../../config/environment','../../config/logger','api','../../config/schedule/cloud-provider','../../mysqldb','redis','defaults','localhost','https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0','email','https://outlook.office365.com/IMAP.AccessAsUser.All','https://outlook.office.com/POP.AccessAsUser.All','https://outlook.office.com/SMTP.Send','openid','/token','map','type','push'];(function(_0x4ccbe2,_0x11eacf){var _0x134525=function(_0x5c9da9){while(--_0x5c9da9){_0x4ccbe2['push'](_0x4ccbe2['shift']());}};_0x134525(++_0x11eacf);}(_0xf426,0x1a1));var _0x6f42=function(_0x65a0db,_0x55c5a0){_0x65a0db=_0x65a0db-0x0;var _0x987620=_0xf426[_0x65a0db];return _0x987620;};'use strict';var _=require(_0x6f42('0x0'));var crypto=require('crypto');var jwt=require('jsonwebtoken');var moment=require('moment');var Redis=require('ioredis');var rp=require(_0x6f42('0x1'));var util=require('util');var encryptor=require(_0x6f42('0x2'));var config=require(_0x6f42('0x3'));var logger=require(_0x6f42('0x4'))(_0x6f42('0x5'));var schedule=require(_0x6f42('0x6'));var db=require(_0x6f42('0x7'))['db'];config[_0x6f42('0x8')]=_[_0x6f42('0x9')](config['redis'],{'host':_0x6f42('0xa'),'port':0x18eb});var redis=new Redis(config[_0x6f42('0x8')]);var MICROSOFT_AUTH_URL=_0x6f42('0xb');var AZURE_AUTH_SCOPES={'Outlook365':['openid','profile',_0x6f42('0xc'),'offline_access',_0x6f42('0xd'),_0x6f42('0xe'),_0x6f42('0xf')],'Dynamics365':[_0x6f42('0x10'),'offline_access']};var MICROSOFT_AUTH_ENDPOINT='/authorize';var MICROSOFT_TOKEN_ENDPOINT=_0x6f42('0x11');var OAUTH_REFRESH_INTERVAL=0xfa;function getAuthorizationScopes(_0xd76454){var _0x56df64=_[_0x6f42('0x12')](AZURE_AUTH_SCOPES[_0xd76454[_0x6f42('0x13')]]);if(_0xd76454['type']==='Dynamics365')_0x56df64[_0x6f42('0x14')](_0xd76454[_0x6f42('0x15')]+_0x6f42('0x16'));return _0x56df64[_0x6f42('0x17')]('\x20');}function getAccessTokenScope(_0x3abffa){if(_0x3abffa[_0x6f42('0x13')]===_0x6f42('0x18'))return _0x6f42('0xd');if(_0x3abffa[_0x6f42('0x13')]===_0x6f42('0x19'))return _0x3abffa[_0x6f42('0x15')]+_0x6f42('0x16');}function generateMicrosoftAuthorizationUrl(_0x39cab0,_0x1a38a9){var _0x588f3e=MICROSOFT_AUTH_URL[_0x6f42('0x1a')]('{TENANT_ID}',_0x39cab0[_0x6f42('0x1b')]);var _0x4a12ef=['code',_0x6f42('0x1c')];var _0x5b994e=Buffer[_0x6f42('0x1d')](JSON[_0x6f42('0x1e')]({'id':_0x1a38a9}))[_0x6f42('0x1f')](_0x6f42('0x20'));var _0x21aab2=crypto[_0x6f42('0x21')](0x10)[_0x6f42('0x1f')]('hex');var _0x1b1469=getAuthorizationScopes(_0x39cab0);var _0x1036a5=[{'key':_0x6f42('0x22'),'value':_0x39cab0[_0x6f42('0x23')]},{'key':'response_type','value':encodeURIComponent(_0x4a12ef[_0x6f42('0x17')]('\x20'))},{'key':_0x6f42('0x24'),'value':_0x39cab0['data4']},{'key':_0x6f42('0x25'),'value':'form_post'},{'key':_0x6f42('0x26'),'value':encodeURIComponent(_0x1b1469)},{'key':_0x6f42('0x27'),'value':_0x5b994e},{'key':_0x6f42('0x28'),'value':_0x21aab2},{'key':'prompt','value':_0x6f42('0x29')}];var _0x964ea3=_0x588f3e+MICROSOFT_AUTH_ENDPOINT+'?'+_[_0x6f42('0x12')](_0x1036a5,function(_0xf9f553){return _0xf9f553[_0x6f42('0x2a')]+'='+_0xf9f553['value'];})[_0x6f42('0x17')]('&');_0x39cab0[_0x6f42('0x2b')]={'issuer':_0x588f3e[_0x6f42('0x1a')]('oauth2/',''),'audience':_0x39cab0[_0x6f42('0x23')],'state':_0x5b994e,'nonce':_0x21aab2};redis[_0x6f42('0x2c')](_0x5b994e,JSON['stringify'](_0x39cab0));return _0x964ea3;}function isValidIdToken(_0x4de54f,_0x4f0a6a){try{var _0x25310c=jwt[_0x6f42('0x2d')](_0x4de54f,{'complete':!![]});var _0x1d143b=_0x25310c[_0x6f42('0x2e')];if(_0x1d143b[_0x6f42('0x2f')]!==_0x4f0a6a['issuer'])return![];if(_0x1d143b[_0x6f42('0x30')]!==_0x4f0a6a[_0x6f42('0x31')])return![];if(_0x1d143b['nonce']!==_0x4f0a6a[_0x6f42('0x28')])return![];if(moment()[_0x6f42('0x32')](moment[_0x6f42('0x33')](_0x1d143b[_0x6f42('0x34')])))return![];return!![];}catch(_0x5d828e){throw _0x5d828e;}}function refreshOauth2MicrosoftAccessToken(_0x1177b0){return Promise[_0x6f42('0x35')]()[_0x6f42('0x36')](function(){if(_0x1177b0[_0x6f42('0x37')])return _0x1177b0;return db[_0x6f42('0x38')]['findOne']({'where':{'id':_0x1177b0['id']},'raw':!![]});})['then'](function(_0x2d0a38){var _0x20b783={'grant_type':_0x6f42('0x39'),'refresh_token':_0x2d0a38[_0x6f42('0x3a')],'scope':getAccessTokenScope(_0x2d0a38),'redirect_uri':_0x2d0a38[_0x6f42('0x3b')],'client_id':_0x2d0a38['data1'],'client_secret':encryptor[_0x6f42('0x3c')](_0x2d0a38[_0x6f42('0x37')])};var _0x44f6ed={'method':'POST','uri':MICROSOFT_AUTH_URL[_0x6f42('0x1a')](_0x6f42('0x3d'),_0x2d0a38[_0x6f42('0x1b')])+MICROSOFT_TOKEN_ENDPOINT,'form':_0x20b783,'json':!![]};return rp(_0x44f6ed);})[_0x6f42('0x36')](function(_0x111dbd){_0x1177b0[_0x6f42('0x3e')]=_0x111dbd[_0x6f42('0x3f')];_0x1177b0['data6']=_0x111dbd[_0x6f42('0x39')];return db[_0x6f42('0x38')]['update']({'data5':_0x1177b0[_0x6f42('0x3e')],'data6':_0x1177b0[_0x6f42('0x3a')]},{'where':{'id':_0x1177b0['id']}});})['then'](function(){return _0x1177b0;})['catch'](function(_0x41c308){logger[_0x6f42('0x40')](_0x6f42('0x41'),_0x1177b0['id'],_0x41c308);});}function getOauth2MicrosoftAccessToken(_0x36cd43,_0x2cb2dc){var _0x135934={'grant_type':'authorization_code','code':_0x36cd43,'scope':getAccessTokenScope(_0x2cb2dc),'redirect_uri':_0x2cb2dc['data4'],'client_id':_0x2cb2dc[_0x6f42('0x23')],'client_secret':encryptor[_0x6f42('0x3c')](_0x2cb2dc[_0x6f42('0x37')])};var _0x2ed499={'method':_0x6f42('0x42'),'uri':MICROSOFT_AUTH_URL[_0x6f42('0x1a')](_0x6f42('0x3d'),_0x2cb2dc[_0x6f42('0x1b')])+MICROSOFT_TOKEN_ENDPOINT,'form':_0x135934,'json':!![]};return rp(_0x2ed499);}function startRefreshInterval(_0x201447){var _0x5335fc=schedule[_0x6f42('0x43')];if(_0x5335fc[_0x201447['id']])clearInterval(_0x5335fc[_0x201447['id']]);_0x5335fc[_0x201447['id']]=setInterval(refreshOauth2MicrosoftAccessToken['bind'](this,{'id':_0x201447['id']}),OAUTH_REFRESH_INTERVAL*0x3e8);schedule[_0x6f42('0x43')]=_0x5335fc;}function startAllRefreshIntervals(){return db[_0x6f42('0x38')][_0x6f42('0x44')]({'where':{'data6':{'$ne':null}},'raw':!![]})[_0x6f42('0x36')](function(_0x1a6389){var _0x169f9d=_0x1a6389[_0x6f42('0x12')](function(_0x1b62f1){return refreshOauth2MicrosoftAccessToken(_0x1b62f1)[_0x6f42('0x36')](function(_0x58e9eb){startRefreshInterval(_0x58e9eb);});});return Promise[_0x6f42('0x45')](_0x169f9d);})[_0x6f42('0x46')](function(_0x45a9ef){var _0x5aa087=_0x45a9ef?util['inspect'](_0x45a9ef,{'showHidden':![],'depth':null}):'';logger[_0x6f42('0x40')](_0x6f42('0x47'),_0x5aa087);});}module[_0x6f42('0x48')]={'generateMicrosoftAuthorizationUrl':generateMicrosoftAuthorizationUrl,'getOauth2MicrosoftAccessToken':getOauth2MicrosoftAccessToken,'isValidIdToken':isValidIdToken,'startAllRefreshIntervals':startAllRefreshIntervals,'refreshOauth2MicrosoftAccessToken':refreshOauth2MicrosoftAccessToken,'startRefreshInterval':startRefreshInterval};
\ No newline at end of file