capless / warrant

Python library for using AWS Cognito. With support for SRP.
Apache License 2.0
468 stars 192 forks source link

Get user's group data #91

Open martinn opened 6 years ago

martinn commented 6 years ago

Hi there, first of all, thanks for a great library and apologies if this exists already, haven't been able to find it.

Is it possible to get the groups assigned to a user? And similarly, be able to assign a group to a user when creating it?

I can see methods for retrieving group data but not linked to users.

Thanks!

martinn commented 6 years ago

For future reference, the cognito groups are stored in the id_token under 'cognito:groups' when authenticating.

Unsure how to get them for each user.

aaronhayes commented 6 years ago

You do have access to the underlying boto3 client via the client attribute.

Then it's just a matter of reading the boto3 documentation, you can do something like

groups = u.client.admin_get_list_groups_for_user(Username=username)
ryanlwh commented 3 years ago

For future reference, the cognito groups are stored in the id_token under 'cognito:groups' when authenticating.

Unsure how to get them for each user.

You can decode the id_token with verify_token. Then it's just a matter of getting cognito:groups:

decodedIdToken = u.verify_token(u.id_token, 'id_token', 'id')
groups = decodedIdToken['cognito:groups']