Skip to content

Commit 099e86e

Browse files
authored
fix(settings): fixed sso form validation (#2189)
1 parent 042de6a commit 099e86e

File tree

1 file changed

+38
-23
lines changed
  • apps/sim/app/workspace/[workspaceId]/w/components/sidebar/components-new/settings-modal/components/sso

1 file changed

+38
-23
lines changed

apps/sim/app/workspace/[workspaceId]/w/components/sidebar/components-new/settings-modal/components/sso/sso.tsx

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ export function SSO() {
136136
const [errors, setErrors] = useState<Record<string, string[]>>(DEFAULT_ERRORS)
137137
const [showErrors, setShowErrors] = useState(false)
138138

139-
// Permission checks with early returns
140139
if (isBillingEnabled) {
141140
if (!activeOrganization) {
142141
return (
@@ -172,10 +171,9 @@ export function SSO() {
172171
}
173172

174173
const validateProviderId = (value: string): string[] => {
175-
const out: string[] = []
176-
if (!value || !value.trim()) out.push('Provider ID is required.')
177-
if (!/^[-a-z0-9]+$/i.test(value.trim())) out.push('Use letters, numbers, and dashes only.')
178-
return out
174+
if (!value || !value.trim()) return ['Provider ID is required.']
175+
if (!/^[-a-z0-9]+$/i.test(value.trim())) return ['Use letters, numbers, and dashes only.']
176+
return []
179177
}
180178

181179
const validateIssuerUrl = (value: string): string[] => {
@@ -223,13 +221,15 @@ export function SSO() {
223221
audience: [],
224222
}
225223

226-
if (data.providerType === 'oidc') {
224+
const providerType = data.providerType || 'oidc'
225+
226+
if (providerType === 'oidc') {
227227
newErrors.clientId = validateRequired('Client ID', data.clientId)
228228
newErrors.clientSecret = validateRequired('Client Secret', data.clientSecret)
229229
if (!data.scopes || !data.scopes.trim()) {
230230
newErrors.scopes = ['Scopes are required for OIDC providers']
231231
}
232-
} else if (data.providerType === 'saml') {
232+
} else if (providerType === 'saml') {
233233
newErrors.entryPoint = validateIssuerUrl(data.entryPoint || '')
234234
if (!newErrors.entryPoint.length && !data.entryPoint) {
235235
newErrors.entryPoint = ['Entry Point URL is required for SAML providers']
@@ -251,15 +251,17 @@ export function SSO() {
251251
return typeof value === 'string' && value.trim() !== ''
252252
})
253253

254-
if (formData.providerType === 'oidc') {
254+
const providerType = formData.providerType || 'oidc'
255+
256+
if (providerType === 'oidc') {
255257
return (
256258
hasRequiredFields &&
257259
formData.clientId.trim() !== '' &&
258260
formData.clientSecret.trim() !== '' &&
259261
formData.scopes.trim() !== ''
260262
)
261263
}
262-
if (formData.providerType === 'saml') {
264+
if (providerType === 'saml') {
263265
return hasRequiredFields && formData.entryPoint.trim() !== '' && formData.cert.trim() !== ''
264266
}
265267

@@ -277,11 +279,13 @@ export function SSO() {
277279
}
278280

279281
try {
282+
const providerType = formData.providerType || 'oidc'
283+
280284
const requestBody: any = {
281285
providerId: formData.providerId,
282286
issuer: formData.issuerUrl,
283287
domain: formData.domain,
284-
providerType: formData.providerType,
288+
providerType,
285289
orgId: activeOrganization?.id,
286290
mapping: {
287291
id: 'sub',
@@ -291,11 +295,11 @@ export function SSO() {
291295
},
292296
}
293297

294-
if (formData.providerType === 'oidc') {
298+
if (providerType === 'oidc') {
295299
requestBody.clientId = formData.clientId
296300
requestBody.clientSecret = formData.clientSecret
297301
requestBody.scopes = formData.scopes.split(',').map((s) => s.trim())
298-
} else if (formData.providerType === 'saml') {
302+
} else if (providerType === 'saml') {
299303
requestBody.entryPoint = formData.entryPoint
300304
requestBody.cert = formData.cert
301305
requestBody.wantAssertionsSigned = formData.wantAssertionsSigned
@@ -337,11 +341,10 @@ export function SSO() {
337341

338342
if (field === 'providerType') {
339343
setShowErrors(false)
340-
setErrors(DEFAULT_ERRORS)
341-
} else {
342-
validateAll(next)
343344
}
344345

346+
validateAll(next)
347+
345348
return next
346349
})
347350
}
@@ -363,12 +366,26 @@ export function SSO() {
363366
let clientId = ''
364367
let clientSecret = ''
365368
let scopes = 'openid,profile,email'
369+
let entryPoint = ''
370+
let cert = ''
371+
let callbackUrl = ''
372+
let audience = ''
373+
let wantAssertionsSigned = true
374+
let idpMetadata = ''
366375

367376
if (existingProvider.providerType === 'oidc' && existingProvider.oidcConfig) {
368377
const config = JSON.parse(existingProvider.oidcConfig)
369378
clientId = config.clientId || ''
370379
clientSecret = config.clientSecret || ''
371380
scopes = config.scopes?.join(',') || 'openid,profile,email'
381+
} else if (existingProvider.providerType === 'saml' && existingProvider.samlConfig) {
382+
const config = JSON.parse(existingProvider.samlConfig)
383+
entryPoint = config.entryPoint || ''
384+
cert = config.cert || ''
385+
callbackUrl = config.callbackUrl || ''
386+
audience = config.audience || ''
387+
wantAssertionsSigned = config.wantAssertionsSigned ?? true
388+
idpMetadata = config.idpMetadata || ''
372389
}
373390

374391
setFormData({
@@ -379,12 +396,12 @@ export function SSO() {
379396
clientId,
380397
clientSecret,
381398
scopes,
382-
entryPoint: '',
383-
cert: '',
384-
callbackUrl: '',
385-
audience: '',
386-
wantAssertionsSigned: true,
387-
idpMetadata: '',
399+
entryPoint,
400+
cert,
401+
callbackUrl,
402+
audience,
403+
wantAssertionsSigned,
404+
idpMetadata,
388405
showAdvanced: false,
389406
})
390407
setIsEditing(true)
@@ -400,7 +417,6 @@ export function SSO() {
400417
return <SsoSkeleton />
401418
}
402419

403-
// Show preview if provider exists and not editing
404420
if (existingProvider && !isEditing) {
405421
const providerCallbackUrl = `${getBaseUrl()}/api/auth/sso/callback/${existingProvider.providerId}`
406422

@@ -488,7 +504,6 @@ export function SSO() {
488504
)
489505
}
490506

491-
// Form View (no provider or editing)
492507
return (
493508
<form onSubmit={handleSubmit} autoComplete='off' className='flex h-full flex-col gap-[16px]'>
494509
{/* Hidden dummy inputs to prevent browser password manager autofill */}

0 commit comments

Comments
 (0)