Using KeyCloak(OpenID Connect) with Apache SuperSet

前端 未结 1 1028
無奈伤痛
無奈伤痛 2021-01-06 13:52

I started with Using OpenID/Keycloak with Superset and did everything as explained. However, it is an old post, and not everything worked. I\'m also trying to implement a cu

相关标签:
1条回答
  • 2021-01-06 14:18

    I ended up figuring it out myself.

    The solution I ended up with does not make use of a FAB add-on, but you also don't have to edit existing code/files.

    I've renamed the manager.py file to security.py, and it now looks like this:

    from flask import redirect, request
    from flask_appbuilder.security.manager import AUTH_OID
    from superset.security import SupersetSecurityManager
    from flask_oidc import OpenIDConnect
    from flask_appbuilder.security.views import AuthOIDView
    from flask_login import login_user
    from urllib.parse import quote
    from flask_appbuilder.views import ModelView, SimpleFormView, expose
    import logging
    
    class AuthOIDCView(AuthOIDView):
    
        @expose('/login/', methods=['GET', 'POST'])
        def login(self, flag=True):
            sm = self.appbuilder.sm
            oidc = sm.oid
    
            @self.appbuilder.sm.oid.require_login
            def handle_login(): 
                user = sm.auth_user_oid(oidc.user_getfield('email'))
    
                if user is None:
                    info = oidc.user_getinfo(['preferred_username', 'given_name', 'family_name', 'email'])
                    user = sm.add_user(info.get('preferred_username'), info.get('given_name'), info.get('family_name'), info.get('email'), sm.find_role('Gamma')) 
    
                login_user(user, remember=False)
                return redirect(self.appbuilder.get_url_for_index)  
    
            return handle_login()  
    
        @expose('/logout/', methods=['GET', 'POST'])
        def logout(self):
    
            oidc = self.appbuilder.sm.oid
    
            oidc.logout()
            super(AuthOIDCView, self).logout()        
            redirect_url = request.url_root.strip('/') + self.appbuilder.get_url_for_login
    
            return redirect(oidc.client_secrets.get('issuer') + '/protocol/openid-connect/logout?redirect_uri=' + quote(redirect_url))
    
    class OIDCSecurityManager(SupersetSecurityManager):
        authoidview = AuthOIDCView
        def __init__(self,appbuilder):
            super(OIDCSecurityManager, self).__init__(appbuilder)
            if self.auth_type == AUTH_OID:
                self.oid = OpenIDConnect(self.appbuilder.get_app)
    

    I place the security.py file next to my superset_config_py file.

    The JSON configuration file stays unchanged.

    Then I've changed the superset_config.py file to include the following lines:

    from security import OIDCSecurityManager
    AUTH_TYPE = AUTH_OID
    OIDC_CLIENT_SECRETS = <path_to_configuration_file>
    OIDC_ID_TOKEN_COOKIE_SECURE = False
    OIDC_REQUIRE_VERIFIED_EMAIL = False
    AUTH_USER_REGISTRATION = True
    AUTH_USER_REGISTRATION_ROLE = 'Gamma'
    CUSTOM_SECURITY_MANAGER = OIDCSecurityManager
    

    That's it.

    Now when I navigate to my site, it automatically goes to the KeyCloak login screen, and upon successful sign in I am redirected back to my application.

    0 讨论(0)
提交回复
热议问题