update import api keys, remove redundant functions
This commit is contained in:
parent
6f8079f6ee
commit
9e8ae84921
|
|
@ -97,6 +97,7 @@ const deleteApiKey = async (id: string, workspaceId?: string) => {
|
||||||
const importKeys = async (body: any) => {
|
const importKeys = async (body: any) => {
|
||||||
try {
|
try {
|
||||||
const jsonFile = body.jsonFile
|
const jsonFile = body.jsonFile
|
||||||
|
const workspaceId = body.workspaceId
|
||||||
const splitDataURI = jsonFile.split(',')
|
const splitDataURI = jsonFile.split(',')
|
||||||
if (splitDataURI[0] !== 'data:application/json;base64') {
|
if (splitDataURI[0] !== 'data:application/json;base64') {
|
||||||
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Invalid dataURI`)
|
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Invalid dataURI`)
|
||||||
|
|
@ -105,11 +106,46 @@ const importKeys = async (body: any) => {
|
||||||
const plain = bf.toString('utf8')
|
const plain = bf.toString('utf8')
|
||||||
const keys = JSON.parse(plain)
|
const keys = JSON.parse(plain)
|
||||||
|
|
||||||
|
// Validate schema of imported keys
|
||||||
|
if (!Array.isArray(keys)) {
|
||||||
|
throw new InternalFlowiseError(StatusCodes.BAD_REQUEST, `Invalid format: Expected an array of API keys`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const requiredFields = ['keyName', 'apiKey', 'apiSecret', 'createdAt', 'id']
|
||||||
|
for (let i = 0; i < keys.length; i++) {
|
||||||
|
const key = keys[i]
|
||||||
|
if (typeof key !== 'object' || key === null) {
|
||||||
|
throw new InternalFlowiseError(StatusCodes.BAD_REQUEST, `Invalid format: Key at index ${i} is not an object`)
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const field of requiredFields) {
|
||||||
|
if (!(field in key)) {
|
||||||
|
throw new InternalFlowiseError(
|
||||||
|
StatusCodes.BAD_REQUEST,
|
||||||
|
`Invalid format: Key at index ${i} is missing required field '${field}'`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if (typeof key[field] !== 'string') {
|
||||||
|
throw new InternalFlowiseError(
|
||||||
|
StatusCodes.BAD_REQUEST,
|
||||||
|
`Invalid format: Key at index ${i} field '${field}' must be a string`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if (key[field].trim() === '') {
|
||||||
|
throw new InternalFlowiseError(
|
||||||
|
StatusCodes.BAD_REQUEST,
|
||||||
|
`Invalid format: Key at index ${i} field '${field}' cannot be empty`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const appServer = getRunningExpressApp()
|
const appServer = getRunningExpressApp()
|
||||||
const allApiKeys = await appServer.AppDataSource.getRepository(ApiKey).find()
|
const allApiKeys = await appServer.AppDataSource.getRepository(ApiKey).findBy(getWorkspaceSearchOptions(workspaceId))
|
||||||
if (body.importMode === 'replaceAll') {
|
if (body.importMode === 'replaceAll') {
|
||||||
await appServer.AppDataSource.getRepository(ApiKey).delete({
|
await appServer.AppDataSource.getRepository(ApiKey).delete({
|
||||||
id: Not(IsNull())
|
id: Not(IsNull()),
|
||||||
|
workspaceId: workspaceId
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if (body.importMode === 'errorIfExist') {
|
if (body.importMode === 'errorIfExist') {
|
||||||
|
|
@ -127,12 +163,13 @@ const importKeys = async (body: any) => {
|
||||||
if (keyNameExists) {
|
if (keyNameExists) {
|
||||||
const keyIndex = allApiKeys.findIndex((k) => k.keyName === key.keyName)
|
const keyIndex = allApiKeys.findIndex((k) => k.keyName === key.keyName)
|
||||||
switch (body.importMode) {
|
switch (body.importMode) {
|
||||||
case 'overwriteIfExist': {
|
case 'overwriteIfExist':
|
||||||
|
case 'replaceAll': {
|
||||||
const currentKey = allApiKeys[keyIndex]
|
const currentKey = allApiKeys[keyIndex]
|
||||||
currentKey.id = uuidv4()
|
currentKey.id = uuidv4()
|
||||||
currentKey.apiKey = key.apiKey
|
currentKey.apiKey = key.apiKey
|
||||||
currentKey.apiSecret = key.apiSecret
|
currentKey.apiSecret = key.apiSecret
|
||||||
currentKey.workspaceId = body.workspaceId
|
currentKey.workspaceId = workspaceId
|
||||||
await appServer.AppDataSource.getRepository(ApiKey).save(currentKey)
|
await appServer.AppDataSource.getRepository(ApiKey).save(currentKey)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
@ -154,12 +191,12 @@ const importKeys = async (body: any) => {
|
||||||
newKey.apiKey = key.apiKey
|
newKey.apiKey = key.apiKey
|
||||||
newKey.apiSecret = key.apiSecret
|
newKey.apiSecret = key.apiSecret
|
||||||
newKey.keyName = key.keyName
|
newKey.keyName = key.keyName
|
||||||
newKey.workspaceId = body.workspaceId
|
newKey.workspaceId = workspaceId
|
||||||
const newKeyEntity = appServer.AppDataSource.getRepository(ApiKey).create(newKey)
|
const newKeyEntity = appServer.AppDataSource.getRepository(ApiKey).create(newKey)
|
||||||
await appServer.AppDataSource.getRepository(ApiKey).save(newKeyEntity)
|
await appServer.AppDataSource.getRepository(ApiKey).save(newKeyEntity)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return await getAllApiKeysFromDB(body.workspaceId)
|
return await getAllApiKeysFromDB(workspaceId)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: apikeyService.importKeys - ${getErrorMessage(error)}`)
|
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: apikeyService.importKeys - ${getErrorMessage(error)}`)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -201,16 +201,6 @@ const abortChatMessage = async (chatId: string, chatflowid: string) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getAllMessages(): Promise<ChatMessage[]> {
|
|
||||||
const appServer = getRunningExpressApp()
|
|
||||||
return await appServer.AppDataSource.getRepository(ChatMessage).find()
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getAllMessagesFeedback(): Promise<ChatMessageFeedback[]> {
|
|
||||||
const appServer = getRunningExpressApp()
|
|
||||||
return await appServer.AppDataSource.getRepository(ChatMessageFeedback).find()
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getMessagesByChatflowIds(chatflowIds: string[]): Promise<ChatMessage[]> {
|
async function getMessagesByChatflowIds(chatflowIds: string[]): Promise<ChatMessage[]> {
|
||||||
const appServer = getRunningExpressApp()
|
const appServer = getRunningExpressApp()
|
||||||
return await appServer.AppDataSource.getRepository(ChatMessage).find({ where: { chatflowid: In(chatflowIds) } })
|
return await appServer.AppDataSource.getRepository(ChatMessage).find({ where: { chatflowid: In(chatflowIds) } })
|
||||||
|
|
@ -228,8 +218,6 @@ export default {
|
||||||
removeAllChatMessages,
|
removeAllChatMessages,
|
||||||
removeChatMessagesByMessageIds,
|
removeChatMessagesByMessageIds,
|
||||||
abortChatMessage,
|
abortChatMessage,
|
||||||
getAllMessages,
|
|
||||||
getAllMessagesFeedback,
|
|
||||||
getMessagesByChatflowIds,
|
getMessagesByChatflowIds,
|
||||||
getMessagesFeedbackByChatflowIds
|
getMessagesFeedbackByChatflowIds
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -90,19 +90,6 @@ const getAllDocumentStores = async (workspaceId?: string) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const getAllDocumentFileChunks = async () => {
|
|
||||||
try {
|
|
||||||
const appServer = getRunningExpressApp()
|
|
||||||
const entities = await appServer.AppDataSource.getRepository(DocumentStoreFileChunk).find()
|
|
||||||
return entities
|
|
||||||
} catch (error) {
|
|
||||||
throw new InternalFlowiseError(
|
|
||||||
StatusCodes.INTERNAL_SERVER_ERROR,
|
|
||||||
`Error: documentStoreServices.getAllDocumentFileChunks - ${getErrorMessage(error)}`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const getAllDocumentFileChunksByDocumentStoreIds = async (documentStoreIds: string[]) => {
|
const getAllDocumentFileChunksByDocumentStoreIds = async (documentStoreIds: string[]) => {
|
||||||
const appServer = getRunningExpressApp()
|
const appServer = getRunningExpressApp()
|
||||||
return await appServer.AppDataSource.getRepository(DocumentStoreFileChunk).find({ where: { storeId: In(documentStoreIds) } })
|
return await appServer.AppDataSource.getRepository(DocumentStoreFileChunk).find({ where: { storeId: In(documentStoreIds) } })
|
||||||
|
|
@ -2258,7 +2245,6 @@ export default {
|
||||||
createDocumentStore,
|
createDocumentStore,
|
||||||
deleteLoaderFromDocumentStore,
|
deleteLoaderFromDocumentStore,
|
||||||
getAllDocumentStores,
|
getAllDocumentStores,
|
||||||
getAllDocumentFileChunks,
|
|
||||||
getAllDocumentFileChunksByDocumentStoreIds,
|
getAllDocumentFileChunksByDocumentStoreIds,
|
||||||
getDocumentStoreById,
|
getDocumentStoreById,
|
||||||
getUsedChatflowNames,
|
getUsedChatflowNames,
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ export const validateChatflowAPIKey = async (req: Request, chatflow: ChatFlow) =
|
||||||
if (suppliedKey) {
|
if (suppliedKey) {
|
||||||
const keys = await apikeyService.getAllApiKeys()
|
const keys = await apikeyService.getAllApiKeys()
|
||||||
const apiSecret = keys.find((key: any) => key.id === chatFlowApiKeyId)?.apiSecret
|
const apiSecret = keys.find((key: any) => key.id === chatFlowApiKeyId)?.apiSecret
|
||||||
|
if (!apiSecret) return false
|
||||||
if (!compareKeys(apiSecret, suppliedKey)) return false
|
if (!compareKeys(apiSecret, suppliedKey)) return false
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -424,7 +424,7 @@ const APIKey = () => {
|
||||||
<StyledTableCell>Key Name</StyledTableCell>
|
<StyledTableCell>Key Name</StyledTableCell>
|
||||||
<StyledTableCell>API Key</StyledTableCell>
|
<StyledTableCell>API Key</StyledTableCell>
|
||||||
<StyledTableCell>Usage</StyledTableCell>
|
<StyledTableCell>Usage</StyledTableCell>
|
||||||
<StyledTableCell>Created</StyledTableCell>
|
<StyledTableCell>Updated</StyledTableCell>
|
||||||
<Available permission={'apikeys:update,apikeys:create'}>
|
<Available permission={'apikeys:update,apikeys:create'}>
|
||||||
<StyledTableCell> </StyledTableCell>
|
<StyledTableCell> </StyledTableCell>
|
||||||
</Available>
|
</Available>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue