Ejemplo n.º 1
0
def begin_authentication(event, _context=None):
    """
    Begin authenticating into TripIt by authorizing your account (and AWS access key)
    with Tripit.
    """
    access_key = get_access_key(event)
    if not access_key:
        return return_error(message="Failed to get access key from event.")
    endpoint = trim_auth_from_endpoint(get_endpoint(event), "auth")
    if not endpoint:
        return return_error(message="Failed to get endpoint from event.")
    host = get_host(event)
    if not host:
        return return_error(message="Failed to get endpoint from event.")
    reauthorize = get_query_parameter(event, "reauthorize") or False
    auth_url = get_authn_url(
        access_key=access_key, host=host, reauthorize=reauthorize, api_gateway_endpoint=endpoint
    )
    if not auth_url:
        return return_error(message="No authorization URL received.")
    if auth_url.split("/")[-1] == "token":
        return return_ok(message="Already authorized.")
    message = "".join(
        [
            "You will need to authenticate into TripIt first; ",
            "click on or copy/paste this URL to get started: ",
            auth_url,
        ]
    )
    return return_ok(message=message)
Ejemplo n.º 2
0
def test_ok_with_message():
    """
    Tests returning basic successful API calls with messages attached.
    """
    assert return_ok(message="Howdy") == {
        "statusCode": 200,
        "body": json.dumps({"status": "ok", "message": "Howdy"}),
    }
Ejemplo n.º 3
0
def callback(event, _context=None):
    """
    Handle the callback from TripIt.
    """
    endpoint = get_endpoint(event)
    if not endpoint:
        return return_error(message="Failed to get endpoint from event.")
    host = get_host(event)
    if not host:
        return return_error(message="Failed to get endpoint from event.")
    request_token = get_query_parameter(event, "oauth_token")
    if not request_token:
        return return_error(message="No request token in response.")
    if handle_callback(request_token):
        return return_ok()
    return return_error(message="Authentication failed.")
Ejemplo n.º 4
0
def current_trip(event, _context=None):
    """
    Gets all trips associated with a TripIt account.
    """
    access_key = get_access_key(event)
    if not access_key:
        return return_error(message="Failed to get access key from event.")
    token_data = get_token_data_for_access_key(access_key)
    if not token_data:
        return return_error(code=403,
                            message="Access denied; go to /auth first.")
    return return_ok(
        additional_json={
            "trip":
            get_current_trip(
                token=token_data["token"],
                token_secret=token_data["token_secret"],
            )
        })
Ejemplo n.º 5
0
def get_trips(event, _context=None):
    """
    Gets all trips associated with a TripIt account.
    """
    access_key = get_access_key(event)
    show_human_times = get_query_parameter(event, "human_times") or False
    if not access_key:
        return return_error(message="Failed to get access key from event.")
    token_data = get_token_data_for_access_key(access_key)
    if not token_data:
        return return_error(code=403,
                            message="Access denied; go to /auth first.")
    return return_ok(
        additional_json={
            "trips":
            get_all_trips(
                token=token_data["token"],
                token_secret=token_data["token_secret"],
                human_times=show_human_times,
            )
        })
Ejemplo n.º 6
0
def test_begin_authentication_endpoint(monkeypatch, query_request_token_table,
                                       drop_request_token_table):
    """
    Ensure that we can get an authorization URL from this endpoint.
    We'll only test the happy path where our access key doesn't have any tokens yet,
    since the other paths are covered in tests/unit/auth/step_1.
    """
    monkeypatch.setattr(
        "tripit.core.v1.oauth.fetch_token",
        lambda: {
            "token": "fake_request_token",
            "token_secret": "fake-secret"
        },
    )
    fake_event = {
        "requestContext": {
            "path": "/develop/auth",
            "identity": {
                "apiKey": "fake-key"
            },
        },
        "headers": {
            "Host": "example.fake"
        },
    }
    expected_message = "".join([
        "You will need to authenticate into TripIt first; ",
        "click on or copy/paste this URL to get started: ",
        "https://www.tripit.com/oauth/authorize?oauth_token=fake_request_token&",
        "oauth_callback=https://example.fake/develop/callback",
    ])
    expected_response = return_ok(message=expected_message)
    assert begin_authentication(fake_event, None) == expected_response
    request_token_mapping = query_request_token_table("fake_request_token")
    assert request_token_mapping == {
        "access_key": "fake-key",
        "token": "fake_request_token",
        "token_secret": "fake-secret",
    }
    drop_request_token_table()
Ejemplo n.º 7
0
def test_begin_authentication_endpoint_when_already_has_tokens(
        set_access_token_table, drop_access_token_table):
    # pylint: enable=bad-continuation
    """
    Ensure that we know when we're already authorized.
    """
    set_access_token_table("fake-key", "fake_request_token", "fake-secret")
    fake_event = {
        "requestContext": {
            "path": "/develop/auth",
            "identity": {
                "apiKey": "fake-key"
            },
        },
        "headers": {
            "Host": "example.fake"
        },
    }
    expected_message = "Already authorized."
    expected_response = return_ok(message=expected_message)
    assert begin_authentication(fake_event, None) == expected_response
    drop_access_token_table()
Ejemplo n.º 8
0
def ping(_event, _context=None):
    """ If API Gateway is working, we should see an ok back. """
    return return_ok()
Ejemplo n.º 9
0
def test_ok_with_no_message():
    """
    Test returning basic successful API calls.
    """
    assert return_ok() == {"statusCode": 200, "body": json.dumps({"status": "ok"})}