1

有一个带有自定义的 Flask-Appbuilder 应用程序,SecurityManager可以查找从浏览器获取的用户令牌。我们在应用程序启动时获取客户端凭据。直到凭证轮换为止,它都可以正常工作。
是否有一个扩展点,我可以在其中实现请求customer_idcustomer_secret来自外部资源?

SecurityManager执行:

class MySecurityManager(SecurityManager):

    TOKENINFO_URL = "..."
    USERINFO_URL = ".../{}"

    def __init__(self, appbuilder):
        super(MySecurityManager, self).__init__(appbuilder)

    def get_oauth_user_info(self, provider, resp=None):
        """
            We authenticate users against Our OAuth provider
        """
        if provider == 'MyProvider':
            tokeninfo = self.appbuilder.sm.oauth_remotes[provider].get(self.TOKENINFO_URL)
            uid = tokeninfo.data.get('uid')
            user = self.appbuilder.sm.oauth_remotes[provider].get(self.USERINFO_URL.format(uid))
            log.debug("Token info: {0}".format(tokeninfo.data))
            log.debug("User info: {0}".format(user.data))
            return {'username': tokeninfo.data.get('uid', ''),
                    'email': user.data.get('email', ''),
                    'first_name': user.data.get('name', '').split(" ")[0],
                    'last_name': user.data.get('name', '').split(" ")[-1]}

        else:
            return super(MySecurityManager, self).get_oauth_user_info(provider, resp=None)

config.py

OAUTH_PROVIDERS = [
  {
    'name': 'MyProvider',
    'icon': ...,
    'token_key': ...,
    'remote_app': {
      'base_url': ...,
      'consumer_key': SUPERSET_OAUTH_CONSUMER_KEY,
      'consumer_secret': SUPERSET_OAUTH_CONSUMER_SECRET,
      'request_token_params': {
        'scope': ...,
      },
      'request_token_url': ...,
      'access_token_url': ...,
      'authorize_url': ...,
    }
  }
]
4

1 回答 1

1

我解决了它,覆盖了 get oauth_providers 表单https://github.com/dpgaspar/Flask-AppBuilder/blob/master/flask_appbuilder/security/manager.py#L306。这里有一个例子:

 @property
    def oauth_providers(self):
        providers = self.appbuilder.get_app.config['OAUTH_PROVIDERS']
        for provider in providers:
            if provider['name'] == 'XXXX':
                # rotate logic here
                provider['remote_app']['consumer_key'] = xxxxx
                provider['remote_app']['consumer_secret'] = xxxx
        return providers
于 2018-08-17T08:48:08.963 回答