Exemplo n.º 1
0
def _get_api_key(email: str, is_crs_user: bool) -> Optional[str]:
    api_key = APIKeyRepo.get_api_key(email)

    if api_key:
        # Record if existing users are covid response simulator users as they
        # may have registered not in the CRS.  This ensures that anyone who
        # gets their api key through the CRS registration will be recorded as a CRS user.
        if is_crs_user:
            APIKeyRepo.record_covid_response_simulator_user(email)

        return api_key

    return None
Exemplo n.º 2
0
def check_api_key_edge(event, context):
    request = event["Records"][0]["cf"]["request"]

    query_parameters = urllib.parse.parse_qs(request["querystring"])
    # parse query parameter by taking first api key in query string arg.
    api_key = None
    for api_key in query_parameters.get("apiKey") or []:
        break

    if not api_key:
        return _make_error_message(
            "API key required. Visit https://apidocs.covidactnow.org/#register to get an API key"
        )

    record = APIKeyRepo.get_record_for_api_key(api_key)
    if not record:
        return _make_error_message("Invalid API key.")

    if record["email"] in Config.Constants.EMAIL_BLOCKLIST:
        error_message = "Unauthorized. Please contact [email protected] to restore access."
        return _make_error_message(error_message)

    _record_successful_request(request, record)

    # Return request, which forwards to S3 backend.
    return request
Exemplo n.º 3
0
def _get_or_create_api_key(email):
    api_key = APIKeyRepo.get_api_key(email)
    if api_key:
        return api_key

    _logger.info(f"No API Key found for email {email}, creating new key")

    api_key = _create_api_key(email)
    APIKeyRepo.add_api_key(email, api_key)

    welcome_email = _build_welcome_email(email, api_key)
    if EmailRepo.send_email(welcome_email):
        APIKeyRepo.record_email_sent(email)
    else:
        _logger.error(f"Failed to send email to {email}")

    return api_key
Exemplo n.º 4
0
def _get_or_create_api_key(email: str, is_crs_user: bool):
    api_key = APIKeyRepo.get_api_key(email)

    if api_key:
        # Record if existing users are covid response simulator users as they
        # may have registered not in the CRS.  This ensures that anyone who
        # gets their api key through the CRS registration will be recorded as a CRS user.
        if is_crs_user:
            APIKeyRepo.record_covid_response_simulator_user(email)

        return api_key

    _logger.info(f"No API Key found for email {email}, creating new key")

    api_key = _create_api_key(email)
    APIKeyRepo.add_api_key(email, api_key, is_crs_user)

    welcome_email = _build_welcome_email(email, api_key)
    if EmailRepo.send_email(welcome_email):
        APIKeyRepo.record_email_sent(email)
    else:
        _logger.error(f"Failed to send email to {email}")

    # attempt to add hubspot contact, but don't block reg on failure.
    try:
        registry.hubspot_client.create_contact(email)
    except hubspot_client.HubSpotAPICallFailed:
        _logger.error("HubSpot call failed")
        sentry_sdk.capture_exception()

    return api_key
Exemplo n.º 5
0
def check_api_key(event, context):
    """Checks API Key included in request for registered value."""
    method_arn = event["methodArn"]

    if not event["queryStringParameters"]["apiKey"]:
        return _generate_deny_policy(method_arn)

    api_key = event["queryStringParameters"]["apiKey"]

    record = APIKeyRepo.get_record_for_api_key(api_key)
    if not record:
        return _generate_deny_policy(method_arn)

    return _generate_accept_policy(record, method_arn)
Exemplo n.º 6
0
def _create_new_user(args: RegistrationArguments) -> str:
    email = args.email

    api_key = _create_api_key()
    APIKeyRepo.add_api_key(email, api_key, args.is_crs_user)

    welcome_email = _build_welcome_email(email, api_key)
    if EmailRepo.send_email(welcome_email):
        APIKeyRepo.record_email_sent(email)
    else:
        _logger.error(f"Failed to send email to {email}")

    # attempt to add hubspot contact, but don't block reg on failure.
    try:
        registry.hubspot_client.submit_reg_form(
            email,
            hubspot_token=args.hubspot_token,
            page_uri=args.page_uri,
            use_case=args.use_case)
    except hubspot_client.HubSpotAPICallFailed:
        _logger.error("HubSpot call failed")
        sentry_sdk.capture_exception()

    return api_key
Exemplo n.º 7
0
def check_api_key_edge(event, context):
    request = event["Records"][0]["cf"]["request"]

    query_parameters = urllib.parse.parse_qs(request["querystring"])
    # parse query parameter by taking first api key in query string arg.
    api_key = None
    for api_key in query_parameters.get("apiKey") or []:
        break

    if not api_key:
        return {"status": 403, "statusDescription": "Unauthorized"}

    record = APIKeyRepo.get_record_for_api_key(api_key)
    if not record:
        return {"status": 403, "statusDescription": "Unauthorized"}

    if record["email"] in Config.Constants.EMAIL_BLOCKLIST:
        error_message = {
            "error":
            "Unauthorized. Please contact [email protected] to restore access."
        }
        return {
            "status":
            403,
            "body":
            json.dumps(error_message),
            "headers": {
                "content-type": [{
                    "value": "application/json"
                }]
            },
            "bodyEncoding":
            "text",
            "statusDescription":
            ("Unauthorized. Please contact [email protected] to restore access."
             ),
        }

    _record_successful_request(request, record)

    # Return request, which forwards to S3 backend.
    return request
Exemplo n.º 8
0
def check_api_key_edge(event, context):
    request = event["Records"][0]["cf"]["request"]

    query_parameters = urllib.parse.parse_qs(request["querystring"])
    # parse query parameter by taking first api key in query string arg.
    api_key = None
    for api_key in query_parameters.get("apiKey") or []:
        break

    if not api_key:
        return {"status": 403, "statusDescription": "Unauthorized"}

    record = APIKeyRepo.get_record_for_api_key(api_key)
    if not record:
        return {"status": 403, "statusDescription": "Unauthorized"}

    _record_successful_request(request, record)

    # Return request, which forwards to S3 backend.
    return request