Improve code.

main
Paulo Gustavo Veiga 2024-03-24 08:33:06 -07:00
parent e99424db01
commit ede2fb7622
2 changed files with 17 additions and 13 deletions

View File

@ -56,8 +56,12 @@ public class OAuth2Controller extends BaseController {
@RequestMapping(method = RequestMethod.POST, value = "googlecallback", produces = {"application/json"}) @RequestMapping(method = RequestMethod.POST, value = "googlecallback", produces = {"application/json"})
@ResponseStatus(value = HttpStatus.OK) @ResponseStatus(value = HttpStatus.OK)
public RestOath2CallbackResponse processGoogleCallback(@NotNull @RequestParam String code, @NotNull HttpServletResponse response, @NotNull HttpServletRequest request) throws WiseMappingException { 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; String jwtToken = null;
if (user.getGoogleSync()) { if (user.getGoogleSync()) {
jwtToken = jwtTokenUtil.doLogin(response, user.getEmail()); jwtToken = jwtTokenUtil.doLogin(response, user.getEmail());
@ -75,8 +79,8 @@ public class OAuth2Controller extends BaseController {
@RequestMapping(method = RequestMethod.PUT, value = "confirmaccountsync", produces = {"application/json"}) @RequestMapping(method = RequestMethod.PUT, value = "confirmaccountsync", produces = {"application/json"})
@ResponseStatus(value = HttpStatus.OK) @ResponseStatus(value = HttpStatus.OK)
public RestOath2CallbackResponse confirmAccountSync(@NotNull @RequestParam String email, @NotNull @RequestParam String code, @NotNull HttpServletResponse response) throws WiseMappingException { public RestOath2CallbackResponse confirmAccountSync(@NotNull @RequestParam String email, @NotNull @RequestParam String code, @NotNull HttpServletResponse response) throws WiseMappingException {
logger.debug("confirmAccountSync:" + email + " - " + code); logger.debug("ConfirmAccountSync:" + email + " - " + code);
if (email == null || code == null) { if (code == null) {
throw new WiseMappingException("Illegal argument exception: " + email + " - " + code); throw new WiseMappingException("Illegal argument exception: " + email + " - " + code);
} }
@ -84,7 +88,7 @@ public class OAuth2Controller extends BaseController {
final Account user = userService.createAndAuthUserFromGoogle(code); final Account user = userService.createAndAuthUserFromGoogle(code);
// Update login // Update login
userService.confirmAccountSync(email, code); userService.confirmAccountSync(user.getEmail(), code);
// Add header ... // Add header ...
final String jwtToken = jwtTokenUtil.doLogin(response, email); final String jwtToken = jwtTokenUtil.doLogin(response, email);

View File

@ -187,6 +187,7 @@ public class UserServiceImpl
throw new OAuthAuthenticationException(e); throw new OAuthAuthenticationException(e);
} }
// Callback is successful, the email of the user exits. Is an existing account ?
Account result = userManager.getUserBy(data.getEmail()); Account result = userManager.getUserBy(data.getEmail());
if (result == null) { if (result == null) {
Account newUser = new Account(); Account newUser = new Account();
@ -198,18 +199,17 @@ public class UserServiceImpl
newUser.setAuthenticationType(AuthenticationType.GOOGLE_OAUTH2); newUser.setAuthenticationType(AuthenticationType.GOOGLE_OAUTH2);
newUser.setGoogleToken(data.getAccessToken()); newUser.setGoogleToken(data.getAccessToken());
result = this.createUser(newUser, false, true); result = this.createUser(newUser, false, true);
} else { logger.debug("Google account successfully created");
// user exists and doesn't have confirmed account linking, I must wait for confirmation }
if (result.getGoogleSync() == null) {
// Is the user a non-oauth user ?
if (result.getGoogleSync() == null || !result.getGoogleSync()) {
result.setGoogleSync(false); result.setGoogleSync(false);
result.setSyncCode(callbackCode); result.setSyncCode(callbackCode);
result.setGoogleToken(data.getAccessToken()); result.setGoogleToken(data.getAccessToken());
userManager.updateUser(result); userManager.updateUser(result);
} }
}
return result; return result;
} }
public Account confirmAccountSync(@NotNull String email, @NotNull String code) throws WiseMappingException { public Account confirmAccountSync(@NotNull String email, @NotNull String code) throws WiseMappingException {