Skip to content

Add security to FastAPIAdapter - #442

Merged
davorrunje merged 18 commits into
mainfrom
390-add-security-to-fastapi-adapter
Oct 23, 2024
Merged

Add security to FastAPIAdapter#442
davorrunje merged 18 commits into
mainfrom
390-add-security-to-fastapi-adapter

Conversation

@sternakt

@sternakt sternakt commented Oct 21, 2024

Copy link
Copy Markdown
Contributor

Description

Type of change

  • Documentation (typos, code examples, or any documentation updates)
  • Bug fix (a non-breaking change that resolves an issue)
  • New feature (a non-breaking change that adds functionality)
  • Breaking change (a fix or feature that would disrupt existing functionality)
  • This change requires a documentation update

Checklist

  • I have conducted a self-review of my own code
  • I have made the necessary changes to the documentation
  • My changes do not generate any new warnings
  • I have added tests to validate the effectiveness of my fix or the functionality of my new feature
  • I have included code examples to illustrate the modifications

@sternakt sternakt linked an issue Oct 21, 2024 that may be closed by this pull request
Comment thread fastagency/adapters/nats/base.py Outdated
f"Message in subject 'chat.server.initiate_chat': {body=} -> from process id {os.getpid()}"
)
user_id = body.user_id.hex if body.user_id else "None"
user_id = body.user_id.hex if body.user_id else "None" # type: ignore

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert back

Comment thread pyproject.toml Outdated
"watchfiles==0.24.0",
"email-validator==2.2.0",
"python-multipart>=0.0.12",
"PyJWT>=2.9.0",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"PyJWT>=2.9.0",
"PyJWT==2.9.0",

Comment thread pyproject.toml
@@ -149,6 +149,8 @@ testing = [
"watchfiles==0.24.0",
"email-validator==2.2.0",
"python-multipart>=0.0.12",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"python-multipart>=0.0.12",
"python-multipart==0.0.12",

Comment thread pyproject.toml Outdated
"email-validator==2.2.0",
"python-multipart>=0.0.12",
"PyJWT>=2.9.0",
"passlib[bcrypt]>=1.7.4",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"passlib[bcrypt]>=1.7.4",
"passlib[bcrypt]==1.7.4",

Comment thread fastagency/adapters/fastapi/base.py Outdated
def get_user_id_depends(
user_id: Optional[str] = Depends(self.get_user_id),
) -> Optional[str]:
return user_id

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return user_id
raise RuntimeError("should never be called") # pragma: no cover

Comment thread fastagency/adapters/fastapi/base.py Outdated

self.router = self.setup_routes()

async def get_user_id_websocket(self, websocket: WebSocket) -> Optional[UUID]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
async def get_user_id_websocket(self, websocket: WebSocket) -> Optional[UUID]:
async def get_user_id_websocket_stub(self, websocket: WebSocket) -> Optional[UUID]:

Comment thread fastagency/adapters/fastapi/base.py Outdated
) -> Optional[str]:
return user_id

dependant = get_dependant(path="", call=get_user_id_depends)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dependant = get_dependant(path="", call=get_user_id_depends)
dependant = get_dependant(path="", call=get_user_id_websocket_stub)

Comment thread fastagency/messages.py Outdated

class InitiateWorkflowModel(BaseModel):
user_id: Optional[UUID] = None
user_id: Optional[Union[UUID, str]] = None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
user_id: Optional[Union[UUID, str]] = None
user_id: Optional[str] = None

To use JWT tokens, you need to install [PyJWT](https://pyjwt.readthedocs.io/en/stable/) and [passlib](https://passlib.readthedocs.io/en/stable/) in addition to fastagency.

```console
pip install "fastagency[autogen,mesop,fastapi,server]"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

single line install

@davorrunje
davorrunje added this pull request to the merge queue Oct 23, 2024
Merged via the queue into main with commit 14b086a Oct 23, 2024
@davorrunje
davorrunje deleted the 390-add-security-to-fastapi-adapter branch October 23, 2024 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add security to FastAPIAdapter

2 participants