feign_client_impl | error handling with other grant types.

remotes/1732343805045002592/master^2
Tharusha Sandeepa 2 months ago
parent 77ceab7a88
commit 46d0a90f01

@ -0,0 +1,19 @@
package io.entgra.auth_token_getter.exceptions;
import lombok.Data;
@Data
public class ErrorResponse {
private int code;
private String description;
private String message;
public static ErrorResponse createErrorResponse(int code, String message, String description) {
ErrorResponse errorResponse = new ErrorResponse();
errorResponse.setCode(code);
errorResponse.setMessage(message);
errorResponse.setDescription(description);
return errorResponse;
}
}

@ -12,11 +12,12 @@ import java.nio.charset.StandardCharsets;
import java.util.Base64; import java.util.Base64;
import java.util.Map; import java.util.Map;
import static io.entgra.auth_token_getter.exceptions.ErrorResponse.createErrorResponse;
@Slf4j @Slf4j
@Component @Component
public class TokenService { public class TokenService {
@Autowired @Autowired
private AuthFeignClient authFeignClient; private AuthFeignClient authFeignClient;
@ -24,37 +25,111 @@ public class TokenService {
private TokenDataHolder tokenDataHolder; private TokenDataHolder tokenDataHolder;
@Value("${client-id}") @Value("${client-id}")
String clientId; private String clientId;
@Value("${client-secret}") @Value("${client-secret}")
String clientSecret; private String clientSecret;
@Value("${grant-type}") @Value("${grant-type}")
String grantType; private String grantType;
@Value("${user-name}")
private String userName;
@Value("${password}")
private String password;
@Value("${refresh-token}")
private String refreshToken;
@Value("${jwt-token}")
private String jwtToken;
@Value("${saml2-assertion}")
private String saml2Assertion;
// Method to fetch the token and store it in TokenDataHolder // Method to fetch the token and store it in TokenDataHolder
public String fetchToken(String scope) { public Object fetchToken(String scope) {
// Check if scope is null or empty
if (clientId == null || clientId.isEmpty()) {
return createErrorResponse(400,
"Missing client ID",
"clientId is not configured.");
}
if (clientSecret == null || clientSecret.isEmpty()) {
return createErrorResponse(400,
"Missing client secret",
"clientSecret is not configured.");
}
if (grantType == null || grantType.isEmpty()) {
return createErrorResponse(400,
"Missing grant type",
"grantType is not configured.");
}
if (scope == null || scope.isEmpty()) { if (scope == null || scope.isEmpty()) {
log.error("Scope is null or empty, cannot fetch token."); return createErrorResponse(400,
return null; "Missing scope",
"Scope is required to fetch the token.");
} }
// Create Authorization header (Basic auth)
String auth = clientId + ":" + clientSecret; String auth = clientId + ":" + clientSecret;
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8)); String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
String authHeader = "Basic " + encodedAuth; String authHeader;
String body = "grant_type=" + grantType + "&scope=" + scope + "&username = admin" + "&password = admin"; String body;
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;
break;
case "client_credentials":
authHeader = "Basic " + encodedAuth;
body = "grant_type=client_credentials&scope=" + scope;
break;
case "refresh_token":
authHeader = "Basic " + encodedAuth;
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:saml2-bearer":
authHeader = "Basic " + encodedAuth;
body = "grant_type=urn:ietf:params:oauth:grant-type:saml2-bearer&assertion=" + saml2Assertion;
break;
case "iwa-ntlm":
return createErrorResponse(501,
"Not Implemented",
"IWA-NTLM grant type not handled directly.");
default:
return createErrorResponse(400,
"Invalid grant type",
"Unsupported grant type: " + grantType);
}
// Declare the response map outside the try block
Map<String, String> response; Map<String, String> response;
try { try {
// Call FeignClient to generate the token // Call FeignClient to generate the token
response = authFeignClient.getToken(body, authHeader); response = authFeignClient.getToken(body, authHeader);
log.info("Request sent to oauth2 to get the token"); log.info("Request sent to OAuth2 server to get the token");
// Store the token in TokenDataHolder
if (response != null) { if (response != null) {
String accessToken = response.get("access_token"); String accessToken = response.get("access_token");
if (accessToken != null) { if (accessToken != null) {
@ -66,14 +141,26 @@ public class TokenService {
return accessToken; return accessToken;
} else { } else {
log.error("Access token not found in response"); log.error("Access token not found in response");
return createErrorResponse(500,
"Token Error",
"Access token not found in the response.");
} }
} }
} catch (FeignException e) { } catch (FeignException e) {
log.error("Error while fetching token: " + e.getMessage()); log.error("Error while fetching token:{}",e.getMessage());
return createErrorResponse(500,
"Feign Client Error",
e.getMessage());
} catch (Exception e) { } catch (Exception e) {
log.error("An unexpected error occurred: " + e.getMessage()); log.error("An unexpected error occurred:{}",e.getMessage());
return createErrorResponse(500,
"Unexpected Error",
e.getMessage());
} }
return null;
return createErrorResponse(500,
"Unknown Error",
"Failed to fetch the token for unknown reasons.");
} }
} }

@ -1,11 +1,11 @@
package io.entgra.auth_token_getter.util; package io.entgra.auth_token_getter.util;
import lombok.Data;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Setter @Data
@Getter
@Component @Component
public class TokenDataHolder { public class TokenDataHolder {

@ -3,4 +3,9 @@ auth_token_generation_uri=https://mgt.sg.local/
client-id=AtczeBBwunLMt7Ol4Xc7eNDEuXsa client-id=AtczeBBwunLMt7Ol4Xc7eNDEuXsa
client-secret=DX8dGTmN7elF3zQsSfbH4yjVm08a client-secret=DX8dGTmN7elF3zQsSfbH4yjVm08a
grant-type=client_credentials grant-type=client_credentials
#grant-type=password
refresh-token=""
jwt-token =""
saml2-assertion=""
user-name =admin
password = admin

Loading…
Cancel
Save