diff --git a/swh/deposit/api/checks.py b/swh/deposit/api/checks.py --- a/swh/deposit/api/checks.py +++ b/swh/deposit/api/checks.py @@ -22,6 +22,7 @@ import pkg_resources import xmlschema +from swh.deposit.errors import FORBIDDEN, DepositError from swh.deposit.utils import NAMESPACES, parse_swh_metadata_provenance MANDATORY_FIELDS_MISSING = "Mandatory fields are missing" @@ -113,3 +114,16 @@ return True, {"metadata": suggested_fields} return True, None + + +def check_url_match_provider(url: str, provider_url: str) -> None: + """Check url matches the provider url. + + Raises DepositError in case of mismatch + + """ + provider_url = provider_url.rstrip("/") + "/" + if not url.startswith(provider_url): + raise DepositError( + FORBIDDEN, f"URL mismatch: {url} must start with {provider_url}", + ) diff --git a/swh/deposit/api/common.py b/swh/deposit/api/common.py --- a/swh/deposit/api/common.py +++ b/swh/deposit/api/common.py @@ -24,14 +24,13 @@ from rest_framework.request import Request from rest_framework.views import APIView -from swh.deposit.api.checks import check_metadata +from swh.deposit.api.checks import check_metadata, check_url_match_provider from swh.deposit.api.converters import convert_status_detail from swh.deposit.auth import HasDepositPermission, KeycloakBasicAuthentication from swh.deposit.models import DEPOSIT_METADATA_ONLY, Deposit from swh.deposit.parsers import parse_xml from swh.deposit.utils import ( NAMESPACES, - check_url_match_provider, compute_metadata_context, parse_swh_metadata_provenance, ) diff --git a/swh/deposit/utils.py b/swh/deposit/utils.py --- a/swh/deposit/utils.py +++ b/swh/deposit/utils.py @@ -9,7 +9,6 @@ import iso8601 -from swh.deposit.errors import FORBIDDEN, DepositError from swh.model.exceptions import ValidationError from swh.model.model import TimestampWithTimezone from swh.model.swhids import ExtendedSWHID, ObjectType, QualifiedSWHID @@ -256,16 +255,3 @@ """ return f'<{link}>; rel="{link_name}"' - - -def check_url_match_provider(url: str, provider_url: str) -> None: - """Check url matches the provider url. - - Raises DepositError in case of mismatch - - """ - provider_url = provider_url.rstrip("/") + "/" - if not url.startswith(provider_url): - raise DepositError( - FORBIDDEN, f"URL mismatch: {url} must start with {provider_url}", - )