Ejemplo n.º 1
0
def _phase1_background(meta, dialog, verifier, builder):
    try:
        logger.info("starting token obtaining in background")
        r = get_instance_info(instance_uri=meta.instance_base_uri,
                              verifier=verifier)
        meta.api_base_uri, meta.authorization_endpoint, meta.token_endpoint = r
    except Exception as e:
        GLib.idle_add(lambda: error_helper(dialog, "Can't fetch instance info",
                                           "{}".format(str(e))))
        GLib.idle_add(lambda: dialog.hide())
        raise

    meta.refresh_token()
    if not meta.token:
        code_verifier = gen_code_verifier()
        port = get_open_port()
        try:
            oauth = create_oauth_session(port,
                                         auto_refresh_url=meta.token_endpoint)
            auth_url = get_auth_url(oauth, code_verifier,
                                    meta.authorization_endpoint)
        except Exception as e:
            GLib.idle_add(lambda: error_helper(
                dialog, "Can't create oauth session", "{}".format(str(e))))
            GLib.idle_add(lambda: dialog.hide())
            raise
        else:
            GLib.idle_add(lambda: _phase1_callback(
                meta, port, code_verifier, oauth, auth_url, dialog, builder))
    else:
        logger.info("we already have a token, skipping browser step")
        oauth = oauth_from_token(meta=meta)
        GLib.idle_add(lambda: _phase2_callback(
            meta=meta, oauth=oauth, dialog=dialog, builder=builder))
Ejemplo n.º 2
0
def _phase1_background(meta, dialog, verifier, builder, force_token_refresh,
                       lets_connect):
    # type: (Metadata, Any, str, Gtk.builder, Optional[bool], bool) -> None
    try:
        logger.info(u"starting token obtaining in background")
        r = get_instance_info(instance_uri=meta.instance_base_uri,
                              verifier=verifier)
        meta.api_base_uri, meta.authorization_endpoint, meta.token_endpoint = r  # type: ignore
    except Exception as e:
        error = e
        GLib.idle_add(lambda: error_helper(dialog, "Can't fetch instance info",
                                           "{}".format(str(error))))
        GLib.idle_add(lambda: dialog.hide())
        raise

    meta.refresh_token()

    if not meta.token and not force_token_refresh:
        # lets see if other profiles already have a token we can use
        token = reuse_token_from_base_uri(meta.instance_base_uri)
        if token:
            meta.token = token

    if not meta.token:
        code_verifier = gen_code_verifier()
        port = get_open_port()
        try:
            oauth = create_oauth_session(port,
                                         lets_connect=lets_connect,
                                         auto_refresh_url=meta.token_endpoint)
            auth_url, state = get_auth_url(
                oauth, code_verifier,
                meta.authorization_endpoint)  # type: ignore
        except Exception as e:
            error = e
            GLib.idle_add(lambda: error_helper(
                dialog, "Can't create oauth session", "{}".format(str(error))))
            GLib.idle_add(lambda: dialog.hide())
            raise
        else:
            GLib.idle_add(lambda: _phase1_callback(meta,
                                                   port,
                                                   code_verifier,
                                                   oauth,
                                                   auth_url,
                                                   dialog,
                                                   builder,
                                                   state,
                                                   lets_connect=lets_connect))
    else:
        logger.info(u"we already have a token, skipping browser step")
        oauth = oauth_from_token(meta=meta, lets_connect=lets_connect)
        GLib.idle_add(lambda: _phase2_callback(meta=meta,
                                               oauth=oauth,
                                               dialog=dialog,
                                               builder=builder,
                                               lets_connect=lets_connect))
Ejemplo n.º 3
0
def get_oauth_token(user, password, instance_uri):
    meta = Metadata()
    meta.api_base_uri, meta.authorization_endpoint, meta.token_endpoint = get_instance_info(
        instance_uri=instance_uri)
    meta.refresh_token()
    code_verifier = gen_code_verifier()
    port = get_open_port()
    oauth = create_oauth_session(port, auto_refresh_url=meta.token_endpoint)
    auth_url, state = get_auth_url(oauth, code_verifier,
                                   meta.authorization_endpoint)

    with ThreadPoolExecutor(max_workers=1) as executor:
        future = executor.submit(get_oauth_token_code, port, timeout=5)
        authorize(auth_url, user, password)
        code, other_state = future.result()

    assert (state == other_state)
    meta.token = oauth.fetch_token(meta.token_endpoint,
                                   code=code,
                                   code_verifier=code_verifier)
    return oauth, meta
Ejemplo n.º 4
0
 def test_get_open_port(self):
     get_open_port()