Beispiel #1
0
def set_fcm_token():
    """
    Sets a participants Firebase CLoud Messaging (FCM) instance token, called whenever a new token
    is generated. Expects a patient_id and and fcm_token in the request body.
    """
    participant = get_session_participant()
    token = request.values.get('fcm_token', "")
    now = timezone.now()

    # force to unregistered on success, force every not-unregistered as unregistered.

    # need to get_or_create rather than catching DoesNotExist to handle if two set_fcm_token
    # requests are made with the same token one after another and one request.
    try:
        p, _ = ParticipantFCMHistory.objects.get_or_create(
            token=token, participant=participant)
        p.unregistered = None
        p.save()  # retain as save, we want last_updated to mutate
        ParticipantFCMHistory.objects.exclude(token=token).filter(
            participant=participant,
            unregistered=None).update(unregistered=now, last_updated=now)
    # ValidationError happens when the app sends a blank token
    except ValidationError:
        ParticipantFCMHistory.objects.filter(participant=participant,
                                             unregistered=None).update(
                                                 unregistered=now,
                                                 last_updated=now)

    participant.push_notification_unreachable_count = 0
    participant.save()

    return '', 204
Beispiel #2
0
def set_fcm_token():
    """
    Sets a participants Firebase CLoud Messaging (FCM) instance token, called whenever a new token
    is generated. Expects a patient_id and and fcm_token in the request body.
    """
    participant = get_session_participant()
    token = request.values.get('fcm_token', "")
    now = make_aware(datetime.utcnow(), timezone=pytz.utc)

    # These cases shouldn't happen.  All we want is to force the state that the app is serving us.
    try:
        # try, force to unregistered on success, force every not-unregistered as unregistered.
        p = ParticipantFCMHistory.objects.get(token=token)
        p.unregistered = None
        p.save()  # retain as save, we want last_updated to mutate
        ParticipantFCMHistory.objects.exclude(token=token).filter(
            unregistered=None).update(unregistered=now, last_updated=now)
    except ParticipantFCMHistory.DoesNotExist:
        # It wasn't found, it is new, unregister the existing and create the new one.
        ParticipantFCMHistory.objects.filter(participant=participant,
                                             unregistered=None).update(
                                                 unregistered=now,
                                                 last_updated=now)
        ParticipantFCMHistory.objects.create(token=token,
                                             participant=participant,
                                             unregistered=None)

    return '', 204
Beispiel #3
0
def get_latest_surveys(OS_API=""):
    study = get_session_participant().study
    survey_json_list = []
    for survey in study.surveys.filter(deleted=False):
        # Exclude image surveys for the Android app to avoid crashing it
        if not (OS_API == "ANDROID" and survey.survey_type == "image_survey"):
            survey_json_list.append(survey.format_survey_for_study())
    return json.dumps(survey_json_list)
Beispiel #4
0
def fetch_graph():
    """ Fetches the patient's answers to the most recent survey, marked by survey ID. The results
    are dumped into a jinja template and pushed to the device. """
    participant = get_session_participant()
    # See docs in config manipulations for details
    study_object_id = participant.study.object_id
    survey_object_id_set = participant.study.surveys.values_list('object_id',
                                                                 flat=True)
    data = []
    for survey_id in survey_object_id_set:
        data.append(
            get_survey_results(study_object_id, participant.patient_id,
                               survey_id, 7))
    return render_template("phone_graphs.html", data=data)
Beispiel #5
0
def send_notification():
    """
    Sends a push notification to the participant, used for testing
    Expects a patient_id in the request body.
    """
    message = messaging.Message(
        data={
            'type': 'fake',
            'content': 'hello good sir',
        },
        token=get_session_participant().get_fcm_token().token,
    )
    response = messaging.send(message)
    print('Successfully sent notification message:', response)
    return '', 204
Beispiel #6
0
def send_survey_notification():
    """
    Sends a push notification to the participant with survey data, used for testing
    Expects a patient_id in the request body
    """
    participant = get_session_participant()
    survey_ids = list(
        participant.study.surveys.filter(deleted=False).exclude(
            survey_type="image_survey").values_list("object_id",
                                                    flat=True)[:4])
    message = messaging.Message(
        data={
            'type': 'survey',
            'survey_ids': json.dumps(survey_ids),
            'sent_time': datetime.now().strftime(constants.API_TIME_FORMAT),
        },
        token=participant.get_fcm_token().token,
    )
    response = messaging.send(message)
    print('Successfully sent survey message:', response)
    return '', 204
