This repository has been archived by the owner on Feb 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(API): Add authentication (#533)
* Adds migration for RLS for existing tables * Modified Supabase session with auth credentials * Updates all endpoints with auth * Adds auth to relevant API tests * Refactored base CRUD operations to handle DB connection in constructor
- Loading branch information
1 parent
884761b
commit a634a59
Showing
27 changed files
with
694 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.jwt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,38 @@ | ||
SHELL := /bin/bash | ||
|
||
|
||
export SUPABASE_URL=$(shell supabase status | grep -oP '(?<=API URL: ).*') | ||
export SUPABASE_ANON_KEY=$(shell supabase status | grep -oP '(?<=anon key: ).*') | ||
|
||
install: | ||
python -m pip install ../../src/leapfrogai_sdk | ||
python -m pip install -e . | ||
|
||
dev: | ||
make install | ||
python -m uvicorn main:app --port 3000 --reload | ||
python -m uvicorn main:app --port 3000 --reload --log-level info | ||
|
||
test-integration: | ||
cd ../../ && python -m pytest tests/integration/api | ||
|
||
define get_jwt_token | ||
echo "Getting JWT token from ${SUPABASE_URL}..."; \ | ||
TOKEN_RESPONSE=$$(curl -s -X POST $(1) \ | ||
-H "apikey: ${SUPABASE_ANON_KEY}" \ | ||
-H "Content-Type: application/json" \ | ||
-d '{ "email": "admin@localhost", "password": "$$SUPABASE_PASS"}'); \ | ||
echo "Extracting token from $(TOKEN_RESPONSE)"; \ | ||
JWT=$$(echo $$TOKEN_RESPONSE | grep -oP '(?<="access_token":")[^"]*'); \ | ||
echo -n "$$JWT" | xclip -selection clipboard; \ | ||
echo "export SUPABASE_USER_JWT=$$JWT" > .jwt; \ | ||
echo "DONE - JWT token copied to clipboard" | ||
endef | ||
|
||
supabase-user: | ||
@read -s -p "Enter your Supabase password: " SUPABASE_PASS; echo; \ | ||
echo "Creating new supabase user..."; \ | ||
$(call get_jwt_token,"${SUPABASE_URL}/auth/v1/signup") | ||
|
||
supabase-jwt-token: | ||
@read -s -p "Enter your Supabase password: " SUPABASE_PASS; echo; \ | ||
$(call get_jwt_token,"${SUPABASE_URL}/auth/v1/token?grant_type=password") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,51 @@ | ||
"""CRUD Operations for Assistant.""" | ||
|
||
from pydantic import Field | ||
from openai.types.beta import Assistant | ||
from supabase_py_async import AsyncClient | ||
from leapfrogai_api.data.crud_base import CRUDBase | ||
|
||
from leapfrogai_api.data.crud_base import CRUDBase, ModelType | ||
|
||
class CRUDAssistant(CRUDBase[Assistant]): | ||
|
||
class AuthAssistant(Assistant): | ||
"""A wrapper for the Assistant that includes a user_id for auth""" | ||
|
||
user_id: str = Field(default="") | ||
|
||
|
||
class CRUDAssistant(CRUDBase[AuthAssistant]): | ||
"""CRUD Operations for Assistant""" | ||
|
||
def __init__(self, model: type[Assistant], table_name: str = "assistant_objects"): | ||
super().__init__(model=model, table_name=table_name) | ||
def __init__( | ||
self, | ||
db: AsyncClient, | ||
model: type[ModelType] = AuthAssistant, | ||
table_name: str = "assistant_objects", | ||
): | ||
super().__init__(db, model, table_name) | ||
|
||
async def create(self, db: AsyncClient, object_: Assistant) -> Assistant | None: | ||
async def create(self, object_: Assistant) -> AuthAssistant | None: | ||
"""Create a new assistant.""" | ||
return await super().create(db=db, object_=object_) | ||
user_id: str = (await self.db.auth.get_user()).user.id | ||
return await super().create( | ||
object_=AuthAssistant(user_id=user_id, **object_.model_dump()) | ||
) | ||
|
||
async def get(self, id_: str, db: AsyncClient) -> Assistant | None: | ||
async def get(self, id_: str) -> AuthAssistant | None: | ||
"""Get an assistant by its ID.""" | ||
return await super().get(db=db, id_=id_) | ||
return await super().get(id_=id_) | ||
|
||
async def list(self, db: AsyncClient) -> list[Assistant] | None: | ||
async def list(self) -> list[AuthAssistant] | None: | ||
"""List all assistants.""" | ||
return await super().list(db=db) | ||
return await super().list() | ||
|
||
async def update( | ||
self, id_: str, db: AsyncClient, object_: Assistant | ||
) -> Assistant | None: | ||
async def update(self, id_: str, object_: Assistant) -> AuthAssistant | None: | ||
"""Update an assistant by its ID.""" | ||
return await super().update(id_=id_, db=db, object_=object_) | ||
user_id: str = (await self.db.auth.get_user()).user.id | ||
return await super().update( | ||
id_=id_, object_=AuthAssistant(user_id=user_id, **object_.model_dump()) | ||
) | ||
|
||
async def delete(self, id_: str, db: AsyncClient) -> bool: | ||
async def delete(self, id_: str) -> bool: | ||
"""Delete an assistant by its ID.""" | ||
return await super().delete(id_=id_, db=db) | ||
return await super().delete(id_=id_) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.