aboutsummaryrefslogtreecommitdiff
path: root/google
diff options
context:
space:
mode:
authorBu Sun Kim <8822365+busunkim96@users.noreply.github.com>2020-06-18 14:05:40 -0700
committerGitHub <noreply@github.com>2020-06-18 14:05:40 -0700
commit15d5fa946177581b52a5a9eb3ca285c088f5c45d (patch)
tree3afb613a3f546652875c52fd47d20a63831094f6 /google
parentf30b45a9b2f824c494724548732c5ce838218c30 (diff)
downloadgoogle-auth-library-python-15d5fa946177581b52a5a9eb3ca285c088f5c45d.tar.gz
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>
Diffstat (limited to 'google')
-rw-r--r--google/auth/__init__.py4
-rw-r--r--google/auth/_default.py19
2 files changed, 15 insertions, 8 deletions
diff --git a/google/auth/__init__.py b/google/auth/__init__.py
index 6b4b78b..5ca20a3 100644
--- a/google/auth/__init__.py
+++ b/google/auth/__init__.py
@@ -16,10 +16,10 @@
import logging
-from google.auth._default import default
+from google.auth._default import default, load_credentials_from_file
-__all__ = ["default"]
+__all__ = ["default", "load_credentials_from_file"]
# Set default logging handler to avoid "No handler found" warnings.
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 @@ def _warn_about_problematic_credentials(credentials):
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 @@ def _load_credentials_from_file(filename):
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 @@ def _load_credentials_from_file(filename):
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 @@ def _get_gcloud_sdk_credentials():
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 @@ def _get_explicit_environ_credentials():
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]
)