# Buggy code
def oauth_authorized(self, provider):
    log.debug("Authorized init")
    resp = self.appbuilder.sm.oauth_remotes[provider].authorized_response()
    if resp is None:
        flash("You denied the request to sign in.", "warning")
        return redirect("login")
    log.debug("OAUTH Authorized resp: {0}".format(resp))
    # Retrieves specific user info from the provider
    try:
        self.appbuilder.sm.set_oauth_session(provider, resp)
        userinfo = self.appbuilder.sm.oauth_user_info(provider, resp)
    except Exception as e:
        log.error("Error returning OAuth user info: {0}".format(e))
        user = None
    else:
        log.debug("User info retrieved from {0}: {1}".format(provider, userinfo))
        # User email is not whitelisted
        if provider in self.appbuilder.sm.oauth_whitelists:
            whitelist = self.appbuilder.sm.oauth_whitelists[provider]
            allow = False
            for e in whitelist:
                if re.search(e, userinfo["email"]):
                    allow = True
                    break
            if not allow:
                flash("You are not authorized.", "warning")
                return redirect("login")
        else:
            log.debug("No whitelist for OAuth provider")
        user = self.appbuilder.sm.auth_user_oauth(userinfo)

    if user is None:
        flash(as_unicode(self.invalid_login_message), "warning")
        return redirect("login")
    else:
        login_user(user)
        try:
            state = jwt.decode(
                request.args["state"],
                self.appbuilder.app.config["SECRET_KEY"],
                algorithms=["HS256"],
            )
        except jwt.InvalidTokenError:
            raise Exception("State signature is not valid!")

        try:
            next_url = state["next"][0] or self.appbuilder.get_url_for_index
        except (KeyError, IndexError):
            next_url = self.appbuilder.get_url_for_index

        return redirect(next_url)


# Traceback (KeyError)
File "/home/aman/superset/lib/python3.6/site-packages/flask_appbuilder/security/views.py", line 678, in oauth_authorized
resp = self.appbuilder.sm.oauth_remotes[provider].authorized_response()
KeyError: 'login'

# Fixed code
def oauth_authorized(self, provider):
    log.debug("Authorized init")
    resp = self.appbuilder.sm.oauth_remotes[provider].authorized_response()
    if resp is None:
        flash("You denied the request to sign in.", "warning")
        return redirect(self.appbuilder.get_url_for_login)
    log.debug("OAUTH Authorized resp: {0}".format(resp))
    # Retrieves specific user info from the provider
    try:
        self.appbuilder.sm.set_oauth_session(provider, resp)
        userinfo = self.appbuilder.sm.oauth_user_info(provider, resp)
    except Exception as e:
        log.error("Error returning OAuth user info: {0}".format(e))
        user = None
    else:
        log.debug("User info retrieved from {0}: {1}".format(provider, userinfo))
        # User email is not whitelisted
        if provider in self.appbuilder.sm.oauth_whitelists:
            whitelist = self.appbuilder.sm.oauth_whitelists[provider]
            allow = False
            for e in whitelist:
                if re.search(e, userinfo["email"]):
                    allow = True
                    break
            if not allow:
                flash("You are not authorized.", "warning")
                return redirect(self.appbuilder.get_url_for_login)
        else:
            log.debug("No whitelist for OAuth provider")
        user = self.appbuilder.sm.auth_user_oauth(userinfo)

    if user is None:
        flash(as_unicode(self.invalid_login_message), "warning")
        return redirect(self.appbuilder.get_url_for_login)
    else:
        login_user(user)
        try:
            state = jwt.decode(
                request.args["state"],
                self.appbuilder.app.config["SECRET_KEY"],
                algorithms=["HS256"],
            )
        except jwt.InvalidTokenError:
            raise Exception("State signature is not valid!")

        try:
            next_url = state["next"][0] or self.appbuilder.get_url_for_index
        except (KeyError, IndexError):
            next_url = self.appbuilder.get_url_for_index

        return redirect(next_url)