Skip to content

Commit edc3571

Browse files
committed
refactor(servicenow): mark the username with requiredForAuthMethods so the catalog scopes it to the password sign-in
1 parent 8925e0c commit edc3571

4 files changed

Lines changed: 51 additions & 15 deletions

File tree

‎apps/sim/app/workspace/[workspaceId]/integrations/components/connect-service-account-modal/token-service-account-modal.tsx‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ import type { ServiceAccountSecretFields } from '@/lib/credentials/service-accou
2222
import {
2323
CLIENT_CREDENTIALS_AUTH_METHOD,
2424
getTokenServiceAccountErrorMessage,
25+
type TokenServiceAccountAuthMethod,
2526
type TokenServiceAccountDescriptor,
2627
type TokenServiceAccountField,
28+
tokenFieldBelongsToAuthMethod,
2729
} from '@/lib/credentials/token-service-accounts/descriptors'
2830
import { ClientCredentialsFields } from '@/app/workspace/[workspaceId]/integrations/components/connect-service-account-modal/client-credentials-fields'
2931
import { withBrandIcon } from '@/blocks/brand-icon'
@@ -41,8 +43,6 @@ function normalizeDomainInput(raw: string): string {
4143
.replace(/[/?#].*$/, '')
4244
}
4345

44-
type TokenAuthMethod = 'token' | typeof CLIENT_CREDENTIALS_AUTH_METHOD
45-
4646
function openDocs(url: string): void {
4747
window.open(url, '_blank', 'noopener,noreferrer')
4848
}
@@ -100,14 +100,15 @@ function TokenServiceAccountModalForm({
100100
submitPending,
101101
}: TokenServiceAccountModalProps) {
102102
const clientCredentials = descriptor.clientCredentials
103-
const [selectedAuthMethod, setSelectedAuthMethod] = useState<TokenAuthMethod | null>(null)
103+
const [selectedAuthMethod, setSelectedAuthMethod] =
104+
useState<TokenServiceAccountAuthMethod | null>(null)
104105
/**
105106
* A reconnect cannot pre-select the mode: the stored one lives inside the encrypted secret and
106107
* is never returned to the client, so the admin restates it while retyping the secret. Until
107108
* then neither mode's fields show, so a reconnect never switches modes by default.
108109
*/
109110
const mustRestateAuthMethod = Boolean(clientCredentials && credentialId && !selectedAuthMethod)
110-
const authMethod: TokenAuthMethod | null =
111+
const authMethod: TokenServiceAccountAuthMethod | null =
111112
selectedAuthMethod ??
112113
(mustRestateAuthMethod
113114
? null
@@ -127,10 +128,14 @@ function TokenServiceAccountModalForm({
127128
const createCredential = useCreateScopedCredential()
128129
const updateCredential = useUpdateScopedCredential()
129130

130-
/** A field of the pasted-token sign-in only (ServiceNow's username) shows once that mode is chosen. */
131+
/** A field that belongs to one sign-in mode (ServiceNow's username) shows once that mode is chosen. */
131132
const shownField = (id: TokenServiceAccountField['id']) =>
132133
descriptor.fields.find(
133-
(field) => field.id === id && !(field.pastedTokenOnly && authMethod !== 'token')
134+
(field) =>
135+
field.id === id &&
136+
(authMethod
137+
? tokenFieldBelongsToAuthMethod(field, authMethod)
138+
: !field.requiredForAuthMethods)
134139
)
135140
const tokenField = descriptor.fields.find((field) => field.id === 'apiToken')
136141
const domainField = shownField('domain')
@@ -228,7 +233,7 @@ function TokenServiceAccountModalForm({
228233
title='Authentication'
229234
value={authMethod ?? undefined}
230235
onChange={(value) => {
231-
setSelectedAuthMethod(value as TokenAuthMethod)
236+
setSelectedAuthMethod(value as TokenServiceAccountAuthMethod)
232237
if (error) setError(null)
233238
}}
234239
options={[

‎apps/sim/lib/credentials/application/provider-catalog-contract.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,23 @@ describe('v2 credential provider catalog contract', () => {
181181
expect(configuration?.hint).not.toContain('Keyless')
182182
})
183183

184+
it('tells headless clients the ServiceNow username belongs to the password sign-in only', async () => {
185+
const providers = await listProviders('servicenow')
186+
const servicenow = providers.find(
187+
(provider) =>
188+
provider.type === 'service_account' && provider.providerId === 'servicenow-service-account'
189+
)
190+
191+
expect(responseSchema.safeParse({ data: providers, nextCursor: null }).success).toBe(true)
192+
expect(servicenow?.fields.find((field) => field.id === 'providerWorkspaceId')).toMatchObject({
193+
required: true,
194+
requiredForAuthMethods: ['token'],
195+
})
196+
expect(
197+
servicenow?.fields.find((field) => field.id === 'domain')?.requiredForAuthMethods
198+
).toBeUndefined()
199+
})
200+
184201
it('lists the native Claude Platform provider when filtered', async () => {
185202
const providers = await listProviders('claude')
186203

‎apps/sim/lib/credentials/service-account-secret.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,9 +635,7 @@ async function buildTokenClientCredentialSecret(
635635
const clientId = fields.clientId?.trim()
636636
const clientSecret = fields.clientSecret?.trim()
637637
const domain = fields.domain?.trim()
638-
const requiresDomain = descriptor.fields.some(
639-
(field) => field.id === 'domain' && !field.pastedTokenOnly
640-
)
638+
const requiresDomain = descriptor.fields.some((field) => field.id === 'domain')
641639
if (!clientId || !clientSecret || (requiresDomain && !domain)) {
642640
throw new ServiceAccountSecretError(
643641
`clientId, clientSecret${requiresDomain ? ', and domain are' : ' are'} required for ${descriptor.serviceLabel} client credentials`

‎apps/sim/lib/credentials/token-service-accounts/descriptors.ts‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ export interface TokenServiceAccountField {
3333
*/
3434
url?: boolean
3535
/**
36-
* Belongs to the pasted-token sign-in only (ServiceNow's username, which a minted token replaces):
37-
* the client-credentials mode neither shows, requires, nor stores it.
36+
* Auth methods this field belongs to, for a provider that also offers client credentials:
37+
* `token` is the pasted-token sign-in and `client_credentials` the grant. A mode not listed
38+
* neither shows, requires, nor stores it (ServiceNow's username, which a minted token replaces).
39+
* Absent means the field belongs to every mode.
3840
*/
39-
pastedTokenOnly?: boolean
41+
requiredForAuthMethods?: readonly TokenServiceAccountAuthMethod[]
4042
/** Soft-format hint shown while the current value doesn't match `hintPattern`. */
4143
hintPattern?: RegExp
4244
hintMessage?: string
@@ -112,6 +114,17 @@ interface TokenServiceAccountClientCredentials {
112114
/** `authMethod` value selecting the client-credentials mode of a provider whose default is a pasted token (a token provider, or the Atlassian service account). */
113115
export const CLIENT_CREDENTIALS_AUTH_METHOD = 'client_credentials' as const
114116

117+
/** A token provider's sign-in modes: the pasted token (the default) or its client-credentials grant. */
118+
export type TokenServiceAccountAuthMethod = 'token' | typeof CLIENT_CREDENTIALS_AUTH_METHOD
119+
120+
/** Whether a descriptor field belongs to the given sign-in mode. */
121+
export function tokenFieldBelongsToAuthMethod(
122+
field: Pick<TokenServiceAccountField, 'requiredForAuthMethods'>,
123+
authMethod: TokenServiceAccountAuthMethod
124+
): boolean {
125+
return !field.requiredForAuthMethods || field.requiredForAuthMethods.includes(authMethod)
126+
}
127+
115128
export const HUBSPOT_SERVICE_ACCOUNT_PROVIDER_ID = 'hubspot-service-account' as const
116129
export const AIRTABLE_SERVICE_ACCOUNT_PROVIDER_ID = 'airtable-service-account' as const
117130
export const NOTION_SERVICE_ACCOUNT_PROVIDER_ID = 'notion-service-account' as const
@@ -2498,7 +2511,7 @@ export const TOKEN_SERVICE_ACCOUNT_DESCRIPTORS: Record<
24982511
label: 'Username',
24992512
placeholder: 'Enter the ServiceNow username',
25002513
secret: false,
2501-
pastedTokenOnly: true,
2514+
requiredForAuthMethods: ['token'],
25022515
},
25032516
{
25042517
id: 'domain',
@@ -2541,7 +2554,10 @@ export const TOKEN_SERVICE_ACCOUNT_CLIENT_CREDENTIAL_FIELDS: Record<
25412554
'clientId',
25422555
'clientSecret',
25432556
...descriptor.fields.flatMap((field) =>
2544-
field.id === 'apiToken' || field.pastedTokenOnly ? [] : [field.id]
2557+
field.id === 'apiToken' ||
2558+
!tokenFieldBelongsToAuthMethod(field, CLIENT_CREDENTIALS_AUTH_METHOD)
2559+
? []
2560+
: [field.id]
25452561
),
25462562
],
25472563
])

0 commit comments

Comments
 (0)