Browse Source

Several OAuth2 improvements

pull/3587/head
Igor Kulikov 6 years ago
parent
commit
7565afca3a
  1. 1
      application/src/main/data/json/system/oauth2_config_templates/github_config.json
  2. 18
      application/src/main/java/org/thingsboard/server/controller/OAuth2Controller.java
  3. 6
      common/data/src/main/java/org/thingsboard/server/common/data/oauth2/OAuth2ClientsDomainParams.java
  4. 6
      common/data/src/main/java/org/thingsboard/server/common/data/oauth2/OAuth2ClientsParams.java
  5. 12
      dao/src/main/java/org/thingsboard/server/dao/oauth2/OAuth2Utils.java
  6. 93
      dao/src/test/java/org/thingsboard/server/dao/service/BaseOAuth2ServiceTest.java
  7. 4
      ui-ngx/src/app/core/http/oauth2.service.ts
  8. 31
      ui-ngx/src/app/modules/home/pages/admin/admin-routing.module.ts
  9. 31
      ui-ngx/src/app/modules/home/pages/admin/oauth2-settings.component.ts

1
application/src/main/data/json/system/oauth2_config_templates/github_config.json

@ -10,6 +10,7 @@
"mapperConfig": { "mapperConfig": {
"type": "GITHUB", "type": "GITHUB",
"basic": { "basic": {
"firstNameAttributeKey": "name",
"tenantNameStrategy": "DOMAIN" "tenantNameStrategy": "DOMAIN"
} }
}, },

18
application/src/main/java/org/thingsboard/server/controller/OAuth2Controller.java

@ -16,6 +16,7 @@
package org.thingsboard.server.controller; package org.thingsboard.server.controller;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -23,6 +24,7 @@ import org.thingsboard.server.common.data.exception.ThingsboardException;
import org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo; import org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo;
import org.thingsboard.server.common.data.oauth2.OAuth2ClientsParams; import org.thingsboard.server.common.data.oauth2.OAuth2ClientsParams;
import org.thingsboard.server.common.data.oauth2.SchemeType; import org.thingsboard.server.common.data.oauth2.SchemeType;
import org.thingsboard.server.dao.oauth2.OAuth2Configuration;
import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.queue.util.TbCoreComponent;
import org.thingsboard.server.service.security.permission.Operation; import org.thingsboard.server.service.security.permission.Operation;
import org.thingsboard.server.service.security.permission.Resource; import org.thingsboard.server.service.security.permission.Resource;
@ -36,6 +38,10 @@ import java.util.List;
@RequestMapping("/api") @RequestMapping("/api")
@Slf4j @Slf4j
public class OAuth2Controller extends BaseController { public class OAuth2Controller extends BaseController {
@Autowired
private OAuth2Configuration oAuth2Configuration;
@RequestMapping(value = "/noauth/oauth2Clients", method = RequestMethod.POST) @RequestMapping(value = "/noauth/oauth2Clients", method = RequestMethod.POST)
@ResponseBody @ResponseBody
public List<OAuth2ClientInfo> getOAuth2Clients(HttpServletRequest request) throws ThingsboardException { public List<OAuth2ClientInfo> getOAuth2Clients(HttpServletRequest request) throws ThingsboardException {
@ -70,4 +76,16 @@ public class OAuth2Controller extends BaseController {
throw handleException(e); throw handleException(e);
} }
} }
@PreAuthorize("hasAnyAuthority('SYS_ADMIN')")
@RequestMapping(value = "/oauth2/loginProcessingUrl", method = RequestMethod.GET)
@ResponseBody
public String getLoginProcessingUrl() throws ThingsboardException {
try {
accessControlService.checkPermission(getCurrentUser(), Resource.OAUTH2_CONFIGURATION_INFO, Operation.READ);
return "\"" + oAuth2Configuration.getLoginProcessingUrl() + "\"";
} catch (Exception e) {
throw handleException(e);
}
}
} }

6
common/data/src/main/java/org/thingsboard/server/common/data/oauth2/OAuth2ClientsDomainParams.java

@ -27,6 +27,6 @@ import java.util.Set;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public class OAuth2ClientsDomainParams { public class OAuth2ClientsDomainParams {
private Set<DomainInfo> domainInfos; private List<DomainInfo> domainInfos;
private Set<ClientRegistrationDto> clientRegistrations; private List<ClientRegistrationDto> clientRegistrations;
} }

