Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get group claims? #11

Closed
philipsd6 opened this issue Nov 3, 2021 · 5 comments
Closed

How to get group claims? #11

philipsd6 opened this issue Nov 3, 2021 · 5 comments

Comments

@philipsd6
Copy link

I'm trying to get group information into my FastAPI app from MSAL, but I can't see that it's supported.

Describe the solution you'd like
I want to add group claims in my token configuration and have them show up in the UserInfo model for use in authorizations in my endpoints.

Describe alternatives you've considered
I've configured my token configuration in my AZ AD App to "emit groups as role claims" for ID and Access types.

Additional context
None.

@dudil
Copy link
Owner

dudil commented Nov 7, 2021

Hi @philipsd6,

Sorry for the delay, busy days.
Can you please elaborate if you are trying to use AAD B2B or AAD B2C?
From my experience they are different in the details they provide.

BTW - I have another project in work which could provide the groups via MS Graph.
I'm pretty much delayed with it but if this works for you I'll try to push that into a stable version this week.

Cheers!

@philipsd6
Copy link
Author

I actually don't know for sure which I'm using! I just followed the quick start you linked to, and also based on running these CLI commands:

TENANT=$(az login --query "[?isDefault].tenantId|[0]" -o tsv)
CLIENT_ID=$(az ad app create --display-name "TestingApp" \
                             --reply-urls http://localhost:8000/docs/oauth2-redirect \
                             --query appId -o tsv)
CLIENT_CREDENTIAL = $(az ad app credential reset --id $CLIENT_ID \
                                                 --query password -o tsv

Which I then customized in the portal by adding group claims in varying configurations to see what/if any worked. But looking at your UserInfo model — there's no group fields in it, so wouldn't they be stripped out anyway?

I look forward to seeing your other project! Thanks for putting all this MSAL stuff together in a reusable FastAPI fashion!

@dudil
Copy link
Owner

dudil commented Jul 13, 2022

Hi @philipsd6 - I wasn't around this project for a while.
If this is still relevant, I published a more complete py365 package for handling the Microsoft Graph API, including groups I believe.
You should try it out!
https://github.com/dudil/py365

I can't recall if this is the latest version I have on my computer that includes the fastapi_msal integration. If not, please let me know and I'll look into update it.

@steffenschumacher
Copy link

So in my case I'm using plain AAD, and the app have been setup so that the token includes the roles of the user directly in the ID token. This means that token_claims : dict in clients/async_conf_clients.py contains a roles: list[str] after decoding:

    async def validate_id_token(self, id_token: str, nonce: OptStr = None) -> IDTokenClaims:
        token_claims: OptStrsDict = await self.__execute_async__(
            self._cca.client.decode_id_token, id_token=id_token, nonce=nonce
        )
        return IDTokenClaims.model_validate(token_claims)

So in practice, since IDTokenClaims is inheriting from UserInfo, it would only be a question of adding:

class UserInfo(BaseModel):
 ...
    roles: Optional[list[str]] = Field(None)  # add this somewhere

Since then it would be automatically set right?

This would make it super easy to implement RBAC, and there's no need to query graph or anything like that..
Pretty please?

steffenschumacher added a commit to steffenschumacher/fastapi_msal that referenced this issue Oct 22, 2023
@dudil
Copy link
Owner

dudil commented Dec 10, 2023

Issue is now closed with release latest of version 2.1.2 🙏

@dudil dudil closed this as completed Dec 10, 2023
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

No branches or pull requests

3 participants