dorinclisu / fastapi-auth0

FastAPI authentication and authorization using auth0.com
MIT License
230 stars 37 forks source link

[Q] - Is it possible to get the "Organization" that the user belong to? #15

Closed RonenYAtZenity closed 3 years ago

dorinclisu commented 3 years ago

It is possible to get any data as long as it is included by auth0 in the access token as a custom claim. Now whether that is possible and how, are questions best suited for the auth0 forum I think.

RonenYAtZenity commented 3 years ago

As far as I know it is. My question is if it can be added to this project to the user object?

dorinclisu commented 3 years ago

Here: https://github.com/dorinclisu/fastapi-auth0/blob/ae5256d06e8cd9bdc17c1138178be9612b24e7dc/tests/test_auth.py Look at how auth_custom is used together with CustomAuth0User model. The idea is to define whatever custom field you want from the access token in a custom user model that subclasses Auth0User.

RonenYAtZenity commented 3 years ago

Got above. The Organization is a new feature in Auth0. It enables to create a meta-container for users and when the user login it should state also the organization it belongs to. This feature is very useful for B2B scenarios. My thought was that it should be added to the Auth0User, it requires a single line to be added: org_id: str = Field(None, alias="org_id") . WDYT? (for the meantime I will do it as you suggested with `CustomAuth0User'

dorinclisu commented 3 years ago

We will probably not add it the the default Auth0User because it's something non-standard, quite specific and few users of this library would actually need it. Now, email is also non-standard in the access token but I made an exception and added it because most users need it.

Btw as a small correction to your line, org_id type should be Optional[str] to prevent internal server error when the access token does not contain org_id for whatever external reason such as incorrectly configured auth0 rule / pipeline.

RonenYAtZenity commented 3 years ago

OK, Thank you for help.