Skip to content

Commit

Permalink
feat(ms teams): improve MSTeams error message for refresh token (#2337)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksiivanov committed Apr 24, 2024
1 parent f229ee6 commit 7398b7e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.azure.identity.ClientSecretCredential;
import com.azure.identity.ClientSecretCredentialBuilder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.microsoft.graph.serviceclient.GraphServiceClient;
import io.camunda.connector.model.authentication.BearerAuthentication;
import io.camunda.connector.model.authentication.ClientSecretAuthentication;
Expand Down Expand Up @@ -97,18 +98,25 @@ private Request buildRequest(final RefreshTokenAuthentication authentication) {

private String getAccessToken(final Request request) {
try (Response response = okHttpClient.newCall(request).execute()) {
if (response.isSuccessful()) {
return ObjectMapperSupplier.objectMapper()
.readTree(response.body().string())
.get(ACCESS_TOKEN)
.asText();
if (response.isSuccessful() && response.body() != null) {
JsonNode jsonNode = ObjectMapperSupplier.objectMapper().readTree(response.body().string());
if (jsonNode.has(ACCESS_TOKEN)) {
return jsonNode.get(ACCESS_TOKEN).asText();
} else {
throw new RuntimeException("Access token not found in the response");
}
} else {
throw new RuntimeException(response.message());
throw new RuntimeException(
"Failed to fetch access token. Verify authentication details. "
+ "Note: Client secret is optional, depending on the client's privacy status. Status code: "
+ response.code()
+ ", message: "
+ response.message());
}
} catch (JsonProcessingException e) {
throw new RuntimeException("Error while parse refresh token response", e);
throw new RuntimeException("Error while parsing refresh token response", e);
} catch (IOException e) {
throw new RuntimeException(e);
throw new RuntimeException("Network error occurred", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ public void buildAndGetGraphServiceClient_shouldThrowExceptionWhenResponseBodyIs
RuntimeException.class,
() -> supplier.buildAndGetGraphServiceClient(authentication),
"RuntimeException was expected");
assertThat(thrown.getMessage()).contains("Error while parse refresh token response");
assertThat(thrown.getMessage()).contains("Error while parsing refresh token response");
}
}

0 comments on commit 7398b7e

Please sign in to comment.