|
1 | 1 | import path from 'path'
|
2 | 2 | import fs from 'fs'
|
3 |
| -import moment from 'moment' |
4 | 3 | import logger from './logger'
|
5 | 4 | import {
|
| 5 | + IComponentCredentials, |
6 | 6 | IComponentNodes,
|
| 7 | + ICredentialDataDecrypted, |
| 8 | + ICredentialReqBody, |
7 | 9 | IDepthQueue,
|
8 | 10 | IExploredNode,
|
| 11 | + INodeData, |
9 | 12 | INodeDependencies,
|
10 | 13 | INodeDirectedGraph,
|
11 | 14 | INodeQueue,
|
| 15 | + IOverrideConfig, |
12 | 16 | IReactFlowEdge,
|
13 | 17 | IReactFlowNode,
|
14 |
| - IVariableDict, |
15 |
| - INodeData, |
16 |
| - IOverrideConfig, |
17 |
| - ICredentialDataDecrypted, |
18 |
| - IComponentCredentials, |
19 |
| - ICredentialReqBody |
| 18 | + IVariableDict |
20 | 19 | } from '../Interface'
|
21 | 20 | import { cloneDeep, get, isEqual } from 'lodash'
|
22 | 21 | import {
|
23 |
| - ICommonObject, |
| 22 | + convertChatHistoryToText, |
24 | 23 | getInputVariables,
|
25 |
| - IDatabaseEntity, |
26 | 24 | handleEscapeCharacters,
|
27 |
| - IMessage, |
28 |
| - convertChatHistoryToText |
| 25 | + ICommonObject, |
| 26 | + IDatabaseEntity, |
| 27 | + IMessage |
29 | 28 | } from 'flowise-components'
|
30 |
| -import { scryptSync, randomBytes, timingSafeEqual } from 'crypto' |
| 29 | +import { randomBytes } from 'crypto' |
31 | 30 | import { AES, enc } from 'crypto-js'
|
32 | 31 |
|
33 | 32 | import { ChatFlow } from '../database/entities/ChatFlow'
|
@@ -574,147 +573,6 @@ export const isSameOverrideConfig = (
|
574 | 573 | return false
|
575 | 574 | }
|
576 | 575 |
|
577 |
| -/** |
578 |
| - * Returns the api key path |
579 |
| - * @returns {string} |
580 |
| - */ |
581 |
| -export const getAPIKeyPath = (): string => { |
582 |
| - return process.env.APIKEY_PATH ? path.join(process.env.APIKEY_PATH, 'api.json') : path.join(__dirname, '..', '..', 'api.json') |
583 |
| -} |
584 |
| - |
585 |
| -/** |
586 |
| - * Generate the api key |
587 |
| - * @returns {string} |
588 |
| - */ |
589 |
| -export const generateAPIKey = (): string => { |
590 |
| - const buffer = randomBytes(32) |
591 |
| - return buffer.toString('base64') |
592 |
| -} |
593 |
| - |
594 |
| -/** |
595 |
| - * Generate the secret key |
596 |
| - * @param {string} apiKey |
597 |
| - * @returns {string} |
598 |
| - */ |
599 |
| -export const generateSecretHash = (apiKey: string): string => { |
600 |
| - const salt = randomBytes(8).toString('hex') |
601 |
| - const buffer = scryptSync(apiKey, salt, 64) as Buffer |
602 |
| - return `${buffer.toString('hex')}.${salt}` |
603 |
| -} |
604 |
| - |
605 |
| -/** |
606 |
| - * Verify valid keys |
607 |
| - * @param {string} storedKey |
608 |
| - * @param {string} suppliedKey |
609 |
| - * @returns {boolean} |
610 |
| - */ |
611 |
| -export const compareKeys = (storedKey: string, suppliedKey: string): boolean => { |
612 |
| - const [hashedPassword, salt] = storedKey.split('.') |
613 |
| - const buffer = scryptSync(suppliedKey, salt, 64) as Buffer |
614 |
| - return timingSafeEqual(Buffer.from(hashedPassword, 'hex'), buffer) |
615 |
| -} |
616 |
| - |
617 |
| -/** |
618 |
| - * Get API keys |
619 |
| - * @returns {Promise<ICommonObject[]>} |
620 |
| - */ |
621 |
| -export const getAPIKeys = async (): Promise<ICommonObject[]> => { |
622 |
| - try { |
623 |
| - const content = await fs.promises.readFile(getAPIKeyPath(), 'utf8') |
624 |
| - return JSON.parse(content) |
625 |
| - } catch (error) { |
626 |
| - const keyName = 'DefaultKey' |
627 |
| - const apiKey = generateAPIKey() |
628 |
| - const apiSecret = generateSecretHash(apiKey) |
629 |
| - const content = [ |
630 |
| - { |
631 |
| - keyName, |
632 |
| - apiKey, |
633 |
| - apiSecret, |
634 |
| - createdAt: moment().format('DD-MMM-YY'), |
635 |
| - id: randomBytes(16).toString('hex') |
636 |
| - } |
637 |
| - ] |
638 |
| - await fs.promises.writeFile(getAPIKeyPath(), JSON.stringify(content), 'utf8') |
639 |
| - return content |
640 |
| - } |
641 |
| -} |
642 |
| - |
643 |
| -/** |
644 |
| - * Add new API key |
645 |
| - * @param {string} keyName |
646 |
| - * @returns {Promise<ICommonObject[]>} |
647 |
| - */ |
648 |
| -export const addAPIKey = async (keyName: string): Promise<ICommonObject[]> => { |
649 |
| - const existingAPIKeys = await getAPIKeys() |
650 |
| - const apiKey = generateAPIKey() |
651 |
| - const apiSecret = generateSecretHash(apiKey) |
652 |
| - const content = [ |
653 |
| - ...existingAPIKeys, |
654 |
| - { |
655 |
| - keyName, |
656 |
| - apiKey, |
657 |
| - apiSecret, |
658 |
| - createdAt: moment().format('DD-MMM-YY'), |
659 |
| - id: randomBytes(16).toString('hex') |
660 |
| - } |
661 |
| - ] |
662 |
| - await fs.promises.writeFile(getAPIKeyPath(), JSON.stringify(content), 'utf8') |
663 |
| - return content |
664 |
| -} |
665 |
| - |
666 |
| -/** |
667 |
| - * Get API Key details |
668 |
| - * @param {string} apiKey |
669 |
| - * @returns {Promise<ICommonObject[]>} |
670 |
| - */ |
671 |
| -export const getApiKey = async (apiKey: string) => { |
672 |
| - const existingAPIKeys = await getAPIKeys() |
673 |
| - const keyIndex = existingAPIKeys.findIndex((key) => key.apiKey === apiKey) |
674 |
| - if (keyIndex < 0) return undefined |
675 |
| - return existingAPIKeys[keyIndex] |
676 |
| -} |
677 |
| - |
678 |
| -/** |
679 |
| - * Update existing API key |
680 |
| - * @param {string} keyIdToUpdate |
681 |
| - * @param {string} newKeyName |
682 |
| - * @returns {Promise<ICommonObject[]>} |
683 |
| - */ |
684 |
| -export const updateAPIKey = async (keyIdToUpdate: string, newKeyName: string): Promise<ICommonObject[]> => { |
685 |
| - const existingAPIKeys = await getAPIKeys() |
686 |
| - const keyIndex = existingAPIKeys.findIndex((key) => key.id === keyIdToUpdate) |
687 |
| - if (keyIndex < 0) return [] |
688 |
| - existingAPIKeys[keyIndex].keyName = newKeyName |
689 |
| - await fs.promises.writeFile(getAPIKeyPath(), JSON.stringify(existingAPIKeys), 'utf8') |
690 |
| - return existingAPIKeys |
691 |
| -} |
692 |
| - |
693 |
| -/** |
694 |
| - * Delete API key |
695 |
| - * @param {string} keyIdToDelete |
696 |
| - * @returns {Promise<ICommonObject[]>} |
697 |
| - */ |
698 |
| -export const deleteAPIKey = async (keyIdToDelete: string): Promise<ICommonObject[]> => { |
699 |
| - const existingAPIKeys = await getAPIKeys() |
700 |
| - const result = existingAPIKeys.filter((key) => key.id !== keyIdToDelete) |
701 |
| - await fs.promises.writeFile(getAPIKeyPath(), JSON.stringify(result), 'utf8') |
702 |
| - return result |
703 |
| -} |
704 |
| - |
705 |
| -/** |
706 |
| - * Replace all api keys |
707 |
| - * @param {ICommonObject[]} content |
708 |
| - * @returns {Promise<void>} |
709 |
| - */ |
710 |
| -export const replaceAllAPIKeys = async (content: ICommonObject[]): Promise<void> => { |
711 |
| - try { |
712 |
| - await fs.promises.writeFile(getAPIKeyPath(), JSON.stringify(content), 'utf8') |
713 |
| - } catch (error) { |
714 |
| - logger.error(error) |
715 |
| - } |
716 |
| -} |
717 |
| - |
718 | 576 | /**
|
719 | 577 | * Map MimeType to InputField
|
720 | 578 | * @param {string} mimeType
|
|
0 commit comments