diff --git a/packages/google-auth/google/auth/_helpers.py b/packages/google-auth/google/auth/_helpers.py index 5599f2004f85..08c7bf0bbe2b 100644 --- a/packages/google-auth/google/auth/_helpers.py +++ b/packages/google-auth/google/auth/_helpers.py @@ -298,6 +298,31 @@ def validate_universe_domain(universe_domain: Any) -> None: ) +def compare_universe_domains( + universe_domain: Any, expected_universe_domain: Any +) -> None: + """Verifies that ``universe_domain`` matches ``expected_universe_domain``. + + Performs a case-insensitive comparison after validating both domain strings. + + Args: + universe_domain (Any): The configured universe domain. + expected_universe_domain (Any): The expected universe domain (for + example, from wrapped source credentials). + + Raises: + google.auth.exceptions.InvalidValue: If either universe domain is + invalid or the two universe domains do not match. + """ + validate_universe_domain(universe_domain) + validate_universe_domain(expected_universe_domain) + if universe_domain.lower() != expected_universe_domain.lower(): + raise exceptions.InvalidValue( + f"The configured universe domain ({universe_domain}) does not " + f"match the expected universe domain ({expected_universe_domain})." + ) + + def scopes_to_string(scopes): """Converts scope value to a string suitable for sending to OAuth 2.0 authorization servers. diff --git a/packages/google-auth/tests/test__helpers.py b/packages/google-auth/tests/test__helpers.py index 41fd4befb71c..94dcbb720842 100644 --- a/packages/google-auth/tests/test__helpers.py +++ b/packages/google-auth/tests/test__helpers.py @@ -798,3 +798,46 @@ def test_validate_universe_domain_invalid(invalid_value): match="Universe domain must be a valid non-empty domain string", ): _helpers.validate_universe_domain(invalid_value) + + +@pytest.mark.parametrize( + "universe_domain,expected_universe_domain", + [ + ("example.com", "example.com"), + ("Googleapis.com", "googleapis.com"), + ("googleapis.com", "GOOGLEAPIS.COM"), + ("My-Universe.Example.Com", "my-universe.example.com"), + ], +) +def test_compare_universe_domains_match(universe_domain, expected_universe_domain): + _helpers.compare_universe_domains(universe_domain, expected_universe_domain) + + +def test_compare_universe_domains_mismatch(): + with pytest.raises( + exceptions.InvalidValue, + match=r"The configured universe domain \(Example\.com\) does not " + r"match the expected universe domain \(googleapis\.com\)\.", + ): + _helpers.compare_universe_domains("Example.com", "googleapis.com") + + +@pytest.mark.parametrize( + "universe_domain,expected_universe_domain", + [ + (None, None), + ("", ""), + (" ", " "), + ("example.com", None), + (None, "example.com"), + ("example.com", " "), + ], +) +def test_compare_universe_domains_invalid_inputs( + universe_domain, expected_universe_domain +): + with pytest.raises( + exceptions.InvalidValue, + match="Universe domain must be a valid non-empty domain string", + ): + _helpers.compare_universe_domains(universe_domain, expected_universe_domain)