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 try: device_os = request.values['device_os'] except BadRequestKeyError: device_os = "none" try: os_version = request.values['os_version'] except BadRequestKeyError: os_version = "none" try: product = request.values["product"] except BadRequestKeyError: product = "none" try: brand = request.values["brand"] except BadRequestKeyError: brand = "none" try: hardware_id = request.values["hardware_id"] except BadRequestKeyError: hardware_id = "none" try: manufacturer = request.values["manufacturer"] except BadRequestKeyError: manufacturer = "none" try: model = request.values["model"] except BadRequestKeyError: model = "none" try: beiwe_version = request.values["beiwe_version"] except BadRequestKeyError: beiwe_version = "none" # This value may not be returned by later versions of the beiwe app. try: mac_address = request.values['bluetooth_id'] except BadRequestKeyError: mac_address = "none" user = Participant.objects.get(patient_id=patient_id) study_id = user.study.object_id if user.device_id and user.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 user.os_type and user.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)) # print(file_contents + "\n") s3_upload(file_name, file_contents, study_id) FileToProcess.append_file_for_processing(file_name, user.study.object_id, participant=user) # set up device. user.set_device(device_id) user.set_os_type(OS_API) user.set_password(request.values['new_password']) device_settings = user.study.device_settings.as_native_python() device_settings.pop('_id', None) return_obj = { 'client_public_key': get_client_public_key_string(patient_id, study_id), 'device_settings': device_settings } return json.dumps(return_obj), 200
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