Built motion from commit 0900f080.|2.5.45
[motion2.git] / server / api / cloudProvider / cloudProvider.oauth.js
index bda323d..5727292 100644 (file)
@@ -15,4 +15,4 @@
 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                        *
 // *                                                                       *
 // *************************************************************************
-var _0x326f=['../../config/logger','api','../../config/schedule/cloud-provider','../../mysqldb','defaults','redis','localhost','email','offline_access','https://outlook.office365.com/IMAP.AccessAsUser.All','https://outlook.office.com/POP.AccessAsUser.All','https://outlook.office.com/SMTP.Send','/token','type','push','data7','/.default','join','Dynamics365','replace','data2','code','id_token','from','randomBytes','data1','response_type','data4','response_mode','scope','state','nonce','prompt','login','value','oauth2Claims','oauth2/','stringify','decode','iss','issuer','aud','unix','exp','resolve','then','data3','CloudProvider','findOne','refresh_token','data6','decryptString','{TENANT_ID}','data5','access_token','update','catch','Failed\x20to\x20refresh\x20access\x20token\x20for\x20cloud\x20provider\x20#%s','authorization_code','intervals','bind','findAll','inspect','error','[CLOUD_PROVIDER]\x20Error\x20while\x20refreshing\x20the\x20tokens\x20after\x20service\x20restart','lodash','moment','ioredis','request-promise','util','../../components/encryptor','../../config/environment'];(function(_0x397e5f,_0x454c8f){var _0x5302b8=function(_0x1a29ef){while(--_0x1a29ef){_0x397e5f['push'](_0x397e5f['shift']());}};_0x5302b8(++_0x454c8f);}(_0x326f,0xd1));var _0xf326=function(_0x4ac7a6,_0x257bce){_0x4ac7a6=_0x4ac7a6-0x0;var _0x529f01=_0x326f[_0x4ac7a6];return _0x529f01;};'use strict';var _=require(_0xf326('0x0'));var crypto=require('crypto');var jwt=require('jsonwebtoken');var moment=require(_0xf326('0x1'));var Redis=require(_0xf326('0x2'));var rp=require(_0xf326('0x3'));var util=require(_0xf326('0x4'));var encryptor=require(_0xf326('0x5'));var config=require(_0xf326('0x6'));var logger=require(_0xf326('0x7'))(_0xf326('0x8'));var schedule=require(_0xf326('0x9'));var db=require(_0xf326('0xa'))['db'];config['redis']=_[_0xf326('0xb')](config[_0xf326('0xc')],{'host':_0xf326('0xd'),'port':0x18eb});var redis=new Redis(config[_0xf326('0xc')]);var MICROSOFT_AUTH_URL='https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0';var AZURE_AUTH_SCOPES={'Outlook365':['openid','profile',_0xf326('0xe'),_0xf326('0xf'),_0xf326('0x10'),_0xf326('0x11'),_0xf326('0x12')],'Dynamics365':['openid',_0xf326('0xf')]};var MICROSOFT_AUTH_ENDPOINT='/authorize';var MICROSOFT_TOKEN_ENDPOINT=_0xf326('0x13');var OAUTH_REFRESH_INTERVAL=0xfa;function getAuthorizationScopes(_0x4aaf3a){var _0x2e4b71=_['map'](AZURE_AUTH_SCOPES[_0x4aaf3a[_0xf326('0x14')]]);if(_0x4aaf3a[_0xf326('0x14')]==='Dynamics365')_0x2e4b71[_0xf326('0x15')](_0x4aaf3a[_0xf326('0x16')]+_0xf326('0x17'));return _0x2e4b71[_0xf326('0x18')]('\x20');}function getAccessTokenScope(_0x5c8483){if(_0x5c8483[_0xf326('0x14')]==='Outlook365')return'https://outlook.office365.com/IMAP.AccessAsUser.All';if(_0x5c8483[_0xf326('0x14')]===_0xf326('0x19'))return _0x5c8483[_0xf326('0x16')]+_0xf326('0x17');}function generateMicrosoftAuthorizationUrl(_0x292f4b,_0x287a2a){var _0x34128e=MICROSOFT_AUTH_URL[_0xf326('0x1a')]('{TENANT_ID}',_0x292f4b[_0xf326('0x1b')]);var _0x453100=[_0xf326('0x1c'),_0xf326('0x1d')];var _0xcef2b5=Buffer[_0xf326('0x1e')](JSON['stringify']({'id':_0x287a2a}))['toString']('base64');var _0x16e605=crypto[_0xf326('0x1f')](0x10)['toString']('hex');var _0x128c68=getAuthorizationScopes(_0x292f4b);var _0x4dfa76=[{'key':'client_id','value':_0x292f4b[_0xf326('0x20')]},{'key':_0xf326('0x21'),'value':encodeURIComponent(_0x453100['join']('\x20'))},{'key':'redirect_uri','value':_0x292f4b[_0xf326('0x22')]},{'key':_0xf326('0x23'),'value':'form_post'},{'key':_0xf326('0x24'),'value':encodeURIComponent(_0x128c68)},{'key':_0xf326('0x25'),'value':_0xcef2b5},{'key':_0xf326('0x26'),'value':_0x16e605},{'key':_0xf326('0x27'),'value':_0xf326('0x28')}];var _0x2049ec=_0x34128e+MICROSOFT_AUTH_ENDPOINT+'?'+_['map'](_0x4dfa76,function(_0x55b9f4){return _0x55b9f4['key']+'='+_0x55b9f4[_0xf326('0x29')];})[_0xf326('0x18')]('&');_0x292f4b[_0xf326('0x2a')]={'issuer':_0x34128e[_0xf326('0x1a')](_0xf326('0x2b'),''),'audience':_0x292f4b[_0xf326('0x20')],'state':_0xcef2b5,'nonce':_0x16e605};redis['set'](_0xcef2b5,JSON[_0xf326('0x2c')](_0x292f4b));return _0x2049ec;}function isValidIdToken(_0xa3c0b,_0x212d5d){try{var _0x581531=jwt[_0xf326('0x2d')](_0xa3c0b,{'complete':!![]});var _0x55ad0f=_0x581531['payload'];if(_0x55ad0f[_0xf326('0x2e')]!==_0x212d5d[_0xf326('0x2f')])return![];if(_0x55ad0f[_0xf326('0x30')]!==_0x212d5d['audience'])return![];if(_0x55ad0f[_0xf326('0x26')]!==_0x212d5d[_0xf326('0x26')])return![];if(moment()['isAfter'](moment[_0xf326('0x31')](_0x55ad0f[_0xf326('0x32')])))return![];return!![];}catch(_0xd77aba){throw _0xd77aba;}}function refreshOauth2MicrosoftAccessToken(_0x18d110){return Promise[_0xf326('0x33')]()[_0xf326('0x34')](function(){if(_0x18d110[_0xf326('0x35')])return _0x18d110;return db[_0xf326('0x36')][_0xf326('0x37')]({'where':{'id':_0x18d110['id']},'raw':!![]});})[_0xf326('0x34')](function(_0x4b52f2){var _0x1035ed={'grant_type':_0xf326('0x38'),'refresh_token':_0x4b52f2[_0xf326('0x39')],'scope':getAccessTokenScope(_0x4b52f2),'redirect_uri':_0x4b52f2['data4'],'client_id':_0x4b52f2[_0xf326('0x20')],'client_secret':encryptor[_0xf326('0x3a')](_0x4b52f2[_0xf326('0x35')])};var _0x4df451={'method':'POST','uri':MICROSOFT_AUTH_URL[_0xf326('0x1a')](_0xf326('0x3b'),_0x4b52f2[_0xf326('0x1b')])+MICROSOFT_TOKEN_ENDPOINT,'form':_0x1035ed,'json':!![]};return rp(_0x4df451);})[_0xf326('0x34')](function(_0x30694f){_0x18d110[_0xf326('0x3c')]=_0x30694f[_0xf326('0x3d')];_0x18d110[_0xf326('0x39')]=_0x30694f['refresh_token'];return db[_0xf326('0x36')][_0xf326('0x3e')]({'data5':_0x18d110['data5'],'data6':_0x18d110[_0xf326('0x39')]},{'where':{'id':_0x18d110['id']}});})['then'](function(){return _0x18d110;})[_0xf326('0x3f')](function(_0x481b64){logger['error'](_0xf326('0x40'),_0x18d110['id'],_0x481b64);});}function getOauth2MicrosoftAccessToken(_0x4d8eac,_0x40dcb4){var _0x57aa27={'grant_type':_0xf326('0x41'),'code':_0x4d8eac,'scope':getAccessTokenScope(_0x40dcb4),'redirect_uri':_0x40dcb4[_0xf326('0x22')],'client_id':_0x40dcb4[_0xf326('0x20')],'client_secret':encryptor['decryptString'](_0x40dcb4[_0xf326('0x35')])};var _0x4095e6={'method':'POST','uri':MICROSOFT_AUTH_URL[_0xf326('0x1a')](_0xf326('0x3b'),_0x40dcb4[_0xf326('0x1b')])+MICROSOFT_TOKEN_ENDPOINT,'form':_0x57aa27,'json':!![]};return rp(_0x4095e6);}function startRefreshInterval(_0xaf65e0){var _0x51076c=schedule[_0xf326('0x42')];if(_0x51076c[_0xaf65e0['id']])clearInterval(_0x51076c[_0xaf65e0['id']]);_0x51076c[_0xaf65e0['id']]=setInterval(refreshOauth2MicrosoftAccessToken[_0xf326('0x43')](this,{'id':_0xaf65e0['id']}),OAUTH_REFRESH_INTERVAL*0x3e8);schedule[_0xf326('0x42')]=_0x51076c;}function startAllRefreshIntervals(){return db[_0xf326('0x36')][_0xf326('0x44')]({'where':{'data6':{'$ne':null}},'raw':!![]})['then'](function(_0x3e830d){var _0x1b7bf6=_0x3e830d['map'](function(_0x4dd361){return refreshOauth2MicrosoftAccessToken(_0x4dd361)[_0xf326('0x34')](function(_0x577634){startRefreshInterval(_0x577634);});});return Promise['all'](_0x1b7bf6);})[_0xf326('0x3f')](function(_0x5d6ae2){var _0x3e1e8c=_0x5d6ae2?util[_0xf326('0x45')](_0x5d6ae2,{'showHidden':![],'depth':null}):'';logger[_0xf326('0x46')](_0xf326('0x47'),_0x3e1e8c);});}module['exports']={'generateMicrosoftAuthorizationUrl':generateMicrosoftAuthorizationUrl,'getOauth2MicrosoftAccessToken':getOauth2MicrosoftAccessToken,'isValidIdToken':isValidIdToken,'startAllRefreshIntervals':startAllRefreshIntervals,'refreshOauth2MicrosoftAccessToken':refreshOauth2MicrosoftAccessToken,'startRefreshInterval':startRefreshInterval};
\ No newline at end of file
+var _0xf282=['push','data7','/.default','https://outlook.office365.com/IMAP.AccessAsUser.All','Dynamics365','replace','{TENANT_ID}','data2','id_token','from','toString','randomBytes','hex','client_id','data1','response_type','join','redirect_uri','data4','response_mode','form_post','scope','state','nonce','prompt','login','key','oauth2Claims','oauth2/','set','decode','payload','iss','audience','unix','then','data3','CloudProvider','findOne','refresh_token','data6','decryptString','POST','data5','access_token','update','catch','error','Failed\x20to\x20refresh\x20access\x20token\x20for\x20cloud\x20provider\x20#%s,\x20-\x20err:%s','authorization_code','intervals','bind','findAll','map','all','inspect','[CLOUD_PROVIDER]\x20Error\x20while\x20refreshing\x20the\x20tokens\x20after\x20service\x20restart\x20error:%s','exports','lodash','crypto','jsonwebtoken','ioredis','request-promise','util','../../components/encryptor','../../config/environment','../../config/logger','../../config/schedule/cloud-provider','../../mysqldb','redis','defaults','localhost','https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0','profile','offline_access','https://outlook.office.com/POP.AccessAsUser.All','https://outlook.office.com/SMTP.Send','openid','/authorize','/token','type'];(function(_0x14add3,_0x13ce8d){var _0x4a174f=function(_0x2b3ed7){while(--_0x2b3ed7){_0x14add3['push'](_0x14add3['shift']());}};_0x4a174f(++_0x13ce8d);}(_0xf282,0x1cf));var _0x2f28=function(_0x5d6456,_0x58ecc7){_0x5d6456=_0x5d6456-0x0;var _0x4d66c1=_0xf282[_0x5d6456];return _0x4d66c1;};'use strict';var _=require(_0x2f28('0x0'));var crypto=require(_0x2f28('0x1'));var jwt=require(_0x2f28('0x2'));var moment=require('moment');var Redis=require(_0x2f28('0x3'));var rp=require(_0x2f28('0x4'));var util=require(_0x2f28('0x5'));var encryptor=require(_0x2f28('0x6'));var config=require(_0x2f28('0x7'));var logger=require(_0x2f28('0x8'))('api');var schedule=require(_0x2f28('0x9'));var db=require(_0x2f28('0xa'))['db'];config[_0x2f28('0xb')]=_[_0x2f28('0xc')](config[_0x2f28('0xb')],{'host':_0x2f28('0xd'),'port':0x18eb});var redis=new Redis(config['redis']);var MICROSOFT_AUTH_URL=_0x2f28('0xe');var AZURE_AUTH_SCOPES={'Outlook365':['openid',_0x2f28('0xf'),'email',_0x2f28('0x10'),'https://outlook.office365.com/IMAP.AccessAsUser.All',_0x2f28('0x11'),_0x2f28('0x12')],'Dynamics365':[_0x2f28('0x13'),_0x2f28('0x10')]};var MICROSOFT_AUTH_ENDPOINT=_0x2f28('0x14');var MICROSOFT_TOKEN_ENDPOINT=_0x2f28('0x15');var OAUTH_REFRESH_INTERVAL=0xfa;function getAuthorizationScopes(_0x27274c){var _0x528428=_['map'](AZURE_AUTH_SCOPES[_0x27274c['type']]);if(_0x27274c[_0x2f28('0x16')]==='Dynamics365')_0x528428[_0x2f28('0x17')](_0x27274c[_0x2f28('0x18')]+_0x2f28('0x19'));return _0x528428['join']('\x20');}function getAccessTokenScope(_0x491c){if(_0x491c['type']==='Outlook365')return _0x2f28('0x1a');if(_0x491c['type']===_0x2f28('0x1b'))return _0x491c[_0x2f28('0x18')]+'/.default';}function generateMicrosoftAuthorizationUrl(_0x524f57,_0x48585a){var _0x35c4a3=MICROSOFT_AUTH_URL[_0x2f28('0x1c')](_0x2f28('0x1d'),_0x524f57[_0x2f28('0x1e')]);var _0x146d15=['code',_0x2f28('0x1f')];var _0x376d73=Buffer[_0x2f28('0x20')](JSON['stringify']({'id':_0x48585a}))[_0x2f28('0x21')]('base64');var _0x384896=crypto[_0x2f28('0x22')](0x10)[_0x2f28('0x21')](_0x2f28('0x23'));var _0x5a2aa=getAuthorizationScopes(_0x524f57);var _0x147083=[{'key':_0x2f28('0x24'),'value':_0x524f57[_0x2f28('0x25')]},{'key':_0x2f28('0x26'),'value':encodeURIComponent(_0x146d15[_0x2f28('0x27')]('\x20'))},{'key':_0x2f28('0x28'),'value':_0x524f57[_0x2f28('0x29')]},{'key':_0x2f28('0x2a'),'value':_0x2f28('0x2b')},{'key':_0x2f28('0x2c'),'value':encodeURIComponent(_0x5a2aa)},{'key':_0x2f28('0x2d'),'value':_0x376d73},{'key':_0x2f28('0x2e'),'value':_0x384896},{'key':_0x2f28('0x2f'),'value':_0x2f28('0x30')}];var _0xe1f21a=_0x35c4a3+MICROSOFT_AUTH_ENDPOINT+'?'+_['map'](_0x147083,function(_0x5c4469){return _0x5c4469[_0x2f28('0x31')]+'='+_0x5c4469['value'];})[_0x2f28('0x27')]('&');_0x524f57[_0x2f28('0x32')]={'issuer':_0x35c4a3[_0x2f28('0x1c')](_0x2f28('0x33'),''),'audience':_0x524f57[_0x2f28('0x25')],'state':_0x376d73,'nonce':_0x384896};redis[_0x2f28('0x34')](_0x376d73,JSON['stringify'](_0x524f57));return _0xe1f21a;}function isValidIdToken(_0x175222,_0xa9cff6){try{var _0x2fe561=jwt[_0x2f28('0x35')](_0x175222,{'complete':!![]});var _0x2f6dc1=_0x2fe561[_0x2f28('0x36')];if(_0x2f6dc1[_0x2f28('0x37')]!==_0xa9cff6['issuer'])return![];if(_0x2f6dc1['aud']!==_0xa9cff6[_0x2f28('0x38')])return![];if(_0x2f6dc1[_0x2f28('0x2e')]!==_0xa9cff6[_0x2f28('0x2e')])return![];if(moment()['isAfter'](moment[_0x2f28('0x39')](_0x2f6dc1['exp'])))return![];return!![];}catch(_0x1b7057){throw _0x1b7057;}}function refreshOauth2MicrosoftAccessToken(_0x2a0e87){return Promise['resolve']()[_0x2f28('0x3a')](function(){if(_0x2a0e87[_0x2f28('0x3b')])return _0x2a0e87;return db[_0x2f28('0x3c')][_0x2f28('0x3d')]({'where':{'id':_0x2a0e87['id']},'raw':!![]});})[_0x2f28('0x3a')](function(_0xa07dbf){var _0x4228e5={'grant_type':_0x2f28('0x3e'),'refresh_token':_0xa07dbf[_0x2f28('0x3f')],'scope':getAccessTokenScope(_0xa07dbf),'redirect_uri':_0xa07dbf[_0x2f28('0x29')],'client_id':_0xa07dbf[_0x2f28('0x25')],'client_secret':encryptor[_0x2f28('0x40')](_0xa07dbf[_0x2f28('0x3b')])};var _0x2f9f82={'method':_0x2f28('0x41'),'uri':MICROSOFT_AUTH_URL['replace'](_0x2f28('0x1d'),_0xa07dbf[_0x2f28('0x1e')])+MICROSOFT_TOKEN_ENDPOINT,'form':_0x4228e5,'json':!![]};return rp(_0x2f9f82);})[_0x2f28('0x3a')](function(_0x44779b){_0x2a0e87[_0x2f28('0x42')]=_0x44779b[_0x2f28('0x43')];_0x2a0e87[_0x2f28('0x3f')]=_0x44779b['refresh_token'];return db['CloudProvider'][_0x2f28('0x44')]({'data5':_0x2a0e87[_0x2f28('0x42')],'data6':_0x2a0e87['data6']},{'where':{'id':_0x2a0e87['id']}});})[_0x2f28('0x3a')](function(){return _0x2a0e87;})[_0x2f28('0x45')](function(_0x3d0a01){logger[_0x2f28('0x46')](_0x2f28('0x47'),_0x2a0e87['id'],_0x3d0a01);});}function getOauth2MicrosoftAccessToken(_0x3c328a,_0x4795f9){var _0x381e12={'grant_type':_0x2f28('0x48'),'code':_0x3c328a,'scope':getAccessTokenScope(_0x4795f9),'redirect_uri':_0x4795f9[_0x2f28('0x29')],'client_id':_0x4795f9[_0x2f28('0x25')],'client_secret':encryptor['decryptString'](_0x4795f9[_0x2f28('0x3b')])};var _0x4abd90={'method':_0x2f28('0x41'),'uri':MICROSOFT_AUTH_URL[_0x2f28('0x1c')](_0x2f28('0x1d'),_0x4795f9[_0x2f28('0x1e')])+MICROSOFT_TOKEN_ENDPOINT,'form':_0x381e12,'json':!![]};return rp(_0x4abd90);}function startRefreshInterval(_0x2b1893){var _0x37687c=schedule[_0x2f28('0x49')];if(_0x37687c[_0x2b1893['id']])clearInterval(_0x37687c[_0x2b1893['id']]);_0x37687c[_0x2b1893['id']]=setInterval(refreshOauth2MicrosoftAccessToken[_0x2f28('0x4a')](this,{'id':_0x2b1893['id']}),OAUTH_REFRESH_INTERVAL*0x3e8);schedule[_0x2f28('0x49')]=_0x37687c;}function startAllRefreshIntervals(){return db[_0x2f28('0x3c')][_0x2f28('0x4b')]({'where':{'data6':{'$ne':null}},'raw':!![]})[_0x2f28('0x3a')](function(_0x3a901d){var _0x36321f=_0x3a901d[_0x2f28('0x4c')](function(_0x530fef){return refreshOauth2MicrosoftAccessToken(_0x530fef)['then'](function(_0x4d3a59){startRefreshInterval(_0x4d3a59);});});return Promise[_0x2f28('0x4d')](_0x36321f);})[_0x2f28('0x45')](function(_0x44eca7){var _0x543a56=_0x44eca7?util[_0x2f28('0x4e')](_0x44eca7,{'showHidden':![],'depth':null}):'';logger['error'](_0x2f28('0x4f'),_0x543a56);});}module[_0x2f28('0x50')]={'generateMicrosoftAuthorizationUrl':generateMicrosoftAuthorizationUrl,'getOauth2MicrosoftAccessToken':getOauth2MicrosoftAccessToken,'isValidIdToken':isValidIdToken,'startAllRefreshIntervals':startAllRefreshIntervals,'refreshOauth2MicrosoftAccessToken':refreshOauth2MicrosoftAccessToken,'startRefreshInterval':startRefreshInterval};
\ No newline at end of file