tool oauth
parent
ce8bf7b5a2
commit
8968a3e254
@ -0,0 +1,94 @@
|
|||||||
|
import {
|
||||||
|
memo,
|
||||||
|
useCallback,
|
||||||
|
useMemo,
|
||||||
|
useState,
|
||||||
|
} from 'react'
|
||||||
|
import { RiArrowDownSLine } from '@remixicon/react'
|
||||||
|
import Button from '@/app/components/base/button'
|
||||||
|
import Indicator from '@/app/components/header/indicator'
|
||||||
|
import cn from '@/utils/classnames'
|
||||||
|
import type { Credential } from './types'
|
||||||
|
import {
|
||||||
|
Authorized,
|
||||||
|
usePluginAuth,
|
||||||
|
} from '.'
|
||||||
|
|
||||||
|
type AuthorizedInNodeProps = {
|
||||||
|
provider: string
|
||||||
|
onAuthorizationItemClick: (id: string) => void
|
||||||
|
credentialId?: string
|
||||||
|
}
|
||||||
|
const AuthorizedInNode = ({
|
||||||
|
provider = '',
|
||||||
|
onAuthorizationItemClick,
|
||||||
|
credentialId,
|
||||||
|
}: AuthorizedInNodeProps) => {
|
||||||
|
const [isOpen, setIsOpen] = useState(false)
|
||||||
|
const {
|
||||||
|
canApiKey,
|
||||||
|
canOAuth,
|
||||||
|
credentials,
|
||||||
|
disabled,
|
||||||
|
} = usePluginAuth(provider, isOpen)
|
||||||
|
const label = useMemo(() => {
|
||||||
|
if (!credentialId)
|
||||||
|
return 'Workspace default'
|
||||||
|
const credential = credentials.find(c => c.id === credentialId)
|
||||||
|
|
||||||
|
if (!credential)
|
||||||
|
return 'Auth removed'
|
||||||
|
|
||||||
|
return credential.name
|
||||||
|
}, [credentials, credentialId])
|
||||||
|
const renderTrigger = useCallback((open?: boolean) => {
|
||||||
|
return (
|
||||||
|
<Button
|
||||||
|
size='small'
|
||||||
|
className={cn(open && 'bg-components-button-ghost-bg-hover')}
|
||||||
|
>
|
||||||
|
<Indicator className='mr-1.5' />
|
||||||
|
{label}
|
||||||
|
<RiArrowDownSLine className='h-3.5 w-3.5 text-components-button-ghost-text' />
|
||||||
|
</Button>
|
||||||
|
)
|
||||||
|
}, [label])
|
||||||
|
const extraAuthorizationItems: Credential[] = [
|
||||||
|
{
|
||||||
|
id: '__workspace_default__',
|
||||||
|
name: 'Workspace default',
|
||||||
|
provider: '',
|
||||||
|
is_default: false,
|
||||||
|
isWorkspaceDefault: true,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
const handleAuthorizationItemClick = useCallback((id: string) => {
|
||||||
|
onAuthorizationItemClick(id)
|
||||||
|
setIsOpen(false)
|
||||||
|
}, [
|
||||||
|
onAuthorizationItemClick,
|
||||||
|
setIsOpen,
|
||||||
|
])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Authorized
|
||||||
|
provider={provider}
|
||||||
|
credentials={credentials}
|
||||||
|
canOAuth={canOAuth}
|
||||||
|
canApiKey={canApiKey}
|
||||||
|
renderTrigger={renderTrigger}
|
||||||
|
isOpen={isOpen}
|
||||||
|
onOpenChange={setIsOpen}
|
||||||
|
offset={4}
|
||||||
|
placement='bottom-end'
|
||||||
|
triggerPopupSameWidth={false}
|
||||||
|
popupClassName='w-[360px]'
|
||||||
|
disabled={disabled}
|
||||||
|
disableSetDefault
|
||||||
|
onItemClick={handleAuthorizationItemClick}
|
||||||
|
extraAuthorizationItems={extraAuthorizationItems}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default memo(AuthorizedInNode)
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
import { useAppContext } from '@/context/app-context'
|
||||||
|
import { useGetPluginToolCredentialInfo } from '@/service/use-plugins-auth'
|
||||||
|
import { CredentialTypeEnum } from './types'
|
||||||
|
|
||||||
|
export const usePluginAuth = (provider: string, enable?: boolean) => {
|
||||||
|
const { data } = useGetPluginToolCredentialInfo(enable ? provider : '')
|
||||||
|
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)
|
||||||
|
|
||||||
|
return {
|
||||||
|
isAuthorized,
|
||||||
|
canOAuth,
|
||||||
|
canApiKey,
|
||||||
|
credentials: data?.credentials || [],
|
||||||
|
provider,
|
||||||
|
disabled: !isCurrentWorkspaceManager,
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1 +1,4 @@
|
|||||||
export { default as PluginAuth } from './plugin-auth'
|
export { default as PluginAuth } from './plugin-auth'
|
||||||
|
export { default as Authorized } from './authorized'
|
||||||
|
export { default as AuthorizedInNode } from './authorized-in-node'
|
||||||
|
export { usePluginAuth } from './hooks'
|
||||||
|
|||||||
@ -0,0 +1,10 @@
|
|||||||
|
export const transformFormSchemasSecretInput = (isPristineSecretInputNames: string[], values: Record<string, any>) => {
|
||||||
|
const transformedValues: Record<string, any> = { ...values }
|
||||||
|
|
||||||
|
isPristineSecretInputNames.forEach((name) => {
|
||||||
|
if (transformedValues[name])
|
||||||
|
transformedValues[name] = '[__HIDDEN__]'
|
||||||
|
})
|
||||||
|
|
||||||
|
return transformedValues
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue