Skip to content

Conversation

@peterdudfield
Copy link
Contributor

@peterdudfield peterdudfield commented Dec 15, 2025

Pull Request

Description

  • start using fastapi-auth0
  • adds "Authorization" button on swagger code

How Has This Been Tested?

  • CI tests
  • ran things locally

Checklist:

  • My code follows OCF's coding style guidelines
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • I have checked my code and corrected any misspellings

Copy link
Contributor

@devsjc devsjc left a comment

Choose a reason for hiding this comment

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

A few suggestions. I'm not really sure what advantage this brings over the previous method, sure its saves us a few lines of code for another dependency, but do we gain any functionality with that? In fact, it seems we lose access to the sub key which is actually used in other parts of the code - so those parts would need updating also.

Comment on lines 34 to 38
# Lets setup the auths
# 'auth' can be imported into the route if we want to limit a route by scopes
auth = DummyAuth()
if (os.getenv("AUTH0_DOMAIN") is not None) and (os.getenv("AUTH0_AUDIENCE") is not None):
auth = Auth0(api_audience=os.getenv("AUTH0_AUDIENCE"), domain=os.getenv("AUTH0_DOMAIN"))
Copy link
Contributor

Choose a reason for hiding this comment

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

I've tried to keep environment variables limited only to the top level. How come you've changed the access pattern? Seems like you could just have the previous Auth0 class be replaced entirely with this imported one, and remove the remove the call entirely from the DummyAuth and make it instead inherit from the fastapi_auth0.Auth0 class (as you sort of have already with the get_user method override). Then the code in main would be something like

# Override dependencies according to configuration
    auth_instance: fastapi_auth0.Auth0
    match (conf.get_string("auth0.domain"), conf.get_string("auth0.audience")):
        case (_, "") | ("", _):
            auth_instance = auth.DummyAuth()
            log.warning("disabled authentication. NOT recommended for production")
        case (domain, audience):
            auth_instance = fastapi_auth0.Auth0(domain=domain, api_audience=audience)
    
    server.dependency_overrides[auth.get_user] = auth_instance.get_user

You could even make the abstract auth class with a get_user method in the auth middleware that defines the auth_instance type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ill try again, (I think i tried this but somehting wasnt working, but Ill try again as I do like what youve done with env vars there)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know remember, it was tricky to get the "Autherization" green box up on the swagger UI. This is done via fastapis Security. But ill try and get this working

@server.get("/check_authentication", tags=["API Information"])
def check_authentication(user: Annotated[Auth0User, Security(get_user)]) -> dict:
"""Check if the user is authenticated."""
return {"authenticated": True, "user": user}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ive added this route,

@braddf
Copy link
Contributor

braddf commented Dec 18, 2025

hey @peterdudfield, haven't looked into the code/revisions on this but @devsjc left me a tag when he wasn't feeling well I think.
First of all, is there are reason we're integrating this relatively small/less recently updated package instead of the official auth0-fastapi-api package, which seems to have a similar API / implementation?
If there's not a strong reason to use this one, e.g. a simple swap in and out for identical codebases which this is not quite the same implementation I think, then it's usually better to use 1st-party libraries esp. for things like auth.

@peterdudfield
Copy link
Contributor Author

hey @peterdudfield, haven't looked into the code/revisions on this but @devsjc left me a tag when he wasn't feeling well I think. First of all, is there are reason we're integrating this relatively small/less recently updated package instead of the official auth0-fastapi-api package, which seems to have a similar API / implementation? If there's not a strong reason to use this one, e.g. a simple swap in and out for identical codebases which this is not quite the same implementation I think, then it's usually better to use 1st-party libraries esp. for things like auth.

Yea, the library is only one file, so I could just copy that over.
Ive used it all before in the other fastapi's and Auth is hard, so I thought just trying to keep it the same as before (for #147 )
it has nice features like, classes, permission/scopes, Nicer error messages, get_user - which seems to be more commonly used

@peterdudfield peterdudfield marked this pull request as draft December 19, 2025 09:29
@peterdudfield
Copy link
Contributor Author

Ive moved this back to draft, as if we do this, we need to update the routes too

@peterdudfield peterdudfield mentioned this pull request Dec 19, 2025
5 tasks
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.

4 participants