From 25acefd4f4f76c3a2c0d49e1369f0be54d10aa54 Mon Sep 17 00:00:00 2001 From: Tharusha Date: Tue, 8 Oct 2024 12:25:27 +0530 Subject: [PATCH] feign_client_impl | Update the requested changes in PR comments. --- .../service/TokenService.java | 45 +++++++++++-------- .../util/TokenDataHolder.java | 6 +-- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/auth_token_generator/src/main/java/io/entgra/auth_token_getter/service/TokenService.java b/auth_token_generator/src/main/java/io/entgra/auth_token_getter/service/TokenService.java index cfb1292..db6a686 100644 --- a/auth_token_generator/src/main/java/io/entgra/auth_token_getter/service/TokenService.java +++ b/auth_token_generator/src/main/java/io/entgra/auth_token_getter/service/TokenService.java @@ -60,8 +60,8 @@ public class TokenService { @Value("${refresh-token}") private String refreshToken; - @Value("${jwt-token}") - private String jwtToken; +// @Value("${jwt-token}") +// private String jwtToken; @Value("${saml2-assertion}") private String saml2Assertion; @@ -100,11 +100,6 @@ public class TokenService { switch (grantType) { - case "implicit": - return createErrorResponse(501, - "Not Implemented", - "Implicit grant type not handled directly."); - case "password": authHeader = "Basic " + encodedAuth; body = "grant_type=" + grantType + "&username=" + userName + "&password=" + password + "&scope=" + scope; @@ -120,10 +115,10 @@ public class TokenService { body = "grant_type=refresh_token&refresh_token=" + refreshToken; break; - case "urn:ietf:params:oauth:grant-type:jwt-bearer": - authHeader = "Basic " + encodedAuth; - body = "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=" + jwtToken; - break; +// case "urn:ietf:params:oauth:grant-type:jwt-bearer": +// authHeader = "Basic " + encodedAuth; +// body = "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=" + jwtToken; +// break; case "urn:ietf:params:oauth:grant-type:saml2-bearer": authHeader = "Basic " + encodedAuth; @@ -151,9 +146,9 @@ public class TokenService { if (response != null) { String accessToken = response.get("access_token"); if (accessToken != null) { - tokenDataHolder.setAccess_token(accessToken); - tokenDataHolder.setToken_type(response.get("token_type")); - tokenDataHolder.setExpires_in(Integer.parseInt(response.get("expires_in"))); + tokenDataHolder.setAccessToken(accessToken); + tokenDataHolder.setTokenType(response.get("token_type")); + tokenDataHolder.setExpiresIn(Integer.parseInt(response.get("expires_in"))); tokenDataHolder.setScope(response.get("scope")); log.info("Access token stored in the DataHolder"); return accessToken; @@ -165,10 +160,24 @@ public class TokenService { } } } catch (FeignException e) { - log.error("Error while fetching token:{}", e.getMessage()); - return createErrorResponse(500, - "Feign Client Error", - e.getMessage()); + String errorMessage = e.getMessage(); + + if (errorMessage != null && errorMessage.contains("[401]")) { + log.error("Client credentials or client secret is incorrect: {}", errorMessage); + return createErrorResponse(500, + "Client Credentials Error", + "Client credentials or client secret is incorrect."); + } else if (errorMessage.contains("Connection refused")) { + log.error("Resource server is not working: {}", errorMessage); + return createErrorResponse(500, + "Resource Server Error", + "Resource server is not working."); + } else { + log.error("Error while fetching token: {}", errorMessage); + return createErrorResponse(500, + "Feign Client Error", + errorMessage); // Return the original Feign exception message if not matched + } } catch (Exception e) { log.error("An unexpected error occurred:{}", e.getMessage()); return createErrorResponse(500, diff --git a/auth_token_generator/src/main/java/io/entgra/auth_token_getter/util/TokenDataHolder.java b/auth_token_generator/src/main/java/io/entgra/auth_token_getter/util/TokenDataHolder.java index 7b253f3..a9f6a32 100644 --- a/auth_token_generator/src/main/java/io/entgra/auth_token_getter/util/TokenDataHolder.java +++ b/auth_token_generator/src/main/java/io/entgra/auth_token_getter/util/TokenDataHolder.java @@ -25,8 +25,8 @@ import org.springframework.stereotype.Component; @Component public class TokenDataHolder { - private String access_token; + private String accessToken; private String scope; - private int expires_in; - private String token_type; + private int expiresIn; + private String tokenType; }