Skip to content

Commit

Permalink
Notebook style
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw committed Dec 18, 2024
1 parent 6954e63 commit 948027a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 82 deletions.
7 changes: 5 additions & 2 deletions docs/docs/tutorials/auth/add_auth_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ Let's test this with a real user account!

## Testing Authentication Flow

Let's test out our new authentication flow. You can run the following code in a file or notebook.
Let's test out our new authentication flow. You can run the following code in a file or notebook. You will need to provide:
- A valid email address
- A Supabase project URL (from [above](#setup-auth-provider))
- A Supabase service role key (also from [above](#setup-auth-provider))

```python
import os
Expand Down Expand Up @@ -199,7 +202,7 @@ Then run the code.
!!! tip "About test emails"
We'll create two test accounts by adding "+1" and "+2" to your email. For example, if you use "[email protected]", we'll create "[email protected]" and "[email protected]". All emails will be delivered to your original address.

⚠️ Before continuing: Check your email and click both confirmation links.
⚠️ Before continuing: Check your email and click both confirmation links. Supabase will will reject `/login` requests until after you have confirmed your users' email.

Now let's test that users can only see their own data. Make sure the server is running (run `langgraph dev`) before proceeding. The following snippet requires the "anon public" key that you copied from the Supabase dashboard while [setting up the auth provider](#setup-auth-provider) previously.

Expand Down
57 changes: 25 additions & 32 deletions docs/docs/tutorials/auth/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,45 +105,38 @@ langgraph dev --no-browser
}
```

Now let's try to chat with our bot. Create a new file `test_auth.py`:
Now let's try to chat with our bot. Run the following code in a file or notebook:

```python
import asyncio
from langgraph_sdk import get_client


async def test_auth():
# Try without a token (should fail)
client = get_client(url="http://localhost:2024")
try:
thread = await client.threads.create()
print("❌ Should have failed without token!")
except Exception as e:
print("✅ Correctly blocked access:", e)

# Try with a valid token
client = get_client(
url="http://localhost:2024", headers={"Authorization": "Bearer user1-token"}
)

# Create a thread and chat
# Try without a token (should fail)
client = get_client(url="http://localhost:2024")
try:
thread = await client.threads.create()
print(f"✅ Created thread as Alice: {thread['thread_id']}")

response = await client.runs.create(
thread_id=thread["thread_id"],
assistant_id="agent",
input={"messages": [{"role": "user", "content": "Hello!"}]},
)
print("✅ Bot responded:")
print(response)


if __name__ == "__main__":
asyncio.run(test_auth())
print("❌ Should have failed without token!")
except Exception as e:
print("✅ Correctly blocked access:", e)

# Try with a valid token
client = get_client(
url="http://localhost:2024", headers={"Authorization": "Bearer user1-token"}
)

# Create a thread and chat
thread = await client.threads.create()
print(f"✅ Created thread as Alice: {thread['thread_id']}")

response = await client.runs.create(
thread_id=thread["thread_id"],
assistant_id="agent",
input={"messages": [{"role": "user", "content": "Hello!"}]},
)
print("✅ Bot responded:")
print(response)
```

Run the test code and you should see that:
You should see that:
1. Without a valid token, we can't access the bot
2. With a valid token, we can create threads and chat

Expand Down
92 changes: 44 additions & 48 deletions docs/docs/tutorials/auth/resource_auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,58 +87,54 @@ Notice that our simple handler does two things:

## Testing Private Conversations

Let's test our authorization. Create a new file `test_private.py`:
Let's test our authorization. If we have set things up correctly, we should expect to see all ✅ messages. Be sure to have your development server running (run `langgraph dev`):

```python
import asyncio
from langgraph_sdk import get_client

async def test_private():
# Create clients for both users
alice = get_client(
url="http://localhost:2024",
headers={"Authorization": "Bearer user1-token"}
)

bob = get_client(
url="http://localhost:2024",
headers={"Authorization": "Bearer user2-token"}
)

# Alice creates a thread and chats
alice_thread = await alice.threads.create()
print(f"✅ Alice created thread: {alice_thread['thread_id']}")

await alice.runs.create(
thread_id=alice_thread["thread_id"],
assistant_id="agent",
input={"messages": [{"role": "user", "content": "Hi, this is Alice's private chat"}]}
)

# Bob tries to access Alice's thread
try:
await bob.threads.get(alice_thread["thread_id"])
print("❌ Bob shouldn't see Alice's thread!")
except Exception as e:
print("✅ Bob correctly denied access:", e)

# Bob creates his own thread
bob_thread = await bob.threads.create()
await bob.runs.create(
thread_id=bob_thread["thread_id"],
assistant_id="agent",
input={"messages": [{"role": "user", "content": "Hi, this is Bob's private chat"}]}
)
print(f"✅ Bob created his own thread: {bob_thread['thread_id']}")

# List threads - each user only sees their own
alice_threads = await alice.threads.list()
bob_threads = await bob.threads.list()
print(f"✅ Alice sees {len(alice_threads)} thread")
print(f"✅ Bob sees {len(bob_threads)} thread")

if __name__ == "__main__":
asyncio.run(test_private())
# Create clients for both users
alice = get_client(
url="http://localhost:2024",
headers={"Authorization": "Bearer user1-token"}
)

bob = get_client(
url="http://localhost:2024",
headers={"Authorization": "Bearer user2-token"}
)

# Alice creates a thread and chats
alice_thread = await alice.threads.create()
print(f"✅ Alice created thread: {alice_thread['thread_id']}")

await alice.runs.create(
thread_id=alice_thread["thread_id"],
assistant_id="agent",
input={"messages": [{"role": "user", "content": "Hi, this is Alice's private chat"}]}
)

# Bob tries to access Alice's thread
try:
await bob.threads.get(alice_thread["thread_id"])
print("❌ Bob shouldn't see Alice's thread!")
except Exception as e:
print("✅ Bob correctly denied access:", e)

# Bob creates his own thread
bob_thread = await bob.threads.create()
await bob.runs.create(
thread_id=bob_thread["thread_id"],
assistant_id="agent",
input={"messages": [{"role": "user", "content": "Hi, this is Bob's private chat"}]}
)
print(f"✅ Bob created his own thread: {bob_thread['thread_id']}")

# List threads - each user only sees their own
alice_threads = await alice.threads.list()
bob_threads = await bob.threads.list()
print(f"✅ Alice sees {len(alice_threads)} thread")
print(f"✅ Bob sees {len(bob_threads)} thread")

```

Run the test code and you should see output like this:
Expand Down

0 comments on commit 948027a

Please sign in to comment.