dify
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
import {
|
||||
useAddPluginCredential,
|
||||
useDeletePluginCredential,
|
||||
useDeletePluginOAuthCustomClient,
|
||||
useGetPluginCredentialInfo,
|
||||
useGetPluginCredentialSchema,
|
||||
useGetPluginOAuthClientSchema,
|
||||
useGetPluginOAuthUrl,
|
||||
useInvalidPluginCredentialInfo,
|
||||
useInvalidPluginOAuthClientSchema,
|
||||
useSetPluginDefaultCredential,
|
||||
useSetPluginOAuthCustomClient,
|
||||
useUpdatePluginCredential,
|
||||
} from '@/service/use-plugins-auth'
|
||||
import { useGetApi } from './use-get-api'
|
||||
import type { PluginPayload } from '../types'
|
||||
import type { CredentialTypeEnum } from '../types'
|
||||
import { useInvalidToolsByType } from '@/service/use-tools'
|
||||
|
||||
export const useGetPluginCredentialInfoHook = (pluginPayload: PluginPayload, enable?: boolean) => {
|
||||
const apiMap = useGetApi(pluginPayload)
|
||||
return useGetPluginCredentialInfo(enable ? apiMap.getCredentialInfo : '')
|
||||
}
|
||||
|
||||
export const useDeletePluginCredentialHook = (pluginPayload: PluginPayload) => {
|
||||
const apiMap = useGetApi(pluginPayload)
|
||||
|
||||
return useDeletePluginCredential(apiMap.deleteCredential)
|
||||
}
|
||||
|
||||
export const useInvalidPluginCredentialInfoHook = (pluginPayload: PluginPayload) => {
|
||||
const apiMap = useGetApi(pluginPayload)
|
||||
const invalidPluginCredentialInfo = useInvalidPluginCredentialInfo(apiMap.getCredentialInfo)
|
||||
const providerType = pluginPayload.providerType
|
||||
const invalidToolsByType = useInvalidToolsByType(providerType)
|
||||
|
||||
return () => {
|
||||
invalidPluginCredentialInfo()
|
||||
invalidToolsByType()
|
||||
}
|
||||
}
|
||||
|
||||
export const useSetPluginDefaultCredentialHook = (pluginPayload: PluginPayload) => {
|
||||
const apiMap = useGetApi(pluginPayload)
|
||||
|
||||
return useSetPluginDefaultCredential(apiMap.setDefaultCredential)
|
||||
}
|
||||
|
||||
export const useGetPluginCredentialSchemaHook = (pluginPayload: PluginPayload, credentialType: CredentialTypeEnum) => {
|
||||
const apiMap = useGetApi(pluginPayload)
|
||||
|
||||
return useGetPluginCredentialSchema(apiMap.getCredentialSchema(credentialType))
|
||||
}
|
||||
|
||||
export const useAddPluginCredentialHook = (pluginPayload: PluginPayload) => {
|
||||
const apiMap = useGetApi(pluginPayload)
|
||||
|
||||
return useAddPluginCredential(apiMap.addCredential)
|
||||
}
|
||||
|
||||
export const useUpdatePluginCredentialHook = (pluginPayload: PluginPayload) => {
|
||||
const apiMap = useGetApi(pluginPayload)
|
||||
|
||||
return useUpdatePluginCredential(apiMap.updateCredential)
|
||||
}
|
||||
|
||||
export const useGetPluginOAuthUrlHook = (pluginPayload: PluginPayload) => {
|
||||
const apiMap = useGetApi(pluginPayload)
|
||||
|
||||
return useGetPluginOAuthUrl(apiMap.getOauthUrl)
|
||||
}
|
||||
|
||||
export const useGetPluginOAuthClientSchemaHook = (pluginPayload: PluginPayload) => {
|
||||
const apiMap = useGetApi(pluginPayload)
|
||||
|
||||
return useGetPluginOAuthClientSchema(apiMap.getOauthClientSchema)
|
||||
}
|
||||
|
||||
export const useInvalidPluginOAuthClientSchemaHook = (pluginPayload: PluginPayload) => {
|
||||
const apiMap = useGetApi(pluginPayload)
|
||||
|
||||
return useInvalidPluginOAuthClientSchema(apiMap.getOauthClientSchema)
|
||||
}
|
||||
|
||||
export const useSetPluginOAuthCustomClientHook = (pluginPayload: PluginPayload) => {
|
||||
const apiMap = useGetApi(pluginPayload)
|
||||
|
||||
return useSetPluginOAuthCustomClient(apiMap.setCustomOauthClient)
|
||||
}
|
||||
|
||||
export const useDeletePluginOAuthCustomClientHook = (pluginPayload: PluginPayload) => {
|
||||
const apiMap = useGetApi(pluginPayload)
|
||||
|
||||
return useDeletePluginOAuthCustomClient(apiMap.deleteCustomOAuthClient)
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import {
|
||||
AuthCategory,
|
||||
} from '../types'
|
||||
import type {
|
||||
CredentialTypeEnum,
|
||||
PluginPayload,
|
||||
} from '../types'
|
||||
|
||||
export const useGetApi = ({ category = AuthCategory.tool, provider }: PluginPayload) => {
|
||||
if (category === AuthCategory.tool) {
|
||||
return {
|
||||
getCredentialInfo: `/workspaces/current/tool-provider/builtin/${provider}/credential/info`,
|
||||
setDefaultCredential: `/workspaces/current/tool-provider/builtin/${provider}/default-credential`,
|
||||
getCredentials: `/workspaces/current/tool-provider/builtin/${provider}/credentials`,
|
||||
addCredential: `/workspaces/current/tool-provider/builtin/${provider}/add`,
|
||||
updateCredential: `/workspaces/current/tool-provider/builtin/${provider}/update`,
|
||||
deleteCredential: `/workspaces/current/tool-provider/builtin/${provider}/delete`,
|
||||
getCredentialSchema: (credential_type: CredentialTypeEnum) => `/workspaces/current/tool-provider/builtin/${provider}/credential/schema/${credential_type}`,
|
||||
getOauthUrl: `/oauth/plugin/${provider}/tool/authorization-url`,
|
||||
getOauthClientSchema: `/workspaces/current/tool-provider/builtin/${provider}/oauth/client-schema`,
|
||||
setCustomOauthClient: `/workspaces/current/tool-provider/builtin/${provider}/oauth/custom-client`,
|
||||
getCustomOAuthClientValues: `/workspaces/current/tool-provider/builtin/${provider}/oauth/custom-client`,
|
||||
deleteCustomOAuthClient: `/workspaces/current/tool-provider/builtin/${provider}/oauth/custom-client`,
|
||||
}
|
||||
}
|
||||
|
||||
if (category === AuthCategory.datasource) {
|
||||
return {
|
||||
getCredentialInfo: '',
|
||||
setDefaultCredential: `/auth/plugin/datasource/${provider}/default`,
|
||||
getCredentials: `/auth/plugin/datasource/${provider}`,
|
||||
addCredential: `/auth/plugin/datasource/${provider}`,
|
||||
updateCredential: `/auth/plugin/datasource/${provider}/update`,
|
||||
deleteCredential: `/auth/plugin/datasource/${provider}/delete`,
|
||||
getCredentialSchema: () => '',
|
||||
getOauthUrl: `/oauth/plugin/${provider}/datasource/get-authorization-url`,
|
||||
getOauthClientSchema: '',
|
||||
setCustomOauthClient: `/auth/plugin/datasource/${provider}/custom-client`,
|
||||
deleteCustomOAuthClient: `/auth/plugin/datasource/${provider}/custom-client`,
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
getCredentialInfo: '',
|
||||
setDefaultCredential: '',
|
||||
getCredentials: '',
|
||||
addCredential: '',
|
||||
updateCredential: '',
|
||||
deleteCredential: '',
|
||||
getCredentialSchema: () => '',
|
||||
getOauthUrl: '',
|
||||
getOauthClientSchema: '',
|
||||
setCustomOauthClient: '',
|
||||
getCustomOAuthClientValues: '',
|
||||
deleteCustomOAuthClient: '',
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
import {
|
||||
useCallback,
|
||||
useRef,
|
||||
useState,
|
||||
} from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { useToastContext } from '@/app/components/base/toast'
|
||||
import type { PluginPayload } from '../types'
|
||||
import {
|
||||
useDeletePluginCredentialHook,
|
||||
useSetPluginDefaultCredentialHook,
|
||||
useUpdatePluginCredentialHook,
|
||||
} from '../hooks/use-credential'
|
||||
|
||||
export const usePluginAuthAction = (
|
||||
pluginPayload: PluginPayload,
|
||||
onUpdate?: () => void,
|
||||
) => {
|
||||
const { t } = useTranslation()
|
||||
const { notify } = useToastContext()
|
||||
const pendingOperationCredentialId = useRef<string | null>(null)
|
||||
const [deleteCredentialId, setDeleteCredentialId] = useState<string | null>(null)
|
||||
const { mutateAsync: deletePluginCredential } = useDeletePluginCredentialHook(pluginPayload)
|
||||
const openConfirm = useCallback((credentialId?: string) => {
|
||||
if (credentialId)
|
||||
pendingOperationCredentialId.current = credentialId
|
||||
|
||||
setDeleteCredentialId(pendingOperationCredentialId.current)
|
||||
}, [])
|
||||
const closeConfirm = useCallback(() => {
|
||||
setDeleteCredentialId(null)
|
||||
pendingOperationCredentialId.current = null
|
||||
}, [])
|
||||
const [doingAction, setDoingAction] = useState(false)
|
||||
const doingActionRef = useRef(doingAction)
|
||||
const handleSetDoingAction = useCallback((doing: boolean) => {
|
||||
doingActionRef.current = doing
|
||||
setDoingAction(doing)
|
||||
}, [])
|
||||
const [editValues, setEditValues] = useState<Record<string, any> | null>(null)
|
||||
const handleConfirm = useCallback(async () => {
|
||||
if (doingActionRef.current)
|
||||
return
|
||||
if (!pendingOperationCredentialId.current) {
|
||||
setDeleteCredentialId(null)
|
||||
return
|
||||
}
|
||||
try {
|
||||
handleSetDoingAction(true)
|
||||
await deletePluginCredential({ credential_id: pendingOperationCredentialId.current })
|
||||
notify({
|
||||
type: 'success',
|
||||
message: t('common.api.actionSuccess'),
|
||||
})
|
||||
onUpdate?.()
|
||||
setDeleteCredentialId(null)
|
||||
pendingOperationCredentialId.current = null
|
||||
setEditValues(null)
|
||||
}
|
||||
finally {
|
||||
handleSetDoingAction(false)
|
||||
}
|
||||
}, [deletePluginCredential, onUpdate, notify, t, handleSetDoingAction])
|
||||
const handleEdit = useCallback((id: string, values: Record<string, any>) => {
|
||||
pendingOperationCredentialId.current = id
|
||||
setEditValues(values)
|
||||
}, [])
|
||||
const handleRemove = useCallback(() => {
|
||||
setDeleteCredentialId(pendingOperationCredentialId.current)
|
||||
}, [])
|
||||
const { mutateAsync: setPluginDefaultCredential } = useSetPluginDefaultCredentialHook(pluginPayload)
|
||||
const handleSetDefault = useCallback(async (id: string) => {
|
||||
if (doingActionRef.current)
|
||||
return
|
||||
try {
|
||||
handleSetDoingAction(true)
|
||||
await setPluginDefaultCredential(id)
|
||||
notify({
|
||||
type: 'success',
|
||||
message: t('common.api.actionSuccess'),
|
||||
})
|
||||
onUpdate?.()
|
||||
}
|
||||
finally {
|
||||
handleSetDoingAction(false)
|
||||
}
|
||||
}, [setPluginDefaultCredential, onUpdate, notify, t, handleSetDoingAction])
|
||||
const { mutateAsync: updatePluginCredential } = useUpdatePluginCredentialHook(pluginPayload)
|
||||
const handleRename = useCallback(async (payload: {
|
||||
credential_id: string
|
||||
name: string
|
||||
}) => {
|
||||
if (doingActionRef.current)
|
||||
return
|
||||
try {
|
||||
handleSetDoingAction(true)
|
||||
await updatePluginCredential(payload)
|
||||
notify({
|
||||
type: 'success',
|
||||
message: t('common.api.actionSuccess'),
|
||||
})
|
||||
onUpdate?.()
|
||||
}
|
||||
finally {
|
||||
handleSetDoingAction(false)
|
||||
}
|
||||
}, [updatePluginCredential, notify, t, handleSetDoingAction, onUpdate])
|
||||
|
||||
return {
|
||||
doingAction,
|
||||
handleSetDoingAction,
|
||||
openConfirm,
|
||||
closeConfirm,
|
||||
deleteCredentialId,
|
||||
setDeleteCredentialId,
|
||||
handleConfirm,
|
||||
editValues,
|
||||
setEditValues,
|
||||
handleEdit,
|
||||
handleRemove,
|
||||
handleSetDefault,
|
||||
handleRename,
|
||||
pendingOperationCredentialId,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { useAppContext } from '@/context/app-context'
|
||||
import {
|
||||
useGetPluginCredentialInfoHook,
|
||||
useInvalidPluginCredentialInfoHook,
|
||||
} from './use-credential'
|
||||
import { CredentialTypeEnum } from '../types'
|
||||
import type { PluginPayload } from '../types'
|
||||
|
||||
export const usePluginAuth = (pluginPayload: PluginPayload, enable?: boolean) => {
|
||||
const { data } = useGetPluginCredentialInfoHook(pluginPayload, enable)
|
||||
const { isCurrentWorkspaceManager } = useAppContext()
|
||||
const isAuthorized = !!data?.credentials.length
|
||||
const canOAuth = data?.supported_credential_types.includes(CredentialTypeEnum.OAUTH2)
|
||||
const canApiKey = data?.supported_credential_types.includes(CredentialTypeEnum.API_KEY)
|
||||
const invalidPluginCredentialInfo = useInvalidPluginCredentialInfoHook(pluginPayload)
|
||||
|
||||
return {
|
||||
isAuthorized,
|
||||
canOAuth,
|
||||
canApiKey,
|
||||
credentials: data?.credentials || [],
|
||||
disabled: !isCurrentWorkspaceManager,
|
||||
notAllowCustomCredential: data?.allow_custom_token === false,
|
||||
invalidPluginCredentialInfo,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user