Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
268 changes: 263 additions & 5 deletions apps/dot_ext/tests/test_authorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,24 @@
from django.test import Client
from django.urls import reverse
from oauth2_provider.models import get_access_token_model, get_refresh_token_model

# from oauth2_provider.compat import parse_qs, urlparse
from oauthlib.oauth2.rfc6749.errors import AccessDeniedError as AccessDeniedTokenCustomError
from waffle.testutils import override_switch

from apps.authorization.models import ArchivedDataAccessGrant, DataAccessGrant
from apps.constants import CODE_CHALLENGE_METHOD_S256, PATIENT_SCOPE
from apps.constants import (
CODE_CHALLENGE_METHOD_S256,
EOB_SCOPE,
PATIENT_SCOPE,
TEST_APP_CLIENT_ID,
TEST_APP_CLIENT_SECRET,
)
from apps.dot_ext.constants import (
APPLICATION_HAS_CLIENT_CREDENTIALS_ENABLED_NON_CLIENT_CREDENTIALS_AUTH_CALL_MADE,
AUDIT_EVENT_SCOPE_ERROR_MESSAGE,
CLIENT_CREDENTIALS_TYPE,
)
from apps.dot_ext.models import Application, ArchivedToken
from apps.dot_ext.utils import create_authorization_header
from apps.dot_ext.views import AuthorizationView, TokenView
from apps.fhir.bluebutton.models import Crosswalk
from apps.fhir.server.authentication import MatchFhirIdErrorType, MatchFhirIdLookupType, MatchFhirIdResult
Expand Down Expand Up @@ -206,10 +211,10 @@ def test_post_with_valid_non_standard_scheme_granttype_authcode_clienttype_confi
'client_id': application.client_id,
'code_verifier': 'test123456789123456789123456789123456789123456789',
}
# Test that request is unauthorized WITH OUT the client_secret.
# Test that request is bad request WITH OUT the client_secret.
token_request_data.update({'code_verifier': 'test123456789123456789123456789123456789123456789'})
response = self.client.post(reverse('oauth2_provider:token'), data=token_request_data)
self.assertEqual(response.status_code, 401)
self.assertEqual(response.status_code, HTTPStatus.BAD_REQUEST)

