import {z} from 'zod' export type SAMLRequestProtocolBinding = 'POST' | 'REDIRECT'; export type CompleteSSOConfigRequest = {identityProviderSignInUrl: string, identityProviderSigningCertificate: string, userIdAttributeFieldMapping?: string | undefined, externalIdAttributeFieldMapping: string, externalEmailAttributeFieldMapping?: string | undefined, ssoName: string, identityProviderLogoutUrl?: string | undefined, samlRequestProtocolBinding?: SAMLRequestProtocolBinding | undefined, enforcedSubPartitions?: Array | undefined, allowSpInitiatedAuthenticationFlow?: boolean | undefined, allowAlianzaAuthentication?: boolean | undefined}; export type UpdateApiV2AuthnSsoConfigAccountsAccountIdArgs = {accountId: string, body: CompleteSSOConfigRequest}; export type UpdateApiV2AuthnSsoConfigAccountsAccountIdOptions = {baseUrl?: string | undefined, XAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const sSOConfigResponse = z.object({partitionId: z.string().optional(), accountId: z.string().nullable().optional(), ssoName: z.string().optional(), assertionConsumerResponseUrl: z.string().optional(), serviceProviderEntityId: z.string().optional(), serviceProviderSigningCertificate: z.string().optional(), identityProviderUserId: z.string().optional(), identityProviderUserEmail: z.string().optional(), alianzaUserIdFieldMapping: z.string().optional(), identityProviderSignInUrl: z.string().optional(), identityProviderLogoutUrl: z.string().optional(), identityProviderSigningKeyName: z.string().optional(), status: z.enum(["DRAFT", "READY", "ENABLED", "DISABLED"]).optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), validated: z.boolean().optional(), allowAlianzaAuthentication: z.boolean().optional(), allowSpInitiatedAuthenticationFlow: z.boolean().optional(), enforcedSubPartitions: z.array(z.string()).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Update SSO configuration * * PUT /v2/authn/sso-config/accounts/{accountId} */ export const updateApiV2AuthnSsoConfigAccountsAccountId = async (args: UpdateApiV2AuthnSsoConfigAccountsAccountIdArgs, options?: UpdateApiV2AuthnSsoConfigAccountsAccountIdOptions) => { const {baseUrl = "https://api.alianza.com", fetch: fetchFn = fetch, XAuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (XAuthToken !== undefined) headers.set("X-AUTH-TOKEN", XAuthToken) const url = `${baseUrl.replace(/\/+$/, '')}/v2/authn/sso-config/accounts/${encodeURIComponent(String(args.accountId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(sSOConfigResponse, await res.json()) };