Coder Social home page Coder Social logo

Comments (1)

gugupy avatar gugupy commented on May 29, 2024

You can achive by extending the AuthOAuthView.login method and add below line start of the login view.

if provider is None:
    providers = [k for k in self.appbuilder.sm.oauth_remotes.keys()]
    if len(providers) == 1:
        provider = providers[0]

or simply provider = "keycloak"

Since you using superset you have to tell the security manager that you have different authoauthview.

class CustomSecurityManager(SupersetSecurityManager):
    authoauthview = SupersetAuthOAuthView

You can add the above script in your helm values file without modifying the code base.

extraSecrets:
  custom_security_manager.py: |-
    import logging
    import os
    import json

    from flask_appbuilder import const as c
    
    from superset.security import SupersetSecurityManager
    from werkzeug.security import generate_password_hash

    import jwt
    from typing import Optional
    from flask import flash, g, redirect, request, session, url_for
    from flask_appbuilder._compat import as_unicode
    from flask_appbuilder.security.utils import generate_random_string
    from flask_appbuilder.views import expose
    from flask_appbuilder.security.views import AuthOAuthView
    from werkzeug.wrappers import Response as WerkzeugResponse

    log = logging.getLogger(__name__)

    class SupersetAuthOAuthView(AuthOAuthView):
        @expose("/login/")
        @expose("/login/<provider>")
        def login(self, provider: Optional[str] = None) -> WerkzeugResponse:
           if provider is None:
               providers = [k for k in self.appbuilder.sm.oauth_remotes.keys()]
               if len(providers) == 1:
                   provider = providers[0]
            log.debug("Provider: %s", provider)
            if g.user is not None and g.user.is_authenticated:
                log.debug("Already authenticated %s", g.user)
                return redirect(self.appbuilder.get_url_for_index)

            if provider is None:
                return self.render_template(
                    self.login_template,
                    providers=self.appbuilder.sm.oauth_providers,
                    title=self.title,
                    appbuilder=self.appbuilder,
                )

            log.debug("Going to call authorize for: %s", provider)
            random_state = generate_random_string()
            state = jwt.encode(
                request.args.to_dict(flat=False), random_state, algorithm="HS256"
            )
            session["oauth_state"] = random_state
            try:
                if provider == "twitter":
                    return self.appbuilder.sm.oauth_remotes[provider].authorize_redirect(
                        redirect_uri=url_for(
                            ".oauth_authorized",
                            provider=provider,
                            _external=True,
                            state=state,
                        )
                    )
                else:
                    return self.appbuilder.sm.oauth_remotes[provider].authorize_redirect(
                        redirect_uri=url_for(
                            ".oauth_authorized", provider=provider, _external=True
                        ),
                        state=state.decode("ascii") if isinstance(state, bytes) else state,
                    )
            except Exception as e:
                log.error("Error on OAuth authorize: %s", e)
                flash(as_unicode(self.invalid_login_message), "warning")
                return redirect(self.appbuilder.get_url_for_index)

    class CustomSecurityManager(SupersetSecurityManager):
        authoauthview = SupersetAuthOAuthView
configOverrides:
  enable_oauth: |
    from custom_security_manager import CustomSecurityManager
    CUSTOM_SECURITY_MANAGER = CustomSecurityManager

I hope this helps you.

from flask-appbuilder.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.