217 lines
10 KiB
OpenEdge ABL
217 lines
10 KiB
OpenEdge ABL
public with sharing class DropboxAuth extends BaseStorageAuth {
|
|
// private static string key = 'qxsicm15oenit6d'; // '67sl43yt0yda2nh';
|
|
// private static string secert = 'oarcqrgoap4hbbz'; // 'm55vqsl1ejr20rs';
|
|
// private static string redirectUri = 'https://login.salesforce.com/apex/HIC_FilesME__CompleteAuthPage';
|
|
// private static string authUrl='https://www.dropbox.com/oauth2/authorize';
|
|
// private static string scope='account_info.read%20account_info.write';
|
|
// private static string tokenUrl='https://api.dropbox.com/oauth2/token';
|
|
|
|
static String PROVIDER_TYPE = 'Dropbox';
|
|
|
|
// @AuraEnabled
|
|
// public static String createAuthURL(String externalStorage) {
|
|
// String key = EncodingUtil.urlEncode(key,'UTF-8');
|
|
// String uri = EncodingUtil.urlEncode(redirect_uri,'UTF-8');
|
|
// String authuri = '';
|
|
// authuri = authUrl+'?'+
|
|
// 'client_id='+key+
|
|
// '&response_type=code'+
|
|
// '&scope='+scope+
|
|
// '&redirect_uri='+uri+
|
|
// '&access_type=offline';
|
|
// return authuri;
|
|
// }
|
|
|
|
public override void getAccessToken(String code,String Storage,boolean isAdminCred,String userId){
|
|
try {
|
|
|
|
StorageProviderConfigUtil.Config config = StorageProviderConfigUtil.getConfig(PROVIDER_TYPE);
|
|
|
|
String redirectUri = config.redirectUri;
|
|
boolean isProduction = OauthTokenManager.orgSandBoxOrProduction();
|
|
if(!isProduction){
|
|
redirectUri = redirectUri.replace('login.salesforce.com', 'test.salesforce.com');
|
|
}
|
|
|
|
String messageBody =
|
|
'code=' + EncodingUtil.urlEncode(code, 'UTF-8') +
|
|
'&client_id=' + EncodingUtil.urlEncode(config.clientId, 'UTF-8') +
|
|
'&client_secret=' + EncodingUtil.urlEncode(config.clientSecret, 'UTF-8') +
|
|
'&redirect_uri=' + EncodingUtil.urlEncode(redirectUri, 'UTF-8') +
|
|
'&grant_type=authorization_code' +
|
|
'&token_access_type=offline';
|
|
|
|
HttpRequest req = new HttpRequest();
|
|
req.setMethod('POST');
|
|
req.setEndpoint(config.tokenEndpoint);
|
|
req.setHeader('content-type', 'application/x-www-form-urlencoded');
|
|
req.setBody(messageBody);
|
|
req.setTimeout(60*1000);
|
|
|
|
Http callout = new Http();
|
|
String responseText;
|
|
HttpResponse response = callout.send(req);
|
|
if(response.getStatusCode() == 200){
|
|
responseText = response.getBody();
|
|
Map<String,object> responseMap =(Map<String,object>)JSON.deserializeUntyped(responseText) ;
|
|
System.debug('responseMap>>>:'+JSON.serializePretty(responseMap));
|
|
|
|
|
|
String accessToken = (String) responseMap.get('access_token');
|
|
String refreshToken = (String) responseMap.get('refresh_token');
|
|
|
|
String connectedUsername = fetchUserInfo(accessToken);
|
|
|
|
Map<String,object> tokenMapToUpsertInSalesforce = New Map<string,object> () ;
|
|
tokenMapToUpsertInSalesforce.put('HIC_FilesME__Auth_Provider_Name__c','Dropbox');
|
|
tokenMapToUpsertInSalesforce.put('HIC_FilesME__Connected_Username__c',connectedUsername);
|
|
tokenMapToUpsertInSalesforce.put('HIC_FilesME__Provider__c',Storage);
|
|
tokenMapToUpsertInSalesforce.put('HIC_FilesME__Refresh_Token__c',refreshToken);
|
|
tokenMapToUpsertInSalesforce.put('HIC_FilesME__Access_Token__c',accessToken);
|
|
tokenMapToUpsertInSalesforce.put('HIC_FilesME__User__c',userId);
|
|
tokenMapToUpsertInSalesforce.put('HIC_FilesME__Is_Admin_level_Credentials__c',isAdminCred);
|
|
tokenMapToUpsertInSalesforce.put('HIC_FilesME__API_Endpoint_URL__c',config.tokenEndpoint);
|
|
|
|
System.debug('Storing token for Dropbox');
|
|
OAuthTokenManager.storeToken(tokenMapToUpsertInSalesforce);
|
|
}
|
|
}
|
|
catch (Exception e) {
|
|
FME_Logger.error('Error getting access token: ' + e.getMessage(),'DropboxAuth','getAccessToken');
|
|
}
|
|
|
|
}
|
|
|
|
public override String refreshAccessToken(String refreshToken){
|
|
try {
|
|
StorageProviderConfigUtil.Config config = StorageProviderConfigUtil.getConfig(PROVIDER_TYPE);
|
|
|
|
String body = 'grant_type=refresh_token' +
|
|
'&refresh_token=' + EncodingUtil.urlEncode(refreshToken, 'UTF-8') +
|
|
'&client_id=' + EncodingUtil.urlEncode(config.clientId, 'UTF-8') +
|
|
'&client_secret=' + EncodingUtil.urlEncode(config.clientSecret, 'UTF-8');
|
|
|
|
Http http = new Http();
|
|
HttpRequest req = new HttpRequest();
|
|
req.setEndpoint(config.tokenEndpoint);
|
|
req.setMethod('POST');
|
|
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
|
|
req.setBody(body);
|
|
|
|
HttpResponse res = http.send(req);
|
|
if (res.getStatusCode() == 200) {
|
|
Map<String, Object> responseMap = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
|
|
System.debug('responseMap>>>:' + JSON.serializePretty(responseMap));
|
|
String accessToken = String.valueOf(responseMap.get('access_token'));
|
|
System.debug('accessToken>>>:' + JSON.serializePretty(accessToken));
|
|
return accessToken;
|
|
} else {
|
|
System.debug('Failed to refresh token. Response: ' + res.getBody());
|
|
return null;
|
|
}
|
|
} catch (Exception e) {
|
|
System.debug('Exception while refreshing access token: ' + e.getMessage());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// Call Dropbox UserInfo API to fetch connected username/email
|
|
private static String fetchUserInfo(String accessToken){
|
|
try {
|
|
HttpRequest req = new HttpRequest();
|
|
req.setEndpoint('https://api.dropboxapi.com/2/users/get_current_account');
|
|
req.setMethod('POST');
|
|
req.setHeader('Authorization', 'Bearer ' + accessToken);
|
|
|
|
Http http = new Http();
|
|
HttpResponse res = http.send(req);
|
|
|
|
if(res.getStatusCode() == 200){
|
|
Map<String, Object> response1 = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
|
|
|
|
System.debug('>>>>>response1='+response1);
|
|
Map<String, Object> nameObject = (Map<String, Object>) response1.get('name');
|
|
System.debug('Name object: '+ nameObject);
|
|
String userName = (String) nameObject.get('display_name');
|
|
String email = (String) response1.get('email');
|
|
|
|
System.debug('User Email: ' + email);
|
|
System.debug('User Display Name: ' + userName);
|
|
return String.isNotBlank(userName) ? userName : email;
|
|
}
|
|
else {
|
|
System.debug('Failed to fetch Dropbox user info: ' + res.getBody());
|
|
return null;
|
|
}
|
|
} catch (Exception e){
|
|
FME_Logger.error('Error fetching user info: ' + e.getMessage(),'DropboxAuth','fetchUserInfo');
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// @AuraEnabled
|
|
// public static Map<String,Object> revokeAccessToken(String accessToken){
|
|
// Map<String,Object> revokeMap=new Map<String,Object>();
|
|
// try {
|
|
// String revokeUrl = 'https://api.dropbox.com/oauth2/revoke';
|
|
// // Create an HTTP request
|
|
// HttpRequest req = new HttpRequest();
|
|
// req.setEndpoint(revokeUrl);
|
|
// req.setMethod('POST');
|
|
|
|
// // Set the body with parameters
|
|
// String body = 'token=' + EncodingUtil.urlEncode(accessToken, 'UTF-8') +
|
|
// '&client_id=' + EncodingUtil.urlEncode(config.clientId, 'UTF-8') +
|
|
// '&client_secret=' + EncodingUtil.urlEncode(config.clientSecret, 'UTF-8');
|
|
|
|
// req.setBody(body);
|
|
// req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
|
|
|
|
// // Create an HTTP object to send the request
|
|
// Http http = new Http();
|
|
// HttpResponse res = http.send(req);
|
|
// System.debug('Response Body: ' + res.getBody());
|
|
// // Check the response status
|
|
// if (res.getStatusCode() == 200) {
|
|
// System.debug('Token revoked successfully');
|
|
// revokeMap.put('message','Token revoked successfully');
|
|
// revokeMap.put('state', (Object)'success');
|
|
// revokeMap.put('code' , (Object)200);
|
|
// } else {
|
|
// System.debug('Failed to revoke token. Status: ' + res.getStatusCode() + ' Response: ' + res.getBody());
|
|
// revokeMap.put('message','Failed to revoke token');
|
|
// revokeMap.put('state', (Object)'Failed');
|
|
// revokeMap.put('code' , (Object)500);
|
|
// }
|
|
// // }
|
|
// } catch (Exception e) {
|
|
// revokeMap.put('message','error Occured');
|
|
// revokeMap.put('state', (Object)'Error');
|
|
// revokeMap.put('code' , (Object)401);
|
|
// System.debug('>>>Error message='+e.getMessage());
|
|
// throw new AuraHandledException(e.getMessage());
|
|
|
|
// }
|
|
// return revokeMap;
|
|
// }
|
|
|
|
// public static boolean OrgSandBoxOrProduction(){
|
|
// boolean isProduction;
|
|
// try {
|
|
// List<Organization> orgRecord= [SELECT Id,TrialExpirationDate,IsSandbox,Name from Organization ];
|
|
// System.debug('>>>>>orgRecord'+orgRecord);
|
|
// if(orgRecord[0].IsSandbox==false){
|
|
// isProduction=true;
|
|
// }
|
|
// else{
|
|
// isProduction=false;
|
|
// }
|
|
// System.debug('>>>>isProduction'+isProduction);
|
|
// } catch (Exception e) {
|
|
// throw new AuraHandledException(e.getMessage());
|
|
// }
|
|
// return isProduction;
|
|
// }
|
|
|
|
|
|
} |