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 983cbc6d..84f7d2b2 100644 --- a/wise-api/src/main/java/com/wisemapping/rest/OAuth2Controller.java +++ b/wise-api/src/main/java/com/wisemapping/rest/OAuth2Controller.java @@ -56,8 +56,12 @@ 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 { - final Account user = userService.createAndAuthUserFromGoogle(code); + logger.debug("processGoogleCallback:" + code); + if (code == null) { + throw new WiseMappingException("Illegal argument exception: " + code); + } + final Account user = userService.createAndAuthUserFromGoogle(code); String jwtToken = null; if (user.getGoogleSync()) { jwtToken = jwtTokenUtil.doLogin(response, user.getEmail()); @@ -75,8 +79,8 @@ public class OAuth2Controller extends BaseController { @RequestMapping(method = RequestMethod.PUT, value = "confirmaccountsync", produces = {"application/json"}) @ResponseStatus(value = HttpStatus.OK) public RestOath2CallbackResponse confirmAccountSync(@NotNull @RequestParam String email, @NotNull @RequestParam String code, @NotNull HttpServletResponse response) throws WiseMappingException { - logger.debug("confirmAccountSync:" + email + " - " + code); - if (email == null || code == null) { + logger.debug("ConfirmAccountSync:" + email + " - " + code); + if (code == null) { throw new WiseMappingException("Illegal argument exception: " + email + " - " + code); } @@ -84,7 +88,7 @@ public class OAuth2Controller extends BaseController { final Account user = userService.createAndAuthUserFromGoogle(code); // Update login - userService.confirmAccountSync(email, code); + userService.confirmAccountSync(user.getEmail(), code); // Add header ... final String jwtToken = jwtTokenUtil.doLogin(response, email); 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 e0768353..9578064d 100755 --- a/wise-api/src/main/java/com/wisemapping/service/UserServiceImpl.java +++ b/wise-api/src/main/java/com/wisemapping/service/UserServiceImpl.java @@ -187,6 +187,7 @@ public class UserServiceImpl throw new OAuthAuthenticationException(e); } + // Callback is successful, the email of the user exits. Is an existing account ? Account result = userManager.getUserBy(data.getEmail()); if (result == null) { Account newUser = new Account(); @@ -198,18 +199,17 @@ public class UserServiceImpl newUser.setAuthenticationType(AuthenticationType.GOOGLE_OAUTH2); newUser.setGoogleToken(data.getAccessToken()); result = this.createUser(newUser, false, true); - } else { - // user exists and doesn't have confirmed account linking, I must wait for confirmation - if (result.getGoogleSync() == null) { - result.setGoogleSync(false); - result.setSyncCode(callbackCode); - result.setGoogleToken(data.getAccessToken()); - userManager.updateUser(result); - } + logger.debug("Google account successfully created"); + } + // Is the user a non-oauth user ? + if (result.getGoogleSync() == null || !result.getGoogleSync()) { + result.setGoogleSync(false); + result.setSyncCode(callbackCode); + result.setGoogleToken(data.getAccessToken()); + userManager.updateUser(result); } return result; - } public Account confirmAccountSync(@NotNull String email, @NotNull String code) throws WiseMappingException {