From eb26587068a9a8bcce5003730a4d44f04f495478 Mon Sep 17 00:00:00 2001 From: Dennis Date: Wed, 26 Jul 2023 10:06:07 -0400 Subject: [PATCH 1/4] WIP --- .../dsm/route/AuthenticationRoute.java | 106 ++++++++++-------- .../dsm/route/KitDiscardRoute.java | 3 +- .../dsm/security/Auth0Util.java | 6 +- .../org/broadinstitute/dsm/util/UserUtil.java | 5 +- 4 files changed, 67 insertions(+), 53 deletions(-) diff --git a/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/route/AuthenticationRoute.java b/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/route/AuthenticationRoute.java index abc2a7da9e..5924a0fa86 100644 --- a/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/route/AuthenticationRoute.java +++ b/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/route/AuthenticationRoute.java @@ -8,6 +8,7 @@ import com.google.gson.Gson; import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; import com.google.gson.JsonParser; import com.google.gson.JsonSyntaxException; import lombok.NonNull; @@ -17,6 +18,8 @@ import org.broadinstitute.dsm.db.dao.user.UserDao; import org.broadinstitute.dsm.db.dto.user.UserDto; import org.broadinstitute.dsm.exception.AuthenticationException; +import org.broadinstitute.dsm.exception.DSMBadRequestException; +import org.broadinstitute.dsm.exception.DsmInternalError; import org.broadinstitute.dsm.util.UserUtil; import org.broadinstitute.dsm.security.Auth0Util; import org.slf4j.Logger; @@ -62,57 +65,64 @@ public Object handle(Request request, Response response) { try { JsonObject jsonObject = JsonParser.parseString(request.body()).getAsJsonObject(); String auth0Token = jsonObject.get(payloadToken).getAsString(); - if (StringUtils.isNotBlank(auth0Token)) { - // checking if Auth0 knows that token - try { - Auth0Util.Auth0UserInfo auth0UserInfo = auth0Util.getAuth0UserInfo(auth0Token, auth0Domain); - if (auth0UserInfo != null) { - String email = auth0UserInfo.getEmail(); - logger.info("User (" + email + ") was found "); - Gson gson = new Gson(); - Map claims = new HashMap<>(); - UserDao userDao = new UserDao(); - UserDto userDto = - userDao.getUserByEmail(email).orElseThrow(() -> new RuntimeException("User " + email + " not found!")); - if (userDto == null) { - userUtil.insertUser(email, email); - userDto = userDao.getUserByEmail(email) - .orElseThrow(() -> new RuntimeException("new inserted user " + email + " not found!")); - claims.put(userAccessRoles, "user needs roles and groups"); - } else { - String userSetting = gson.toJson(userUtil.getUserAccessRoles(email), ArrayList.class); - claims.put(userAccessRoles, userSetting); - logger.info(userSetting); - claims.put(userSettings, gson.toJson(UserSettings.getUserSettings(email), UserSettings.class)); - } - claims.put(authUserId, String.valueOf(userDto.getId())); - claims.put(authUserName, userDto.getName().orElse("")); - claims.put(authUserEmail, email); - - try { - String dsmToken = auth0Util.getNewAuth0TokenWithCustomClaims(claims, clientSecret, auth0ClientId, auth0Domain, - auth0MgmntAudience, audienceNameSpace); - if (dsmToken != null) { - return new DSMToken(dsmToken); - } else { - haltWithErrorMsg(401, response, "DSMToken was null! Not authorized user"); - } - } catch (AuthenticationException e) { - haltWithErrorMsg(401, response, "DSMToken was null! Not authorized user", e); - } - } else { - haltWithErrorMsg(400, response, "user was null"); - } - } catch (AuthenticationException e) { - haltWithErrorMsg(400, response, "Problem getting user info from Auth0 token", e); - } - } else { + if (StringUtils.isBlank(auth0Token)) { haltWithErrorMsg(400, response, "There was no token in the payload"); } - } catch (JsonSyntaxException e) { - haltWithErrorMsg(400, response, "The provided JSON in the request was malformed", e); + + // checking if Auth0 knows that token + try { + + String dsmToken = updateToken(auth0Token); + return new DSMToken(dsmToken); + } catch (AuthenticationException e) { + haltWithErrorMsg(401, response, "DSMToken was null! Not authorized user", e); + } + } catch (AuthenticationException e) { + haltWithErrorMsg(400, response, "Problem getting user info from Auth0 token", e); + } + + return response; + } + + private String updateToken(String auth0Token) { + try { + Auth0Util.Auth0UserInfo auth0UserInfo = auth0Util.getAuth0UserInfo(auth0Token, auth0Domain); + String email = auth0UserInfo.getEmail(); + UserDao userDao = new UserDao(); + UserDto userDto = userDao.getUserByEmail(email).orElseThrow(() -> + new DSMBadRequestException("User not found: " + email)); + + Map claims = updateClaims(userDto); + + String dsmToken = auth0Util.getNewAuth0TokenWithCustomClaims(claims, clientSecret, auth0ClientId, auth0Domain, + auth0MgmntAudience, audienceNameSpace); + if (dsmToken == null) { + throw new DsmInternalError("Assert: Auth token should not be null"); + } + return dsmToken; + } catch (AuthenticationException e) { + haltWithErrorMsg(401, response, "DSMToken was null! Not authorized user", e); + } + } catch (AuthenticationException e) { + haltWithErrorMsg(400, response, "Problem getting user info from Auth0 token", e); + } + } + + private Map updateClaims(UserDto userDto) { + Map claims = new HashMap<>(); + try { + Gson gson = new Gson(); + String email = userDto.getEmail().orElseThrow(() -> new DsmInternalError("User email cannot be null")); + String userSetting = gson.toJson(userUtil.getUserAccessRoles(email), ArrayList.class); + claims.put(userAccessRoles, userSetting); + claims.put(userSettings, gson.toJson(UserSettings.getUserSettings(email), UserSettings.class)); + claims.put(authUserId, String.valueOf(userDto.getId())); + claims.put(authUserName, userDto.getName().orElse("")); + claims.put(authUserEmail, email); + } catch (JsonParseException e) { + throw new DsmInternalError("Error converting class to JSON", e); } - return response; + return claims; } private static class DSMToken { diff --git a/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/route/KitDiscardRoute.java b/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/route/KitDiscardRoute.java index b5c65e4317..9472a21805 100644 --- a/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/route/KitDiscardRoute.java +++ b/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/route/KitDiscardRoute.java @@ -4,6 +4,7 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.ArrayList; +import java.util.List; import com.google.gson.Gson; import lombok.NonNull; @@ -129,7 +130,7 @@ public Object processRequest(Request request, Response response, String userId) String email = auth0UserInfo.getEmail(); UserDto userDto = new UserDao().getUserByEmail(email).orElseThrow(); if (userDto != null && userDto.getId() > 0) { - ArrayList userSetting = userUtil.getUserAccessRoles(email); + List userSetting = userUtil.getUserAccessRoles(email); if (userSetting.contains(DBConstants.KIT_SHIPPING) || userSetting.contains(DBConstants.DISCARD_SAMPLE)) { KitDiscard kit = KitDiscard.getKitDiscard(kitAction.getKitDiscardId()); if (kit.getChangedById() != userDto.getId()) { diff --git a/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/security/Auth0Util.java b/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/security/Auth0Util.java index 599809c46d..cf1a5a165f 100755 --- a/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/security/Auth0Util.java +++ b/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/security/Auth0Util.java @@ -29,6 +29,8 @@ import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.broadinstitute.dsm.exception.AuthenticationException; +import org.broadinstitute.dsm.exception.DSMBadRequestException; +import org.broadinstitute.dsm.exception.DsmInternalError; import org.broadinstitute.dsm.model.auth0.Auth0M2MResponse; import org.broadinstitute.dsm.util.DDPRequestUtil; import org.slf4j.Logger; @@ -104,7 +106,7 @@ private String findUserConnection(List list) { } if (connection == null) { - throw new RuntimeException("User does not have an approved connection."); + throw new DSMBadRequestException("User does not have an approved connection."); } return connection; } @@ -116,7 +118,7 @@ private void verifyUserConnection(@NonNull String userId, @NonNull String email) User user = userRequest.execute(); findUserConnection(user.getIdentities()); } catch (Exception ex) { - throw new RuntimeException("User connection verification failed for user " + email, ex); + throw new DsmInternalError("User connection verification failed for user " + email, ex); } } diff --git a/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/util/UserUtil.java b/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/util/UserUtil.java index 14d0d42d53..27183f7a8c 100644 --- a/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/util/UserUtil.java +++ b/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/util/UserUtil.java @@ -18,6 +18,7 @@ import org.broadinstitute.dsm.db.UserSettings; import org.broadinstitute.dsm.db.dao.user.UserDao; import org.broadinstitute.dsm.db.dto.user.UserDto; +import org.broadinstitute.dsm.exception.DsmInternalError; import org.broadinstitute.dsm.model.NameValue; import org.broadinstitute.dsm.model.patch.Patch; import org.broadinstitute.dsm.statics.ApplicationConfigConstants; @@ -329,7 +330,7 @@ public static boolean checkKitShippingAccessForPatch(String realm, String userId && DBConstants.DDP_KIT_ALIAS.equals(patch.getTableAlias()); } - public ArrayList getUserAccessRoles(@NonNull String email) { + public List getUserAccessRoles(@NonNull String email) { ArrayList roles = new ArrayList<>(); SimpleResult results = inTransaction((conn) -> { SimpleResult dbVals = new SimpleResult(); @@ -347,7 +348,7 @@ public ArrayList getUserAccessRoles(@NonNull String email) { }); if (results.resultException != null) { - throw new RuntimeException("Error getting list of roles ", results.resultException); + throw new DsmInternalError("Error getting roles for " + email, results.resultException); } return roles; } From c69d626c75db058895dec0627ab0f3560d82ef1b Mon Sep 17 00:00:00 2001 From: Dennis Date: Wed, 26 Jul 2023 15:30:46 -0400 Subject: [PATCH 2/4] WIP --- .../org/broadinstitute/dsm/DSMServer.java | 2 + .../dsm/route/AuthenticationRoute.java | 61 ++++++++----------- .../dsm/security/Auth0Util.java | 9 +-- 3 files changed, 31 insertions(+), 41 deletions(-) diff --git a/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/DSMServer.java b/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/DSMServer.java index d49500010f..415e44a199 100644 --- a/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/DSMServer.java +++ b/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/DSMServer.java @@ -1060,6 +1060,8 @@ private void setupRouteGenericErrorHandlers() { response.body(exception.getMessage()); }); exception(DsmInternalError.class, (exception, request, response) -> { + logger.error("Internal error {}", exception.toString()); + exception.printStackTrace(); response.status(500); response.body(exception.getMessage()); }); diff --git a/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/route/AuthenticationRoute.java b/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/route/AuthenticationRoute.java index 5924a0fa86..04ad161fa1 100644 --- a/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/route/AuthenticationRoute.java +++ b/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/route/AuthenticationRoute.java @@ -61,51 +61,38 @@ public AuthenticationRoute(@NonNull Auth0Util auth0Util, @NonNull UserUtil userU @Override public Object handle(Request request, Response response) { - logger.info("Check user..."); try { JsonObject jsonObject = JsonParser.parseString(request.body()).getAsJsonObject(); String auth0Token = jsonObject.get(payloadToken).getAsString(); if (StringUtils.isBlank(auth0Token)) { - haltWithErrorMsg(400, response, "There was no token in the payload"); - } - - // checking if Auth0 knows that token - try { - - String dsmToken = updateToken(auth0Token); - return new DSMToken(dsmToken); - } catch (AuthenticationException e) { - haltWithErrorMsg(401, response, "DSMToken was null! Not authorized user", e); + haltWithErrorMsg(400, response, "There was no Auth0 token in the payload"); } + return new DSMToken(updateToken(auth0Token)); } catch (AuthenticationException e) { - haltWithErrorMsg(400, response, "Problem getting user info from Auth0 token", e); + haltWithErrorMsg(400, response, "Unable to get user information from Auth0 token", e); + } catch (JsonParseException e) { + haltWithErrorMsg(400, response, "Unable to get Auth0 token from request", e); } - - return response; + // DSMInternalError and DSMBadRequestException are handled via Spark + return response; } private String updateToken(String auth0Token) { - try { - Auth0Util.Auth0UserInfo auth0UserInfo = auth0Util.getAuth0UserInfo(auth0Token, auth0Domain); - String email = auth0UserInfo.getEmail(); - UserDao userDao = new UserDao(); - UserDto userDto = userDao.getUserByEmail(email).orElseThrow(() -> - new DSMBadRequestException("User not found: " + email)); - - Map claims = updateClaims(userDto); - - String dsmToken = auth0Util.getNewAuth0TokenWithCustomClaims(claims, clientSecret, auth0ClientId, auth0Domain, - auth0MgmntAudience, audienceNameSpace); - if (dsmToken == null) { - throw new DsmInternalError("Assert: Auth token should not be null"); - } - return dsmToken; - } catch (AuthenticationException e) { - haltWithErrorMsg(401, response, "DSMToken was null! Not authorized user", e); - } - } catch (AuthenticationException e) { - haltWithErrorMsg(400, response, "Problem getting user info from Auth0 token", e); + Auth0Util.Auth0UserInfo auth0UserInfo = auth0Util.getAuth0UserInfo(auth0Token, auth0Domain); + String email = auth0UserInfo.getEmail(); + + logger.info("Authenticating user {}", email); + UserDao userDao = new UserDao(); + UserDto userDto = userDao.getUserByEmail(email).orElseThrow(() -> + new DSMBadRequestException("User not found: " + email)); + + Map claims = updateClaims(userDto); + String dsmToken = auth0Util.getNewAuth0TokenWithCustomClaims(claims, clientSecret, auth0ClientId, auth0Domain, + auth0MgmntAudience, audienceNameSpace); + if (dsmToken == null) { + throw new DsmInternalError("Assert: Auth token should not be null"); } + return dsmToken; } private Map updateClaims(UserDto userDto) { @@ -126,7 +113,7 @@ private Map updateClaims(UserDto userDto) { } private static class DSMToken { - private String dsmToken; + private final String dsmToken; public DSMToken(String token) { this.dsmToken = token; @@ -138,6 +125,8 @@ public DSMToken(String token) { */ public static void haltWithErrorMsg(int responseStatus, Response response, String message) { response.type(ContentType.APPLICATION_JSON.getMimeType()); + // TODO: this is currently called for bad request status. Do we want to log that at error level? + // Or perhaps we could user the return status to determine the log level? -DC logger.error(message); String errorMsgJson = new Gson().toJson(new Error(message)); halt(responseStatus, errorMsgJson); @@ -145,6 +134,8 @@ public static void haltWithErrorMsg(int responseStatus, Response response, Strin public static void haltWithErrorMsg(int responseStatus, Response response, String message, Throwable t) { if (t != null) { + // TODO: this is currently called for bad request status. Do we want to log that at error level? + // Or perhaps we could user the return status to determine the log level? -DC logger.error("Authentication Error", t); } haltWithErrorMsg(responseStatus, response, message); diff --git a/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/security/Auth0Util.java b/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/security/Auth0Util.java index cf1a5a165f..a99e25a090 100755 --- a/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/security/Auth0Util.java +++ b/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/security/Auth0Util.java @@ -72,8 +72,8 @@ public Auth0UserInfo getAuth0UserInfo(@NonNull String idToken, String auth0Domai verifyUserConnection(auth0Claims.get("sub").asString(), userInfo.getEmail()); return userInfo; - } catch (AuthenticationException e) { - throw new AuthenticationException("couldn't get Auth0 user info", e); + } catch (Exception e) { + throw new AuthenticationException("Could not get Auth0 user info", e); } } @@ -123,15 +123,12 @@ private void verifyUserConnection(@NonNull String userId, @NonNull String email) } public static Map verifyAndParseAuth0TokenClaims(String auth0Token, String auth0Domain) throws AuthenticationException { - Map auth0Claims = new HashMap<>(); try { Optional maybeToken = verifyAuth0Token(auth0Token, auth0Domain); - maybeToken.orElseThrow(); - auth0Claims = maybeToken.get().getClaims(); + return maybeToken.orElseThrow().getClaims(); } catch (Exception e) { throw new AuthenticationException("Could not verify auth0 token.", e); } - return auth0Claims; } /** From 60766b606bce5b4223cc3d648e997c10dd980aa4 Mon Sep 17 00:00:00 2001 From: Dennis Date: Thu, 27 Jul 2023 11:26:21 -0400 Subject: [PATCH 3/4] WIP --- .../java/org/broadinstitute/dsm/DSMServer.java | 1 - .../dsm/route/AuthenticationRoute.java | 15 ++++++--------- .../org/broadinstitute/dsm/util/UserUtil.java | 2 +- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/DSMServer.java b/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/DSMServer.java index 415e44a199..c6aa2c6d7e 100644 --- a/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/DSMServer.java +++ b/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/DSMServer.java @@ -647,7 +647,6 @@ protected void setupCustomRouting(@NonNull Config cfg) { setupDDPConfigurationLookup(cfg.getString(ApplicationConfigConstants.DDP)); AuthenticationRoute authenticationRoute = new AuthenticationRoute(auth0Util, - userUtil, cfg.getString(ApplicationConfigConstants.AUTH0_DOMAIN), cfg.getString(ApplicationConfigConstants.AUTH0_MGT_SECRET), cfg.getString(ApplicationConfigConstants.AUTH0_MGT_KEY), diff --git a/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/route/AuthenticationRoute.java b/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/route/AuthenticationRoute.java index 04ad161fa1..31edf2e2b4 100644 --- a/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/route/AuthenticationRoute.java +++ b/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/route/AuthenticationRoute.java @@ -10,7 +10,6 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import com.google.gson.JsonParser; -import com.google.gson.JsonSyntaxException; import lombok.NonNull; import org.apache.commons.lang3.StringUtils; import org.apache.http.entity.ContentType; @@ -20,8 +19,8 @@ import org.broadinstitute.dsm.exception.AuthenticationException; import org.broadinstitute.dsm.exception.DSMBadRequestException; import org.broadinstitute.dsm.exception.DsmInternalError; -import org.broadinstitute.dsm.util.UserUtil; import org.broadinstitute.dsm.security.Auth0Util; +import org.broadinstitute.dsm.util.UserUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import spark.Request; @@ -40,18 +39,16 @@ public class AuthenticationRoute implements Route { private final Auth0Util auth0Util; - private final UserUtil userUtil; private final String auth0Domain; private final String clientSecret; private final String auth0ClientId; private final String auth0MgmntAudience; private final String audienceNameSpace; - public AuthenticationRoute(@NonNull Auth0Util auth0Util, @NonNull UserUtil userUtil, @NonNull String auth0Domain, + public AuthenticationRoute(@NonNull Auth0Util auth0Util, @NonNull String auth0Domain, @NonNull String clientSecret, @NonNull String auth0ClientId, @NonNull String auth0MgmntAudience, @NonNull String audienceNameSpace) { this.auth0Util = auth0Util; - this.userUtil = userUtil; this.auth0Domain = auth0Domain; this.clientSecret = clientSecret; this.auth0ClientId = auth0ClientId; @@ -100,8 +97,8 @@ private Map updateClaims(UserDto userDto) { try { Gson gson = new Gson(); String email = userDto.getEmail().orElseThrow(() -> new DsmInternalError("User email cannot be null")); - String userSetting = gson.toJson(userUtil.getUserAccessRoles(email), ArrayList.class); - claims.put(userAccessRoles, userSetting); + String roles = gson.toJson(UserUtil.getUserAccessRoles(email), ArrayList.class); + claims.put(userAccessRoles, roles); claims.put(userSettings, gson.toJson(UserSettings.getUserSettings(email), UserSettings.class)); claims.put(authUserId, String.valueOf(userDto.getId())); claims.put(authUserName, userDto.getName().orElse("")); @@ -126,7 +123,7 @@ public DSMToken(String token) { public static void haltWithErrorMsg(int responseStatus, Response response, String message) { response.type(ContentType.APPLICATION_JSON.getMimeType()); // TODO: this is currently called for bad request status. Do we want to log that at error level? - // Or perhaps we could user the return status to determine the log level? -DC + // Or perhaps we could use the return status to determine the log level? -DC logger.error(message); String errorMsgJson = new Gson().toJson(new Error(message)); halt(responseStatus, errorMsgJson); @@ -135,7 +132,7 @@ public static void haltWithErrorMsg(int responseStatus, Response response, Strin public static void haltWithErrorMsg(int responseStatus, Response response, String message, Throwable t) { if (t != null) { // TODO: this is currently called for bad request status. Do we want to log that at error level? - // Or perhaps we could user the return status to determine the log level? -DC + // Or perhaps we could use the return status to determine the log level? -DC logger.error("Authentication Error", t); } haltWithErrorMsg(responseStatus, response, message); diff --git a/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/util/UserUtil.java b/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/util/UserUtil.java index 27183f7a8c..40c3cdfe4e 100644 --- a/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/util/UserUtil.java +++ b/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/util/UserUtil.java @@ -330,7 +330,7 @@ public static boolean checkKitShippingAccessForPatch(String realm, String userId && DBConstants.DDP_KIT_ALIAS.equals(patch.getTableAlias()); } - public List getUserAccessRoles(@NonNull String email) { + public static List getUserAccessRoles(@NonNull String email) { ArrayList roles = new ArrayList<>(); SimpleResult results = inTransaction((conn) -> { SimpleResult dbVals = new SimpleResult(); From 5ae79701b85099ce3d405fc15e136f30ea4977de Mon Sep 17 00:00:00 2001 From: Dennis Date: Tue, 1 Aug 2023 15:31:01 -0400 Subject: [PATCH 4/4] Remove obsolete method --- .../org/broadinstitute/dsm/util/UserUtil.java | 40 +++---------------- 1 file changed, 5 insertions(+), 35 deletions(-) diff --git a/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/util/UserUtil.java b/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/util/UserUtil.java index 40c3cdfe4e..3e27b1d0d4 100644 --- a/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/util/UserUtil.java +++ b/pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/util/UserUtil.java @@ -6,7 +6,6 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Statement; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -15,7 +14,6 @@ import lombok.NonNull; import org.apache.commons.lang3.StringUtils; -import org.broadinstitute.dsm.db.UserSettings; import org.broadinstitute.dsm.db.dao.user.UserDao; import org.broadinstitute.dsm.db.dto.user.UserDto; import org.broadinstitute.dsm.exception.DsmInternalError; @@ -41,7 +39,6 @@ public class UserUtil { public static final String SHIPPING_MENU = "shipping"; private static final Logger logger = LoggerFactory.getLogger(UserUtil.class); private static final String SQL_SELECT_USER = "SELECT user_id, name FROM access_user"; - private static final String SQL_INSERT_USER = "INSERT INTO access_user (name, email) VALUES (?,?)"; private static final String SQL_SELECT_USER_ACCESS_ROLE = "SELECT role.name FROM access_user_role_group roleGroup, access_user user, access_role role " + "WHERE roleGroup.user_id = user.user_id AND roleGroup.role_id = role.role_id AND user.is_active = 1"; @@ -284,11 +281,13 @@ public static boolean checkUserAccessForPatch(String realm, String userId, Strin } else { // for now, let's do what DSM did previously and let them change the data. // Still we need to log this and fix the patch from frontend - logger.error("The id in patch is not a number and also not an email, id in patch is "+ userEmailOrIdInPatch + "and id in token is "+ userId); + logger.error("The id in patch is not a number and also not an email, id in patch is " + userEmailOrIdInPatch + + "and id in token is " + userId); return checkUserAccess(realm, userId, role, userIdRequest); } - if (!userId.equals(userIdFromPatch)){ - String msg = "User id in patch did not match the one in token, user Id in patch is " + userIdFromPatch + " user Id in token " + userIdRequest; + if (!userId.equals(userIdFromPatch)) { + String msg = "User id in patch did not match the one in token, user Id in patch is " + userIdFromPatch + " user Id in token " + + userIdRequest; logger.warn(msg); throw new RuntimeException(msg); } @@ -352,33 +351,4 @@ public static List getUserAccessRoles(@NonNull String email) { } return roles; } - - public int insertUser(@NonNull String name, @NonNull String email) { - SimpleResult results = inTransaction((conn) -> { - SimpleResult dbVals = new SimpleResult(); - try (PreparedStatement insertStmt = conn.prepareStatement(SQL_INSERT_USER, Statement.RETURN_GENERATED_KEYS)) { - insertStmt.setString(1, name); - insertStmt.setString(2, email); - insertStmt.executeUpdate(); - try (ResultSet rs = insertStmt.getGeneratedKeys()) { - if (rs.next()) { - UserSettings.insertUserSetting(conn, rs.getInt(1)); - dbVals.resultValue = rs.getInt(1); - } - } catch (Exception e) { - throw new RuntimeException("Error getting id of new kit request ", e); - } - } catch (SQLException ex) { - logger.error( - "User " + name + ", " + email + " already exists but doesn't have any access roles or is set to is_active=0..."); - } - return dbVals; - }); - - if (results.resultException != null) { - throw new RuntimeException("Error getting list of realms ", results.resultException); - } - - return (int) results.resultValue; - } }