Improve code.

main
Paulo Gustavo Veiga 2024-03-24 09:01:43 -07:00
parent ede2fb7622
commit 1e548136ff
4 changed files with 16 additions and 19 deletions

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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);

View File

@ -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)) {