From 14064d1728c015e63fdfb53263fb9dd61bc422ed Mon Sep 17 00:00:00 2001 From: Mahesh Maney R <mahesh-maney@users.noreply.github.com> Date: Tue, 29 Aug 2023 18:10:44 +0530 Subject: [PATCH] adding an API to fetch user info by email attribute <ManeyMR>. --- .../controller/UserController.java | 5 ++++ .../userManagement/handler/UserHandler.java | 6 +++++ .../utility/KeycloakUserGetter.java | 25 +++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/src/main/java/com/tarento/upsmf/userManagement/controller/UserController.java b/src/main/java/com/tarento/upsmf/userManagement/controller/UserController.java index aef379d..4b5f006 100644 --- a/src/main/java/com/tarento/upsmf/userManagement/controller/UserController.java +++ b/src/main/java/com/tarento/upsmf/userManagement/controller/UserController.java @@ -37,6 +37,11 @@ public class UserController { return userHandler.userDetails(body); } + @PostMapping(value = "/emaildetails", consumes = "application/json", produces = "application/json") + public String userEmailDetails(@RequestBody final JsonNode body) throws URISyntaxException, IOException { + return userHandler.userEmailDetails(body); + } + @PostMapping(value = "/list", consumes = "application/json", produces = "application/json") public String listUser(@RequestBody final JsonNode body) throws URISyntaxException, IOException { return userHandler.listUser(body); diff --git a/src/main/java/com/tarento/upsmf/userManagement/handler/UserHandler.java b/src/main/java/com/tarento/upsmf/userManagement/handler/UserHandler.java index 66155e9..d842b6b 100644 --- a/src/main/java/com/tarento/upsmf/userManagement/handler/UserHandler.java +++ b/src/main/java/com/tarento/upsmf/userManagement/handler/UserHandler.java @@ -68,6 +68,12 @@ public class UserHandler { return userName; } + public String userEmailDetails(final JsonNode body) throws IOException { + JsonNode request = body.get("request"); + String userName = keycloakUserGetter.findUserByEmail(request.get("fieldName").asText(), request.get("fieldValue").asText()); + return userName; + } + public String listUser(final JsonNode body) throws URISyntaxException, IOException { logger.info("creating user with payload {} ", body.toPrettyString()); diff --git a/src/main/java/com/tarento/upsmf/userManagement/utility/KeycloakUserGetter.java b/src/main/java/com/tarento/upsmf/userManagement/utility/KeycloakUserGetter.java index 30b2aff..60e5344 100644 --- a/src/main/java/com/tarento/upsmf/userManagement/utility/KeycloakUserGetter.java +++ b/src/main/java/com/tarento/upsmf/userManagement/utility/KeycloakUserGetter.java @@ -68,4 +68,29 @@ public class KeycloakUserGetter { logger.info("ResponseBody {}", responseBody); return responseBody; } + + public String findUserByEmail(final String fieldName, final String fieldValue) throws IOException { + String userEndpoint = KEYCLOAK_USER_BASE_URL; + logger.info("userEndpoint: " ,userEndpoint); + if(fieldName != null && fieldValue!= null ) { + userEndpoint = userEndpoint + "?" + fieldName + "=" + fieldValue; + logger.info("userEndpoint {} after adding email : " ,userEndpoint); + JsonNode adminToken = keycloakTokenRetriever.getAdminToken(); + logger.info("adminToken: " ,adminToken); + String accessToken = adminToken.get("access_token").asText(); + logger.info("accessToken: " ,accessToken); + + HttpClient httpClient = HttpClients.createDefault(); + HttpGet httpGet = new HttpGet(userEndpoint); + + httpGet.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken); + httpGet.setHeader(HttpHeaders.ACCEPT, "application/json"); + + org.apache.http.HttpResponse response = httpClient.execute(httpGet); + String responseBody = EntityUtils.toString(response.getEntity()); + logger.info("ResponseBody {}", responseBody); + return responseBody; + } + return "No Response Generated since the inputs were null/empty."; + } } -- GitLab