6
common/data/src/main/java/org/thingsboard/server/common/data/oauth2/OAuth2ClientsParams.java

@ -16,6 +16,8 @@
package org.thingsboard.server.common.data.oauth2; package org.thingsboard.server.common.data.oauth2;
import lombok.*; import lombok.*;
import java.util.List;
import java.util.Set; import java.util.Set;
@EqualsAndHashCode @EqualsAndHashCode
@ -26,5 +28,5 @@ import java.util.Set;
@AllArgsConstructor @AllArgsConstructor
public class OAuth2ClientsParams { public class OAuth2ClientsParams {
private boolean enabled; private boolean enabled;
private Set<OAuth2ClientsDomainParams> domainsParams; private List<OAuth2ClientsDomainParams> domainsParams;
} }

12
dao/src/main/java/org/thingsboard/server/dao/oauth2/OAuth2Utils.java

@ -32,19 +32,19 @@ public class OAuth2Utils {
} }
public static OAuth2ClientsParams toOAuth2Params(List<ExtendedOAuth2ClientRegistrationInfo> extendedOAuth2ClientRegistrationInfos) { public static OAuth2ClientsParams toOAuth2Params(List<ExtendedOAuth2ClientRegistrationInfo> extendedOAuth2ClientRegistrationInfos) {
Map<OAuth2ClientRegistrationInfoId, Set<DomainInfo>> domainsByInfoId = new HashMap<>(); Map<OAuth2ClientRegistrationInfoId, List<DomainInfo>> domainsByInfoId = new LinkedHashMap<>();
Map<OAuth2ClientRegistrationInfoId, OAuth2ClientRegistrationInfo> infoById = new HashMap<>(); Map<OAuth2ClientRegistrationInfoId, OAuth2ClientRegistrationInfo> infoById = new LinkedHashMap<>();
for (ExtendedOAuth2ClientRegistrationInfo extendedClientRegistrationInfo : extendedOAuth2ClientRegistrationInfos) { for (ExtendedOAuth2ClientRegistrationInfo extendedClientRegistrationInfo : extendedOAuth2ClientRegistrationInfos) {
String domainName = extendedClientRegistrationInfo.getDomainName(); String domainName = extendedClientRegistrationInfo.getDomainName();
SchemeType domainScheme = extendedClientRegistrationInfo.getDomainScheme(); SchemeType domainScheme = extendedClientRegistrationInfo.getDomainScheme();
domainsByInfoId.computeIfAbsent(extendedClientRegistrationInfo.getId(), key -> new HashSet<>()) domainsByInfoId.computeIfAbsent(extendedClientRegistrationInfo.getId(), key -> new ArrayList<>())
.add(new DomainInfo(domainScheme, domainName)); .add(new DomainInfo(domainScheme, domainName));
infoById.put(extendedClientRegistrationInfo.getId(), extendedClientRegistrationInfo); infoById.put(extendedClientRegistrationInfo.getId(), extendedClientRegistrationInfo);
} }
Map<Set<DomainInfo>, OAuth2ClientsDomainParams> domainParamsMap = new HashMap<>(); Map<List<DomainInfo>, OAuth2ClientsDomainParams> domainParamsMap = new HashMap<>();
domainsByInfoId.forEach((clientRegistrationInfoId, domainInfos) -> { domainsByInfoId.forEach((clientRegistrationInfoId, domainInfos) -> {
domainParamsMap.computeIfAbsent(domainInfos, domainParamsMap.computeIfAbsent(domainInfos,
key -> new OAuth2ClientsDomainParams(key, new HashSet<>()) key -> new OAuth2ClientsDomainParams(key, new ArrayList<>())
) )
.getClientRegistrations() .getClientRegistrations()
.add(toClientRegistrationDto(infoById.get(clientRegistrationInfoId))); .add(toClientRegistrationDto(infoById.get(clientRegistrationInfoId)));
@ -52,7 +52,7 @@ public class OAuth2Utils {
boolean enabled = extendedOAuth2ClientRegistrationInfos.stream() boolean enabled = extendedOAuth2ClientRegistrationInfos.stream()
.map(OAuth2ClientRegistrationInfo::isEnabled) .map(OAuth2ClientRegistrationInfo::isEnabled)
.findFirst().orElse(false); .findFirst().orElse(false);
return new OAuth2ClientsParams(enabled, new HashSet<>(domainParamsMap.values())); return new OAuth2ClientsParams(enabled, new ArrayList<>(domainParamsMap.values()));
} }
public static ClientRegistrationDto toClientRegistrationDto(OAuth2ClientRegistrationInfo oAuth2ClientRegistrationInfo) { public static ClientRegistrationDto toClientRegistrationDto(OAuth2ClientRegistrationInfo oAuth2ClientRegistrationInfo) {

93
dao/src/test/java/org/thingsboard/server/dao/service/BaseOAuth2ServiceTest.java

@ -15,6 +15,7 @@
*/ */
package org.thingsboard.server.dao.service; package org.thingsboard.server.dao.service;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import org.junit.After; import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
@ -29,7 +30,7 @@ import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class BaseOAuth2ServiceTest extends AbstractServiceTest { public class BaseOAuth2ServiceTest extends AbstractServiceTest {
private static final OAuth2ClientsParams EMPTY_PARAMS = new OAuth2ClientsParams(false, new HashSet<>()); private static final OAuth2ClientsParams EMPTY_PARAMS = new OAuth2ClientsParams(false, new ArrayList<>());
@Autowired @Autowired
protected OAuth2Service oAuth2Service; protected OAuth2Service oAuth2Service;
@ -48,14 +49,14 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest {
@Test(expected = DataValidationException.class) @Test(expected = DataValidationException.class)
public void testSaveHttpAndMixedDomainsTogether() { public void testSaveHttpAndMixedDomainsTogether() {
OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Sets.newHashSet( OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Lists.newArrayList(
OAuth2ClientsDomainParams.builder() OAuth2ClientsDomainParams.builder()
.domainInfos(Sets.newHashSet( .domainInfos(Lists.newArrayList(
DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(), DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(),
DomainInfo.builder().name("first-domain").scheme(SchemeType.MIXED).build(), DomainInfo.builder().name("first-domain").scheme(SchemeType.MIXED).build(),
DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build() DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build()
)) ))
.clientRegistrations(Sets.newHashSet( .clientRegistrations(Lists.newArrayList(
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto() validClientRegistrationDto()
@ -67,14 +68,14 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest {
@Test(expected = DataValidationException.class) @Test(expected = DataValidationException.class)
public void testSaveHttpsAndMixedDomainsTogether() { public void testSaveHttpsAndMixedDomainsTogether() {
OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Sets.newHashSet( OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Lists.newArrayList(
OAuth2ClientsDomainParams.builder() OAuth2ClientsDomainParams.builder()
.domainInfos(Sets.newHashSet( .domainInfos(Lists.newArrayList(
DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTPS).build(), DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTPS).build(),
DomainInfo.builder().name("first-domain").scheme(SchemeType.MIXED).build(), DomainInfo.builder().name("first-domain").scheme(SchemeType.MIXED).build(),
DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build() DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build()
)) ))
.clientRegistrations(Sets.newHashSet( .clientRegistrations(Lists.newArrayList(
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto() validClientRegistrationDto()
@ -131,20 +132,20 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest {
Assert.assertNotNull(foundClientsParams); Assert.assertNotNull(foundClientsParams);
Assert.assertEquals(clientsParams, foundClientsParams); Assert.assertEquals(clientsParams, foundClientsParams);
OAuth2ClientsParams newClientsParams = new OAuth2ClientsParams(true, Sets.newHashSet( OAuth2ClientsParams newClientsParams = new OAuth2ClientsParams(true, Lists.newArrayList(
OAuth2ClientsDomainParams.builder() OAuth2ClientsDomainParams.builder()
.domainInfos(Sets.newHashSet( .domainInfos(Lists.newArrayList(
DomainInfo.builder().name("another-domain").scheme(SchemeType.HTTPS).build() DomainInfo.builder().name("another-domain").scheme(SchemeType.HTTPS).build()
)) ))
.clientRegistrations(Sets.newHashSet( .clientRegistrations(Lists.newArrayList(
validClientRegistrationDto() validClientRegistrationDto()
)) ))
.build(), .build(),
OAuth2ClientsDomainParams.builder() OAuth2ClientsDomainParams.builder()
.domainInfos(Sets.newHashSet( .domainInfos(Lists.newArrayList(
DomainInfo.builder().name("test-domain").scheme(SchemeType.MIXED).build() DomainInfo.builder().name("test-domain").scheme(SchemeType.MIXED).build()
)) ))
.clientRegistrations(Sets.newHashSet( .clientRegistrations(Lists.newArrayList(
validClientRegistrationDto() validClientRegistrationDto()
)) ))
.build() .build()
@ -157,22 +158,22 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest {
@Test @Test
public void testGetOAuth2Clients() { public void testGetOAuth2Clients() {
Set<ClientRegistrationDto> firstGroup = Sets.newHashSet( List<ClientRegistrationDto> firstGroup = Lists.newArrayList(
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto() validClientRegistrationDto()
); );
Set<ClientRegistrationDto> secondGroup = Sets.newHashSet( List<ClientRegistrationDto> secondGroup = Lists.newArrayList(
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto() validClientRegistrationDto()
); );
Set<ClientRegistrationDto> thirdGroup = Sets.newHashSet( List<ClientRegistrationDto> thirdGroup = Lists.newArrayList(
validClientRegistrationDto() validClientRegistrationDto()
); );
OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Sets.newHashSet( OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Lists.newArrayList(
OAuth2ClientsDomainParams.builder() OAuth2ClientsDomainParams.builder()
.domainInfos(Sets.newHashSet( .domainInfos(Lists.newArrayList(
DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(), DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(),
DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(), DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(),
DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build() DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build()
@ -180,14 +181,14 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest {
.clientRegistrations(firstGroup) .clientRegistrations(firstGroup)
.build(), .build(),
OAuth2ClientsDomainParams.builder() OAuth2ClientsDomainParams.builder()
.domainInfos(Sets.newHashSet( .domainInfos(Lists.newArrayList(
DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTP).build(), DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTP).build(),
DomainInfo.builder().name("fourth-domain").scheme(SchemeType.MIXED).build() DomainInfo.builder().name("fourth-domain").scheme(SchemeType.MIXED).build()
)) ))
.clientRegistrations(secondGroup) .clientRegistrations(secondGroup)
.build(), .build(),
OAuth2ClientsDomainParams.builder() OAuth2ClientsDomainParams.builder()
.domainInfos(Sets.newHashSet( .domainInfos(Lists.newArrayList(
DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTPS).build(), DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTPS).build(),
DomainInfo.builder().name("fifth-domain").scheme(SchemeType.HTTP).build() DomainInfo.builder().name("fifth-domain").scheme(SchemeType.HTTP).build()
)) ))
@ -285,15 +286,15 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest {
@Test @Test
public void testGetOAuth2ClientsForHttpAndHttps() { public void testGetOAuth2ClientsForHttpAndHttps() {
Set<ClientRegistrationDto> firstGroup = Sets.newHashSet( List<ClientRegistrationDto> firstGroup = Lists.newArrayList(
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto() validClientRegistrationDto()
); );
OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Sets.newHashSet( OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Lists.newArrayList(
OAuth2ClientsDomainParams.builder() OAuth2ClientsDomainParams.builder()
.domainInfos(Sets.newHashSet( .domainInfos(Lists.newArrayList(
DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(), DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(),
DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(), DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(),
DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTPS).build() DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTPS).build()
@ -335,25 +336,25 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest {
@Test @Test
public void testGetDisabledOAuth2Clients() { public void testGetDisabledOAuth2Clients() {
OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Sets.newHashSet( OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Lists.newArrayList(
OAuth2ClientsDomainParams.builder() OAuth2ClientsDomainParams.builder()
.domainInfos(Sets.newHashSet( .domainInfos(Lists.newArrayList(
DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(), DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(),
DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(), DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(),
DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build() DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build()
)) ))
.clientRegistrations(Sets.newHashSet( .clientRegistrations(Lists.newArrayList(
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto() validClientRegistrationDto()
)) ))
.build(), .build(),
OAuth2ClientsDomainParams.builder() OAuth2ClientsDomainParams.builder()
.domainInfos(Sets.newHashSet( .domainInfos(Lists.newArrayList(
DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTP).build(), DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTP).build(),
DomainInfo.builder().name("fourth-domain").scheme(SchemeType.MIXED).build() DomainInfo.builder().name("fourth-domain").scheme(SchemeType.MIXED).build()
)) ))
.clientRegistrations(Sets.newHashSet( .clientRegistrations(Lists.newArrayList(
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto() validClientRegistrationDto()
)) ))
@ -374,35 +375,35 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest {
@Test @Test
public void testFindAllClientRegistrationInfos() { public void testFindAllClientRegistrationInfos() {
OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Sets.newHashSet( OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Lists.newArrayList(
OAuth2ClientsDomainParams.builder() OAuth2ClientsDomainParams.builder()
.domainInfos(Sets.newHashSet( .domainInfos(Lists.newArrayList(
DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(), DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(),
DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(), DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(),
DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build() DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build()
)) ))
.clientRegistrations(Sets.newHashSet( .clientRegistrations(Lists.newArrayList(
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto() validClientRegistrationDto()
)) ))
.build(), .build(),
OAuth2ClientsDomainParams.builder() OAuth2ClientsDomainParams.builder()
.domainInfos(Sets.newHashSet( .domainInfos(Lists.newArrayList(
DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTP).build(), DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTP).build(),
DomainInfo.builder().name("fourth-domain").scheme(SchemeType.MIXED).build() DomainInfo.builder().name("fourth-domain").scheme(SchemeType.MIXED).build()
)) ))
.clientRegistrations(Sets.newHashSet( .clientRegistrations(Lists.newArrayList(
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto() validClientRegistrationDto()
)) ))
.build(), .build(),
OAuth2ClientsDomainParams.builder() OAuth2ClientsDomainParams.builder()
.domainInfos(Sets.newHashSet( .domainInfos(Lists.newArrayList(
DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTPS).build(), DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTPS).build(),
DomainInfo.builder().name("fifth-domain").scheme(SchemeType.HTTP).build() DomainInfo.builder().name("fifth-domain").scheme(SchemeType.HTTP).build()
)) ))
.clientRegistrations(Sets.newHashSet( .clientRegistrations(Lists.newArrayList(
validClientRegistrationDto() validClientRegistrationDto()
)) ))
.build() .build()
@ -423,35 +424,35 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest {
@Test @Test
public void testFindClientRegistrationById() { public void testFindClientRegistrationById() {
OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Sets.newHashSet( OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Lists.newArrayList(
OAuth2ClientsDomainParams.builder() OAuth2ClientsDomainParams.builder()
.domainInfos(Sets.newHashSet( .domainInfos(Lists.newArrayList(
DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(), DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(),
DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(), DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(),
DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build() DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build()
)) ))
.clientRegistrations(Sets.newHashSet( .clientRegistrations(Lists.newArrayList(
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto() validClientRegistrationDto()
)) ))
.build(), .build(),
OAuth2ClientsDomainParams.builder() OAuth2ClientsDomainParams.builder()
.domainInfos(Sets.newHashSet( .domainInfos(Lists.newArrayList(
DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTP).build(), DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTP).build(),
DomainInfo.builder().name("fourth-domain").scheme(SchemeType.MIXED).build() DomainInfo.builder().name("fourth-domain").scheme(SchemeType.MIXED).build()
)) ))
.clientRegistrations(Sets.newHashSet( .clientRegistrations(Lists.newArrayList(
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto() validClientRegistrationDto()
)) ))
.build(), .build(),
OAuth2ClientsDomainParams.builder() OAuth2ClientsDomainParams.builder()
.domainInfos(Sets.newHashSet( .domainInfos(Lists.newArrayList(
DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTPS).build(), DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTPS).build(),
DomainInfo.builder().name("fifth-domain").scheme(SchemeType.HTTP).build() DomainInfo.builder().name("fifth-domain").scheme(SchemeType.HTTP).build()
)) ))
.clientRegistrations(Sets.newHashSet( .clientRegistrations(Lists.newArrayList(
validClientRegistrationDto() validClientRegistrationDto()
)) ))
.build() .build()
@ -466,14 +467,14 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest {
} }
private OAuth2ClientsParams createDefaultClientsParams() { private OAuth2ClientsParams createDefaultClientsParams() {
return new OAuth2ClientsParams(true, Sets.newHashSet( return new OAuth2ClientsParams(true, Lists.newArrayList(
OAuth2ClientsDomainParams.builder() OAuth2ClientsDomainParams.builder()
.domainInfos(Sets.newHashSet( .domainInfos(Lists.newArrayList(
DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(), DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(),
DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(), DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(),
DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build() DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build()
)) ))
.clientRegistrations(Sets.newHashSet( .clientRegistrations(Lists.newArrayList(
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto(), validClientRegistrationDto(),
@ -481,11 +482,11 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest {
)) ))
.build(), .build(),
OAuth2ClientsDomainParams.builder() OAuth2ClientsDomainParams.builder()
.domainInfos(Sets.newHashSet( .domainInfos(Lists.newArrayList(
DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(), DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(),
DomainInfo.builder().name("fourth-domain").scheme(SchemeType.MIXED).build() DomainInfo.builder().name("fourth-domain").scheme(SchemeType.MIXED).build()
)) ))
.clientRegistrations(Sets.newHashSet( .clientRegistrations(Lists.newArrayList(
validClientRegistrationDto(), validClientRegistrationDto(),
validClientRegistrationDto() validClientRegistrationDto()
)) ))

4
ui-ngx/src/app/core/http/oauth2.service.ts

@ -41,4 +41,8 @@ export class OAuth2Service {
return this.http.post<OAuth2ClientsParams>('/api/oauth2/config', OAuth2Setting, return this.http.post<OAuth2ClientsParams>('/api/oauth2/config', OAuth2Setting,
defaultHttpOptionsFromConfig(config)); defaultHttpOptionsFromConfig(config));
} }
public getLoginProcessingUrl(config?: RequestConfig): Observable<string> {
return this.http.get<string>(`/api/oauth2/loginProcessingUrl`, defaultHttpOptionsFromConfig(config));
}
} }

31
ui-ngx/src/app/modules/home/pages/admin/admin-routing.module.ts

@ -14,8 +14,8 @@
/// limitations under the License. /// limitations under the License.
/// ///
import { NgModule } from '@angular/core'; import { Injectable, NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router'; import { Resolve, RouterModule, Routes } from '@angular/router';
import { MailServerComponent } from '@modules/home/pages/admin/mail-server.component'; import { MailServerComponent } from '@modules/home/pages/admin/mail-server.component';
import { ConfirmOnExitGuard } from '@core/guards/confirm-on-exit.guard'; import { ConfirmOnExitGuard } from '@core/guards/confirm-on-exit.guard';
@ -23,6 +23,25 @@ import { Authority } from '@shared/models/authority.enum';
import { GeneralSettingsComponent } from '@modules/home/pages/admin/general-settings.component'; import { GeneralSettingsComponent } from '@modules/home/pages/admin/general-settings.component';
import { SecuritySettingsComponent } from '@modules/home/pages/admin/security-settings.component'; import { SecuritySettingsComponent } from '@modules/home/pages/admin/security-settings.component';
import { OAuth2SettingsComponent } from '@home/pages/admin/oauth2-settings.component'; import { OAuth2SettingsComponent } from '@home/pages/admin/oauth2-settings.component';
import { User } from '@shared/models/user.model';
import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
import { UserService } from '@core/http/user.service';
import { Observable } from 'rxjs';
import { getCurrentAuthUser } from '@core/auth/auth.selectors';
import { OAuth2Service } from '@core/http/oauth2.service';
import { UserProfileResolver } from '@home/pages/profile/profile-routing.module';
@Injectable()
export class OAuth2LoginProcessingUrlResolver implements Resolve<string> {
constructor(private oauth2Service: OAuth2Service) {
}
resolve(): Observable<string> {
return this.oauth2Service.getLoginProcessingUrl();
}
}
const routes: Routes = [ const routes: Routes = [
{ {
@ -90,6 +109,9 @@ const routes: Routes = [
label: 'admin.oauth2.oauth2', label: 'admin.oauth2.oauth2',
icon: 'security' icon: 'security'
} }
},
resolve: {
loginProcessingUrl: OAuth2LoginProcessingUrlResolver
} }
} }
] ]
@ -98,6 +120,9 @@ const routes: Routes = [
@NgModule({ @NgModule({
imports: [RouterModule.forChild(routes)], imports: [RouterModule.forChild(routes)],
exports: [RouterModule] exports: [RouterModule],
providers: [
OAuth2LoginProcessingUrlResolver
]
}) })
export class AdminRoutingModule { } export class AdminRoutingModule { }

31
ui-ngx/src/app/modules/home/pages/admin/oauth2-settings.component.ts

@ -43,6 +43,7 @@ import { DialogService } from '@core/services/dialog.service';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { isDefined, isDefinedAndNotNull } from '@core/utils'; import { isDefined, isDefinedAndNotNull } from '@core/utils';
import { OAuth2Service } from '@core/http/oauth2.service'; import { OAuth2Service } from '@core/http/oauth2.service';
import { ActivatedRoute } from '@angular/router';
@Component({ @Component({
selector: 'tb-oauth2-settings', selector: 'tb-oauth2-settings',
@ -87,7 +88,10 @@ export class OAuth2SettingsComponent extends PageComponent implements OnInit, Ha
templateProvider = ['Custom']; templateProvider = ['Custom'];
private loginProcessingUrl: string = this.route.snapshot.data.loginProcessingUrl;
constructor(protected store: Store<AppState>, constructor(protected store: Store<AppState>,
private route: ActivatedRoute,
private oauth2Service: OAuth2Service, private oauth2Service: OAuth2Service,
private fb: FormBuilder, private fb: FormBuilder,
private dialogService: DialogService, private dialogService: DialogService,
@ -130,7 +134,7 @@ export class OAuth2SettingsComponent extends PageComponent implements OnInit, Ha
return this.oauth2SettingsForm.get('domainsParams') as FormArray; return this.oauth2SettingsForm.get('domainsParams') as FormArray;
} }
private formBasicGroup(mapperConfigBasic?: MapperConfigBasic): FormGroup { private formBasicGroup(type: MapperConfigType, mapperConfigBasic?: MapperConfigBasic): FormGroup {
let tenantNamePattern; let tenantNamePattern;
if (mapperConfigBasic?.tenantNamePattern) { if (mapperConfigBasic?.tenantNamePattern) {
tenantNamePattern = mapperConfigBasic.tenantNamePattern; tenantNamePattern = mapperConfigBasic.tenantNamePattern;
@ -138,16 +142,20 @@ export class OAuth2SettingsComponent extends PageComponent implements OnInit, Ha
tenantNamePattern = {value: null, disabled: true}; tenantNamePattern = {value: null, disabled: true};
} }
const basicGroup = this.fb.group({ const basicGroup = this.fb.group({
emailAttributeKey: [mapperConfigBasic?.emailAttributeKey ? mapperConfigBasic.emailAttributeKey : 'email', Validators.required],
firstNameAttributeKey: [mapperConfigBasic?.firstNameAttributeKey ? mapperConfigBasic.firstNameAttributeKey : ''], firstNameAttributeKey: [mapperConfigBasic?.firstNameAttributeKey ? mapperConfigBasic.firstNameAttributeKey : ''],
lastNameAttributeKey: [mapperConfigBasic?.lastNameAttributeKey ? mapperConfigBasic.lastNameAttributeKey : ''], lastNameAttributeKey: [mapperConfigBasic?.lastNameAttributeKey ? mapperConfigBasic.lastNameAttributeKey : ''],
tenantNameStrategy: [mapperConfigBasic?.tenantNameStrategy ? mapperConfigBasic.tenantNameStrategy : TenantNameStrategy.DOMAIN], tenantNameStrategy: [mapperConfigBasic?.tenantNameStrategy ? mapperConfigBasic.tenantNameStrategy : TenantNameStrategy.DOMAIN],
tenantNamePattern: [tenantNamePattern, Validators.required], tenantNamePattern: [tenantNamePattern, Validators.required],
customerNamePattern: [mapperConfigBasic?.customerNamePattern ? mapperConfigBasic.customerNamePattern : null], customerNamePattern: [mapperConfigBasic?.customerNamePattern ? mapperConfigBasic.customerNamePattern : null],
defaultDashboardName: [mapperConfigBasic?.defaultDashboardName ? mapperConfigBasic.defaultDashboardName : null], defaultDashboardName: [mapperConfigBasic?.defaultDashboardName ? mapperConfigBasic.defaultDashboardName : null],
alwaysFullScreen: [mapperConfigBasic?.alwaysFullScreen ? mapperConfigBasic.alwaysFullScreen : false] alwaysFullScreen: [isDefinedAndNotNull(mapperConfigBasic?.alwaysFullScreen) ? mapperConfigBasic.alwaysFullScreen : false]
}); });
if (MapperConfigType.GITHUB !== type) {
basicGroup.addControl('emailAttributeKey',
this.fb.control( mapperConfigBasic?.emailAttributeKey ? mapperConfigBasic.emailAttributeKey : 'email', Validators.required));
}
this.subscriptions.push(basicGroup.get('tenantNameStrategy').valueChanges.subscribe((domain) => { this.subscriptions.push(basicGroup.get('tenantNameStrategy').valueChanges.subscribe((domain) => {
if (domain === 'CUSTOM') { if (domain === 'CUSTOM') {
basicGroup.get('tenantNamePattern').enable(); basicGroup.get('tenantNamePattern').enable();
@ -279,9 +287,12 @@ export class OAuth2SettingsComponent extends PageComponent implements OnInit, Ha
clientRegistration?.userNameAttributeName ? clientRegistration.userNameAttributeName : 'email', Validators.required], clientRegistration?.userNameAttributeName ? clientRegistration.userNameAttributeName : 'email', Validators.required],
mapperConfig: this.fb.group({ mapperConfig: this.fb.group({
allowUserCreation: [ allowUserCreation: [
clientRegistration?.mapperConfig?.allowUserCreation ? clientRegistration.mapperConfig.allowUserCreation : true isDefinedAndNotNull(clientRegistration?.mapperConfig?.allowUserCreation) ?
clientRegistration.mapperConfig.allowUserCreation : true
],
activateUser: [
isDefinedAndNotNull(clientRegistration?.mapperConfig?.activateUser) ? clientRegistration.mapperConfig.activateUser : false
], ],
activateUser: [clientRegistration?.mapperConfig?.activateUser ? clientRegistration.mapperConfig.activateUser : false],
type: [ type: [
clientRegistration?.mapperConfig?.type ? clientRegistration.mapperConfig.type : MapperConfigType.BASIC, Validators.required clientRegistration?.mapperConfig?.type ? clientRegistration.mapperConfig.type : MapperConfigType.BASIC, Validators.required
] ]
@ -308,7 +319,7 @@ export class OAuth2SettingsComponent extends PageComponent implements OnInit, Ha
return clientRegistrationFormGroup; return clientRegistrationFormGroup;
} }
private validateScope (control: AbstractControl): ValidationErrors | null { private validateScope(control: AbstractControl): ValidationErrors | null {
const scope: string[] = control.value; const scope: string[] = control.value;
if (!scope || !scope.length) { if (!scope || !scope.length) {
return { return {
@ -347,7 +358,11 @@ export class OAuth2SettingsComponent extends PageComponent implements OnInit, Ha
mapperConfig.addControl('custom', this.formCustomGroup(predefinedValue?.custom)); mapperConfig.addControl('custom', this.formCustomGroup(predefinedValue?.custom));
} else { } else {
mapperConfig.removeControl('custom'); mapperConfig.removeControl('custom');
mapperConfig.addControl('basic', this.formBasicGroup(predefinedValue?.basic)); if (mapperConfig.get('basic')) {
mapperConfig.setControl('basic', this.formBasicGroup(type, predefinedValue?.basic));
} else {
mapperConfig.addControl('basic', this.formBasicGroup(type, predefinedValue?.basic));
}
} }
} }
@ -490,7 +505,7 @@ export class OAuth2SettingsComponent extends PageComponent implements OnInit, Ha
} else { } else {
protocol = domainInfo.scheme === DomainSchema.MIXED ? DomainSchema.HTTPS.toLowerCase() : domainInfo.scheme.toLowerCase(); protocol = domainInfo.scheme === DomainSchema.MIXED ? DomainSchema.HTTPS.toLowerCase() : domainInfo.scheme.toLowerCase();
} }
return `${protocol}://${domainInfo.name}/login/oauth2/code/`; return `${protocol}://${domainInfo.name}${this.loginProcessingUrl}`;
} }
return ''; return '';
} }

Loading…
Cancel
Save