如何在Flask-Appbuilder中为OAuth2.0使用自定义提供程序[keycloak]?

时间:2019-05-23 13:14:05

标签: python-3.x flask oauth-2.0 keycloak securitymanager

我正在尝试将 keycloak 实现为 OAuth2.0 提供程序。我拥有所有必需的OAUTH_PROVIDER信息,并且已经声明了AUTH_TYPE,AUTH_USER_REGISTRATION,AUTH_USER_REGISTRATION_ROLE (请参见下面的代码)

AUTH_TYPE = AUTH_OAUTH

AUTH_USER_REGISTRATION = True

AUTH_USER_REGISTRATION_ROLE = "Public"

OAUTH_PROVIDERS = [
 {
   ‘name’: ‘provider_name’,
   ‘icon’: ‘fa-icon’,
   ’token_key’: ‘access_token’, 
   ‘remote_app’: {
     ‘base_url’: ‘https://www.base_url.com‘,
     ‘request_token_params’: {
     ‘scope’: ‘email profile’
     },
     ‘request_token_url’: None,
     ‘access_token_url’: ‘https://www.access_token_url.com’,
     ‘authorize_url': ‘https://www.authorize_url.com',
     ‘consumer_key’: ‘PROVIDER_OAUTH_KEY’,
     ‘consumer_secret’: ‘PROVIDER_OAUTH_SECRET’
    }
  }
]

因为这是一个自定义提供程序(除了常见的google,facebook,...提供程序),所以在我的安全管理器中,我已覆盖了 flask中声明的 get_oauth_user_info 函数-appbuilder manager.py

    def get_oauth_user_info(self, provider, response=None):
        logging.debug("Oauth2 provider: {0}.".format(provider))
        me = self.appbuilder.sm.oauth_remotes[provider].get("user")
        logging.error(me)
        return {
            "preferred_username": me.data.get("preferred_username",""),
            "first_name": me.data.get("given_name", ""),
            "last_name": me.data.get("family_name", ""),
            "email": me.data.get("email", "")

我希望结果将应用程序重定向到keycloak登录页面,但是在发布请求中引发以下错误:

DEBUG:flask_oauthlib:Request 'https://...' with 'POST' method
ERROR:flask_appbuilder.security.manager:User info does not have username or email {}
[2019-05-23 12:52:10,130] ERROR in app: Exception on /oauth-authorized/login [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/usr/local/lib/python3.6/site-packages/flask_appbuilder/security/views.py", line 519, in oauth_authorized
    resp = self.appbuilder.sm.oauth_remotes[provider].authorized_response()

如果有人对我做错了什么事情的指示,请帮助我。

1 个答案:

答案 0 :(得分:1)

您应该像在文档https://flask-appbuilder.readthedocs.io/en/latest/security.html#authentication-oauth中一样,将注释@appbuilder.sm.oauth_user_info_getter添加到get_oauth_user_info函数中

也要更改

me = self.appbuilder.sm.oauth_remotes[provider].get("user") 

me = self.appbuilder.sm.oauth_remotes[provider].get("userinfo") 

如果base_urlhttps://{keycloak-domain}/auth/realms/{realm}/protocol/openid-connect/