Beispiel #7
0
def upload(OS_API=""):
    """ Entry point to upload GPS, Accelerometer, Audio, PowerState, Calls Log, Texts Log,
    Survey Response, and debugging files to s3.

    Behavior:
    The Beiwe app is supposed to delete the uploaded file if it receives an html 200 response.
    The API returns a 200 response when the file has A) been successfully handled, B) the file it
    has been sent is empty, C) the file did not decrypt properly.  We encountered problems in
    production with incorrectly encrypted files (as well as Android generating "rList" files
    under unknown circumstances) and the app then uploads them.  When the device receives a 200
    that is its signal to delete the file.
    When a file is undecryptable (this was tracked to a scenario where the device could not
    create/write an AES encryption key) we send a 200 response to stop that device attempting to
    re-upload the data.
    In the event of a single line being undecryptable (can happen due to io errors on the device)
    we drop only that line (and store the erroring line in an attempt to track it down.

    A 400 error means there is something is wrong with the uploaded file or its parameters,
    administrators will be emailed regarding this upload, the event will be logged to the apache
    log.  The app should not delete the file, it should try to upload it again at some point.

    If a 500 error occurs that means there is something wrong server side, administrators will be
    emailed and the event will be logged. The app should not delete the file, it should try to
    upload it again at some point.

    Request format:
    send an http post request to [domain name]/upload, remember to include security
    parameters (see user_authentication for documentation). Provide the contents of the file,
    encrypted (see encryption specification) and properly converted to Base64 encoded text,
    as a request parameter entitled "file".
    Provide the file name in a request parameter entitled "file_name". """

    # Handle these corner cases first because they requires no database input.
    # Crash logs are from truly ancient versions of the android codebase
    # rList are randomly generated by android
    # PersistedInstallation files come from firebase.
    # todo: stop uploading junk files in the app by putting our files into a folder.
    file_name = request.values.get("file_name", None)
    if (
            not bool(file_name)
            or file_name.startswith("rList")
            or file_name.startswith("PersistedInstallation")
            or not contains_valid_extension(file_name)
    ):
        return render_template('blank.html'), 200

    s3_file_location = file_name.replace("_", "/")
    participant = get_session_participant()

    if participant.unregistered:
        # "Unregistered" participants are blocked from uploading further data.
        # If the participant is unregistered, throw away the data file, but
        # return a 200 "OK" status to the phone so the phone decides it can
        # safely delete the file.
        return render_template('blank.html'), 200

    # block duplicate FTPs.  Testing the upload history is too complex
    if FileToProcess.test_file_path_exists(s3_file_location, participant.study.object_id):
        return render_template('blank.html'), 200

    uploaded_file = get_uploaded_file()
    try:
        uploaded_file = decrypt_device_file(uploaded_file, participant)
    except HandledError:
        return render_template('blank.html'), 200
    except DecryptionKeyInvalidError:
        # when the decryption key is invalid the file is lost.  Nothing we can do.
        # record the event, send the device a 200 so it can clear out the file.
        if REPORT_DECRYPTION_KEY_ERRORS:
            tags = {
                "participant": participant.patient_id,
                "operating system": "ios" if "ios" in request.path.lower() else "android",
                "DecryptionKeyError id": str(DecryptionKeyError.objects.last().id),
                "file_name": file_name,
                "bug_report": DECRYPTION_KEY_ADDITIONAL_MESSAGE,
            }
            sentry_client = make_sentry_client(SentryTypes.elastic_beanstalk, tags)
            sentry_client.captureMessage(DECRYPTION_KEY_ERROR_MESSAGE)
        return render_template('blank.html'), 200

    # if uploaded data actually exists, and has a valid extension
    if uploaded_file and file_name and contains_valid_extension(file_name):
        s3_upload(s3_file_location, uploaded_file, participant.study.object_id)

        # race condition: multiple _concurrent_ uploads with same file path. Behavior without
        # try-except is correct, but we don't care about reporting it. Just send the device a 500
        # error so it skips the file, the followup attempt receives 200 code and deletes the file.
        try:
            FileToProcess.append_file_for_processing(
                s3_file_location, participant.study.object_id, participant=participant
            )
        except ValidationError as e:
            # Real error is a second validation inside e.error_dict["s3_file_path"].
            # Ew; just test for this string instead...
            if S3_FILE_PATH_UNIQUE_CONSTRAINT_ERROR in str(e):
                # this tells the device to just move on to the next file, try again later.
                return abort(500)
            else:
                raise

        UploadTracking.objects.create(
            file_path=s3_file_location,
            file_size=len(uploaded_file),
            timestamp=timezone.now(),
            participant=participant,
        )
        return render_template('blank.html'), 200

    elif not uploaded_file:
        # if the file turns out to be empty, delete it, we simply do not care.
        return render_template('blank.html'), 200
    else:
        return make_upload_error_report(participant.patient_id, file_name)
