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
25 changes: 25 additions & 0 deletions packages/google-auth/google/auth/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
43 changes: 43 additions & 0 deletions packages/google-auth/tests/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading