widgetti / solara

A Pure Python, React-style Framework for Scaling Your Jupyter and Web Apps
https://solara.dev
MIT License
1.62k stars 105 forks source link

Oauth2, FastAPI, and Solara #157

Open theeldermillenial opened 11 months ago

theeldermillenial commented 11 months ago

I did my best to search the docs, but I wasn't able to find an answer.

I want to implement Solara into a FastAPI application.

I also want to use Auth0 in the Solara application.

I admittedly not super knowledgeable about auth.

I tried following the docs on Auth0, and I cannot seem to get it to work. I suspect it is because the get_login_url is effectively hard coding the login URL. This means that if I mount Solara to a different path, then the auth endpoint seems like it might not work.

Is this correct? Is there a better workaround for this?

Also, love the project. I have built APIs in the past, and I have always thought a good Python frontend framework was lacking. I was considering using Anvil, but I hate using anything that isn't open source and free to use. Keep up the good work.

maartenbreddels commented 11 months ago

Hi,

If I understand correctly, you try to follow https://solara.dev/docs/enterprise/oauth but with your app mounted under a different path as the root, right? And you are using our Auth0 setup, i.e. you didn't set it up yourself?

The problem lies, that in our default configuration, we have the URL's configured like this:

image

If you have a different root path, auth0 will not allow that. If you create your own auth0 account and follow all the steps in the documentation and prefix the /_solara paths without your root directory (e.g. /myapp/_solara) it should work.

Let us know if you run into issues!

Also, love the project.

Thank you :)

theeldermillenial commented 11 months ago

Hey, sorry for the delayed response.

So, it seems like there are two issues. The first is as you mentioned. I need to route things differently in Auth0.

The second is that the auth path is hardcoded. https://github.com/widgetti/solara/blob/9cac7202072ab3fb3b316292501a1b880cc828de/packages/solara-enterprise/solara_enterprise/auth/utils.py#L35

I guess I can just manually create my own utility function. I'll test this out. Is there a way that the mount path for Solara can be detected and injected into the utility functions?

maartenbreddels commented 10 months ago

Good catch, this is fixed in the linked commit and released as 1.17.4

theeldermillenial commented 10 months ago

Last comment. I'm not sure if this is a bug or not. When mounting Solara to FastAPI using auth, I get this error:

AssertionError: SessionMiddleware must be installed to access request.session

I dug around in the source code, and it looks like the Starlette integrations inject the SessionMiddleware, but because there is no explicit FastAPI, maybe this doesn't happen automatically?

I was able to get it working properly by directly adding in the SessionMiddleware.

from fastapi import FastAPI
import solara.server.fastapi
from starlette.middleware.sessions import SessionMiddleware

app = FastAPI()
app.add_middleware(SessionMiddleware, secret_key="random-string")

app.mount("/", app=solara.server.fastapi.app)

If you comment out the add_middleware line, then this example fails.

theeldermillenial commented 10 months ago

Oh, and just to confirm, I upgraded and things worked. Thanks for the fix on the first issue.

maartenbreddels commented 10 months ago

I was able to get it working properly by directly adding in the SessionMiddleware.

I think this is a bug, we should add the middleware to fastapi as well.