import {z} from 'zod' export type AuthProviderType = 'oidc' | 'saml'; export type AuthProvider = {id?: string | undefined, slug: string, name?: string | undefined, icon?: string | undefined, type: AuthProviderType, config: {client_id: string, client_secret?: string | undefined, authorization_endpoint: string, token_endpoint: string, scopes?: Array | undefined, state?: boolean | undefined, jwks_uri: string, pkce?: boolean | undefined} | {[key: string]: unknown} | {idp_metadata_filepath?: string | undefined, idp_metadata_xml?: string | undefined, issuer?: string | undefined, audience?: string | undefined, skipRequestCompression?: boolean | undefined, wantAuthnResponseSigned?: boolean | undefined, identifierFormat?: string | undefined, identityProviderUrl?: string | undefined, entryPoint?: string | undefined, logoutUrl?: string | undefined, idpCert?: string | undefined} | {[key: string]: unknown}, attributesMapping?: ({firstName?: string | undefined, lastName?: string | undefined, email?: string | undefined} | {[key: string]: string}) | undefined, source?: ('file' | 'database') | undefined, enabled?: boolean | undefined, meta?: {[key: string]: unknown} | undefined, domains?: Array | undefined, orgSlug?: string | undefined}; export type CreateApiV2OrgsOrgSlugAuthSsoArgs = {orgSlug: string, body: AuthProvider}; export type CreateApiV2OrgsOrgSlugAuthSsoOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const authProviderType = z.enum(["oidc", "saml"]); export const authProvider = z.object({id: z.string().optional(), slug: z.string(), name: z.string().optional(), icon: z.string().optional(), type: authProviderType, config: z.union([z.object({client_id: z.string(), client_secret: z.string().optional(), authorization_endpoint: z.string(), token_endpoint: z.string(), scopes: z.array(z.string()).optional(), state: z.boolean().optional(), jwks_uri: z.string(), pkce: z.boolean().optional()}).and(z.record(z.string(), z.unknown())), z.object({idp_metadata_filepath: z.string().optional(), idp_metadata_xml: z.string().optional(), issuer: z.string().optional(), audience: z.string().optional(), skipRequestCompression: z.boolean().optional(), wantAuthnResponseSigned: z.boolean().optional(), identifierFormat: z.string().optional(), identityProviderUrl: z.string().optional(), entryPoint: z.string().optional(), logoutUrl: z.string().optional(), idpCert: z.string().optional()}).and(z.record(z.string(), z.unknown()))]), attributesMapping: z.object({firstName: z.string().optional(), lastName: z.string().optional(), email: z.string().optional()}).and(z.record(z.string(), z.string())).optional(), source: z.enum(["file", "database"]).optional(), enabled: z.boolean().optional(), meta: z.record(z.string(), z.unknown()).optional(), domains: z.array(z.string()).optional(), orgSlug: 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 }; /** * Create Auth Provider * * POST /v2/orgs/{orgSlug}/auth/sso * * Also accepts auth: OrgApiKeyAuth, BearerAuth + OrgApiKeyAuth */ export const createApiV2OrgsOrgSlugAuthSso = async (args: CreateApiV2OrgsOrgSlugAuthSsoArgs, options?: CreateApiV2OrgsOrgSlugAuthSsoOptions) => { const {baseUrl = "https://api.studio.prisme.ai", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/orgs/${encodeURIComponent(String(args.orgSlug))}/auth/sso${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(authProvider, await res.json()) };