import {z} from 'zod' export type OAuth2ClientRegistrationTemplateId = {id: string}; export type OAuth2BasicMapperConfig = {emailAttributeKey?: string | undefined, firstNameAttributeKey?: string | undefined, lastNameAttributeKey?: string | undefined, tenantNameStrategy: 'DOMAIN' | 'EMAIL' | 'CUSTOM', tenantNamePattern?: string | undefined, customerNamePattern?: string | undefined, defaultDashboardName?: string | undefined, alwaysFullScreen?: boolean | undefined}; export type OAuth2CustomMapperConfig = {url?: string | undefined, username?: string | undefined, password?: string | undefined, sendToken?: boolean | undefined}; export type OAuth2MapperConfig = {allowUserCreation?: boolean | undefined, activateUser?: boolean | undefined, type: 'BASIC' | 'CUSTOM' | 'GITHUB' | 'APPLE', basic?: OAuth2BasicMapperConfig | undefined, custom?: OAuth2CustomMapperConfig | undefined}; export type JsonNode = unknown; export type OAuth2ClientRegistrationTemplate = {id?: OAuth2ClientRegistrationTemplateId | undefined, createdTime?: number | undefined, providerId: string, mapperConfig?: OAuth2MapperConfig | undefined, authorizationUri?: string | undefined, accessTokenUri?: string | undefined, scope?: Array | undefined, userInfoUri?: string | undefined, userNameAttributeName?: string | undefined, jwkSetUri?: string | undefined, clientAuthenticationMethod?: string | undefined, comment?: string | undefined, loginButtonIcon?: string | undefined, loginButtonLabel?: string | undefined, helpLink?: string | undefined, name?: string | undefined, additionalInfo?: JsonNode | undefined}; export type CreateApiApiOauth2ConfigTemplateArgs = {body: OAuth2ClientRegistrationTemplate}; export type CreateApiApiOauth2ConfigTemplateOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const oAuth2ClientRegistrationTemplateId = z.object({id: z.string()}); export const oAuth2BasicMapperConfig = z.object({emailAttributeKey: z.string().optional(), firstNameAttributeKey: z.string().optional(), lastNameAttributeKey: z.string().optional(), tenantNameStrategy: z.enum(["DOMAIN", "EMAIL", "CUSTOM"]), tenantNamePattern: z.string().optional(), customerNamePattern: z.string().optional(), defaultDashboardName: z.string().optional(), alwaysFullScreen: z.boolean().optional()}); export const oAuth2CustomMapperConfig = z.object({url: z.string().optional(), username: z.string().optional(), password: z.string().optional(), sendToken: z.boolean().optional()}); export const oAuth2MapperConfig = z.object({allowUserCreation: z.boolean().optional(), activateUser: z.boolean().optional(), type: z.enum(["BASIC", "CUSTOM", "GITHUB", "APPLE"]), basic: oAuth2BasicMapperConfig.optional(), custom: oAuth2CustomMapperConfig.optional()}); export const jsonNode = z.unknown(); export const oAuth2ClientRegistrationTemplate = z.object({id: oAuth2ClientRegistrationTemplateId.optional(), createdTime: z.number().int().optional(), providerId: z.string(), mapperConfig: oAuth2MapperConfig.optional(), authorizationUri: z.string().optional(), accessTokenUri: z.string().optional(), scope: z.array(z.string()).optional(), userInfoUri: z.string().optional(), userNameAttributeName: z.string().optional(), jwkSetUri: z.string().optional(), clientAuthenticationMethod: z.string().optional(), comment: z.string().optional(), loginButtonIcon: z.string().optional(), loginButtonLabel: z.string().optional(), helpLink: z.string().optional(), name: z.string().optional(), additionalInfo: jsonNode.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 or update OAuth2 client registration template (saveClientRegistrationTemplate) * * Available for users with 'SYS_ADMIN' authority. * * POST /api/oauth2/config/template */ export const createApiApiOauth2ConfigTemplate = async (args: CreateApiApiOauth2ConfigTemplateArgs, options?: CreateApiApiOauth2ConfigTemplateOptions) => { const {baseUrl = "https://demo.thingsboard.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/oauth2/config/template${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(oAuth2ClientRegistrationTemplate, await res.json()) };