Python Keycloak获取角色和用户组 [英] Python Keycloak Get Roles and Groups of user

查看:545
本文介绍了Python Keycloak获取角色和用户组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用关键披风创建了组并为这些组分配了角色. 比创建用户并将用户分配到特定组.

Using Key Cloak created groups and assigned roles to the groups. Than created the users and assigned the users to specific groups.

要在我的应用程序中访问所有这些内容,我正在使用 Python -Keycloak

To access all this in my application I am using Python-Keycloak

github doc 所述,使用以下代码访问用户信息.

As mentioned in github doc, using following code to access the user information.

from keycloak import KeycloakOpenID

keycloak_openid = KeycloakOpenID(server_url="http://localhost:8080/auth/",
                    client_id="account",
                    realm_name="demo",
                    client_secret_key="my_secret_key")

config_well_know = keycloak_openid.well_know()

token = keycloak_openid.token("username", "password")
userinfo = keycloak_openid.userinfo(token['access_token'])

获取关注的用户信息

{
    'family_name': 'Lastname', 
    'preferred_username': 'user_name', 
    'sub': 'some_key', 
    'given_name': 'Fistname', 
    'name': 'Firstname Lastname', 
    'email': 'email@example.com'
}

如何访问用户的组和角色信息.

How can I access the group and roles information of the user.

推荐答案

您需要在同一库(python-keycloak)中使用"KeycloakAdmin"类:

You need to use "KeycloakAdmin" class in the same library (python-keycloak):

from keycloak import KeycloakAdmin

admin = KeycloakAdmin(server_url='https://server-url',
                      username='username',
                      password='password',
                      realm_name='realm',
                      verify=True)

user_groups = admin.get_user_groups(user_id="user-id")

要使用KeycloakAdmin,您需要一个有权访问"admin-cli"的用户.

For use KeycloakAdmin, you will need of a user with access to "admin-cli".

这篇关于Python Keycloak获取角色和用户组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