diff --git a/apps/dot_ext/constants.py b/apps/dot_ext/constants.py index 175e26090..eca2315b9 100644 --- a/apps/dot_ext/constants.py +++ b/apps/dot_ext/constants.py @@ -1658,3 +1658,30 @@ SECONDS_IN_ONE_HOUR = 3600 AUDIT_EVENT_SCOPE_ERROR_MESSAGE = 'Requesting an AuditEvent scope is only valid in token requests through CMS Aligned Networks. For more information, go to this page: https://bluebutton.cms.gov/cms-aligned-networks-documentation/' + +PRIOR_IAL_TOKEN_REQUIRE_LIST_FOR_JWT_DECODE = [ + 'iss', + 'sub', + 'aud', + 'jti', + 'exp', + 'iat', + 'identity_assurance_level', + 'auth_time', + 'family_name', + 'given_name', + 'birthdate', +] + +OIDC4IDC_TOKEN_REQUIRE_LIST_FOR_JWT_DECODE = [ + 'iss', + 'sub', + 'aud', + 'jti', + 'exp', + 'iat', + 'verified_claims', + 'uuid', + 'auth_time', + 'at_hash', +] diff --git a/apps/dot_ext/tests/test_authorization_token.py b/apps/dot_ext/tests/test_authorization_token.py index 2775b7475..16d8166b9 100644 --- a/apps/dot_ext/tests/test_authorization_token.py +++ b/apps/dot_ext/tests/test_authorization_token.py @@ -25,6 +25,7 @@ from apps.dot_ext.constants import ( APPLICATION_DOES_NOT_HAVE_THIS_AUTH_TYPE_ENABLED, APPLICATION_HAS_CLIENT_CREDENTIALS_ENABLED_NON_CLIENT_CREDENTIALS_AUTH_CALL_MADE, + AUTH_CODE_AND_CLIENT_CREDENTIALS_TYPE, AUTH_CODE_TYPE, CC_SYSTEM_MEDICARE_NUMBER, CLIENT_ASSERTION_TYPE_VALUE, @@ -600,3 +601,97 @@ def test_client_credentials_returns_patient_match_not_found_401_integration( ) assert response.status_code == HTTPStatus.UNAUTHORIZED assert response.json()['message'] == PATIENT_DATA_CANNOT_BE_FOUND + + +@patch.dict(os.environ, {'TARGET_ENV': 'local'}) +@patch('apps.dot_ext.views.authorization.get_and_update_from_refresh') +@patch('apps.dot_ext.validators.CMSAlignedNetworksValidator.authenticate_and_validate_token') +@patch('apps.dot_ext.views.authorization.TokenView._create_or_retrieve_user') +@patch('apps.dot_ext.views.authorization.get_patient_match_response_json') +@override_switch('v3_endpoints', active=True) +@override_switch('enable_auditevents', active=True) +def test_client_credentials_token_and_refresh_with_oidc4da_token_format( + mock_get_patient, + mock_create_user, + mock_authenticate_and_validate_token, + mock_get_and_update, + basic_user, + create_application, + create_capability, +): + """Verify that an ID token with the OIDC4IDA token format can successfully make it through a CAN request with the + client_credentials_validation switch inactive""" + + # Mocking the matched user + # self.patient_fhir_v3 = 'patient-123-v3' + # self.user = self._create_user('patient_user', 'password123', fhir_id_v3=self.patient_fhir_v3) + user = basic_user(username='patient_user', fhir_id_v3='patient-123-v3') + mock_create_user.return_value = user + + eob_capability = create_capability(name=EOB_SCOPE, urls=[['GET', '/v[123]/fhir/ExplanationOfBenefit[/]?$']]) + application = create_application( + name='Test App', + jwks_uri='http://localhost:8000/.well-known/jwks.json', + capability=eob_capability, + allowed_auth_type=AUTH_CODE_AND_CLIENT_CREDENTIALS_TYPE, + ) + + mock_get_and_update.return_value = None + + mock_response = jwt.encode( + { + 'iss': IDME_LOWER_ISS, + 'sub': '123', + 'aud': 'https://fake.bluebutton.cms.gov', + 'jti': 'jti-1', + 'exp': 9999999999, + 'iat': 1775317326, + }, + 'secret', + algorithm='HS256', + ) + mock_authenticate_and_validate_token.return_value = (mock_response, IDME_LOWER_ISS) + + # Mock patient match result + # is_patient_match_found expects at least 2 entries in successful match + mock_get_patient.return_value = { + 'type': 'searchset', + 'entry': [ + {'resource': {'id': 'org-example', 'resourceType': 'Organization'}}, + { + 'resource': { + 'resourceType': 'Patient', + 'id': 'patient-123-v3', + 'identifier': [ + { + 'system': CC_SYSTEM_MEDICARE_NUMBER, + 'value': '1SA0A00AA00', + } + ], + } + }, + ], + } + + assertion = jwt.encode({'iss': application.client_id}, 'secret', algorithm='HS256') + + token_request_data = { + 'grant_type': CLIENT_CREDENTIALS, + 'client_assertion_type': CLIENT_ASSERTION_TYPE_VALUE, + 'client_assertion': assertion, + 'scope': 'patient/ExplanationOfBenefit.rs openid', + } + + response = Client().post( + f'/v{Versions.V3}/o/token/', + data=urlencode(token_request_data), + content_type='application/x-www-form-urlencoded', + ) + + assert response.status_code == HTTPStatus.OK + data = response.json() + + assert 'patient' in data + assert data['patient'] == 'patient-123-v3' + assert 'access_token' in data + assert 'refresh_token' in data