Beispiel #8
0
def set_password(OS_API=""):
    """ After authenticating a user, sets the new password and returns 200.
    Provide the new password in a parameter named "new_password"."""
    participant = get_session_participant()
    participant.set_password(request.values["new_password"])
    return render_template('blank.html'), 200
Beispiel #9
0
def register_user(OS_API=""):
    """ Checks that the patient id has been granted, and that there is no device registered with
    that id.  If the patient id has no device registered it registers this device and logs the
    bluetooth mac address.
    Check the documentation in user_authentication to ensure you have provided the proper credentials.
    Returns the encryption key for this patient/user. """

    # CASE: If the id and password combination do not match, the decorator returns a 403 error.
    # the following parameter values are required.
    patient_id = request.values['patient_id']
    phone_number = request.values['phone_number']
    device_id = request.values['device_id']

    # These values may not be returned by earlier versions of the beiwe app
    device_os = request.values.get('device_os', "none")
    os_version = request.values.get('os_version', "none")
    product = request.values.get("product", "none")
    brand = request.values.get("brand", "none")
    hardware_id = request.values.get("hardware_id", "none")
    manufacturer = request.values.get("manufacturer", "none")
    model = request.values.get("model", "none")
    beiwe_version = request.values.get("beiwe_version", "none")

    # This value may not be returned by later versions of the beiwe app.
    mac_address = request.values.get('bluetooth_id', "none")

    participant = get_session_participant()
    if participant.device_id and participant.device_id != request.values['device_id']:
        # CASE: this patient has a registered a device already and it does not match this device.
        #   They need to contact the study and unregister their their other device.  The device
        #   will receive a 405 error and should alert the user accordingly.
        # Provided a user does not completely reset their device (which resets the device's
        # unique identifier) they user CAN reregister an existing device, the unlock key they
        # need to enter to at registration is their old password.
        # KG: 405 is good for IOS and Android, no need to check OS_API
        return abort(405)

    if participant.os_type and participant.os_type != OS_API:
        # CASE: this patient has registered, but the user was previously registered with a
        # different device type. To keep the CSV munging code sane and data consistent (don't
        # cross the iOS and Android data streams!) we disallow it.
        return abort(400)

    # At this point the device has been checked for validity and will be registered successfully.
    # Any errors after this point will be server errors and return 500 codes. the final return
    # will be the encryption key associated with this user.

    # Upload the user's various identifiers.
    unix_time = str(calendar.timegm(time.gmtime()))
    file_name = patient_id + '/identifiers_' + unix_time + ".csv"

    # Construct a manual csv of the device attributes
    file_contents = (DEVICE_IDENTIFIERS_HEADER + "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s" %
                     (patient_id, mac_address, phone_number, device_id, device_os,
                      os_version, product, brand, hardware_id, manufacturer, model,
                      beiwe_version)).encode()

    s3_upload(file_name, file_contents, participant.study.object_id)
    FileToProcess.append_file_for_processing(file_name, participant.study.object_id, participant=participant)

    # set up device.
    participant.device_id = device_id
    participant.os_type = OS_API
    participant.set_password(request.values['new_password'])  # set password saves the model
    device_settings = participant.study.device_settings.as_unpacked_native_python()
    device_settings.pop('_id', None)

    # set up FCM files
    firebase_plist_data = None
    firebase_json_data = None
    if participant.os_type == 'IOS':
        ios_credentials = FileAsText.objects.filter(tag=IOS_FIREBASE_CREDENTIALS).first()
        if ios_credentials:
            firebase_plist_data = plistlib.loads(ios_credentials.text.encode())
    elif participant.os_type == 'ANDROID':
        android_credentials = FileAsText.objects.filter(tag=ANDROID_FIREBASE_CREDENTIALS).first()
        if android_credentials:
            firebase_json_data = json.loads(android_credentials.text)

    # ensure the survey schedules are updated for this participant.
    repopulate_all_survey_scheduled_events(participant.study, participant)

    return_obj = {
        'client_public_key': get_client_public_key_string(patient_id, participant.study.object_id),
        'device_settings': device_settings,
        'ios_plist': firebase_plist_data,
        'android_firebase_json': firebase_json_data,
        'study_name': participant.study.name,
        'study_id': participant.study.object_id,
    }
    return json.dumps(return_obj), 200
