mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-29 07:01:04 +03:00
add credentials
This commit is contained in:
@@ -6,6 +6,9 @@ import { JSDOM } from 'jsdom'
|
||||
import { BaseCallbackHandler } from 'langchain/callbacks'
|
||||
import { Server } from 'socket.io'
|
||||
import { ChainValues } from 'langchain/dist/schema'
|
||||
import { DataSource } from 'typeorm'
|
||||
import { ICommonObject, IDatabaseEntity, INodeData } from './Interface'
|
||||
import { AES, enc } from 'crypto-js'
|
||||
|
||||
export const numberOrExpressionRegex = '^(\\d+\\.?\\d*|{{.*}})$' //return true if string consists only numbers OR expression {{}}
|
||||
export const notEmptyRegex = '(.|\\s)*\\S(.|\\s)*' //return true if string is not empty or blank
|
||||
@@ -350,6 +353,89 @@ export const getEnvironmentVariable = (name: string): string | undefined => {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the path of encryption key
|
||||
* @returns {string}
|
||||
*/
|
||||
const getEncryptionKeyFilePath = (): string => {
|
||||
const checkPaths = [
|
||||
path.join(__dirname, '..', '..', 'server', 'encryption.key'),
|
||||
path.join(__dirname, '..', '..', '..', 'server', 'encryption.key'),
|
||||
path.join(__dirname, '..', '..', '..', '..', 'server', 'encryption.key'),
|
||||
path.join(__dirname, '..', '..', '..', '..', '..', 'server', 'encryption.key')
|
||||
]
|
||||
for (const checkPath of checkPaths) {
|
||||
if (fs.existsSync(checkPath)) {
|
||||
return checkPath
|
||||
}
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
const getEncryptionKeyPath = (): string => {
|
||||
return process.env.SECRETKEY_PATH ? path.join(process.env.SECRETKEY_PATH, 'encryption.key') : getEncryptionKeyFilePath()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the encryption key
|
||||
* @returns {Promise<string>}
|
||||
*/
|
||||
const getEncryptionKey = async (): Promise<string> => {
|
||||
try {
|
||||
return await fs.promises.readFile(getEncryptionKeyPath(), 'utf8')
|
||||
} catch (error) {
|
||||
throw new Error(error)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypt credential data
|
||||
* @param {string} encryptedData
|
||||
* @param {string} componentCredentialName
|
||||
* @param {IComponentCredentials} componentCredentials
|
||||
* @returns {Promise<ICommonObject>}
|
||||
*/
|
||||
const decryptCredentialData = async (encryptedData: string): Promise<ICommonObject> => {
|
||||
const encryptKey = await getEncryptionKey()
|
||||
const decryptedData = AES.decrypt(encryptedData, encryptKey)
|
||||
try {
|
||||
return JSON.parse(decryptedData.toString(enc.Utf8))
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
throw new Error('Credentials could not be decrypted.')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get credential data
|
||||
* @param {string} selectedCredentialId
|
||||
* @param {ICommonObject} options
|
||||
* @returns {Promise<ICommonObject>}
|
||||
*/
|
||||
export const getCredentialData = async (selectedCredentialId: string, options: ICommonObject): Promise<ICommonObject> => {
|
||||
const appDataSource = options.appDataSource as DataSource
|
||||
const databaseEntities = options.databaseEntities as IDatabaseEntity
|
||||
|
||||
try {
|
||||
const credential = await appDataSource.getRepository(databaseEntities['Credential']).findOneBy({
|
||||
id: selectedCredentialId
|
||||
})
|
||||
|
||||
if (!credential) throw new Error(`Credential ${selectedCredentialId} not found`)
|
||||
|
||||
// Decrpyt credentialData
|
||||
const decryptedCredentialData = await decryptCredentialData(credential.encryptedData)
|
||||
|
||||
return decryptedCredentialData
|
||||
} catch (e) {
|
||||
throw new Error(e)
|
||||
}
|
||||
}
|
||||
|
||||
export const getCredentialParam = (paramName: string, credentialData: ICommonObject, nodeData: INodeData): any => {
|
||||
return (nodeData.inputs as ICommonObject)[paramName] ?? credentialData[paramName]
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom chain handler class
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user