# Setup application's client_secret
token_request_data.update({'client_secret': application.client_secret_plain})
Expand Down Expand Up @@ -1979,3 +1984,256 @@ def test_failure_on_authorize_non_v3_with_audit_event_scope(

assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.json()['message'] == expected_message


@pytest.mark.integration
@pytest.mark.parametrize(
'post_data, header, app_config, expected_result',
[
( # Empty post body client secret + no header + empty DB client secret returns bad request
{'client_secret': ''},
'',
# app configuration saving as an empty client secret
{
'client_type': Application.CLIENT_CONFIDENTIAL,
'client_secret': '',
},
# Expect a bad request
{
'status_code': HTTPStatus.BAD_REQUEST,
'message': 'The client secret is empty or missing.',
},
),
# Empty client secret in Basic header + no client secret in post body + empty DB client secret returns 400
(
{},
create_authorization_header(TEST_APP_CLIENT_ID, ''),
# app configuration saving as an empty client secret
{
'client_type': Application.CLIENT_CONFIDENTIAL,
'client_secret': '',
},
# Expect a bad request
{
'status_code': HTTPStatus.BAD_REQUEST,
'message': 'The client secret is empty or missing.',
},
),
# No post body client secret + no header + empty DB secret returns 400
(
{},
'',
# app configuration saving as an empty client secret
{
'client_type': Application.CLIENT_CONFIDENTIAL,
'client_secret': '',
},
# Expect a bad request
{
'status_code': HTTPStatus.BAD_REQUEST,
'message': 'The client secret is empty or missing.',
},
),
( # Empty post body client secret + no header + non-empty DB client secret returns 400
{'client_secret': ''},
'',
# app configuration saving as a non-empty client secret
{
'client_type': Application.CLIENT_CONFIDENTIAL,
'client_secret': TEST_APP_CLIENT_SECRET,
},
# Expect a bad request
{
'status_code': HTTPStatus.BAD_REQUEST,
'message': 'The client secret is empty or missing.',
},
),
# Empty client secret in Basic header + no client secret in post body + non-empty DB client secret returns 400
(
{},
create_authorization_header(TEST_APP_CLIENT_ID, ''),
# app configuration saving as a non-empty client secret
{
'client_type': Application.CLIENT_CONFIDENTIAL,
'client_secret': TEST_APP_CLIENT_SECRET,
},
# Expect a bad request
{
'status_code': HTTPStatus.BAD_REQUEST,
'message': 'The client secret is empty or missing.',
},
),
# No post body client secret + no header + empty DB secret returns 400
(
{},
'',
# app configuration saving as a non-empty client secret
{
'client_type': Application.CLIENT_CONFIDENTIAL,
'client_secret': TEST_APP_CLIENT_SECRET,
},
# Expect a bad request
{
'status_code': HTTPStatus.BAD_REQUEST,
'message': 'The client secret is empty or missing.',
},
),
# Valid client secret in post body + no header + non empty/valid DB secret returns 200
(
{'client_secret': TEST_APP_CLIENT_SECRET},
'',
# app configuration saving as a non-empty client secret
{
'client_type': Application.CLIENT_CONFIDENTIAL,
'client_secret': TEST_APP_CLIENT_SECRET,
},
# Expect passing
{
'status_code': HTTPStatus.OK,
'message': 'access_token',
},
),
# No client secret in post body + valid header + non-empty/valid DB client secret returns 200
(
{},
create_authorization_header(TEST_APP_CLIENT_ID, TEST_APP_CLIENT_SECRET),
# app configuration saving as non empty client secret
{
'client_type': Application.CLIENT_CONFIDENTIAL,
'client_secret': TEST_APP_CLIENT_SECRET,
},
# Expect passing
{
'status_code': HTTPStatus.OK,
'message': 'access_token',
},
),
# Valid header client secret + invalid client secret + non-empty/valid DB client secret in body produces a 400
(
{'client_secret': 'invalid-secret'},
create_authorization_header(TEST_APP_CLIENT_ID, TEST_APP_CLIENT_SECRET),
# app configuration saving as a non-empty client secret
{
'client_type': Application.CLIENT_CONFIDENTIAL,
'client_secret': TEST_APP_CLIENT_SECRET,
},
# Expect a bad request
{
'status_code': HTTPStatus.BAD_REQUEST,
'message': 'The client secret in Authorization header and POST body do not match.',
},
),
# Invalid header client secret + valid client secret + non-empty/valid DB client secret in body produces a 400
(
{'client_secret': TEST_APP_CLIENT_SECRET},
create_authorization_header(TEST_APP_CLIENT_ID, 'invalid-secret'),
# app configuration saving as non empty client secret
{
'client_type': Application.CLIENT_CONFIDENTIAL,
'client_secret': TEST_APP_CLIENT_SECRET,
},
# Expect a bad request
{
'status_code': HTTPStatus.BAD_REQUEST,
'message': 'The client secret in Authorization header and POST body do not match.',
},
),
# No header + invalid client secret body + non-empty/valid DB client secret in body produces a 401
(
{'client_secret': 'invalid-secret'},
'',
# app configuration saving as non empty client secret
{
'client_type': Application.CLIENT_CONFIDENTIAL,
'client_secret': TEST_APP_CLIENT_SECRET,
},
# Expect unauthorized
{
'status_code': HTTPStatus.UNAUTHORIZED,
'message': 'invalid_client',
},
),
# No post body client secret + invalid client secret header + non-empty DB client secret in body produces a 401
(
{},
create_authorization_header(TEST_APP_CLIENT_ID, 'invalid-secret'),
# app configuration saving as non empty client secret
{
'client_type': Application.CLIENT_CONFIDENTIAL,
'client_secret': TEST_APP_CLIENT_SECRET,
},
# Expect unauthorized
{
'status_code': HTTPStatus.UNAUTHORIZED,
'message': 'invalid_client',
},
),
# Valid secret in BOTH header and body and valid db secret returns 200
(
{'client_secret': 'test-client-secret'},
create_authorization_header(TEST_APP_CLIENT_ID, TEST_APP_CLIENT_SECRET),
# app configuration saving as non empty client secret
{
'client_type': Application.CLIENT_CONFIDENTIAL,
'client_secret': TEST_APP_CLIENT_SECRET,
},
# Expect passing
{
'status_code': HTTPStatus.OK,
'message': 'access_token',
},
),
# No credentials and empty db client_secret but for public app produces 200
(
{},
'',
# app configuration saving as empty client secret
{
'client_type': Application.CLIENT_PUBLIC,
'client_secret': '',
},
# Expect passing
{
'status_code': HTTPStatus.OK,
'message': 'access_token',
},
),
],
)
@override_switch('v3_endpoints', active=True)
def test_validate_symmetric_auth_client_secret(
post_data, header, app_config, expected_result, create_auth_code, client
):
"""
Ensure validate_symmetric_auth_client_secret function works properly by blocking empty or
mismatched client_secrets
"""
# Assign constants for the test
redirect_uri = 'com.custom.bluebutton://example.it'
client_id = TEST_APP_CLIENT_ID
scope_name = EOB_SCOPE
code_challenge = 'sZrievZsrYqxdnu2NVD603EiYBM18CuzZpwB-pOSZjo'
code_verifier = 'test123456789123456789123456789123456789123456789'
grant_type = 'authorization_code'
auth_code = create_auth_code(
client_secret=app_config['client_secret'],
client_type=app_config['client_type'],
redirect_uri=redirect_uri,
client_id=client_id,
scope_name=scope_name,
code_challenge=code_challenge,
grant_type=grant_type,
)

token_request_data = {
'grant_type': grant_type,
'code': auth_code,
'redirect_uri': redirect_uri,
'code_verifier': code_verifier,
'scope': scope_name,
'client_id': client_id,
}
token_request_data.update(post_data)
response = client.post('/v3/o/token', data=token_request_data, HTTP_AUTHORIZATION=header)
assert response.status_code == expected_result['status_code']
assert expected_result['message'] in response.content.decode()
72 changes: 70 additions & 2 deletions apps/dot_ext/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@
from unittest.mock import MagicMock, patch

import pytest
from django.test import TestCase
from django.test import RequestFactory, TestCase
from oauthlib.oauth2.rfc6749.errors import InvalidClientError

from apps.dot_ext.constants import CLEAR_HIGHER_ISS, IDME_HIGHER_ISS, IDME_LOWER_ISS, SUPPORTED_VERSION_TEST_CASES
from apps.constants import TEST_APP_CLIENT_ID, TEST_APP_CLIENT_SECRET
from apps.dot_ext.constants import (
CLEAR_HIGHER_ISS,
IDME_HIGHER_ISS,
IDME_LOWER_ISS,
SUPPORTED_VERSION_TEST_CASES,
)
from apps.dot_ext.utils import (
build_jwks_urls,
check_can_token_scope_for_audit_event_scopes,
check_session_and_create_access_token_extension,
create_authorization_header,
get_api_version_number_from_url,
remove_application_user_pair_tokens_data_access,
validate_client_id,
validate_latin_extended_string,
validate_symmetric_client_secret,
)
from apps.versions import VersionNotMatched

Expand Down Expand Up @@ -246,3 +254,63 @@ def test_build_jwks_urls_returns_lower_environment(settings) -> None:
}
result = build_jwks_urls()
assert result == expected


