92 lines
3.6 KiB
Java
Raw Normal View History

2022-12-13 02:36:58 +00:00
/*
* Copyright [2022] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wisemapping.rest;
import com.wisemapping.exceptions.WiseMappingException;
2024-02-17 11:18:43 -08:00
import com.wisemapping.model.Account;
2022-12-13 02:36:58 +00:00
import com.wisemapping.rest.model.RestOath2CallbackResponse;
2024-02-11 12:21:38 -08:00
import com.wisemapping.security.JwtTokenUtil;
import com.wisemapping.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
2024-03-23 22:56:12 -07:00
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2022-12-13 02:36:58 +00:00
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.web.bind.annotation.*;
2023-11-27 21:08:28 -08:00
@RestController
2024-03-21 22:16:31 -07:00
@RequestMapping("/api/restful/oauth2/")
2022-12-13 02:36:58 +00:00
@CrossOrigin
2023-04-01 13:33:15 -07:00
public class OAuth2Controller extends BaseController {
2024-03-23 22:56:12 -07:00
final private static Logger logger = LogManager.getLogger();
2024-02-11 12:21:38 -08:00
@Qualifier("userService")
@Autowired
private UserService userService;
@Qualifier("authenticationManager")
@Autowired
private AuthenticationManager authManager;
2022-12-13 02:36:58 +00:00
2024-02-11 12:21:38 -08:00
@Autowired
private JwtTokenUtil jwtTokenUtil;
2022-12-13 02:36:58 +00:00
2024-02-11 12:21:38 -08:00
@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 {
2024-03-24 09:01:43 -07:00
logger.debug("ProcessGoogleCallback:" + code);
2024-03-24 08:33:06 -07:00
if (code == null) {
throw new WiseMappingException("Illegal argument exception: " + code);
}
2024-03-23 23:29:48 -07:00
2024-03-24 08:33:06 -07:00
final Account user = userService.createAndAuthUserFromGoogle(code);
2024-03-23 23:29:48 -07:00
String jwtToken = null;
2024-02-11 12:21:38 -08:00
if (user.getGoogleSync()) {
2024-03-23 23:29:48 -07:00
jwtToken = jwtTokenUtil.doLogin(response, user.getEmail());
2024-02-11 12:21:38 -08:00
}
2022-12-13 02:36:58 +00:00
2024-02-11 12:21:38 -08:00
// Response ...
2024-03-24 09:01:43 -07:00
return new RestOath2CallbackResponse(user, jwtToken);
2024-02-11 12:21:38 -08:00
}
2022-12-13 02:36:58 +00:00
2024-02-11 12:21:38 -08:00
@RequestMapping(method = RequestMethod.PUT, value = "confirmaccountsync", produces = {"application/json"})
@ResponseStatus(value = HttpStatus.OK)
2024-03-24 00:15:04 -07:00
public RestOath2CallbackResponse confirmAccountSync(@NotNull @RequestParam String email, @NotNull @RequestParam String code, @NotNull HttpServletResponse response) throws WiseMappingException {
2024-03-24 08:33:06 -07:00
logger.debug("ConfirmAccountSync:" + email + " - " + code);
if (code == null) {
2024-03-24 00:37:14 -07:00
throw new WiseMappingException("Illegal argument exception: " + email + " - " + code);
}
2024-03-23 22:56:12 -07:00
2024-02-11 12:21:38 -08:00
// Update login
2024-03-24 09:01:43 -07:00
final Account user = userService.confirmGoogleAccountSync(email, code);
2022-12-13 02:36:58 +00:00
2024-02-11 12:21:38 -08:00
// Add header ...
2024-03-24 00:15:04 -07:00
final String jwtToken = jwtTokenUtil.doLogin(response, email);
// Response ...
2024-03-24 09:01:43 -07:00
return new RestOath2CallbackResponse(user, jwtToken);
2024-02-11 12:21:38 -08:00
}
2022-12-13 02:36:58 +00:00
}