Authorization for RAG - Auth0 Docs
AI Identity for B2B
AI Identity for B2B leverages Auth0 FGA to provide fine-grained authorization control for AI agents. As a result, when AI agents use Retrieval Augmented Generation (RAG) to provide sophisticated, relevant responses to user queries, they only have access to authorized data. By the end of this quickstart, you should have an AI application that can:
- Retrieve authorized data as context for a RAG pipeline.
- Use Auth0 FGA to determine if the user has authorization for the data.
Pick your tech stack
- LangGraph.js + Next.js
- Vercel AI + Next.js
- LangGraph + FastAPI
- LangGraph.js + Node.js
- LangGraph + Python
Prerequisites
Before getting started, make sure you:
Create an Auth0 Account To continue with this quickstart, you need to have an Auth0 account.
Create an Auth0 Application Go to your Auth0 Dashboard to create a new Auth0 Application.
- Navigate to Applications > Applications in the left sidebar.
- Click the Create Application button in the top right.
- In the pop-up select Regular Web Applications and click Create.
- Once the Application is created, switch to the Settings tab.
- Scroll down to the Application URIs section.
- Set Allowed Callback URLs as:
http://localhost:3000/auth/callback - Set Allowed Logout URLs as:
http://localhost:3000 - Click Save in the bottom right to save your changes.
Create an Auth0 API
- In your Auth0 Dashboard, go to Applications > APIs.
- Create a new API with an identifier (audience).
- Once API is created, go to the APIs Settings > Access Settings and enable Allow Offline Access.
- Note down the API identifier for your environment variables.
Create an Auth0 FGA account
You need an Auth0 FGA account to complete this quickstart.OpenAI Platform Set up an OpenAI account and API key.
Download sample app
Start by downloading and extracting the sample app. Then open in your preferred IDE.
Install dependencies
In the root directory of your project, install the following dependencies:
npm install @auth0/ai-langchain @langchain/langgraph langchain @langchain/core @langchain/openai zod langgraph-nextjs-api-passthrough
Update the environment file
Copy the .env.example file to .env.local and update the variables with your Auth0 credentials. You can find your Auth0 domain, client ID, and client secret in the application you created in the Auth0 Dashboard.
Set up an FGA Store
In the Auth0 FGA dashboard:
- Navigate to Settings. In the Authorized Clients section, click + Create Client.
- Give your client a name and mark all the client permissions that are required for your use case. For the quickstart, you’ll only need Read and query.
- Click Create.
Once your client is created, you’ll see a modal containing Store ID, Client ID, and Client Secret. Add or update an .env.local file with the following content to the root directory of the project:
OPENAI_API_KEY=<your-openai-api-key>
FGA_STORE_ID=<your-fga-store-id>
FGA_CLIENT_ID=<your-fga-store-client-id>
FGA_CLIENT_SECRET=<your-fga-store-client-secret>
FGA_API_URL=https://api.xxx.fga.dev
FGA_API_AUDIENCE=https://api.xxx.fga.dev/
Secure the RAG Tool
After configuring your FGA Store, secure the RAG tool using Auth0 FGA and Auth0 AI SDK. The starter application is already configured to handle documents and embeddings.
Document Upload and Storage
- You can upload documents through the UI (
src/app/documents/page.tsx). - Uploaded documents are processed by the API route (
src/app/api/documents/upload/route.ts). - APIs for uploading and retrieving documents are defined in (
src/lib/actions/documents.ts). - Database is defined in
src/lib/db. - FGA helpers are defined in
src/lib/fga. - Documents are stored as embeddings in a vector database for efficient retrieval (
src/lib/rag/embedding.ts).
Access Control with Auth0 FGA
When a document is uploaded, the app automatically creates FGA tuples to define which users can access which documents. A tuple signifies a user’s relation to a given object. For example, the below tuple implies that all users can view the <document name> object.
Create a RAG tool
Define a RAG tool that uses the FGAFilter to filter authorized data from the vector database:
import { tool } from "@langchain/core/tools";
import { z } from "zod";
import { FGARetriever } from "@auth0/ai-langchain/RAG";
export const getContextDocumentsTool = tool(async ({ question }, config) => {
const user = config?.configurable?._credentials?.user;
if (!user) {
return "There is no user logged in.";
}
const vectorStore = await getVectorStore();
if (!vectorStore) {
return "There is no vector store.";
}
const retriever = FGARetriever.create({
retriever: vectorStore.asRetriever(),
buildQuery: (doc) => ({
user: `user:${user?.email}`,
object: `doc:${doc.metadata.documentId}`,
relation: "can_view",
}),
});
// filter docs based on FGA authorization
const documents = await retriever.invoke(question);
return documents.map((doc) => doc.pageContent).join("\n\n");
});
Test your application
Start the database and create required tables:
# start the postgres database
docker compose up -d
# create the database schema
npm run db:migrate
Start the application with npm run all:dev. Then, navigate to http://localhost:3000.
This will open the LangGraph Studio in a new tab. You can close it as we won’t require it for testing the application. Upload a document from the documents tab and ask your AI agent a question about the document. You should get a response with the relevant information.
Go to an incognito window, log in as a different user, and ask it the same question. You should not get a response. Share the document from the documents page to the second user and try again. You should see the information now.
That’s it! You successfully integrated RAG protected by Auth0 FGA into your project. Explore the example app on GitHub.
Next steps
- Authorization for RAG docs
- Learn how to use Auth0 FGA to create a Relationship-Based Access Control (ReBAC) authorization model.
- Learn more about OpenFGA.