Skip to content

Commit

Permalink
Merge pull request #18 from Answers-AI/jm/AAI-51-auth
Browse files Browse the repository at this point in the history
require auth
  • Loading branch information
maxtechera authored Aug 6, 2024
2 parents 40b1a6e + 41d7df2 commit adc8b25
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 81 deletions.
132 changes: 57 additions & 75 deletions packages/components/nodes/documentloaders/Contentful/Contentful.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BaseDocumentLoader } from 'langchain/document_loaders/base'
import { Block, Inline, Node, helpers } from '@contentful/rich-text-types'
import { Document } from 'langchain/document'
import * as contentful from 'contentful'
import { getCredentialData, getCredentialParam } from '../../../src/utils'
import { getCredentialData, getCredentialParam, handleEscapeCharacters } from '../../../src/utils'

interface ContentTypeConfig {
contentType: string
Expand Down Expand Up @@ -59,7 +59,6 @@ export function documentToPlainTextString(
console.warn('Invalid embeddedContentObject or sys property missing:', node)
return acc
}

const embeddedContentType = embeddedContentObject?.sys?.contentType?.sys?.id
const embeddedConfig = parsingRules.embeddedContentTypes?.find(
(config: ContentfulConfig) => config.mainContentType.contentType === embeddedContentType
Expand Down Expand Up @@ -248,6 +247,7 @@ class Contentful_DocumentLoaders implements INode {
const limit = nodeData.inputs?.limit as number
const includeAll = nodeData.inputs?.includeAll as boolean
const includeFieldNames = nodeData.inputs?.includeFieldNames as boolean
const output = nodeData.outputs?.output as string

const deliveryToken = getCredentialParam('deliveryToken', credentialData, nodeData)
const previewToken = getCredentialParam('previewToken', credentialData, nodeData)
Expand Down Expand Up @@ -297,14 +297,14 @@ class Contentful_DocumentLoaders implements INode {
finaldocs.push(...docs)
}

const documentToPlainText = (doc: Document): string => {
return doc.pageContent
}

if (nodeData?.outputs?.output === 'stringOutput') {
return finaldocs.map((doc) => documentToPlainText(doc))
} else {
if (output === 'document') {
return finaldocs
} else {
let finaltext = ''
for (const doc of docs) {
finaltext += `${doc.pageContent}\n`
}
return handleEscapeCharacters(finaltext, false)
}
}
}
Expand Down Expand Up @@ -354,7 +354,6 @@ class ContentfulLoader extends BaseDocumentLoader {
public readonly configUtility: ContentfulConfig
public readonly host?: string
public readonly includeFieldNames: boolean

constructor({
spaceId,
environmentId,
Expand Down Expand Up @@ -426,82 +425,65 @@ class ContentfulLoader extends BaseDocumentLoader {
return this.runQuery()
}

private processContentObject(contentObject: IContentObject, contentTypeConfig: ContentTypeConfig): string {
private processContentObject(
contentObject: IContentObject,
contentTypeConfig: ContentTypeConfig,
processedEntryIds: Set<string>
): string {
const entryId = contentObject.sys?.id
if (entryId && processedEntryIds.has(entryId)) {
return ''
}

processedEntryIds.add(entryId)

const fieldsToProcess = contentTypeConfig.fieldsToParse

return fieldsToProcess
const processedContent = fieldsToProcess
.map((fieldPath: string) => {
const fieldValue = this.getNestedProperty(contentObject, fieldPath)

if (fieldValue === undefined) {
console.warn(`Field value for path ${fieldPath} is undefined in content object:`, contentObject)
return ''
}

const fieldName = fieldPath.split('.').pop() || fieldPath
let processedValue = ''

if (typeof fieldValue === 'object' && fieldValue.sys && fieldValue.sys.type === 'Asset') {
// Handle Asset type
const assetTitle = fieldValue.fields.title || 'Asset'
const assetUrl = fieldValue.fields.file.url
processedValue = `![${assetTitle}](https:${assetUrl})`
} else if (typeof fieldValue === 'object' && fieldValue.nodeType === 'document') {
// Handle rich text
let plainText = documentToPlainTextString(
fieldValue,
'\n',
this.configUtility.richTextParsingRules,
this.processContentObject.bind(this)
)
processedValue = plainText.replaceAll('"', '')
} else if (typeof fieldValue === 'string') {
processedValue = fieldValue.replaceAll('"', '')
} else if (Array.isArray(fieldValue)) {
processedValue = fieldValue
.map((item) => {
if (typeof item === 'object' && item !== null && item.sys?.contentType?.sys?.id) {
const contentType = item.sys.contentType.sys.id
const embeddedConfig = this.configUtility.embeddedContentTypes.find(
(config) => config.contentType === contentType
)
if (embeddedConfig) {
try {
return this.processContentObject(item, embeddedConfig)
} catch (error) {
console.error('Error processing nested content object:', error, item)
return ''
}
}
}
return this.processSimpleValue(item)
})
.filter((item) => item !== '')
.join(', ')
} else if (typeof fieldValue === 'object' && fieldValue !== null) {
if (fieldValue.sys?.contentType?.sys?.id) {
const contentType = fieldValue.sys.contentType.sys.id
const embeddedConfig = this.configUtility.embeddedContentTypes.find((config) => config.contentType === contentType)
if (embeddedConfig) {
try {
processedValue = this.processContentObject(fieldValue, embeddedConfig)
} catch (error) {
console.error('Error processing embedded object:', error, fieldValue)
processedValue = ''
}
} else {
processedValue = this.processSimpleValue(fieldValue)
}
} else {
processedValue = this.processSimpleValue(fieldValue)
}
} else {
processedValue = String(fieldValue)
}
let processedValue = this.processSingleValue(fieldValue, contentTypeConfig, processedEntryIds)

return this.includeFieldNames ? `${fieldName}: ${processedValue}\n\n` : `${processedValue}\n\n`
return processedValue ? (this.includeFieldNames ? `${fieldName}: ${processedValue}\n` : `${processedValue}\n`) : ''
})
.filter(Boolean)
.join('')

return processedContent
}

private processSingleValue(value: any, contentTypeConfig: ContentTypeConfig, processedEntryIds: Set<string>): string {
if (typeof value === 'object' && value !== null) {
if (Array.isArray(value)) {
return value
.map((item) => this.processSingleValue(item, contentTypeConfig, processedEntryIds))
.filter(Boolean)
.join(', ')
}

if (value.sys && value.sys.type === 'Entry') {
const contentType = value.sys.contentType?.sys?.id
const embeddedConfig = this.configUtility.embeddedContentTypes.find((config) => config.contentType === contentType)
if (embeddedConfig) {
return this.processContentObject(value, embeddedConfig, processedEntryIds)
}
return ''
} else if (value.sys && value.sys.type === 'Asset') {
const assetTitle = value.fields.title || 'Asset'
const assetUrl = value.fields.file.url
return `![${assetTitle}](https:${assetUrl})`
} else if (value.nodeType === 'document') {
return documentToPlainTextString(value, '\n', this.configUtility.richTextParsingRules, (obj, config) =>
this.processContentObject(obj, config, processedEntryIds)
)
}
}
return this.processSimpleValue(value)
}

private processSimpleValue(value: any): string {
Expand Down Expand Up @@ -534,7 +516,7 @@ class ContentfulLoader extends BaseDocumentLoader {
}

private createDocumentFromEntry(entry: ContentfulEntry): Document {
const textContent = this.processContentObject(entry, this.configUtility.mainContentType)
const textContent = this.processContentObject(entry, this.configUtility.mainContentType, new Set())
const entryUrl = `https://app.contentful.com/spaces/${this.spaceId}/environments/${this.environmentId}/entries/${entry.sys.id}`

const titlePath = this.configUtility.fieldsForCitation.titleField as string
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/middlewares/authentication/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { User } from '../../database/entities/User'
import { Organization } from '../../database/entities/Organization'

const jwtCheck = auth({
authRequired: false,
authRequired: true,
secret: process.env.AUTH0_SECRET,
audience: process.env.AUTH0_AUDIENCE,
issuerBaseURL: process.env.AUTH0_ISSUER_BASE_URL,
Expand Down
8 changes: 3 additions & 5 deletions packages/server/src/utils/buildChatflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,13 @@ export const utilBuildChatflow = async (req: Request, socketIO?: Server, isInter
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Chatflow ${chatflowid} not found`)
}

if (!chatflow?.isPublic && !(await checkOwnership(chatflow, req.user?.id, req.user?.organizationId))) {
throw new InternalFlowiseError(StatusCodes.UNAUTHORIZED, `Unauthorized`)
}
const chatId = incomingInput.chatId ?? incomingInput.overrideConfig?.sessionId ?? uuidv4()
const userMessageDateTime = new Date()

if (!isInternal) {
if (!isInternal && !chatflow?.isPublic) {
const isOwner = await checkOwnership(chatflow, req.user?.id, req.user?.organizationId)
const isKeyValidated = await utilValidateKey(req, chatflow)
if (!isKeyValidated) {
if (!isOwner && !isKeyValidated) {
throw new InternalFlowiseError(StatusCodes.UNAUTHORIZED, `Unauthorized`)
}
}
Expand Down

0 comments on commit adc8b25

Please sign in to comment.