示例#1
0
def logout(next_url, force_era_global_logout=False):
    """
    Return a redirect which another logout from IDP or the provided redirect.
    Depending on the IDP, this logout will propogate. For example, if using
    another fence as an IDP, this will hit that fence's logout endpoint.
    Args:
        next_url (str): Final redirect desired after logout
    """
    logger.debug("IN AUTH LOGOUT, next_url = {0}".format(next_url))

    # propogate logout to IDP
    provider_logout = None
    provider = flask.session.get("provider")
    if force_era_global_logout or provider == IdentityProvider.itrust:
        safe_url = urllib.parse.quote_plus(next_url)
        provider_logout = config["ITRUST_GLOBAL_LOGOUT"] + safe_url
    elif provider == IdentityProvider.fence:
        base = config["OPENID_CONNECT"]["fence"]["api_base_url"]
        provider_logout = base + "/logout?" + urllib.parse.urlencode(
            {"next": next_url})

    flask.session.clear()
    redirect_response = flask.make_response(
        flask.redirect(provider_logout or urllib.parse.unquote(next_url)))
    clear_cookies(redirect_response)
    return redirect_response
示例#2
0
    def _unlink_google_account():
        user_id = current_token["sub"]

        g_account = (current_session.query(UserGoogleAccount).filter(
            UserGoogleAccount.user_id == user_id).first())

        if not g_account:
            error_message = {
                "error":
                "g_acnt_link_error",
                "error_description":
                ("Couldn't unlink account for user, no linked Google "
                 "account found."),
            }
            _clear_google_link_info_from_session()
            return error_message, 404

        g_account_access = (
            current_session.query(UserGoogleAccountToProxyGroup).filter(
                UserGoogleAccountToProxyGroup.user_google_account_id ==
                g_account.id).first())

        if g_account_access:
            try:
                with GoogleCloudManager() as g_manager:
                    g_manager.remove_member_from_group(
                        member_email=g_account.email,
                        group_id=g_account_access.proxy_group_id,
                    )
            except Exception as exc:
                error_message = {
                    "error":
                    "g_acnt_access_error",
                    "error_description":
                    ("Couldn't remove account from user's proxy group, "
                     "Google API failure. Exception: {}".format(exc)),
                }
                _clear_google_link_info_from_session()
                return error_message, 400

            current_session.delete(g_account_access)
            current_session.commit()

        current_session.delete(g_account)
        current_session.commit()

        # clear session and cookies so access token and session don't have
        # outdated linkage info
        flask.session.clear()
        response = flask.make_response("", 200)
        clear_cookies(response)

        return response
示例#3
0
def _get_auth_response_for_prompts(prompts, grant, user, client, scope):
    """
    Get response based on prompt parameter. TODO: not completely conforming yet

    FIXME: To conform to spec, some of the prompt params should be handled
    before AuthN or if it fails (so adequate and useful errors are provided).

    Right now the behavior is that the endpoint will just continue to
    redirect the user to log in without checking these params....

    Args:
        prompts (TYPE): Description
        grant (TYPE): Description
        user (TYPE): Description
        client (TYPE): Description
        scope (TYPE): Description

    Returns:
        TYPE: Description
    """
    show_consent_screen = True

    if prompts:
        prompts = prompts.split(" ")
        if "none" in prompts:
            # don't auth or consent, error if user not logged in
            show_consent_screen = False

            # if none is here, there shouldn't be others
            if len(prompts) != 1:
                error = InvalidRequestError(state=grant.params.get("state"),
                                            uri=grant.params.get("uri"))
                return _get_authorize_error_response(
                    error, grant.params.get("redirect_uri"))

            try:
                get_current_user()
                response = server.create_authorization_response(user)
            except Unauthorized:
                error = AccessDeniedError(state=grant.params.get("state"),
                                          uri=grant.params.get("uri"))
                return _get_authorize_error_response(
                    error, grant.params.get("redirect_uri"))

        if "login" in prompts:
            show_consent_screen = True
            try:
                # Re-AuthN user (kind of).
                # TODO (RR 2018-03-16): this could also include removing active
                # refresh tokens.
                flask.session.clear()

                # For a POST, return the redirect in JSON instead of headers.
                if flask.request.method == "POST":
                    redirect_response = flask.make_response(
                        flask.jsonify(
                            {"redirect": response.headers["Location"]}))
                else:
                    redirect_response = flask.make_response(
                        flask.redirect(flask.url_for(".authorize")))

                clear_cookies(redirect_response)
                return redirect_response
            except Unauthorized:
                error = AccessDeniedError(state=grant.params.get("state"),
                                          uri=grant.params.get("uri"))
                return _get_authorize_error_response(
                    error, grant.params.get("redirect_uri"))

        if "consent" in prompts:
            # show consent screen (which is default behavior so pass)
            pass

        if "select_account" in prompts:
            # allow user to select one of their accounts, we
            # don't support this at the moment
            pass

    if show_consent_screen:
        shown_scopes = [] if not scope else scope.split(" ")
        if "openid" in shown_scopes:
            shown_scopes.remove("openid")

        enabled_idps = config.get("OPENID_CONNECT", {})
        idp_names = []
        for idp, info in enabled_idps.items():
            # prefer name if its there, then just use the key for the provider
            idp_name = info.get("name") or idp.title()
            idp_names.append(idp_name)

        resource_description = [
            SCOPE_DESCRIPTION[s].format(idp_names=" and ".join(idp_names))
            for s in shown_scopes
        ]

        privacy_policy = config.get("BASE_URL").rstrip("/") + "/privacy-policy"

        response = flask.render_template(
            "oauthorize.html",
            grant=grant,
            user=user,
            client=client,
            app_name=config.get("APP_NAME"),
            resource_description=resource_description,
            privacy_policy=privacy_policy,
        )

    return response