Beispiel #10
0
def upload(OS_API=""):
    """ Entry point to upload GPS, Accelerometer, Audio, PowerState, Calls Log, Texts Log,
    Survey Response, and debugging files to s3.

    Behavior:
    The Beiwe app is supposed to delete the uploaded file if it receives an html 200 response.
    The API returns a 200 response when the file has A) been successfully handled, B) the file it
    has been sent is empty, C) the file did not decrypt properly.  We encountered problems in
    production with incorrectly encrypted files (as well as Android generating "rList" files
    under unknown circumstances) and the app then uploads them.  When the device receives a 200
    that is its signal to delete the file.
    When a file is undecryptable (this was tracked to a scenario where the device could not
    create/write an AES encryption key) we send a 200 response to stop that device attempting to
    re-upload the data.
    In the event of a single line being undecryptable (can happen due to io errors on the device)
    we drop only that line (and store the erroring line in an attempt to track it down.

    A 400 error means there is something is wrong with the uploaded file or its parameters,
    administrators will be emailed regarding this upload, the event will be logged to the apache
    log.  The app should not delete the file, it should try to upload it again at some point.

    If a 500 error occurs that means there is something wrong server side, administrators will be
    emailed and the event will be logged. The app should not delete the file, it should try to
    upload it again at some point.

    Request format:
    send an http post request to [domain name]/upload, remember to include security
    parameters (see user_authentication for documentation). Provide the contents of the file,
    encrypted (see encryption specification) and properly converted to Base64 encoded text,
    as a request parameter entitled "file".
    Provide the file name in a request parameter entitled "file_name". """

    # Handle these corner cases first because they requires no database input.
    # Crash logs are from truly ancient versions of the android codebase
    file_name = request.values['file_name']
    if file_name.startswith("rList") or "crashlog" in file_name.lower():
        return render_template('blank.html'), 200

    patient_id = request.values['patient_id']
    user = get_session_participant()

    # Slightly different values for iOS vs Android behavior.
    # Android sends the file data as standard form post parameter (request.values)
    # iOS sends the file as a multipart upload (so ends up in request.files)
    # if neither is found, consider the "body" of the post the file
    # ("body" post is not currently used by any client, only here for completeness)
    if "file" in request.files:
        uploaded_file = request.files['file']
    elif "file" in request.values:
        uploaded_file = request.values['file']
    else:
        uploaded_file = request.data

    if isinstance(uploaded_file, FileStorage):
        uploaded_file = uploaded_file.read()
    elif isinstance(uploaded_file, str):
        uploaded_file = uploaded_file.encode()
    elif isinstance(uploaded_file, bytes):
        # not current behavior on any app
        pass
    else:
        raise TypeError("uploaded_file was a %s" % type(uploaded_file))


    client_private_key = get_client_private_key(patient_id, user.study.object_id)
    try:
        uploaded_file = decrypt_device_file(patient_id, uploaded_file, client_private_key, user)
    except HandledError as e:
        # when decrypting fails, regardless of why, we rely on the decryption code
        # to log it correctly and return 200 OK to get the device to delete the file.
        # We do not want emails on these types of errors, so we use log_error explicitly.
        # this log statement hasn't been valuable since 2015, turning it off.
        # log_error(e, "%s; %s; %s" % (patient_id, file_name, e))
        return render_template('blank.html'), 200

    except DecryptionKeyInvalidError:
        # when the decryption key is invalid the file is lost.  Nothing we can do.
        # record the event, send the device a 200 so it can clear out the file.
        tags = {
            "participant": patient_id,
            "operating system": "ios" if "ios" in request.path.lower() else "android",
            "DecryptionKeyError id": str(DecryptionKeyError.objects.last().id),
            "file_name": file_name,
        }
        make_sentry_client('eb', tags).captureMessage("DecryptionKeyInvalidError")
        return render_template('blank.html'), 200

    s3_file_location = file_name.replace("_", "/")

    # if uploaded data a) actually exists, B) is validly named and typed...
    if uploaded_file and file_name and contains_valid_extension(file_name):
        s3_upload(s3_file_location, uploaded_file, user.study.object_id)
        FileToProcess.append_file_for_processing(
            s3_file_location, user.study.object_id, participant=user
        )
        UploadTracking.objects.create(
            file_path=s3_file_location,
            file_size=len(uploaded_file),
            timestamp=timezone.now(),
            participant=user,
        )
        return render_template('blank.html'), 200

    else:
        error_message = "an upload has failed " + patient_id + ", " + file_name + ", "
        if not uploaded_file:
            # it appears that occasionally the app creates some spurious files
            # with a name like "rList-org.beiwe.app.LoadingActivity"
            error_message += "there was no/an empty file, returning 200 OK so device deletes bad file."
            log_error(Exception("upload error"), error_message)
            return render_template('blank.html'), 200

        elif not file_name:
            error_message += "there was no provided file name, this is an app error."
        elif file_name and not contains_valid_extension(file_name):
            error_message += "contains an invalid extension, it was interpreted as "
            error_message += grab_file_extension(file_name)
        else:
            error_message += "AN UNKNOWN ERROR OCCURRED."

        tags = {"upload_error": "upload error", "user_id": patient_id}
        sentry_client = make_sentry_client('eb', tags)
        sentry_client.captureMessage(error_message)

        return abort(400)