180 lines
8.9 KiB
OpenEdge ABL
180 lines
8.9 KiB
OpenEdge ABL
public with sharing class GoogleDriveAuth extends BaseStorageAuth {
|
|
private static final String PROVIDER_TYPE = 'Google Drive';
|
|
|
|
// Commenting out: didn't find any use case or call
|
|
// public override String initiateAuth(String externalStorage){
|
|
// try{
|
|
|
|
// StorageProviderConfigUtil.Config config = StorageProviderConfigUtil.getConfig(PROVIDER_TYPE);
|
|
|
|
// String clientId = EncodingUtil.urlEncode(config.clientId, 'UTF-8');
|
|
// //openid email profile is used to get the name of the connected google account
|
|
// String rawScope = config.scope + ' openid email profile';
|
|
// String scope = EncodingUtil.urlEncode(rawScope, 'UTF-8');
|
|
|
|
// String redirectUri = EncodingUtil.urlEncode(config.redirectUri, 'UTF-8');
|
|
|
|
// boolean isProduction = orgSandBoxOrProduction();
|
|
// if(!isProduction){
|
|
// redirectUri = redirectUri.replace('login.salesforce.com', 'test.salesforce.com');
|
|
// }
|
|
|
|
// String authUrl = config.authEndpoint +
|
|
// '?client_id=' + clientId +
|
|
// '&redirect_uri=' + redirectUri +
|
|
// '&scope=' + scope +
|
|
// '&access_type=offline&response_type=code';
|
|
// return authUrl;
|
|
// } Catch(Exception e){
|
|
// FME_Logger.info('Error while initiating Google Drive authentication: ' + e.getMessage(),'GoogleDriveAuth', 'initiateAuth');
|
|
// return null;
|
|
// } finally {
|
|
// FME_Logger.saveLog();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
public override void getAccessToken(String code, String storage, Boolean isAdminCred, String userId){
|
|
try {
|
|
// add
|
|
// Boolean isAdminCred = Boolean.valueOf(isAdminCredStr);
|
|
boolean isProduction = orgSandBoxOrProduction();
|
|
StorageProviderConfigUtil.Config config = StorageProviderConfigUtil.getConfig(PROVIDER_TYPE);
|
|
|
|
String redirectUri = config.redirectUri;
|
|
if(!isProduction){
|
|
redirectUri = redirectUri.replace('login.salesforce.com', 'test.salesforce.com');
|
|
}
|
|
|
|
HttpRequest req = new HttpRequest();
|
|
req.setMethod('POST');
|
|
req.setEndpoint(config.tokenEndpoint);
|
|
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
|
|
|
|
String body = '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';
|
|
req.setBody(body);
|
|
|
|
Http http = new Http();
|
|
HttpResponse response = http.send(req);
|
|
|
|
system.debug('refreshAccesstoken res>>>'+JSON.serializePretty(response.getBody()));
|
|
if(response.getStatusCode() == 200){
|
|
Map<String, Object> responseMap = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
|
|
|
|
String accessToken = (String) responseMap.get('access_token');
|
|
String refreshToken = (String) responseMap.get('refresh_token');
|
|
|
|
// Call Google UserInfo API to fetch connected username/email
|
|
// String connectedUsername = fetchGoogleUserInfo(accessToken);
|
|
String connectedUsername = '';
|
|
Map<String,Object> info = fetchGoogleUserInfo(accessToken);
|
|
if(info.containsKey('email')){
|
|
connectedUsername = (String) info.get('email');
|
|
}
|
|
|
|
|
|
Map<String, Object> tokenMap = new Map<String, Object>();
|
|
tokenMap.put('HIC_FilesME__Auth_Provider_Name__c', 'Google');
|
|
tokenMap.put('HIC_FilesME__Provider__c', storage);
|
|
tokenMap.put('HIC_FilesME__Refresh_Token__c', refreshToken);
|
|
tokenMap.put('HIC_FilesME__Access_Token__c', accessToken);
|
|
tokenMap.put('HIC_FilesME__User__c', userId);
|
|
tokenMap.put('HIC_FilesME__Is_Admin_level_Credentials__c', isAdminCred);
|
|
tokenMap.put('HIC_FilesME__API_Endpoint_URL__c', config.tokenEndpoint);
|
|
tokenMap.put('HIC_FilesME__Connected_Username__c', connectedUsername);
|
|
|
|
System.debug('Storing token');
|
|
OAuthTokenManager.storeToken(tokenMap);
|
|
} else {
|
|
throw new AuraHandledException('Failed to get access token. Status: ' + response.getStatusCode() + ', Body: ' + response.getBody());
|
|
}
|
|
} catch (Exception e){
|
|
// throw new AuraHandledException('Exception in getAccessToken(): ' + e.getMessage());
|
|
FME_Logger.error(e.getMessage(), 'GoogleDriveAuth' + e.getLineNumber(), 'getAccessToken');
|
|
|
|
}
|
|
}
|
|
|
|
// Call Google UserInfo API to fetch connected username/email
|
|
private static Map<String,Object> fetchGoogleUserInfo(String accessToken){
|
|
Map<String, Object> info = new Map<String, Object>();
|
|
try {
|
|
HttpRequest req = new HttpRequest();
|
|
req.setEndpoint('https://openidconnect.googleapis.com/v1/userinfo');
|
|
req.setMethod('GET');
|
|
req.setHeader('Authorization', 'Bearer ' + accessToken);
|
|
|
|
Http http = new Http();
|
|
HttpResponse res = http.send(req);
|
|
|
|
if(res.getStatusCode() == 200){
|
|
Map<String, Object> userInfo = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
|
|
String name = (String) userInfo.get('name');
|
|
String email = (String) userInfo.get('email');
|
|
info.put('name', name);
|
|
info.put('email', email);
|
|
return info;
|
|
// return String.isNotBlank(name) ? name : email;
|
|
} else {
|
|
System.debug('Failed to fetch Google user info: ' + res.getBody());
|
|
return info;
|
|
}
|
|
} catch (Exception e){
|
|
// System.debug('Error fetching Google user info: ' + e.getMessage());
|
|
FME_Logger.error('Error fetching Google user info: ' + e.getMessage(),'GoogleDriveAuth','fetchGoogleUserInfo');
|
|
return info;
|
|
}
|
|
}
|
|
|
|
|
|
public override String refreshAccessToken(String refreshToken){
|
|
StorageProviderConfigUtil.Config config = StorageProviderConfigUtil.getConfig(PROVIDER_TYPE);
|
|
Http http = new Http();
|
|
HttpRequest req = new HttpRequest();
|
|
req.setEndpoint('https://www.googleapis.com/oauth2/v4/token');
|
|
req.setMethod('POST');
|
|
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
|
|
|
|
String body = 'client_id=' + EncodingUtil.urlEncode(config.clientId, 'UTF-8') +
|
|
'&client_secret=' + EncodingUtil.urlEncode(config.clientSecret, 'UTF-8') +
|
|
'&refresh_token=' + EncodingUtil.urlEncode(refreshToken, 'UTF-8') +
|
|
'&grant_type=refresh_token';
|
|
req.setBody(body);
|
|
|
|
try {
|
|
HttpResponse res = http.send(req);
|
|
if(res.getStatusCode() == 200){
|
|
Map<String, Object> responseMap = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
|
|
return (String) responseMap.get('access_token');
|
|
}
|
|
} catch (Exception e){
|
|
// System.debug('Error refreshing token: ' + e.getMessage());
|
|
FME_Logger.error('Error refreshing token: ' + e.getMessage(),'GoogleDriveAuth','refreshAccessToken');
|
|
} finally {
|
|
FME_Logger.saveLog();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// public override void revokeAccessToken(String accessToken) {
|
|
// // tobe implemented ...
|
|
// }
|
|
|
|
public static boolean orgSandBoxOrProduction(){
|
|
try {
|
|
List<Organization> orgRecord = [
|
|
SELECT Id, TrialExpirationDate, IsSandbox, Name
|
|
FROM Organization WITH USER_MODE LIMIT 1
|
|
];
|
|
return !orgRecord[0].IsSandbox;
|
|
} catch (Exception e){
|
|
FME_Logger.error('Error determining org type: ' + e.getMessage(), 'GoogleDriveAuth', 'orgSandBoxOrProduction');
|
|
return true; // default to production if error
|
|
}
|
|
}
|
|
} |