Updated code for retry authentication for login to odessa.
This commit is contained in:
@@ -430,77 +430,85 @@ public class AppointmentDao {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private HubEntity authenticateAndSaveToken(HubEntity hub) {
|
private HubEntity authenticateAndSaveToken(HubEntity hub) {
|
||||||
|
int maxRetries = 3;
|
||||||
try {
|
int attempt = 0;
|
||||||
//code to generate token with payload having "iat" epoch timestamp and secret key with no expiry and send in below method call
|
boolean success = false;
|
||||||
String authJwtToken = Utils.generateAuthTokenForLoginToOdessa();
|
while (attempt < maxRetries && !success) {
|
||||||
log.info("Got the auth for login to odessa {}", authJwtToken);
|
|
||||||
hub.setAuthToken(authJwtToken);
|
|
||||||
hubRepository.save(hub);
|
|
||||||
// Prepare the request body (adjust if necessary for login API)
|
|
||||||
Map<String, Object> body = Collections.emptyMap();
|
|
||||||
// Perform login API call
|
|
||||||
ResponseEntity<Object> responseLogin = appointmentApiService.loginWithOdessa(authJwtToken, source, context, user, password, body);
|
|
||||||
|
|
||||||
// Handle successful login
|
|
||||||
if (responseLogin.getStatusCode() == HttpStatus.OK) {
|
|
||||||
log.info("Login successful to odessa. Parsing response.");
|
|
||||||
String loginResponseJson = Utils.convertObjectToJson(responseLogin.getBody());
|
|
||||||
AppointmentLoginResponse parsedResponse = parseLoginResponse(loginResponseJson);
|
|
||||||
|
|
||||||
// Validate and save token
|
|
||||||
if (parsedResponse.getTokenId() != null) {
|
|
||||||
hub.setAppointmentAuthTokenId(parsedResponse.getTokenId());
|
|
||||||
hub.setAreaCode(parsedResponse.getAreaCode());
|
|
||||||
hubRepository.save(hub);
|
|
||||||
|
|
||||||
log.info("Saved new authToken and areaCode for Hub.");
|
|
||||||
return hub;
|
|
||||||
} else {
|
|
||||||
throw new RuntimeException("Login response is missing a valid tokenId for login to odessa system, please try again.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Handle non-OK response
|
|
||||||
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.ERROR_IN_GENERATING_NDG_TRY_AGAIN));
|
|
||||||
} catch (FeignException.Forbidden forbiddenException) {
|
|
||||||
logForbiddenError();
|
|
||||||
|
|
||||||
// Extract raw response body
|
|
||||||
String responseBody = forbiddenException.contentUTF8(); // Extract raw JSON response
|
|
||||||
|
|
||||||
// Parse JSON to check for "PasswordExpired"
|
|
||||||
try {
|
try {
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
//code to generate token with payload having "iat" epoch timestamp and secret key with no expiry and send in below method call
|
||||||
JsonNode rootNode = objectMapper.readTree(responseBody);
|
String authJwtToken = Utils.generateAuthTokenForLoginToOdessa();
|
||||||
JsonNode errorsNode = rootNode.path("errors");
|
log.info("Got the auth for login to odessa {}", authJwtToken);
|
||||||
|
hub.setAuthToken(authJwtToken);
|
||||||
|
hubRepository.save(hub);
|
||||||
|
// Prepare the request body (adjust if necessary for login API)
|
||||||
|
Map<String, Object> body = Collections.emptyMap();
|
||||||
|
// Perform login API call
|
||||||
|
ResponseEntity<Object> responseLogin = appointmentApiService.loginWithOdessa(authJwtToken, source, context, user, password, body);
|
||||||
|
|
||||||
if (errorsNode.isArray()) {
|
// Handle successful login
|
||||||
for (JsonNode error : errorsNode) {
|
if (responseLogin.getStatusCode() == HttpStatus.OK) {
|
||||||
// Check the main errorCode
|
log.info("Login successful to odessa. Parsing response.");
|
||||||
if (GepafinConstant.PASSWORD_EXPIRED.equals(error.path("errorCode").asText())) {
|
String loginResponseJson = Utils.convertObjectToJson(responseLogin.getBody());
|
||||||
throw new CustomValidationException(Status.FORBIDDEN, Translator.toLocale(GepafinConstant.PASSWORD_EXPIRED_LOGIN_TO_ODESSA));
|
AppointmentLoginResponse parsedResponse = parseLoginResponse(loginResponseJson);
|
||||||
}
|
|
||||||
|
|
||||||
// Check inside "subErrors"
|
// Validate and save token
|
||||||
JsonNode subErrorsNode = error.path("subErrors");
|
if (parsedResponse.getTokenId() != null) {
|
||||||
if (subErrorsNode.isArray()) {
|
hub.setAppointmentAuthTokenId(parsedResponse.getTokenId());
|
||||||
for (JsonNode subError : subErrorsNode) {
|
hub.setAreaCode(parsedResponse.getAreaCode());
|
||||||
if (GepafinConstant.PASSWORD_EXPIRED.equals(subError.path("errorCode").asText())) {
|
hubRepository.save(hub);
|
||||||
throw new CustomValidationException(Status.FORBIDDEN, Translator.toLocale(GepafinConstant.PASSWORD_EXPIRED_LOGIN_TO_ODESSA));
|
|
||||||
|
log.info("Saved new authToken and areaCode for Hub.");
|
||||||
|
success = true;
|
||||||
|
return hub;
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("Login response is missing a valid tokenId for login to odessa system, please try again.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Handle non-OK response
|
||||||
|
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.ERROR_IN_GENERATING_NDG_TRY_AGAIN));
|
||||||
|
} catch (FeignException.Forbidden forbiddenException) {
|
||||||
|
attempt++;
|
||||||
|
log.error("Failed to login to odessa due to some error occurred.");
|
||||||
|
|
||||||
|
// Extract raw response body
|
||||||
|
String responseBody = forbiddenException.contentUTF8(); // Extract raw JSON response
|
||||||
|
|
||||||
|
// Parse JSON to check for "PasswordExpired"
|
||||||
|
try {
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
JsonNode rootNode = objectMapper.readTree(responseBody);
|
||||||
|
JsonNode errorsNode = rootNode.path("errors");
|
||||||
|
|
||||||
|
if (errorsNode.isArray()) {
|
||||||
|
for (JsonNode error : errorsNode) {
|
||||||
|
// Check the main errorCode
|
||||||
|
if (GepafinConstant.PASSWORD_EXPIRED.equals(error.path("errorCode").asText())) {
|
||||||
|
throw new CustomValidationException(Status.FORBIDDEN, Translator.toLocale(GepafinConstant.PASSWORD_EXPIRED_LOGIN_TO_ODESSA));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check inside "subErrors"
|
||||||
|
JsonNode subErrorsNode = error.path("subErrors");
|
||||||
|
if (subErrorsNode.isArray()) {
|
||||||
|
for (JsonNode subError : subErrorsNode) {
|
||||||
|
if (GepafinConstant.PASSWORD_EXPIRED.equals(subError.path("errorCode").asText())) {
|
||||||
|
throw new CustomValidationException(Status.FORBIDDEN, Translator.toLocale(GepafinConstant.PASSWORD_EXPIRED_LOGIN_TO_ODESSA));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("Error parsing JSON response: {}", e.getMessage());
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
if (attempt >= maxRetries) {
|
||||||
log.error("Error parsing JSON response: {}", e.getMessage());
|
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale("Maximum retry attempts reached while trying to login to Odessa."));
|
||||||
|
} else {
|
||||||
|
regenerateTokenAndSave(hub);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Failed to authenticate user on Odessa : {}", e.getMessage(), e);
|
||||||
|
throw new RuntimeException("Authentication failed on Odessa. try again", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Regenerate the token and retry
|
|
||||||
regenerateTokenAndSave(hub);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Failed to authenticate user on Odessa : {}", e.getMessage(), e);
|
|
||||||
throw new RuntimeException("Authentication failed on Odessa. try again", e);
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user