Skip to content

Commit

Permalink
Merge pull request #142 from forcedotcom/develop
Browse files Browse the repository at this point in the history
1.19.1 release
  • Loading branch information
soaggarwal authored Apr 18, 2023
2 parents 51e72df + 9cfda7a commit 269684c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.queryService</groupId>
<artifactId>Salesforce-CDP-jdbc</artifactId>
<version>1.19.0</version>
<version>1.19.1</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -280,6 +280,13 @@
<exclude>codegen/**</exclude>
</excludes>
</filter>
<filter>
<artifact>org.slf4j:slf4j-simple</artifact>
<excludes>
<!-- Causes logging binding warnings with other java projects that use the shaded jar -->
<exclude>org/slf4j/**</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public class Constants {

// Common server information
public static final String PROD_SERVER_URL = ".salesforce.com";
public static final String NA45_SERVER_URL = "na45.test1.pc-rnd.salesforce.com";
public static final String NA46_SERVER_URL = "na46.test1.pc-rnd.salesforce.com";
public static final String TEST_SERVER_URL = "test1.pc-rnd.salesforce.com";


//Audience constants for different environments
public static final String PROD_SERVER_AUD = "login.salesforce.com";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ public static Token getToken(Properties properties, OkHttpClient client) throws
&& properties.containsKey(Constants.PRIVATE_KEY)) {
return retrieveTokenWithJWTBearerGrant(properties, client);
}
Token newToken = exchangeToken(properties.getProperty(Constants.LOGIN_URL), properties.getProperty(Constants.CORETOKEN), properties.getProperty(Constants.DATASPACE),client);
Token newToken = exchangeTokenWithRefresh(properties.getProperty(Constants.LOGIN_URL),
properties.getProperty(Constants.REFRESHTOKEN),properties.getProperty(Constants.CORETOKEN)
, properties.getProperty(Constants.CLIENT_ID),properties.getProperty(Constants.CLIENT_SECRET),properties.getProperty(Constants.DATASPACE),
client);
tokenCache.put(properties.getProperty(Constants.CORETOKEN), newToken);
return newToken;
}
Expand Down Expand Up @@ -190,7 +193,7 @@ private static Token retrieveTokenWithJWTBearerGrant(Properties properties, OkHt

private static String getAudienceForJWTAssertion(String serviceRootUrl) throws SQLException {
String serverUrl = serviceRootUrl.toLowerCase();
if (serverUrl.endsWith(Constants.NA45_SERVER_URL) || serverUrl.endsWith(Constants.NA46_SERVER_URL)) {
if (serverUrl.contains(Constants.TEST_SERVER_URL)) {
return Constants.DEV_TEST_SERVER_AUD;
} else if (serverUrl.endsWith(Constants.PROD_SERVER_URL)) {
return Constants.PROD_SERVER_AUD;
Expand Down Expand Up @@ -236,6 +239,17 @@ public static CoreTokenRenewResponse getCoreToken(String url, String refreshToke
return coreTokenRenewResponse;
}

private static Token exchangeTokenWithRefresh(String url, String refreshToken,
String coreToken, String clientId, String secret,
String dataspace,OkHttpClient client) throws TokenException {
try{
return exchangeToken(url,coreToken,dataspace,client);
} catch (TokenException e) {
log.error("Core token is not valid fetching a new token ", e);
return renewToken(url,refreshToken,clientId,secret,dataspace,client);
}
}

private static Token exchangeToken(String url, String coreToken,String dataspace, OkHttpClient client) throws TokenException {
String token_url = url + Constants.TOKEN_EXCHANGE_URL;
Map<String, String> requestBody = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,12 @@
import org.apache.http.HttpStatus;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.assertj.core.api.Assertions.catchThrowableOfType;
import static org.mockito.Mockito.*;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
Expand Down Expand Up @@ -151,7 +148,6 @@ public void testTokenExchangeWithException() throws Exception {
TokenHelper.getToken(properties, client);
}, TokenException.class);
assertThat(ex.getCause()).isInstanceOf(IOException.class);
assertThat(ex.getCause().getMessage()).contains("expired authorization code");
}

@Test
Expand Down Expand Up @@ -188,21 +184,25 @@ public void testInvalidateCoreToken() throws Exception {
request(buildRequest()).protocol(Protocol.HTTP_1_1).
message("Internal Server Error").
body(ResponseBody.create(errorString, MediaType.parse("application/json"))).build();
when(remoteCall.execute()).thenReturn(errorResponse);
when(client.newCall(any())).thenReturn(remoteCall);
Response refreshResponse = new Response.Builder().code(HttpStatus.SC_OK).
request(buildRequest()).protocol(Protocol.HTTP_1_1).
message("Internal Server Error").
body(ResponseBody.create(errorString, MediaType.parse("application/json"))).build();
when(remoteCall.execute()).thenReturn(errorResponse).thenReturn(refreshResponse);
when(client.newCall(any())).thenReturn(remoteCall).thenReturn(remoteCall);
ArgumentCaptor<Request> eventCaptor =
ArgumentCaptor.forClass(Request.class);

Throwable ex = catchThrowableOfType(() -> {
TokenHelper.getToken(properties, client);
}, TokenException.class);
assertThat(ex.getCause()).isInstanceOf(JsonParseException.class);
assertThat(ex.getMessage()).contains("Token exchange failed. Please login again");
assertThat(ex.getMessage()).contains("Failed to Renew Token. Please retry");

verify(client, times(2)).newCall(eventCaptor.capture());
verify(client, times(3)).newCall(eventCaptor.capture());
Request request = eventCaptor.getValue();
String url = request.url().toString();
Assert.assertTrue(url.contains(Constants.TOKEN_REVOKE_URL));
Assert.assertTrue(url.contains(Constants.CORE_TOKEN_URL));
}

private Request buildRequest() {
Expand Down

0 comments on commit 269684c

Please sign in to comment.