feat: make ``load_credentials_from_file`` a public method (#530)
* feat: make load_credentials_from_file public and alllow scopes
* test: update tests
* fix: raise error for json with no type
* test: fix test names
* refactor: simplify control flow
* fix: raise coverage
* test: update test
Co-authored-by: arithmetic1728 <58957152+arithmetic1728@users.noreply.github.com>
Co-authored-by: Sijun Liu <sijunliu@google.com>
diff --git a/google/auth/_default.py b/google/auth/_default.py
index 559695e..d3bbbda 100644
--- a/google/auth/_default.py
+++ b/google/auth/_default.py
@@ -69,14 +69,17 @@
warnings.warn(_CLOUD_SDK_CREDENTIALS_WARNING)
-def _load_credentials_from_file(filename):
- """Loads credentials from a file.
+def load_credentials_from_file(filename, scopes=None):
+ """Loads Google credentials from a file.
The credentials file must be a service account key or stored authorized
user credentials.
Args:
filename (str): The full path to the credentials file.
+ scopes (Optional[Sequence[str]]): The list of scopes for the credentials. If
+ specified, the credentials will automatically be scoped if
+ necessary.
Returns:
Tuple[google.auth.credentials.Credentials, Optional[str]]: Loaded
@@ -109,7 +112,9 @@
from google.oauth2 import credentials
try:
- credentials = credentials.Credentials.from_authorized_user_info(info)
+ credentials = credentials.Credentials.from_authorized_user_info(
+ info, scopes=scopes
+ )
except ValueError as caught_exc:
msg = "Failed to load authorized user credentials from {}".format(filename)
new_exc = exceptions.DefaultCredentialsError(msg, caught_exc)
@@ -122,7 +127,9 @@
from google.oauth2 import service_account
try:
- credentials = service_account.Credentials.from_service_account_info(info)
+ credentials = service_account.Credentials.from_service_account_info(
+ info, scopes=scopes
+ )
except ValueError as caught_exc:
msg = "Failed to load service account credentials from {}".format(filename)
new_exc = exceptions.DefaultCredentialsError(msg, caught_exc)
@@ -148,7 +155,7 @@
if not os.path.isfile(credentials_filename):
return None, None
- credentials, project_id = _load_credentials_from_file(credentials_filename)
+ credentials, project_id = load_credentials_from_file(credentials_filename)
if not project_id:
project_id = _cloud_sdk.get_project_id()
@@ -162,7 +169,7 @@
explicit_file = os.environ.get(environment_vars.CREDENTIALS)
if explicit_file is not None:
- credentials, project_id = _load_credentials_from_file(
+ credentials, project_id = load_credentials_from_file(
os.environ[environment_vars.CREDENTIALS]
)