import {z} from 'zod' export type ExemptionType = 'USER' | 'GROUP' | 'ACCOUNT'; export type SSOExemptionRequest = {partitionId?: string | undefined, accountId?: string | undefined, exemptionName?: string | undefined, exemptionType: ExemptionType, exemptions: Array, scope?: ('ACCOUNT_LEVEL_SSO' | 'PARTITION_ENFORCED_SSO' | 'PARTITION_SSO') | undefined}; export type CreateApiV2AuthnSsoConfigPartitionsEnforcedPartitionIdExemptionsArgs = {partitionId: string, body: SSOExemptionRequest}; export type CreateApiV2AuthnSsoConfigPartitionsEnforcedPartitionIdExemptionsOptions = {baseUrl?: string | undefined, XAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const exemptionType = z.enum(["USER", "GROUP", "ACCOUNT"]); export const sSOExemptionResponse = z.object({exemptionId: z.string().optional(), partitionId: z.string().optional(), accountId: z.string().optional(), accountName: z.string().optional(), exemptionName: z.string().optional(), exemptionType: exemptionType.optional(), exemptions: 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 }; /** * Add enforced SSO exemption * * POST /v2/authn/sso-config/partitions/enforced/{partitionId}/exemptions */ export const createApiV2AuthnSsoConfigPartitionsEnforcedPartitionIdExemptions = async (args: CreateApiV2AuthnSsoConfigPartitionsEnforcedPartitionIdExemptionsArgs, options?: CreateApiV2AuthnSsoConfigPartitionsEnforcedPartitionIdExemptionsOptions) => { 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/partitions/enforced/${encodeURIComponent(String(args.partitionId))}/exemptions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(sSOExemptionResponse, await res.json()) };