diff --git a/application/src/main/data/json/system/oauth2_config_templates/github_config.json b/application/src/main/data/json/system/oauth2_config_templates/github_config.json index 0a1ae5779b..439043d961 100644 --- a/application/src/main/data/json/system/oauth2_config_templates/github_config.json +++ b/application/src/main/data/json/system/oauth2_config_templates/github_config.json @@ -10,6 +10,7 @@ "mapperConfig": { "type": "GITHUB", "basic": { + "firstNameAttributeKey": "name", "tenantNameStrategy": "DOMAIN" } }, diff --git a/application/src/main/java/org/thingsboard/server/controller/OAuth2Controller.java b/application/src/main/java/org/thingsboard/server/controller/OAuth2Controller.java index 323eb19bcd..fc5e0a1426 100644 --- a/application/src/main/java/org/thingsboard/server/controller/OAuth2Controller.java +++ b/application/src/main/java/org/thingsboard/server/controller/OAuth2Controller.java @@ -16,6 +16,7 @@ package org.thingsboard.server.controller; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.security.access.prepost.PreAuthorize; 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.OAuth2ClientsParams; 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.service.security.permission.Operation; import org.thingsboard.server.service.security.permission.Resource; @@ -36,6 +38,10 @@ import java.util.List; @RequestMapping("/api") @Slf4j public class OAuth2Controller extends BaseController { + + @Autowired + private OAuth2Configuration oAuth2Configuration; + @RequestMapping(value = "/noauth/oauth2Clients", method = RequestMethod.POST) @ResponseBody public List getOAuth2Clients(HttpServletRequest request) throws ThingsboardException { @@ -70,4 +76,16 @@ public class OAuth2Controller extends BaseController { 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); + } + } } diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/oauth2/OAuth2ClientsDomainParams.java b/common/data/src/main/java/org/thingsboard/server/common/data/oauth2/OAuth2ClientsDomainParams.java index d93401f5cc..4e932fad85 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/oauth2/OAuth2ClientsDomainParams.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/oauth2/OAuth2ClientsDomainParams.java @@ -27,6 +27,6 @@ import java.util.Set; @NoArgsConstructor @AllArgsConstructor public class OAuth2ClientsDomainParams { - private Set domainInfos; - private Set clientRegistrations; -} \ No newline at end of file + private List domainInfos; + private List clientRegistrations; +} diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/oauth2/OAuth2ClientsParams.java b/common/data/src/main/java/org/thingsboard/server/common/data/oauth2/OAuth2ClientsParams.java index ee20021aca..ad5d5305bd 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/oauth2/OAuth2ClientsParams.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/oauth2/OAuth2ClientsParams.java @@ -16,6 +16,8 @@ package org.thingsboard.server.common.data.oauth2; import lombok.*; + +import java.util.List; import java.util.Set; @EqualsAndHashCode @@ -26,5 +28,5 @@ import java.util.Set; @AllArgsConstructor public class OAuth2ClientsParams { private boolean enabled; - private Set domainsParams; -} \ No newline at end of file + private List domainsParams; +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/oauth2/OAuth2Utils.java b/dao/src/main/java/org/thingsboard/server/dao/oauth2/OAuth2Utils.java index a96a0d36c8..1ed0332bf8 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/oauth2/OAuth2Utils.java +++ b/dao/src/main/java/org/thingsboard/server/dao/oauth2/OAuth2Utils.java @@ -32,19 +32,19 @@ public class OAuth2Utils { } public static OAuth2ClientsParams toOAuth2Params(List extendedOAuth2ClientRegistrationInfos) { - Map> domainsByInfoId = new HashMap<>(); - Map infoById = new HashMap<>(); + Map> domainsByInfoId = new LinkedHashMap<>(); + Map infoById = new LinkedHashMap<>(); for (ExtendedOAuth2ClientRegistrationInfo extendedClientRegistrationInfo : extendedOAuth2ClientRegistrationInfos) { String domainName = extendedClientRegistrationInfo.getDomainName(); SchemeType domainScheme = extendedClientRegistrationInfo.getDomainScheme(); - domainsByInfoId.computeIfAbsent(extendedClientRegistrationInfo.getId(), key -> new HashSet<>()) + domainsByInfoId.computeIfAbsent(extendedClientRegistrationInfo.getId(), key -> new ArrayList<>()) .add(new DomainInfo(domainScheme, domainName)); infoById.put(extendedClientRegistrationInfo.getId(), extendedClientRegistrationInfo); } - Map, OAuth2ClientsDomainParams> domainParamsMap = new HashMap<>(); + Map, OAuth2ClientsDomainParams> domainParamsMap = new HashMap<>(); domainsByInfoId.forEach((clientRegistrationInfoId, domainInfos) -> { domainParamsMap.computeIfAbsent(domainInfos, - key -> new OAuth2ClientsDomainParams(key, new HashSet<>()) + key -> new OAuth2ClientsDomainParams(key, new ArrayList<>()) ) .getClientRegistrations() .add(toClientRegistrationDto(infoById.get(clientRegistrationInfoId))); @@ -52,7 +52,7 @@ public class OAuth2Utils { boolean enabled = extendedOAuth2ClientRegistrationInfos.stream() .map(OAuth2ClientRegistrationInfo::isEnabled) .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) { diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseOAuth2ServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseOAuth2ServiceTest.java index b54505a3d7..a927f1e78c 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseOAuth2ServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseOAuth2ServiceTest.java @@ -15,6 +15,7 @@ */ package org.thingsboard.server.dao.service; +import com.google.common.collect.Lists; import com.google.common.collect.Sets; import org.junit.After; import org.junit.Assert; @@ -29,7 +30,7 @@ import java.util.*; import java.util.stream.Collectors; 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 protected OAuth2Service oAuth2Service; @@ -48,14 +49,14 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { @Test(expected = DataValidationException.class) public void testSaveHttpAndMixedDomainsTogether() { - OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Sets.newHashSet( + OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Lists.newArrayList( OAuth2ClientsDomainParams.builder() - .domainInfos(Sets.newHashSet( + .domainInfos(Lists.newArrayList( DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(), DomainInfo.builder().name("first-domain").scheme(SchemeType.MIXED).build(), DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build() )) - .clientRegistrations(Sets.newHashSet( + .clientRegistrations(Lists.newArrayList( validClientRegistrationDto(), validClientRegistrationDto(), validClientRegistrationDto() @@ -67,14 +68,14 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { @Test(expected = DataValidationException.class) public void testSaveHttpsAndMixedDomainsTogether() { - OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Sets.newHashSet( + OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Lists.newArrayList( OAuth2ClientsDomainParams.builder() - .domainInfos(Sets.newHashSet( + .domainInfos(Lists.newArrayList( DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTPS).build(), DomainInfo.builder().name("first-domain").scheme(SchemeType.MIXED).build(), DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build() )) - .clientRegistrations(Sets.newHashSet( + .clientRegistrations(Lists.newArrayList( validClientRegistrationDto(), validClientRegistrationDto(), validClientRegistrationDto() @@ -131,20 +132,20 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { Assert.assertNotNull(foundClientsParams); Assert.assertEquals(clientsParams, foundClientsParams); - OAuth2ClientsParams newClientsParams = new OAuth2ClientsParams(true, Sets.newHashSet( + OAuth2ClientsParams newClientsParams = new OAuth2ClientsParams(true, Lists.newArrayList( OAuth2ClientsDomainParams.builder() - .domainInfos(Sets.newHashSet( + .domainInfos(Lists.newArrayList( DomainInfo.builder().name("another-domain").scheme(SchemeType.HTTPS).build() )) - .clientRegistrations(Sets.newHashSet( + .clientRegistrations(Lists.newArrayList( validClientRegistrationDto() )) .build(), OAuth2ClientsDomainParams.builder() - .domainInfos(Sets.newHashSet( + .domainInfos(Lists.newArrayList( DomainInfo.builder().name("test-domain").scheme(SchemeType.MIXED).build() )) - .clientRegistrations(Sets.newHashSet( + .clientRegistrations(Lists.newArrayList( validClientRegistrationDto() )) .build() @@ -157,22 +158,22 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { @Test public void testGetOAuth2Clients() { - Set firstGroup = Sets.newHashSet( + List firstGroup = Lists.newArrayList( validClientRegistrationDto(), validClientRegistrationDto(), validClientRegistrationDto(), validClientRegistrationDto() ); - Set secondGroup = Sets.newHashSet( + List secondGroup = Lists.newArrayList( validClientRegistrationDto(), validClientRegistrationDto() ); - Set thirdGroup = Sets.newHashSet( + List thirdGroup = Lists.newArrayList( validClientRegistrationDto() ); - OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Sets.newHashSet( + OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Lists.newArrayList( OAuth2ClientsDomainParams.builder() - .domainInfos(Sets.newHashSet( + .domainInfos(Lists.newArrayList( DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(), DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(), DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build() @@ -180,14 +181,14 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { .clientRegistrations(firstGroup) .build(), OAuth2ClientsDomainParams.builder() - .domainInfos(Sets.newHashSet( + .domainInfos(Lists.newArrayList( DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTP).build(), DomainInfo.builder().name("fourth-domain").scheme(SchemeType.MIXED).build() )) .clientRegistrations(secondGroup) .build(), OAuth2ClientsDomainParams.builder() - .domainInfos(Sets.newHashSet( + .domainInfos(Lists.newArrayList( DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTPS).build(), DomainInfo.builder().name("fifth-domain").scheme(SchemeType.HTTP).build() )) @@ -285,15 +286,15 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { @Test public void testGetOAuth2ClientsForHttpAndHttps() { - Set firstGroup = Sets.newHashSet( + List firstGroup = Lists.newArrayList( validClientRegistrationDto(), validClientRegistrationDto(), validClientRegistrationDto(), validClientRegistrationDto() ); - OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Sets.newHashSet( + OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Lists.newArrayList( OAuth2ClientsDomainParams.builder() - .domainInfos(Sets.newHashSet( + .domainInfos(Lists.newArrayList( DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(), DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(), DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTPS).build() @@ -335,25 +336,25 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { @Test public void testGetDisabledOAuth2Clients() { - OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Sets.newHashSet( + OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Lists.newArrayList( OAuth2ClientsDomainParams.builder() - .domainInfos(Sets.newHashSet( + .domainInfos(Lists.newArrayList( DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(), DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(), DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build() )) - .clientRegistrations(Sets.newHashSet( + .clientRegistrations(Lists.newArrayList( validClientRegistrationDto(), validClientRegistrationDto(), validClientRegistrationDto() )) .build(), OAuth2ClientsDomainParams.builder() - .domainInfos(Sets.newHashSet( + .domainInfos(Lists.newArrayList( DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTP).build(), DomainInfo.builder().name("fourth-domain").scheme(SchemeType.MIXED).build() )) - .clientRegistrations(Sets.newHashSet( + .clientRegistrations(Lists.newArrayList( validClientRegistrationDto(), validClientRegistrationDto() )) @@ -374,35 +375,35 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { @Test public void testFindAllClientRegistrationInfos() { - OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Sets.newHashSet( + OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Lists.newArrayList( OAuth2ClientsDomainParams.builder() - .domainInfos(Sets.newHashSet( + .domainInfos(Lists.newArrayList( DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(), DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(), DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build() )) - .clientRegistrations(Sets.newHashSet( + .clientRegistrations(Lists.newArrayList( validClientRegistrationDto(), validClientRegistrationDto(), validClientRegistrationDto() )) .build(), OAuth2ClientsDomainParams.builder() - .domainInfos(Sets.newHashSet( + .domainInfos(Lists.newArrayList( DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTP).build(), DomainInfo.builder().name("fourth-domain").scheme(SchemeType.MIXED).build() )) - .clientRegistrations(Sets.newHashSet( + .clientRegistrations(Lists.newArrayList( validClientRegistrationDto(), validClientRegistrationDto() )) .build(), OAuth2ClientsDomainParams.builder() - .domainInfos(Sets.newHashSet( + .domainInfos(Lists.newArrayList( DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTPS).build(), DomainInfo.builder().name("fifth-domain").scheme(SchemeType.HTTP).build() )) - .clientRegistrations(Sets.newHashSet( + .clientRegistrations(Lists.newArrayList( validClientRegistrationDto() )) .build() @@ -423,35 +424,35 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { @Test public void testFindClientRegistrationById() { - OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Sets.newHashSet( + OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Lists.newArrayList( OAuth2ClientsDomainParams.builder() - .domainInfos(Sets.newHashSet( + .domainInfos(Lists.newArrayList( DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(), DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(), DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build() )) - .clientRegistrations(Sets.newHashSet( + .clientRegistrations(Lists.newArrayList( validClientRegistrationDto(), validClientRegistrationDto(), validClientRegistrationDto() )) .build(), OAuth2ClientsDomainParams.builder() - .domainInfos(Sets.newHashSet( + .domainInfos(Lists.newArrayList( DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTP).build(), DomainInfo.builder().name("fourth-domain").scheme(SchemeType.MIXED).build() )) - .clientRegistrations(Sets.newHashSet( + .clientRegistrations(Lists.newArrayList( validClientRegistrationDto(), validClientRegistrationDto() )) .build(), OAuth2ClientsDomainParams.builder() - .domainInfos(Sets.newHashSet( + .domainInfos(Lists.newArrayList( DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTPS).build(), DomainInfo.builder().name("fifth-domain").scheme(SchemeType.HTTP).build() )) - .clientRegistrations(Sets.newHashSet( + .clientRegistrations(Lists.newArrayList( validClientRegistrationDto() )) .build() @@ -466,14 +467,14 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { } private OAuth2ClientsParams createDefaultClientsParams() { - return new OAuth2ClientsParams(true, Sets.newHashSet( + return new OAuth2ClientsParams(true, Lists.newArrayList( OAuth2ClientsDomainParams.builder() - .domainInfos(Sets.newHashSet( + .domainInfos(Lists.newArrayList( DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(), DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(), DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build() )) - .clientRegistrations(Sets.newHashSet( + .clientRegistrations(Lists.newArrayList( validClientRegistrationDto(), validClientRegistrationDto(), validClientRegistrationDto(), @@ -481,11 +482,11 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { )) .build(), OAuth2ClientsDomainParams.builder() - .domainInfos(Sets.newHashSet( + .domainInfos(Lists.newArrayList( DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(), DomainInfo.builder().name("fourth-domain").scheme(SchemeType.MIXED).build() )) - .clientRegistrations(Sets.newHashSet( + .clientRegistrations(Lists.newArrayList( validClientRegistrationDto(), validClientRegistrationDto() )) diff --git a/ui-ngx/src/app/core/http/oauth2.service.ts b/ui-ngx/src/app/core/http/oauth2.service.ts index a8f890223d..2eff9fa339 100644 --- a/ui-ngx/src/app/core/http/oauth2.service.ts +++ b/ui-ngx/src/app/core/http/oauth2.service.ts @@ -41,4 +41,8 @@ export class OAuth2Service { return this.http.post('/api/oauth2/config', OAuth2Setting, defaultHttpOptionsFromConfig(config)); } + + public getLoginProcessingUrl(config?: RequestConfig): Observable { + return this.http.get(`/api/oauth2/loginProcessingUrl`, defaultHttpOptionsFromConfig(config)); + } } diff --git a/ui-ngx/src/app/modules/home/pages/admin/admin-routing.module.ts b/ui-ngx/src/app/modules/home/pages/admin/admin-routing.module.ts index af7dc98e19..7fefbfb521 100644 --- a/ui-ngx/src/app/modules/home/pages/admin/admin-routing.module.ts +++ b/ui-ngx/src/app/modules/home/pages/admin/admin-routing.module.ts @@ -14,8 +14,8 @@ /// limitations under the License. /// -import { NgModule } from '@angular/core'; -import { RouterModule, Routes } from '@angular/router'; +import { Injectable, NgModule } from '@angular/core'; +import { Resolve, RouterModule, Routes } from '@angular/router'; import { MailServerComponent } from '@modules/home/pages/admin/mail-server.component'; 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 { SecuritySettingsComponent } from '@modules/home/pages/admin/security-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 { + + constructor(private oauth2Service: OAuth2Service) { + } + + resolve(): Observable { + return this.oauth2Service.getLoginProcessingUrl(); + } +} const routes: Routes = [ { @@ -90,6 +109,9 @@ const routes: Routes = [ label: 'admin.oauth2.oauth2', icon: 'security' } + }, + resolve: { + loginProcessingUrl: OAuth2LoginProcessingUrlResolver } } ] @@ -98,6 +120,9 @@ const routes: Routes = [ @NgModule({ imports: [RouterModule.forChild(routes)], - exports: [RouterModule] + exports: [RouterModule], + providers: [ + OAuth2LoginProcessingUrlResolver + ] }) export class AdminRoutingModule { } diff --git a/ui-ngx/src/app/modules/home/pages/admin/oauth2-settings.component.ts b/ui-ngx/src/app/modules/home/pages/admin/oauth2-settings.component.ts index 58cbe536d7..37ae0433d3 100644 --- a/ui-ngx/src/app/modules/home/pages/admin/oauth2-settings.component.ts +++ b/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 { isDefined, isDefinedAndNotNull } from '@core/utils'; import { OAuth2Service } from '@core/http/oauth2.service'; +import { ActivatedRoute } from '@angular/router'; @Component({ selector: 'tb-oauth2-settings', @@ -87,7 +88,10 @@ export class OAuth2SettingsComponent extends PageComponent implements OnInit, Ha templateProvider = ['Custom']; + private loginProcessingUrl: string = this.route.snapshot.data.loginProcessingUrl; + constructor(protected store: Store, + private route: ActivatedRoute, private oauth2Service: OAuth2Service, private fb: FormBuilder, private dialogService: DialogService, @@ -130,7 +134,7 @@ export class OAuth2SettingsComponent extends PageComponent implements OnInit, Ha return this.oauth2SettingsForm.get('domainsParams') as FormArray; } - private formBasicGroup(mapperConfigBasic?: MapperConfigBasic): FormGroup { + private formBasicGroup(type: MapperConfigType, mapperConfigBasic?: MapperConfigBasic): FormGroup { let tenantNamePattern; if (mapperConfigBasic?.tenantNamePattern) { tenantNamePattern = mapperConfigBasic.tenantNamePattern; @@ -138,16 +142,20 @@ export class OAuth2SettingsComponent extends PageComponent implements OnInit, Ha tenantNamePattern = {value: null, disabled: true}; } const basicGroup = this.fb.group({ - emailAttributeKey: [mapperConfigBasic?.emailAttributeKey ? mapperConfigBasic.emailAttributeKey : 'email', Validators.required], firstNameAttributeKey: [mapperConfigBasic?.firstNameAttributeKey ? mapperConfigBasic.firstNameAttributeKey : ''], lastNameAttributeKey: [mapperConfigBasic?.lastNameAttributeKey ? mapperConfigBasic.lastNameAttributeKey : ''], tenantNameStrategy: [mapperConfigBasic?.tenantNameStrategy ? mapperConfigBasic.tenantNameStrategy : TenantNameStrategy.DOMAIN], tenantNamePattern: [tenantNamePattern, Validators.required], customerNamePattern: [mapperConfigBasic?.customerNamePattern ? mapperConfigBasic.customerNamePattern : 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) => { if (domain === 'CUSTOM') { basicGroup.get('tenantNamePattern').enable(); @@ -279,9 +287,12 @@ export class OAuth2SettingsComponent extends PageComponent implements OnInit, Ha clientRegistration?.userNameAttributeName ? clientRegistration.userNameAttributeName : 'email', Validators.required], mapperConfig: this.fb.group({ 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: [ clientRegistration?.mapperConfig?.type ? clientRegistration.mapperConfig.type : MapperConfigType.BASIC, Validators.required ] @@ -308,7 +319,7 @@ export class OAuth2SettingsComponent extends PageComponent implements OnInit, Ha return clientRegistrationFormGroup; } - private validateScope (control: AbstractControl): ValidationErrors | null { + private validateScope(control: AbstractControl): ValidationErrors | null { const scope: string[] = control.value; if (!scope || !scope.length) { return { @@ -347,7 +358,11 @@ export class OAuth2SettingsComponent extends PageComponent implements OnInit, Ha mapperConfig.addControl('custom', this.formCustomGroup(predefinedValue?.custom)); } else { 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 { 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 ''; }