fix(console): Improve SAML SP form typing (#10557)

This PR fixes a problem for the SAML provider in console where the
binding selection was not correctly applied when editing existing
providers

# Which Problems Are Solved

- SAML provider binding selection was not correctly applied when editing
existing providers
- Form used untyped reactive forms leading to potential runtime errors
- Hardcoded enum handling made the code fragile to API changes

# How the Problems Are Solved

- Created reusable utility functions (enum.utils.ts) that properly
convert between numeric enum values (from backend) and string keys (for
form controls)
- Improved type safety: Migrated from
UntypedFormGroup/UntypedFormControl to strongly typed
FormGroup<SAMLProviderForm> with FormControl<T>
This commit is contained in:
Max Peintner
2025-08-27 09:29:14 +02:00
committed by GitHub
parent 2a78fdfe1f
commit b6a2b7c70e
3 changed files with 162 additions and 70 deletions
@@ -54,7 +54,7 @@
</cnsl-form-field> </cnsl-form-field>
<cnsl-form-field class="formfield"> <cnsl-form-field class="formfield">
<cnsl-label>{{ 'IDP.SAML.BINDING' | translate }}</cnsl-label> <cnsl-label>{{ 'IDP.SAML.BINDING' | translate }}</cnsl-label>
<mat-select formControlName="binding" [compareWith]="compareBinding"> <mat-select formControlName="binding">
<mat-option *ngFor="let binding of bindingValues" [value]="binding">{{ binding }}</mat-option> <mat-option *ngFor="let binding of bindingValues" [value]="binding">{{ binding }}</mat-option>
</mat-select> </mat-select>
</cnsl-form-field> </cnsl-form-field>
@@ -73,7 +73,7 @@
<div class="transient-info"> <div class="transient-info">
<cnsl-form-field class="formfield"> <cnsl-form-field class="formfield">
<cnsl-label>{{ 'IDP.SAML.NAMEIDFORMAT' | translate }}</cnsl-label> <cnsl-label>{{ 'IDP.SAML.NAMEIDFORMAT' | translate }}</cnsl-label>
<mat-select formControlName="nameIdFormat" [compareWith]="compareNameIDFormat"> <mat-select formControlName="nameIdFormat">
<mat-option *ngFor="let nameIdFormat of nameIDFormatValues" [value]="nameIdFormat">{{ <mat-option *ngFor="let nameIdFormat of nameIDFormatValues" [value]="nameIdFormat">{{
nameIdFormat nameIdFormat
}}</mat-option> }}</mat-option>
@@ -7,7 +7,7 @@ import {
SAMLBinding, SAMLBinding,
SAMLNameIDFormat, SAMLNameIDFormat,
} from '../../../proto/generated/zitadel/idp_pb'; } from '../../../proto/generated/zitadel/idp_pb';
import { AbstractControl, FormGroup, UntypedFormControl, UntypedFormGroup } from '@angular/forms'; import { AbstractControl, FormControl, FormGroup } from '@angular/forms';
import { PolicyComponentServiceType } from '../../policies/policy-component-types.enum'; import { PolicyComponentServiceType } from '../../policies/policy-component-types.enum';
import { ManagementService } from '../../../services/mgmt.service'; import { ManagementService } from '../../../services/mgmt.service';
import { AdminService } from '../../../services/admin.service'; import { AdminService } from '../../../services/admin.service';
@@ -30,6 +30,18 @@ import {
import { Environment, EnvironmentService } from '../../../services/environment.service'; import { Environment, EnvironmentService } from '../../../services/environment.service';
import { filter, map } from 'rxjs/operators'; import { filter, map } from 'rxjs/operators';
import { ProviderNextService } from '../provider-next/provider-next.service'; import { ProviderNextService } from '../provider-next/provider-next.service';
import { getEnumKeys, getEnumKeyFromValue, convertEnumValuesToKeys } from '../../../utils/enum.utils';
interface SAMLProviderForm {
name: FormControl<string>;
metadataXml: FormControl<string>;
metadataUrl: FormControl<string>;
binding: FormControl<string>;
withSignedRequest: FormControl<boolean>;
nameIdFormat: FormControl<string>;
transientMappingAttributeName: FormControl<string>;
federatedLogoutEnabled: FormControl<boolean>;
}
@Component({ @Component({
selector: 'cnsl-provider-saml-sp', selector: 'cnsl-provider-saml-sp',
@@ -41,7 +53,7 @@ export class ProviderSamlSpComponent {
public id: string | null = ''; public id: string | null = '';
public loading: boolean = false; public loading: boolean = false;
public provider?: Provider.AsObject; public provider?: Provider.AsObject;
public form!: FormGroup; public form!: FormGroup<SAMLProviderForm>;
public showOptional: boolean = false; public showOptional: boolean = false;
public options: Options = new Options() public options: Options = new Options()
.setIsCreationAllowed(true) .setIsCreationAllowed(true)
@@ -51,8 +63,8 @@ export class ProviderSamlSpComponent {
public serviceType: PolicyComponentServiceType = PolicyComponentServiceType.MGMT; public serviceType: PolicyComponentServiceType = PolicyComponentServiceType.MGMT;
// DEPRECATED: use service$ instead // DEPRECATED: use service$ instead
private service!: ManagementService | AdminService; private service!: ManagementService | AdminService;
bindingValues: string[] = Object.keys(SAMLBinding); bindingValues: string[] = getEnumKeys(SAMLBinding);
nameIDFormatValues: string[] = Object.keys(SAMLNameIDFormat); nameIDFormatValues: string[] = getEnumKeys(SAMLNameIDFormat);
public justCreated$: BehaviorSubject<string> = new BehaviorSubject<string>(''); public justCreated$: BehaviorSubject<string> = new BehaviorSubject<string>('');
public justActivated$ = new BehaviorSubject<boolean>(false); public justActivated$ = new BehaviorSubject<boolean>(false);
@@ -118,16 +130,20 @@ export class ProviderSamlSpComponent {
} }
private _initializeForm(): void { private _initializeForm(): void {
this.form = new UntypedFormGroup( const defaultBinding = getEnumKeyFromValue(SAMLBinding, SAMLBinding.SAML_BINDING_POST) || this.bindingValues[0];
const defaultNameIdFormat =
getEnumKeyFromValue(SAMLNameIDFormat, SAMLNameIDFormat.SAML_NAME_ID_FORMAT_PERSISTENT) || this.nameIDFormatValues[0];
this.form = new FormGroup<SAMLProviderForm>(
{ {
name: new UntypedFormControl('', [requiredValidator]), name: new FormControl('', { nonNullable: true, validators: [requiredValidator] }),
metadataXml: new UntypedFormControl('', []), metadataXml: new FormControl('', { nonNullable: true }),
metadataUrl: new UntypedFormControl('', []), metadataUrl: new FormControl('', { nonNullable: true }),
binding: new UntypedFormControl(this.bindingValues[0], [requiredValidator]), binding: new FormControl(defaultBinding, { nonNullable: true, validators: [requiredValidator] }),
withSignedRequest: new UntypedFormControl(true, [requiredValidator]), withSignedRequest: new FormControl(true, { nonNullable: true, validators: [requiredValidator] }),
nameIdFormat: new UntypedFormControl(SAMLNameIDFormat.SAML_NAME_ID_FORMAT_PERSISTENT, []), nameIdFormat: new FormControl(defaultNameIdFormat, { nonNullable: true }),
transientMappingAttributeName: new UntypedFormControl('', []), transientMappingAttributeName: new FormControl('', { nonNullable: true }),
federatedLogoutEnabled: new UntypedFormControl(false, []), federatedLogoutEnabled: new FormControl(false, { nonNullable: true }),
}, },
atLeastOneIsFilled('metadataXml', 'metadataUrl'), atLeastOneIsFilled('metadataXml', 'metadataUrl'),
); );
@@ -197,21 +213,19 @@ export class ProviderSamlSpComponent {
: new AdminUpdateSAMLProviderRequest(); : new AdminUpdateSAMLProviderRequest();
req.setId(this.provider?.id || this.justCreated$.value); req.setId(this.provider?.id || this.justCreated$.value);
req.setName(this.name?.value); req.setName(this.name.value);
if (this.metadataXml?.value) { if (this.metadataXml.value) {
req.setMetadataUrl(''); req.setMetadataUrl('');
req.setMetadataXml(this.metadataXml?.value); req.setMetadataXml(this.metadataXml.value);
} else { } else {
req.setMetadataXml(''); req.setMetadataXml('');
req.setMetadataUrl(this.metadataUrl?.value); req.setMetadataUrl(this.metadataUrl.value);
} }
req.setWithSignedRequest(this.withSignedRequest?.value); req.setWithSignedRequest(this.withSignedRequest.value);
// @ts-ignore req.setBinding(SAMLBinding[this.binding.value as keyof typeof SAMLBinding]);
req.setBinding(SAMLBinding[this.binding?.value]); req.setNameIdFormat(SAMLNameIDFormat[this.nameIDFormat.value as keyof typeof SAMLNameIDFormat]);
// @ts-ignore req.setTransientMappingAttributeName(this.transientMapping.value);
req.setNameIdFormat(SAMLNameIDFormat[this.nameIDFormat?.value]); req.setFederatedLogoutEnabled(this.federatedLogoutEnabled.value);
req.setTransientMappingAttributeName(this.transientMapping?.value);
req.setFederatedLogoutEnabled(this.federatedLogoutEnabled?.value);
req.setProviderOptions(this.options); req.setProviderOptions(this.options);
this.loading = true; this.loading = true;
@@ -235,24 +249,22 @@ export class ProviderSamlSpComponent {
this.serviceType === PolicyComponentServiceType.MGMT this.serviceType === PolicyComponentServiceType.MGMT
? new MgmtAddSAMLProviderRequest() ? new MgmtAddSAMLProviderRequest()
: new AdminAddSAMLProviderRequest(); : new AdminAddSAMLProviderRequest();
req.setName(this.name?.value); req.setName(this.name.value);
if (this.metadataXml?.value) { if (this.metadataXml.value) {
req.setMetadataUrl(''); req.setMetadataUrl('');
req.setMetadataXml(this.metadataXml?.value); req.setMetadataXml(this.metadataXml.value);
} else { } else {
req.setMetadataXml(''); req.setMetadataXml('');
req.setMetadataUrl(this.metadataUrl?.value); req.setMetadataUrl(this.metadataUrl.value);
} }
req.setProviderOptions(this.options); req.setProviderOptions(this.options);
// @ts-ignore req.setBinding(SAMLBinding[this.binding.value as keyof typeof SAMLBinding]);
req.setBinding(SAMLBinding[this.binding?.value]); req.setWithSignedRequest(this.withSignedRequest.value);
req.setWithSignedRequest(this.withSignedRequest?.value);
if (this.nameIDFormat) { if (this.nameIDFormat) {
// @ts-ignore req.setNameIdFormat(SAMLNameIDFormat[this.nameIDFormat.value as keyof typeof SAMLNameIDFormat]);
req.setNameIdFormat(SAMLNameIDFormat[this.nameIDFormat.value]);
} }
req.setTransientMappingAttributeName(this.transientMapping?.value); req.setTransientMappingAttributeName(this.transientMapping.value);
req.setFederatedLogoutEnabled(this.federatedLogoutEnabled?.value); req.setFederatedLogoutEnabled(this.federatedLogoutEnabled.value);
this.loading = true; this.loading = true;
this.service this.service
.addSAMLProvider(req) .addSAMLProvider(req)
@@ -281,9 +293,20 @@ export class ProviderSamlSpComponent {
.then((resp) => { .then((resp) => {
this.provider = resp.idp; this.provider = resp.idp;
this.loading = false; this.loading = false;
this.name?.setValue(this.provider?.name); this.name.setValue(this.provider?.name || '');
if (this.provider?.config?.saml) { if (this.provider?.config?.saml) {
this.form.patchValue(this.provider.config.saml); const samlConfig = this.provider.config.saml;
const bindingKey = getEnumKeyFromValue(SAMLBinding, samlConfig.binding) || '';
const nameIdFormatKey = getEnumKeyFromValue(SAMLNameIDFormat, samlConfig.nameIdFormat) || '';
this.form.patchValue({
metadataXml: typeof samlConfig.metadataXml === 'string' ? samlConfig.metadataXml : '',
binding: bindingKey,
withSignedRequest: samlConfig.withSignedRequest,
nameIdFormat: nameIdFormatKey,
transientMappingAttributeName: samlConfig.transientMappingAttributeName || '',
federatedLogoutEnabled: samlConfig.federatedLogoutEnabled || false,
});
} }
}) })
.catch((error) => { .catch((error) => {
@@ -296,50 +319,35 @@ export class ProviderSamlSpComponent {
this._location.back(); this._location.back();
} }
compareBinding(value: string, index: number) { private get name(): FormControl<string> {
if (value) { return this.form.controls.name;
return value === Object.keys(SAMLBinding)[index];
}
return false;
} }
compareNameIDFormat(value: string, index: number) { private get metadataXml(): FormControl<string> {
console.log(value, index); return this.form.controls.metadataXml;
if (value) {
return value === Object.keys(SAMLNameIDFormat)[index];
}
return false;
} }
private get name(): AbstractControl | null { private get metadataUrl(): FormControl<string> {
return this.form.get('name'); return this.form.controls.metadataUrl;
} }
private get metadataXml(): AbstractControl | null { private get binding(): FormControl<string> {
return this.form.get('metadataXml'); return this.form.controls.binding;
} }
private get metadataUrl(): AbstractControl | null { private get withSignedRequest(): FormControl<boolean> {
return this.form.get('metadataUrl'); return this.form.controls.withSignedRequest;
} }
private get binding(): AbstractControl | null { private get nameIDFormat(): FormControl<string> {
return this.form.get('binding'); return this.form.controls.nameIdFormat;
} }
private get withSignedRequest(): AbstractControl | null { private get transientMapping(): FormControl<string> {
return this.form.get('withSignedRequest'); return this.form.controls.transientMappingAttributeName;
} }
private get nameIDFormat(): AbstractControl | null { private get federatedLogoutEnabled(): FormControl<boolean> {
return this.form.get('nameIdFormat'); return this.form.controls.federatedLogoutEnabled;
}
private get transientMapping(): AbstractControl | null {
return this.form.get('transientMappingAttributeName');
}
private get federatedLogoutEnabled(): AbstractControl | null {
return this.form.get('federatedLogoutEnabled');
} }
} }
+84
View File
@@ -0,0 +1,84 @@
/**
* Utility functions for working with TypeScript enums in Angular forms
*
* Example usage:
* ```typescript
* // Get dropdown options
* const bindingOptions = getEnumKeys(SAMLBinding);
*
* // Convert backend enum values to form-friendly string keys
* const formConfig = convertEnumValuesToKeys(backendConfig, {
* binding: SAMLBinding,
* nameIdFormat: SAMLNameIDFormat
* });
*
* // Find specific enum key
* const defaultBinding = getEnumKeyFromValue(SAMLBinding, SAMLBinding.SAML_BINDING_POST);
* ```
*/
// Type constraint for TypeScript numeric enums
type NumericEnum = Record<string, string | number> & Record<number, string>;
/**
* Get string keys from a TypeScript enum, excluding numeric reverse mappings
* @param enumObject The enum object
* @returns Array of string keys
* @example
* ```typescript
* const bindingOptions = getEnumKeys(SAMLBinding);
* // Returns: ['SAML_BINDING_UNSPECIFIED', 'SAML_BINDING_POST', ...]
* ```
*/
export function getEnumKeys<T extends NumericEnum>(enumObject: T): string[] {
return Object.keys(enumObject).filter((key) => isNaN(Number(key)));
}
/**
* Find the string key for a given numeric enum value
* @param enumObject The enum object
* @param value The numeric enum value
* @returns The corresponding string key or undefined if not found
* @example
* ```typescript
* const key = getEnumKeyFromValue(SAMLBinding, 1);
* // Returns: 'SAML_BINDING_POST'
* ```
*/
export function getEnumKeyFromValue<T extends NumericEnum>(enumObject: T, value: number): string | undefined {
return Object.keys(enumObject).find((key) => enumObject[key] === value && isNaN(Number(key)));
}
/**
* Convert enum values to string keys for form controls
* @param config Object containing enum values to convert
* @param enumMappings Object mapping config property names to enum objects
* @returns Modified config object with string keys
* @example
* ```typescript
* const formConfig = convertEnumValuesToKeys(samlConfig, {
* binding: SAMLBinding,
* nameIdFormat: SAMLNameIDFormat
* });
* // Converts: { binding: 1, nameIdFormat: 2 }
* // To: { binding: 'SAML_BINDING_POST', nameIdFormat: 'SAML_NAME_ID_FORMAT_PERSISTENT' }
* ```
*/
export function convertEnumValuesToKeys<T extends Record<string, unknown>>(
config: T,
enumMappings: { [K in keyof Partial<T>]: NumericEnum },
): T {
const converted = { ...config };
for (const [propertyName, enumObject] of Object.entries(enumMappings)) {
const typedPropertyName = propertyName as keyof T;
if (converted[typedPropertyName] !== undefined && typeof converted[typedPropertyName] === 'number') {
const key = getEnumKeyFromValue(enumObject, converted[typedPropertyName] as number);
if (key) {
(converted as Record<string, unknown>)[propertyName] = key;
}
}
}
return converted;
}