Commit 6b41fb9c0eb726533a017552bf8ce8da0ae82535

Authored by vzikratyi
1 parent c7e23649

Refactored and used MiscUtils

... ... @@ -32,13 +32,12 @@ import org.springframework.security.oauth2.core.oidc.endpoint.OidcParameterNames
32 32 import org.springframework.security.web.util.UrlUtils;
33 33 import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
34 34 import org.springframework.stereotype.Service;
35   -import org.springframework.util.Assert;
36 35 import org.springframework.util.CollectionUtils;
37 36 import org.springframework.util.StringUtils;
38 37 import org.springframework.web.util.UriComponents;
39 38 import org.springframework.web.util.UriComponentsBuilder;
40 39 import org.thingsboard.server.dao.oauth2.OAuth2Configuration;
41   -import org.thingsboard.server.utils.WebUtils;
  40 +import org.thingsboard.server.utils.MiscUtils;
42 41
43 42 import javax.servlet.http.HttpServletRequest;
44 43 import java.nio.charset.StandardCharsets;
... ... @@ -198,13 +197,8 @@ public class CustomOAuth2AuthorizationRequestResolver implements OAuth2Authoriza
198 197
199 198 private String getRedirectUri(HttpServletRequest request) {
200 199 String loginProcessingUri = oauth2Configuration != null ? oauth2Configuration.getLoginProcessingUrl() : DEFAULT_LOGIN_PROCESSING_URI;
201   -
202   - String scheme = WebUtils.getScheme(request);
203   - String host = WebUtils.getHost(request);
204   - String port = WebUtils.getPort(request);
205   - log.trace("Scheme - {}, host - {}, port - {}.", scheme, host, port);
206   - String requestHost = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
207   - return requestHost + loginProcessingUri;
  200 + String baseUrl= MiscUtils.constructBaseUrl(request);
  201 + return baseUrl + loginProcessingUri;
208 202 }
209 203
210 204 /**
... ...
... ... @@ -24,6 +24,7 @@ import org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo;
24 24 import org.thingsboard.server.common.data.oauth2.OAuth2ClientsParams;
25 25 import org.thingsboard.server.common.data.oauth2.SchemeType;
26 26 import org.thingsboard.server.queue.util.TbCoreComponent;
  27 +import org.thingsboard.server.utils.MiscUtils;
27 28
28 29 import javax.servlet.http.HttpServletRequest;
29 30 import java.util.List;
... ... @@ -37,7 +38,7 @@ public class OAuth2Controller extends BaseController {
37 38 @ResponseBody
38 39 public List<OAuth2ClientInfo> getOAuth2Clients(HttpServletRequest request) throws ThingsboardException {
39 40 try {
40   - return oAuth2Service.getOAuth2Clients(request.getScheme(), request.getServerName());
  41 + return oAuth2Service.getOAuth2Clients(MiscUtils.getScheme(request), MiscUtils.getDomainName(request));
41 42 } catch (Exception e) {
42 43 throw handleException(e);
43 44 }
... ...
... ... @@ -49,12 +49,27 @@ public class MiscUtils {
49 49 }
50 50
51 51 public static String constructBaseUrl(HttpServletRequest request) {
52   - String scheme = request.getScheme();
  52 + return String.format("%s://%s:%d",
  53 + getScheme(request),
  54 + getDomainName(request),
  55 + getPort(request));
  56 + }
53 57
  58 + public static String getScheme(HttpServletRequest request){
  59 + String scheme = request.getScheme();
54 60 String forwardedProto = request.getHeader("x-forwarded-proto");
55 61 if (forwardedProto != null) {
56 62 scheme = forwardedProto;
57 63 }
  64 + return scheme;
  65 + }
  66 +
  67 + public static String getDomainName(HttpServletRequest request){
  68 + return request.getServerName();
  69 + }
  70 +
  71 + public static int getPort(HttpServletRequest request){
  72 + String forwardedProto = request.getHeader("x-forwarded-proto");
58 73
59 74 int serverPort = request.getServerPort();
60 75 if (request.getHeader("x-forwarded-port") != null) {
... ... @@ -72,11 +87,6 @@ public class MiscUtils {
72 87 break;
73 88 }
74 89 }
75   -
76   - String baseUrl = String.format("%s://%s:%d",
77   - scheme,
78   - request.getServerName(),
79   - serverPort);
80   - return baseUrl;
  90 + return serverPort;
81 91 }
82 92 }
... ...
1   -/**
2   - * Copyright © 2016-2020 The Thingsboard Authors
3   - *
4   - * Licensed under the Apache License, Version 2.0 (the "License");
5   - * you may not use this file except in compliance with the License.
6   - * You may obtain a copy of the License at
7   - *
8   - * http://www.apache.org/licenses/LICENSE-2.0
9   - *
10   - * Unless required by applicable law or agreed to in writing, software
11   - * distributed under the License is distributed on an "AS IS" BASIS,
12   - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   - * See the License for the specific language governing permissions and
14   - * limitations under the License.
15   - */
16   -package org.thingsboard.server.utils;
17   -
18   -import lombok.extern.slf4j.Slf4j;
19   -import org.apache.commons.lang.StringUtils;
20   -
21   -import javax.servlet.http.HttpServletRequest;
22   -
23   -@Slf4j
24   -public class WebUtils {
25   - private static final String X_FORWARDED_HOST_HEADER_KEY = "x-forwarded-host";
26   - private static final String X_FORWARDED_PORT_HEADER_KEY = "x-forwarded-port";
27   - private static final String X_FORWARDED_PROTO_HEADER_KEY = "x-forwarded-proto";
28   -
29   - public static String getHost(HttpServletRequest request) {
30   - String forwardedHost = request.getHeader(X_FORWARDED_HOST_HEADER_KEY);
31   - log.trace("Forwarded host - {}.", forwardedHost);
32   - if (!StringUtils.isEmpty(forwardedHost)) {
33   - if (forwardedHost.contains(":")) {
34   - return forwardedHost.substring(0, forwardedHost.indexOf(":"));
35   - } else {
36   - return forwardedHost;
37   - }
38   - } else {
39   - return request.getServerName();
40   - }
41   - }
42   -
43   - public static String getScheme(HttpServletRequest request) {
44   - String forwardedProto = request.getHeader(X_FORWARDED_PROTO_HEADER_KEY);
45   - log.trace("Forwarded proto - {}.", forwardedProto);
46   - if (!StringUtils.isEmpty(forwardedProto)) {
47   - return forwardedProto;
48   - } else {
49   - return request.getServerName();
50   - }
51   - }
52   -
53   - public static String getPort(HttpServletRequest request) {
54   - String forwardedPort = request.getHeader(X_FORWARDED_PORT_HEADER_KEY);
55   - log.trace("Forwarded port - {}.", forwardedPort);
56   - if (!StringUtils.isEmpty(forwardedPort)) {
57   - return forwardedPort;
58   - }
59   - String forwardedHost = request.getHeader(X_FORWARDED_HOST_HEADER_KEY);
60   - if (!StringUtils.isEmpty(forwardedHost)) {
61   - if (forwardedHost.contains(":")) {
62   - return forwardedHost.substring(forwardedHost.indexOf(":"));
63   - } else {
64   - return "HTTP".equals(getScheme(request).toUpperCase()) ?
65   - "80" : "443";
66   - }
67   - }
68   - return Integer.toString(request.getServerPort());
69   - }
70   -}
... ... @@ -27,6 +27,7 @@ import java.util.UUID;
27 27
28 28 @Component
29 29 public class HybridClientRegistrationRepository implements ClientRegistrationRepository {
  30 + private static final String defaultRedirectUriTemplate = "{baseUrl}/login/oauth2/code/{registrationId}";
30 31
31 32 @Autowired
32 33 private OAuth2Service oAuth2Service;
... ... @@ -52,6 +53,7 @@ public class HybridClientRegistrationRepository implements ClientRegistrationRep
52 53 .userNameAttributeName(localClientRegistration.getUserNameAttributeName())
53 54 .jwkSetUri(localClientRegistration.getJwkSetUri())
54 55 .clientAuthenticationMethod(new ClientAuthenticationMethod(localClientRegistration.getClientAuthenticationMethod()))
  56 + .redirectUriTemplate(defaultRedirectUriTemplate)
55 57 .build();
56 58 }
57 59 }
... ...