def test_FhirServerAuth(self):
        """  Check FHIR Server ClientAuth settings """
        """ Test 1: pass nothing"""

        resource_router = get_resourcerouter()
        expected = {}
        expected['client_auth'] = resource_router.client_auth
        expected['cert_file'] = os.path.join(settings.FHIR_CLIENT_CERTSTORE,
                                             resource_router.cert_file)
        expected['key_file'] = os.path.join(settings.FHIR_CLIENT_CERTSTORE,
                                            resource_router.key_file)

        response = FhirServerAuth()

        self.assertDictEqual(response, expected)
        """ Test 2: pass crosswalk """
        crosswalk = Crosswalk.objects.get(pk=1)

        response = FhirServerAuth(crosswalk)

        expected = {
            'client_auth':
            crosswalk.fhir_source.client_auth,
            'cert_file':
            os.path.join(settings.FHIR_CLIENT_CERTSTORE,
                         crosswalk.fhir_source.cert_file),
            'key_file':
            os.path.join(settings.FHIR_CLIENT_CERTSTORE,
                         crosswalk.fhir_source.key_file)
        }

        self.assertDictEqual(response, expected)
    def test_FhirServerAuth(self):
        """  Check FHIR Server ClientAuth settings """
        """ Test 1: pass nothing"""

        rr = get_resourcerouter()
        expected = {}
        expected['client_auth'] = rr.client_auth
        expected['cert_file'] = os.path.join(settings.FHIR_CLIENT_CERTSTORE,
                                             rr.cert_file)
        expected['key_file'] = os.path.join(settings.FHIR_CLIENT_CERTSTORE,
                                            rr.key_file)

        response = FhirServerAuth()

        # print("Test 1: FHIRServerAuth %s %s" % (response, expected))

        self.assertDictEqual(response, expected)
        """ Test 2: pass cx """
        cx = Crosswalk.objects.get(pk=1)

        response = FhirServerAuth(cx)

        expected = {
            'client_auth':
            cx.fhir_source.client_auth,
            'cert_file':
            os.path.join(settings.FHIR_CLIENT_CERTSTORE,
                         cx.fhir_source.cert_file),
            'key_file':
            os.path.join(settings.FHIR_CLIENT_CERTSTORE,
                         cx.fhir_source.key_file)
        }
        # print("\n Test 2: FHIRServerAuth %s %s" % (response, expected))

        self.assertDictEqual(response, expected)
    def test_FhirServerAuth(self):
        """  Check FHIR Server ClientAuth settings """
        """ Test 1: pass nothing"""

        response = FhirServerAuth()
        expected = settings.FHIR_DEFAULT_AUTH
        # print("Test 1: FHIRServerAuth %s %s" % (response, expected))

        self.assertDictEqual(response, expected)
        """ Test 2: pass cx """
        cx = Crosswalk.objects.get(pk=1)
        response = FhirServerAuth(cx)

        expected = {
            'client_auth':
            True,
            'cert_file':
            os.path.join(settings.FHIR_CLIENT_CERTSTORE, "cert_file.pem"),
            'key_file':
            os.path.join(settings.FHIR_CLIENT_CERTSTORE, "key_file.pem")
        }
        # print("\n Test 2: FHIRServerAuth %s %s" % (response, expected))

        self.assertDictEqual(response, expected)
    def test_FhirServerAuth(self):
        """  Check FHIR Server ClientAuth settings """
        """ Test 1: pass nothing"""

        resource_router = get_resourcerouter()
        expected = {}
        expected['client_auth'] = resource_router.client_auth
        expected['cert_file'] = os.path.join(settings.FHIR_CLIENT_CERTSTORE,
                                             resource_router.cert_file)
        expected['key_file'] = os.path.join(settings.FHIR_CLIENT_CERTSTORE,
                                            resource_router.key_file)

        response = FhirServerAuth()

        self.assertDictEqual(response, expected)
Beispiel #5
0
def certs(crosswalk=None):
    auth_state = FhirServerAuth(crosswalk)
    return (auth_state.get('cert_file',
                           None), auth_state.get('key_file', None))
Beispiel #6
0
def get_and_update_user(user_info):
    username = convert_sls_uuid(user_info['sub'])
    try:
        user = User.objects.get(username=username)
        if not user.first_name:
            user.first_name = user_info['given_name']
        if not user.last_name:
            user.last_name = user_info['family_name']
        if not user.email:
            user.email = user_info['email']
        user.save()
    except User.DoesNotExist:
        # Create a new user. Note that we can set password
        # to anything, because it won't be checked.
        user = User(username=username,
                    password='',
                    first_name=user_info['given_name'],
                    last_name=user_info['family_name'],
                    email=user_info['email'])
        user.set_unusable_password()
        user.save()
    UserProfile.objects.get_or_create(user=user, user_type='BEN')
    group = Group.objects.get(name='BlueButton')
    user.groups.add(group)
    # Log in the user
    user.backend = 'django.contrib.auth.backends.ModelBackend'

    # Determine patient_id
    fhir_source = get_resourcerouter()
    crosswalk, _ = Crosswalk.objects.get_or_create(user=user,
                                                   fhir_source=fhir_source)
    hicn = user_info.get('hicn', "")
    crosswalk.user_id_hash = hicn
    crosswalk.save()

    auth_state = FhirServerAuth(None)
    certs = (auth_state['cert_file'], auth_state['key_file'])

    # URL for patient ID.
    url = fhir_source.fhir_url + \
        "Patient/?identifier=http%3A%2F%2Fbluebutton.cms.hhs.gov%2Fidentifier%23hicnHash%7C" + \
        crosswalk.user_id_hash + \
        "&_format=json"
    response = requests.get(url, cert=certs, verify=False)
    backend_data = response.json()

    if 'entry' in backend_data and backend_data['total'] == 1:
        fhir_id = backend_data['entry'][0]['resource']['id']
        crosswalk.fhir_id = fhir_id
        crosswalk.save()

        logger.info("Success:Beneficiary connected to FHIR")
    else:
        logger.error("Failed to connect Beneficiary " "to FHIR")

    # Get first and last name from FHIR if not in OIDC Userinfo response.
    if user_info['given_name'] == "" or user_info['family_name'] == "":
        if 'entry' in backend_data:
            if 'name' in backend_data['entry'][0]['resource']:
                names = backend_data['entry'][0]['resource']['name']
                first_name = ""
                last_name = ""
                for n in names:
                    if n['use'] == 'usual':
                        last_name = n['family']
                        first_name = n['given'][0]
                    if last_name or first_name:
                        user.first_name = first_name
                        user.last_name = last_name
                        user.save()

    return user