|
|
|
@ -1,18 +1,5 @@ |
|
|
|
/** |
|
|
|
* Copyright © 2016-2026 The Thingsboard Authors |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* You may obtain a copy of the License at |
|
|
|
* |
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
* |
|
|
|
* 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. |
|
|
|
*/ |
|
|
|
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package org.thingsboard.server.controller; |
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.type.TypeReference; |
|
|
|
@ -76,6 +63,7 @@ import org.thingsboard.server.dao.settings.SecuritySettingsService; |
|
|
|
import org.thingsboard.server.queue.util.TbCoreComponent; |
|
|
|
import org.thingsboard.server.service.security.auth.jwt.settings.JwtSettingsService; |
|
|
|
import org.thingsboard.server.service.security.auth.oauth2.CookieUtils; |
|
|
|
import org.thingsboard.server.service.security.auth.oauth2.PrevUriValidator; |
|
|
|
import org.thingsboard.server.service.security.model.SecurityUser; |
|
|
|
import org.thingsboard.server.service.security.model.token.JwtTokenFactory; |
|
|
|
import org.thingsboard.server.service.security.permission.Operation; |
|
|
|
@ -92,6 +80,8 @@ import java.util.Optional; |
|
|
|
|
|
|
|
import static org.thingsboard.server.controller.ControllerConstants.SYSTEM_AUTHORITY_PARAGRAPH; |
|
|
|
import static org.thingsboard.server.controller.ControllerConstants.TENANT_AUTHORITY_PARAGRAPH; |
|
|
|
import static org.thingsboard.server.service.security.auth.oauth2.HttpCookieOAuth2AuthorizationRequestRepository.PREV_URI_COOKIE_NAME; |
|
|
|
import static org.thingsboard.server.service.security.auth.oauth2.HttpCookieOAuth2AuthorizationRequestRepository.PREV_URI_PARAMETER; |
|
|
|
|
|
|
|
@RestController |
|
|
|
@TbCoreComponent |
|
|
|
@ -100,8 +90,7 @@ import static org.thingsboard.server.controller.ControllerConstants.TENANT_AUTHO |
|
|
|
@RequiredArgsConstructor |
|
|
|
public class AdminController extends BaseController { |
|
|
|
|
|
|
|
private static final String PREV_URI_PATH_PARAMETER = "prevUri"; |
|
|
|
private static final String PREV_URI_COOKIE_NAME = "prev_uri"; |
|
|
|
private static final String DEFAULT_PREV_URI = "/settings/outgoing-mail"; |
|
|
|
private static final String STATE_COOKIE_NAME = "state"; |
|
|
|
private static final String MAIL_SETTINGS_KEY = "mail"; |
|
|
|
|
|
|
|
@ -419,8 +408,9 @@ public class AdminController extends BaseController { |
|
|
|
@GetMapping(value = "/mail/oauth2/authorize", produces = "application/text") |
|
|
|
public String getMailOAuth2AuthorizationUrl(HttpServletRequest request, HttpServletResponse response) throws ThingsboardException { |
|
|
|
String state = StringUtils.generateSafeToken(); |
|
|
|
if (request.getParameter(PREV_URI_PATH_PARAMETER) != null) { |
|
|
|
CookieUtils.addCookie(response, PREV_URI_COOKIE_NAME, request.getParameter(PREV_URI_PATH_PARAMETER), 180); |
|
|
|
String prevUriParam = request.getParameter(PREV_URI_PARAMETER); |
|
|
|
if (PrevUriValidator.isValid(prevUriParam)) { |
|
|
|
CookieUtils.addCookie(response, PREV_URI_COOKIE_NAME, prevUriParam, 180); |
|
|
|
} |
|
|
|
CookieUtils.addCookie(response, STATE_COOKIE_NAME, state, 180); |
|
|
|
|
|
|
|
@ -445,12 +435,9 @@ public class AdminController extends BaseController { |
|
|
|
public void handleMailOAuth2Callback( |
|
|
|
@RequestParam(value = "code") String code, @RequestParam(value = "state") String state, |
|
|
|
HttpServletRequest request, HttpServletResponse response) throws ThingsboardException, IOException { |
|
|
|
Optional<Cookie> prevUrlOpt = CookieUtils.getCookie(request, PREV_URI_COOKIE_NAME); |
|
|
|
String redirectUrl = getMailOAuth2RedirectUrl(request); |
|
|
|
Optional<Cookie> cookieState = CookieUtils.getCookie(request, STATE_COOKIE_NAME); |
|
|
|
|
|
|
|
String baseUrl = this.systemSecurityService.getBaseUrl(TenantId.SYS_TENANT_ID, new CustomerId(EntityId.NULL_UUID), request); |
|
|
|
String prevUri = baseUrl + (prevUrlOpt.isPresent() ? prevUrlOpt.get().getValue() : "/settings/outgoing-mail"); |
|
|
|
|
|
|
|
if (cookieState.isEmpty() || !cookieState.get().getValue().equals(state)) { |
|
|
|
CookieUtils.deleteCookie(request, response, STATE_COOKIE_NAME); |
|
|
|
throw new ThingsboardException("Refresh token was not generated, invalid state param", ThingsboardErrorCode.BAD_REQUEST_PARAMS); |
|
|
|
@ -480,7 +467,16 @@ public class AdminController extends BaseController { |
|
|
|
((ObjectNode) jsonValue).put("tokenGenerated", true); |
|
|
|
|
|
|
|
adminSettingsService.saveAdminSettings(TenantId.SYS_TENANT_ID, adminSettings); |
|
|
|
response.sendRedirect(prevUri); |
|
|
|
response.sendRedirect(redirectUrl); |
|
|
|
} |
|
|
|
|
|
|
|
String getMailOAuth2RedirectUrl(HttpServletRequest request) { |
|
|
|
String baseUrl = this.systemSecurityService.getBaseUrl(TenantId.SYS_TENANT_ID, new CustomerId(EntityId.NULL_UUID), request); |
|
|
|
String prevUri = CookieUtils.getCookie(request, PREV_URI_COOKIE_NAME) |
|
|
|
.map(Cookie::getValue) |
|
|
|
.filter(PrevUriValidator::isValid) |
|
|
|
.orElse(DEFAULT_PREV_URI); |
|
|
|
return baseUrl + prevUri; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|