@pytest.mark.django_db
@pytest.mark.parametrize(
'post_data, headers, expected_result',
[
# Valid client secret in post body ensures client_secret is not empty and passes validation
({'client_secret': 'valid_secret_123'}, {}, {'passed': True, 'message': ''}),
# Valid client secret in header ensures client secret is not empty and passes validation
(
{},
{
'HTTP_AUTHORIZATION': create_authorization_header(TEST_APP_CLIENT_ID, TEST_APP_CLIENT_SECRET),
},
{'passed': True, 'message': ''},
),
# Empty client secret being passed in to the post body fails validation
({'client_secret': ''}, {}, {'passed': False, 'message': 'The client secret is empty or missing.'}),
# Client secret with spaces being passed in fails validation
({'client_secret': ' '}, {}, {'passed': False, 'message': 'The client secret is empty or missing.'}),
# Empty client secret being passed into the header and fails validation
(
{},
{
'HTTP_AUTHORIZATION': create_authorization_header(TEST_APP_CLIENT_ID, ''),
},
{'passed': False, 'message': 'The client secret is empty or missing.'},
),
# No client secret in header or body fails validation
({}, {}, {'passed': False, 'message': 'The client secret is empty or missing.'}),
# Mismatched client secrets between post body and header fails validation
(
{'client_secret': TEST_APP_CLIENT_SECRET},
{
'HTTP_AUTHORIZATION': create_authorization_header(TEST_APP_CLIENT_ID, 'invalid-secret'),
},
{'passed': False, 'message': 'The client secret in Authorization header and POST body do not match.'},
),
# Matching client secrets between post body and header passes validation
(
{'client_secret': TEST_APP_CLIENT_SECRET},
{
'HTTP_AUTHORIZATION': create_authorization_header(TEST_APP_CLIENT_ID, TEST_APP_CLIENT_SECRET),
},
{'passed': True, 'message': ''},
),
],
)
def test_validate_symmetric_client_secret(post_data, headers, expected_result):
"""
Test successful and unsuccessful outcomes of validate_symmetric_client_secret
"""
factory = RequestFactory()

request = factory.post('/token', data=post_data, **headers)

passed_validation, error_message = validate_symmetric_client_secret(request)

assert passed_validation is expected_result['passed']
assert error_message == expected_result['message']
Loading
Loading