无法使用 Flask-OIDC 和 Keycloak 访问 resource_access.client-test.roles
Cannot access resource_access.client-test.roles with Flask-OIDC and Keycloak
我配置了 Keycloak 来验证我客户端的用户,return 它对我的应用程序起作用。以下 JSON 是我的用户的带有 OIDC 的数据 Keycloak returns。在数据中,我们可以清楚地看到字段 resource_access.test-client.roles 存在。
{
....some data..
"allowed-origins": [
"http://localhost:5000"
],
"realm_access": {
"roles": [
"offline_access",
"uma_authorization"
]
},
"resource_access": {
"test-client": {
"roles": [
"DemoRole"
]
},
"account": {
"roles": [
"manage-account",
"manage-account-links",
"view-profile"
]
}
},
....some data..
}
在我的应用程序方面,我使用 Flask-OIDC 进行身份验证并获得我的用户角色。
应用配置是这样完成的
app.config.update({
'SECRET_KEY': 'u\x91\xcf\xfa\x0c\xb9\x95\xe3t\xba2K\x7f\xfd\xca\xa3\x9f\x90\x88\xb8\xee\xa4\xd6\xe4',
'TESTING': True,
'DEBUG': True,
'OIDC_CLIENT_SECRETS': 'client_secrets.json',
'OIDC_ID_TOKEN_COOKIE_SECURE': False,
'OIDC_REQUIRE_VERIFIED_EMAIL': False,
'OIDC_USER_INFO_ENABLED': True,
'OIDC_SCOPES': ['openid', 'email', 'profile', 'roles'],
'OIDC_VALID_ISSUERS': ['http://localhost:8080/auth/realms/MyDemo'],
'OIDC_OPENID_REALM': 'http://localhost:5000/oidc_callback'
})
client_secrets.json是
{
"web": {
"auth_uri": "http://localhost:8080/auth/realms/MyDemo/protocol/openid-connect/auth",
"client_id": "test-client",
"client_secret": "40074399-b1b6-442c-9862-68b655ef8dad",
"redirect_uris": [
"http://localhost:5000/oidc_callback"
],
"userinfo_uri": "http://localhost:8080/auth/realms/MyDemo/protocol/openid-connect/userinfo",
"token_uri": "http://localhost:8080/auth/realms/MyDemo/protocol/openid-connect/token",
"token_introspection_uri": "http://localhost:8080/auth/realms/MyDemo/protocol/openid-connect/token/introspect"
}
}
完成后,我正尝试在以下端点中从我的用户那里检索角色
@app.route('/private')
@oidc.require_login
def hello_me():
info = oidc.user_getinfo(['resource_access'])
client = info['test-client']
role = client['roles']
问题是 'info' 被 return 清空。我可以访问字段 'email'、'surname' 和其他字段,但我无法访问 'resource_access',即使它出现在 returned JSON 中。我希望上面的代码序列可以 return 我的用户角色。
我做错了什么?
声称resource_access
可以暴露在access token
and/orid token
and/oruserinfo response
。从您的问题描述中不清楚,它到底暴露在哪里。显然它在错误的位置,而不是您的应用程序代码所期望的位置。
所以修复声明的位置,你应该没问题:
- 惰性选项:在任何地方公开它(
access token
和 id token
和 userinfo response
)
- 正确的选择:阅读所用库的文档并仅将声明公开到正确的位置(
access token
或 id token
或 userinfo response
)
我配置了 Keycloak 来验证我客户端的用户,return 它对我的应用程序起作用。以下 JSON 是我的用户的带有 OIDC 的数据 Keycloak returns。在数据中,我们可以清楚地看到字段 resource_access.test-client.roles 存在。
{
....some data..
"allowed-origins": [
"http://localhost:5000"
],
"realm_access": {
"roles": [
"offline_access",
"uma_authorization"
]
},
"resource_access": {
"test-client": {
"roles": [
"DemoRole"
]
},
"account": {
"roles": [
"manage-account",
"manage-account-links",
"view-profile"
]
}
},
....some data..
}
在我的应用程序方面,我使用 Flask-OIDC 进行身份验证并获得我的用户角色。
应用配置是这样完成的
app.config.update({
'SECRET_KEY': 'u\x91\xcf\xfa\x0c\xb9\x95\xe3t\xba2K\x7f\xfd\xca\xa3\x9f\x90\x88\xb8\xee\xa4\xd6\xe4',
'TESTING': True,
'DEBUG': True,
'OIDC_CLIENT_SECRETS': 'client_secrets.json',
'OIDC_ID_TOKEN_COOKIE_SECURE': False,
'OIDC_REQUIRE_VERIFIED_EMAIL': False,
'OIDC_USER_INFO_ENABLED': True,
'OIDC_SCOPES': ['openid', 'email', 'profile', 'roles'],
'OIDC_VALID_ISSUERS': ['http://localhost:8080/auth/realms/MyDemo'],
'OIDC_OPENID_REALM': 'http://localhost:5000/oidc_callback'
})
client_secrets.json是
{
"web": {
"auth_uri": "http://localhost:8080/auth/realms/MyDemo/protocol/openid-connect/auth",
"client_id": "test-client",
"client_secret": "40074399-b1b6-442c-9862-68b655ef8dad",
"redirect_uris": [
"http://localhost:5000/oidc_callback"
],
"userinfo_uri": "http://localhost:8080/auth/realms/MyDemo/protocol/openid-connect/userinfo",
"token_uri": "http://localhost:8080/auth/realms/MyDemo/protocol/openid-connect/token",
"token_introspection_uri": "http://localhost:8080/auth/realms/MyDemo/protocol/openid-connect/token/introspect"
}
}
完成后,我正尝试在以下端点中从我的用户那里检索角色
@app.route('/private')
@oidc.require_login
def hello_me():
info = oidc.user_getinfo(['resource_access'])
client = info['test-client']
role = client['roles']
问题是 'info' 被 return 清空。我可以访问字段 'email'、'surname' 和其他字段,但我无法访问 'resource_access',即使它出现在 returned JSON 中。我希望上面的代码序列可以 return 我的用户角色。
我做错了什么?
声称resource_access
可以暴露在access token
and/orid token
and/oruserinfo response
。从您的问题描述中不清楚,它到底暴露在哪里。显然它在错误的位置,而不是您的应用程序代码所期望的位置。
所以修复声明的位置,你应该没问题:
- 惰性选项:在任何地方公开它(
access token
和id token
和userinfo response
) - 正确的选择:阅读所用库的文档并仅将声明公开到正确的位置(
access token
或id token
或userinfo response
)