From 1e548136ff72cdcb9a72f81ca5b2b94ea7721708 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Sun, 24 Mar 2024 09:01:43 -0700 Subject: [PATCH] Improve code. --- .../wisemapping/rest/OAuth2Controller.java | 21 ++++--------------- .../rest/model/RestOath2CallbackResponse.java | 10 +++++++++ .../com/wisemapping/service/UserService.java | 2 +- .../wisemapping/service/UserServiceImpl.java | 2 +- 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/wise-api/src/main/java/com/wisemapping/rest/OAuth2Controller.java b/wise-api/src/main/java/com/wisemapping/rest/OAuth2Controller.java index 84f7d2b2..961f3ebb 100644 --- a/wise-api/src/main/java/com/wisemapping/rest/OAuth2Controller.java +++ b/wise-api/src/main/java/com/wisemapping/rest/OAuth2Controller.java @@ -56,7 +56,7 @@ public class OAuth2Controller extends BaseController { @RequestMapping(method = RequestMethod.POST, value = "googlecallback", produces = {"application/json"}) @ResponseStatus(value = HttpStatus.OK) public RestOath2CallbackResponse processGoogleCallback(@NotNull @RequestParam String code, @NotNull HttpServletResponse response, @NotNull HttpServletRequest request) throws WiseMappingException { - logger.debug("processGoogleCallback:" + code); + logger.debug("ProcessGoogleCallback:" + code); if (code == null) { throw new WiseMappingException("Illegal argument exception: " + code); } @@ -68,12 +68,7 @@ public class OAuth2Controller extends BaseController { } // Response ... - final RestOath2CallbackResponse result = new RestOath2CallbackResponse(); - result.setEmail(user.getEmail()); - result.setGoogleSync(user.getGoogleSync()); - result.setSyncCode(code); - result.setJwtToken(jwtToken); - return result; + return new RestOath2CallbackResponse(user, jwtToken); } @RequestMapping(method = RequestMethod.PUT, value = "confirmaccountsync", produces = {"application/json"}) @@ -84,21 +79,13 @@ public class OAuth2Controller extends BaseController { throw new WiseMappingException("Illegal argument exception: " + email + " - " + code); } - // Authenticate ... - final Account user = userService.createAndAuthUserFromGoogle(code); - // Update login - userService.confirmAccountSync(user.getEmail(), code); + final Account user = userService.confirmGoogleAccountSync(email, code); // Add header ... final String jwtToken = jwtTokenUtil.doLogin(response, email); // Response ... - final RestOath2CallbackResponse result = new RestOath2CallbackResponse(); - result.setEmail(user.getEmail()); - result.setGoogleSync(user.getGoogleSync()); - result.setSyncCode(user.getSyncCode()); - result.setJwtToken(jwtToken); - return result; + return new RestOath2CallbackResponse(user, jwtToken); } } diff --git a/wise-api/src/main/java/com/wisemapping/rest/model/RestOath2CallbackResponse.java b/wise-api/src/main/java/com/wisemapping/rest/model/RestOath2CallbackResponse.java index 3e10259d..ac9b834a 100644 --- a/wise-api/src/main/java/com/wisemapping/rest/model/RestOath2CallbackResponse.java +++ b/wise-api/src/main/java/com/wisemapping/rest/model/RestOath2CallbackResponse.java @@ -2,6 +2,9 @@ package com.wisemapping.rest.model; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.wisemapping.model.Account; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; @JsonAutoDetect( fieldVisibility = JsonAutoDetect.Visibility.NONE, @@ -17,6 +20,13 @@ public class RestOath2CallbackResponse { private String syncCode; private String jwtToken; + public RestOath2CallbackResponse(@NotNull Account user, @Nullable String jwtToken) { + this.setEmail(user.getEmail()); + this.setGoogleSync(user.getGoogleSync()); + this.setSyncCode(user.getSyncCode()); + this.setJwtToken(jwtToken); + } + public String getEmail() { return email; } diff --git a/wise-api/src/main/java/com/wisemapping/service/UserService.java b/wise-api/src/main/java/com/wisemapping/service/UserService.java index dd980bc0..e8023295 100755 --- a/wise-api/src/main/java/com/wisemapping/service/UserService.java +++ b/wise-api/src/main/java/com/wisemapping/service/UserService.java @@ -32,7 +32,7 @@ public interface UserService { Account createAndAuthUserFromGoogle(@NotNull String callbackCode) throws WiseMappingException; - Account confirmAccountSync(@NotNull String email, @NotNull String code) throws WiseMappingException; + Account confirmGoogleAccountSync(@NotNull String email, @NotNull String code) throws WiseMappingException; void changePassword(@NotNull Account user); diff --git a/wise-api/src/main/java/com/wisemapping/service/UserServiceImpl.java b/wise-api/src/main/java/com/wisemapping/service/UserServiceImpl.java index 9578064d..8cd87bf4 100755 --- a/wise-api/src/main/java/com/wisemapping/service/UserServiceImpl.java +++ b/wise-api/src/main/java/com/wisemapping/service/UserServiceImpl.java @@ -212,7 +212,7 @@ public class UserServiceImpl return result; } - public Account confirmAccountSync(@NotNull String email, @NotNull String code) throws WiseMappingException { + public Account confirmGoogleAccountSync(@NotNull String email, @NotNull String code) throws WiseMappingException { final Account existingUser = userManager.getUserBy(email); // additional security check if (existingUser == null || !existingUser.getSyncCode().equals(code)) {