simonsobs / tilemaker

The tilemaker backend for the SO Map viewer.
0 stars 1 forks source link

Implement Github authentication for backend endpoints #2

Open zonca opened 3 months ago

zonca commented 3 months ago

Requirements:

@JBorrow please let me know if this is right

JBorrow commented 3 months ago

Yes, sounds perfect. I think checking against simonsobs is good, but we may want to be able to have an additional database table with 'super' users who can do things like ingest maps later down the line.

zonca commented 3 months ago

I tested the Github example of fastapi-sso and works fine: https://github.com/tomasvotava/fastapi-sso/blob/master/examples/github.py

the problem is that this doesn't store authentication, we will need to augment it following: https://tomasvotava.github.io/fastapi-sso/how-to-guides/use-with-fastapi-security/

zonca commented 3 months ago

following the tutorial, it works fine for protecting an endpoint, tested login/logout, access without authenticating first, everything working as expected:

https://gist.github.com/zonca/cd6b618593aaa8352f88ab5881d7598e

next I'll work on checking group membership and then integrating with tilemaker

zonca commented 3 months ago

I plan to leave the maps endpoint not authenticated, and protect all the other endpoints.

JBorrow commented 3 months ago

I think we want the ability to have conditional authentication at each endpoint; some maps will be public, and some will be behind the login.

zonca commented 3 months ago

ok, I implemented the organization membership check in the toy example: https://gist.github.com/zonca/21c78b1bbbb920035b84e480e976f9b3

In this situation an endpoint can be either authenticated or not authenticated. If it is authenticated, then we can check membership and have permissions at the object level, so any Github account will work for public maps and private maps will need simonsobs membership. This is a bit annoying for people accessing public data but it makes the implementation easier. Is it ok?

A note about the implementation: I cannot reuse the OAuth token because it doesn't have permissions to access the org API. I got around that using a PAT (classic) from Github with read:orgs permission. To simplify API calls I'm using PyGithub.

JBorrow commented 3 months ago

This looks great. Reading between the lines here you mean that there will need to be two endpoints, one for public maps and one for private maps? I guess that works, though we will need to think about structuring the code to avoid too much duplication. I guess this is a better scenario than having to be logged in to GitHub to even view the public maps, which I imagine is not going to be acceptable.

zonca commented 2 months ago

ok, working on this, I'm trying to have a single endpoint that redirects to authentication only for some query parameters (which will be the datasets).

zonca commented 2 months ago

@JBorrow ok, I got this working in a simple toy example:

https://gist.github.com/zonca/12a6b41fb53574a6a469f6a93d7db013

In this simple example /protected/0 does not require authentication, while /protected/1 gives permissions denied.

Once the users logs in successfully via Github using /auth/login and is member of the right Github organization, they can now access also the /protected/1 endpoint.

@JBorrow what should I do next?