Skip to content

Commit 0eca92b

Browse files
authored
chore: refactoring common used constant from the same source (#1472)
1 parent 84719cc commit 0eca92b

File tree

10 files changed

+52
-45
lines changed

10 files changed

+52
-45
lines changed

packages/google-auth/google/auth/external_account.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@
5151
# Cloud resource manager URL used to retrieve project information.
5252
_CLOUD_RESOURCE_MANAGER = "https://cloudresourcemanager.googleapis.com/v1/projects/"
5353

54-
_DEFAULT_UNIVERSE_DOMAIN = "googleapis.com"
55-
5654

5755
class Credentials(
5856
credentials.Scoped,
@@ -83,7 +81,7 @@ def __init__(
8381
scopes=None,
8482
default_scopes=None,
8583
workforce_pool_user_project=None,
86-
universe_domain=_DEFAULT_UNIVERSE_DOMAIN,
84+
universe_domain=credentials.DEFAULT_UNIVERSE_DOMAIN,
8785
trust_boundary=None,
8886
):
8987
"""Instantiates an external account credentials object.
@@ -131,7 +129,7 @@ def __init__(
131129
self._scopes = scopes
132130
self._default_scopes = default_scopes
133131
self._workforce_pool_user_project = workforce_pool_user_project
134-
self._universe_domain = universe_domain or _DEFAULT_UNIVERSE_DOMAIN
132+
self._universe_domain = universe_domain or credentials.DEFAULT_UNIVERSE_DOMAIN
135133
self._trust_boundary = {
136134
"locations": [],
137135
"encoded_locations": "0x0",
@@ -513,7 +511,9 @@ def from_info(cls, info, **kwargs):
513511
credential_source=info.get("credential_source"),
514512
quota_project_id=info.get("quota_project_id"),
515513
workforce_pool_user_project=info.get("workforce_pool_user_project"),
516-
universe_domain=info.get("universe_domain", _DEFAULT_UNIVERSE_DOMAIN),
514+
universe_domain=info.get(
515+
"universe_domain", credentials.DEFAULT_UNIVERSE_DOMAIN
516+
),
517517
**kwargs
518518
)
519519

packages/google-auth/google/auth/external_account_authorized_user.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
from google.oauth2 import sts
4444
from google.oauth2 import utils
4545

46-
_DEFAULT_UNIVERSE_DOMAIN = "googleapis.com"
4746
_EXTERNAL_ACCOUNT_AUTHORIZED_USER_JSON_TYPE = "external_account_authorized_user"
4847

4948

@@ -76,7 +75,7 @@ def __init__(
7675
revoke_url=None,
7776
scopes=None,
7877
quota_project_id=None,
79-
universe_domain=_DEFAULT_UNIVERSE_DOMAIN,
78+
universe_domain=credentials.DEFAULT_UNIVERSE_DOMAIN,
8079
):
8180
"""Instantiates a external account authorized user credentials object.
8281
@@ -120,7 +119,7 @@ def __init__(
120119
self._revoke_url = revoke_url
121120
self._quota_project_id = quota_project_id
122121
self._scopes = scopes
123-
self._universe_domain = universe_domain or _DEFAULT_UNIVERSE_DOMAIN
122+
self._universe_domain = universe_domain or credentials.DEFAULT_UNIVERSE_DOMAIN
124123

125124
if not self.valid and not self.can_refresh:
126125
raise exceptions.InvalidOperation(
@@ -342,7 +341,9 @@ def from_info(cls, info, **kwargs):
342341
revoke_url=info.get("revoke_url"),
343342
quota_project_id=info.get("quota_project_id"),
344343
scopes=info.get("scopes"),
345-
universe_domain=info.get("universe_domain", _DEFAULT_UNIVERSE_DOMAIN),
344+
universe_domain=info.get(
345+
"universe_domain", credentials.DEFAULT_UNIVERSE_DOMAIN
346+
),
346347
**kwargs
347348
)
348349

packages/google-auth/google/oauth2/credentials.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949

5050
# The Google OAuth 2.0 token endpoint. Used for authorized user credentials.
5151
_GOOGLE_OAUTH2_TOKEN_ENDPOINT = "https://oauth2.googleapis.com/token"
52-
_DEFAULT_UNIVERSE_DOMAIN = "googleapis.com"
5352

5453

5554
class Credentials(credentials.ReadOnlyScoped, credentials.CredentialsWithQuotaProject):
@@ -86,7 +85,7 @@ def __init__(
8685
enable_reauth_refresh=False,
8786
granted_scopes=None,
8887
trust_boundary=None,
89-
universe_domain=_DEFAULT_UNIVERSE_DOMAIN,
88+
universe_domain=credentials.DEFAULT_UNIVERSE_DOMAIN,
9089
account=None,
9190
):
9291
"""
@@ -150,7 +149,7 @@ def __init__(
150149
self.refresh_handler = refresh_handler
151150
self._enable_reauth_refresh = enable_reauth_refresh
152151
self._trust_boundary = trust_boundary
153-
self._universe_domain = universe_domain or _DEFAULT_UNIVERSE_DOMAIN
152+
self._universe_domain = universe_domain or credentials.DEFAULT_UNIVERSE_DOMAIN
154153
self._account = account or ""
155154

156155
def __getstate__(self):
@@ -187,7 +186,9 @@ def __setstate__(self, d):
187186
self._rapt_token = d.get("_rapt_token")
188187
self._enable_reauth_refresh = d.get("_enable_reauth_refresh")
189188
self._trust_boundary = d.get("_trust_boundary")
190-
self._universe_domain = d.get("_universe_domain") or _DEFAULT_UNIVERSE_DOMAIN
189+
self._universe_domain = (
190+
d.get("_universe_domain") or credentials.DEFAULT_UNIVERSE_DOMAIN
191+
)
191192
# The refresh_handler setter should be used to repopulate this.
192193
self._refresh_handler = None
193194
self._refresh_worker = None
@@ -373,7 +374,7 @@ def _metric_header_for_usage(self):
373374

374375
@_helpers.copy_docstring(credentials.Credentials)
375376
def refresh(self, request):
376-
if self._universe_domain != _DEFAULT_UNIVERSE_DOMAIN:
377+
if self._universe_domain != credentials.DEFAULT_UNIVERSE_DOMAIN:
377378
raise exceptions.RefreshError(
378379
"User credential refresh is only supported in the default "
379380
"googleapis.com universe domain, but the current universe "

packages/google-auth/google/oauth2/service_account.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@
8282
from google.oauth2 import _client
8383

8484
_DEFAULT_TOKEN_LIFETIME_SECS = 3600 # 1 hour in seconds
85-
_DEFAULT_UNIVERSE_DOMAIN = "googleapis.com"
8685
_GOOGLE_OAUTH2_TOKEN_ENDPOINT = "https://oauth2.googleapis.com/token"
8786

8887

@@ -139,7 +138,7 @@ def __init__(
139138
quota_project_id=None,
140139
additional_claims=None,
141140
always_use_jwt_access=False,
142-
universe_domain=_DEFAULT_UNIVERSE_DOMAIN,
141+
universe_domain=credentials.DEFAULT_UNIVERSE_DOMAIN,
143142
trust_boundary=None,
144143
):
145144
"""
@@ -182,9 +181,9 @@ def __init__(
182181
self._quota_project_id = quota_project_id
183182
self._token_uri = token_uri
184183
self._always_use_jwt_access = always_use_jwt_access
185-
self._universe_domain = universe_domain or _DEFAULT_UNIVERSE_DOMAIN
184+
self._universe_domain = universe_domain or credentials.DEFAULT_UNIVERSE_DOMAIN
186185

187-
if universe_domain != _DEFAULT_UNIVERSE_DOMAIN:
186+
if universe_domain != credentials.DEFAULT_UNIVERSE_DOMAIN:
188187
self._always_use_jwt_access = True
189188

190189
self._jwt_credentials = None
@@ -216,7 +215,9 @@ def _from_signer_and_info(cls, signer, info, **kwargs):
216215
service_account_email=info["client_email"],
217216
token_uri=info["token_uri"],
218217
project_id=info.get("project_id"),
219-
universe_domain=info.get("universe_domain", _DEFAULT_UNIVERSE_DOMAIN),
218+
universe_domain=info.get(
219+
"universe_domain", credentials.DEFAULT_UNIVERSE_DOMAIN
220+
),
220221
trust_boundary=info.get("trust_boundary"),
221222
**kwargs
222223
)
@@ -316,7 +317,7 @@ def with_always_use_jwt_access(self, always_use_jwt_access):
316317
"""
317318
cred = self._make_copy()
318319
if (
319-
cred._universe_domain != _DEFAULT_UNIVERSE_DOMAIN
320+
cred._universe_domain != credentials.DEFAULT_UNIVERSE_DOMAIN
320321
and not always_use_jwt_access
321322
):
322323
raise exceptions.InvalidValue(
@@ -329,7 +330,7 @@ def with_always_use_jwt_access(self, always_use_jwt_access):
329330
def with_universe_domain(self, universe_domain):
330331
cred = self._make_copy()
331332
cred._universe_domain = universe_domain
332-
if universe_domain != _DEFAULT_UNIVERSE_DOMAIN:
333+
if universe_domain != credentials.DEFAULT_UNIVERSE_DOMAIN:
333334
cred._always_use_jwt_access = True
334335
return cred
335336

@@ -427,7 +428,10 @@ def refresh(self, request):
427428
# created, try to create one with scopes
428429
self._create_self_signed_jwt(None)
429430

430-
if self._universe_domain != _DEFAULT_UNIVERSE_DOMAIN and self._subject:
431+
if (
432+
self._universe_domain != credentials.DEFAULT_UNIVERSE_DOMAIN
433+
and self._subject
434+
):
431435
raise exceptions.RefreshError(
432436
"domain wide delegation is not supported for non-default universe domain"
433437
)
@@ -556,7 +560,7 @@ def __init__(
556560
target_audience,
557561
additional_claims=None,
558562
quota_project_id=None,
559-
universe_domain=_DEFAULT_UNIVERSE_DOMAIN,
563+
universe_domain=credentials.DEFAULT_UNIVERSE_DOMAIN,
560564
):
561565
"""
562566
Args:
@@ -588,11 +592,11 @@ def __init__(
588592
self._use_iam_endpoint = False
589593

590594
if not universe_domain:
591-
self._universe_domain = _DEFAULT_UNIVERSE_DOMAIN
595+
self._universe_domain = credentials.DEFAULT_UNIVERSE_DOMAIN
592596
else:
593597
self._universe_domain = universe_domain
594598

595-
if universe_domain != _DEFAULT_UNIVERSE_DOMAIN:
599+
if universe_domain != credentials.DEFAULT_UNIVERSE_DOMAIN:
596600
self._use_iam_endpoint = True
597601

598602
if additional_claims is not None:
@@ -708,7 +712,10 @@ def _with_use_iam_endpoint(self, use_iam_endpoint):
708712
default and use_iam_endpoint is False.
709713
"""
710714
cred = self._make_copy()
711-
if cred._universe_domain != _DEFAULT_UNIVERSE_DOMAIN and not use_iam_endpoint:
715+
if (
716+
cred._universe_domain != credentials.DEFAULT_UNIVERSE_DOMAIN
717+
and not use_iam_endpoint
718+
):
712719
raise exceptions.InvalidValue(
713720
"use_iam_endpoint should be True for non-default universe domain"
714721
)

packages/google-auth/tests/oauth2/test_service_account.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from google.auth import exceptions
2525
from google.auth import jwt
2626
from google.auth import transport
27+
from google.auth.credentials import DEFAULT_UNIVERSE_DOMAIN
2728
from google.oauth2 import service_account
2829

2930

@@ -58,7 +59,7 @@ class TestCredentials(object):
5859
TOKEN_URI = "https://example.com/oauth2/token"
5960

6061
@classmethod
61-
def make_credentials(cls, universe_domain=service_account._DEFAULT_UNIVERSE_DOMAIN):
62+
def make_credentials(cls, universe_domain=DEFAULT_UNIVERSE_DOMAIN):
6263
return service_account.Credentials(
6364
SIGNER,
6465
cls.SERVICE_ACCOUNT_EMAIL,
@@ -70,7 +71,7 @@ def test_constructor_no_universe_domain(self):
7071
credentials = service_account.Credentials(
7172
SIGNER, self.SERVICE_ACCOUNT_EMAIL, self.TOKEN_URI, universe_domain=None
7273
)
73-
assert credentials.universe_domain == service_account._DEFAULT_UNIVERSE_DOMAIN
74+
assert credentials.universe_domain == DEFAULT_UNIVERSE_DOMAIN
7475

7576
def test_from_service_account_info(self):
7677
credentials = service_account.Credentials.from_service_account_info(
@@ -80,7 +81,7 @@ def test_from_service_account_info(self):
8081
assert credentials._signer.key_id == SERVICE_ACCOUNT_INFO["private_key_id"]
8182
assert credentials.service_account_email == SERVICE_ACCOUNT_INFO["client_email"]
8283
assert credentials._token_uri == SERVICE_ACCOUNT_INFO["token_uri"]
83-
assert credentials._universe_domain == service_account._DEFAULT_UNIVERSE_DOMAIN
84+
assert credentials._universe_domain == DEFAULT_UNIVERSE_DOMAIN
8485
assert not credentials._always_use_jwt_access
8586

8687
def test_from_service_account_info_non_gdu(self):
@@ -595,7 +596,7 @@ class TestIDTokenCredentials(object):
595596
TARGET_AUDIENCE = "https://example.com"
596597

597598
@classmethod
598-
def make_credentials(cls, universe_domain=service_account._DEFAULT_UNIVERSE_DOMAIN):
599+
def make_credentials(cls, universe_domain=DEFAULT_UNIVERSE_DOMAIN):
599600
return service_account.IDTokenCredentials(
600601
SIGNER,
601602
cls.SERVICE_ACCOUNT_EMAIL,
@@ -612,7 +613,7 @@ def test_constructor_no_universe_domain(self):
612613
self.TARGET_AUDIENCE,
613614
universe_domain=None,
614615
)
615-
assert credentials._universe_domain == service_account._DEFAULT_UNIVERSE_DOMAIN
616+
assert credentials._universe_domain == DEFAULT_UNIVERSE_DOMAIN
616617

617618
def test_from_service_account_info(self):
618619
credentials = service_account.IDTokenCredentials.from_service_account_info(

packages/google-auth/tests/test_aws.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from google.auth import environment_vars
2727
from google.auth import exceptions
2828
from google.auth import transport
29-
29+
from google.auth.credentials import DEFAULT_UNIVERSE_DOMAIN
3030

3131
IMPERSONATE_ACCESS_TOKEN_REQUEST_METRICS_HEADER_VALUE = (
3232
"gl-python/3.7 auth/1.1 auth-request-type/at cred-type/imp"
@@ -75,7 +75,6 @@
7575
# Each tuple contains the following entries:
7676
# region, time, credentials, original_request, signed_request
7777

78-
DEFAULT_UNIVERSE_DOMAIN = "googleapis.com"
7978
VALID_TOKEN_URLS = [
8079
"https://sts.googleapis.com",
8180
"https://us-east-1.sts.googleapis.com",

packages/google-auth/tests/test_external_account.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
from google.auth import exceptions
2525
from google.auth import external_account
2626
from google.auth import transport
27+
from google.auth.credentials import DEFAULT_UNIVERSE_DOMAIN
2728
from google.auth.credentials import TokenState
2829

29-
3030
IMPERSONATE_ACCESS_TOKEN_REQUEST_METRICS_HEADER_VALUE = (
3131
"gl-python/3.7 auth/1.1 auth-request-type/at cred-type/imp"
3232
)
@@ -150,7 +150,7 @@ def make_credentials(
150150
default_scopes=None,
151151
service_account_impersonation_url=None,
152152
service_account_impersonation_options={},
153-
universe_domain=external_account._DEFAULT_UNIVERSE_DOMAIN,
153+
universe_domain=DEFAULT_UNIVERSE_DOMAIN,
154154
):
155155
return CredentialsImpl(
156156
audience=cls.AUDIENCE,
@@ -386,7 +386,7 @@ def test_with_scopes_full_options_propagated(self):
386386
quota_project_id=self.QUOTA_PROJECT_ID,
387387
scopes=["email"],
388388
default_scopes=["default2"],
389-
universe_domain=external_account._DEFAULT_UNIVERSE_DOMAIN,
389+
universe_domain=DEFAULT_UNIVERSE_DOMAIN,
390390
)
391391

392392
def test_with_token_uri(self):
@@ -474,7 +474,7 @@ def test_with_quota_project_full_options_propagated(self):
474474
quota_project_id="project-foo",
475475
scopes=self.SCOPES,
476476
default_scopes=["default1"],
477-
universe_domain=external_account._DEFAULT_UNIVERSE_DOMAIN,
477+
universe_domain=DEFAULT_UNIVERSE_DOMAIN,
478478
)
479479

480480
def test_with_invalid_impersonation_target_principal(self):
@@ -504,7 +504,7 @@ def test_universe_domain(self):
504504
assert credentials.universe_domain == "dummy_universe.com"
505505

506506
credentials = self.make_credentials()
507-
assert credentials.universe_domain == external_account._DEFAULT_UNIVERSE_DOMAIN
507+
assert credentials.universe_domain == DEFAULT_UNIVERSE_DOMAIN
508508

509509
def test_with_universe_domain(self):
510510
credentials = self.make_credentials()
@@ -523,7 +523,7 @@ def test_info_workforce_pool(self):
523523
"token_url": self.TOKEN_URL,
524524
"credential_source": self.CREDENTIAL_SOURCE.copy(),
525525
"workforce_pool_user_project": self.WORKFORCE_POOL_USER_PROJECT,
526-
"universe_domain": external_account._DEFAULT_UNIVERSE_DOMAIN,
526+
"universe_domain": DEFAULT_UNIVERSE_DOMAIN,
527527
}
528528

529529
def test_info_with_full_options(self):
@@ -548,7 +548,7 @@ def test_info_with_full_options(self):
548548
"quota_project_id": self.QUOTA_PROJECT_ID,
549549
"client_id": CLIENT_ID,
550550
"client_secret": CLIENT_SECRET,
551-
"universe_domain": external_account._DEFAULT_UNIVERSE_DOMAIN,
551+
"universe_domain": DEFAULT_UNIVERSE_DOMAIN,
552552
}
553553

554554
def test_service_account_email_without_impersonation(self):

packages/google-auth/tests/test_external_account_authorized_user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from google.auth import exceptions
2323
from google.auth import external_account_authorized_user
2424
from google.auth import transport
25+
from google.auth.credentials import DEFAULT_UNIVERSE_DOMAIN
2526

2627
TOKEN_URL = "https://sts.googleapis.com/v1/token"
2728
TOKEN_INFO_URL = "https://sts.googleapis.com/v1/introspect"
@@ -45,7 +46,6 @@
4546
SCOPES = ["email", "profile"]
4647
NOW = datetime.datetime(1990, 8, 27, 6, 54, 30)
4748
FAKE_UNIVERSE_DOMAIN = "fake-universe-domain"
48-
DEFAULT_UNIVERSE_DOMAIN = external_account_authorized_user._DEFAULT_UNIVERSE_DOMAIN
4949

5050

5151
class TestCredentials(object):

packages/google-auth/tests/test_identity_pool.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from google.auth import identity_pool
2727
from google.auth import metrics
2828
from google.auth import transport
29-
29+
from google.auth.credentials import DEFAULT_UNIVERSE_DOMAIN
3030

3131
CLIENT_ID = "username"
3232
CLIENT_SECRET = "password"
@@ -67,8 +67,6 @@
6767
WORKFORCE_SUBJECT_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:id_token"
6868
WORKFORCE_POOL_USER_PROJECT = "WORKFORCE_POOL_USER_PROJECT_NUMBER"
6969

70-
DEFAULT_UNIVERSE_DOMAIN = "googleapis.com"
71-
7270
VALID_TOKEN_URLS = [
7371
"https://sts.googleapis.com",
7472
"https://us-east-1.sts.googleapis.com",

0 commit comments

Comments
 (0)