/*! Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ import { Common as ConnectorsCommon } from '@ada/connectors'; import { CustomComponentTypes, FileUploadComponent } from '$common/components/form-renderer'; import { CustomValidatorTypes, JSONSCHEMA_VALIDATOR } from '$common/components/form-renderer/validators'; import { Field } from '@data-driven-forms/react-form-renderer/common-types'; import { FormField, Inline, KeyValuePair } from 'aws-northstar'; import { Option, componentTypes } from 'aws-northstar/components/FormRenderer/types'; import { ValidatorFunction } from '@data-driven-forms/react-form-renderer/validators'; import { camelCase, get, isEmpty, pick } from 'lodash'; import { getPersistentGoogleServiceAccountDetails } from './google-session-credentials'; import { validatorTypes } from 'aws-northstar/components/FormRenderer'; import useFormApi from '@data-driven-forms/react-form-renderer/use-form-api'; type TFormData = Required> const SOURCE_DETAILS_KEY = 'sourceDetails' const FORM_KEY_PREFIX = '__googleAuth' const SOURCE_KEY = `${FORM_KEY_PREFIX}.source`; const FILE_KEY = `${FORM_KEY_PREFIX}.file`; const ERROR_KEY = `${FORM_KEY_PREFIX}.error` enum SOURCES { FILE = 'file', MANUAL = 'manual', PERSISTENT = 'persistent', } function extractGoogleServiceAccountDetails( details: ConnectorsCommon.Google.IGoogleServiceAccountAuth | string ): TFormData { try { if (typeof details === 'string') { details = Object.fromEntries(Object.entries(JSON.parse(details)).map(([key, value]) => { return [camelCase(key), value]; })) as unknown as TFormData; } return pick(details, ConnectorsCommon.Google.GOOGLE_SERVICE_ACCOUNT_JSON_KEYS) as TFormData; } catch (error: any) { console.warn(error); throw error; } } const GOOGLE_AUTH_VALIDATOR = JSONSCHEMA_VALIDATOR({ schema: ConnectorsCommon.Google.GOOGLE_AUTH_SCHEMA }) const googleAuthValidator = ((_value: any, allValues: TFormData) => { const error: string | null = get(allValues, ERROR_KEY) if (error) { return error; } const sourceDetails = allValues[SOURCE_DETAILS_KEY as keyof TFormData]; if (sourceDetails == null || isEmpty(sourceDetails)) { return 'Invalid credential details provided' } return GOOGLE_AUTH_VALIDATOR(sourceDetails); }) as ValidatorFunction; export const GOOGLE_AUTH_FIELD: Field = { component: componentTypes.SUB_FORM, title: 'Google Auth', name: FORM_KEY_PREFIX, fields: [ { component: componentTypes.RADIO, name: SOURCE_KEY, label: 'How would you like to provide the Service Account credentials?', isRequired: true, validate: [ { type: validatorTypes.REQUIRED, }, ], resolveProps: (_props, _field, _formOptions): { options: Partial