Skip to content

Commit

Permalink
Allow use of loading credentials from file
Browse files Browse the repository at this point in the history
  • Loading branch information
devonh committed Jun 10, 2024
1 parent a4797c3 commit fccaa44
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions sygnal/gcmpushkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# https://github.com/googleapis/google-auth-library-python/issues/613
import aiohttp
import google.auth.transport._aiohttp_requests
from google.auth._default_async import default_async
from google.auth._default_async import default_async, load_credentials_from_file
from google.oauth2._credentials_async import Credentials
from opentracing import Span, logs, tags
from prometheus_client import Counter, Gauge, Histogram
Expand Down Expand Up @@ -126,6 +126,7 @@ class GcmPushkin(ConcurrencyLimitedPushkin):
"fcm_options",
"max_connections",
"project_id",
"service_account_file",
} | ConcurrencyLimitedPushkin.UNDERSTOOD_CONFIG_FIELDS

def __init__(self, name: str, sygnal: "Sygnal", config: Dict[str, Any]) -> None:
Expand Down Expand Up @@ -200,12 +201,24 @@ def _load_credentials(self, proxy_url: Optional[str]) -> None:
self.credentials: Optional[Credentials] = None

if self.api_version is APIVersion.V1:
try:
self.credentials, _ = default_async(scopes=AUTH_SCOPES)
except google.auth.exceptions.DefaultCredentialsError as e:
raise PushkinSetupException(
f"Failed loading google credentials: {str(e)}",
)
self.service_account_file = self.get_config("service_account_file", str)
if self.service_account_file:
try:
self.credentials, _ = load_credentials_from_file(
str(self.service_account_file),
scopes=AUTH_SCOPES,
)
except google.auth.exceptions.DefaultCredentialsError as e:
raise PushkinSetupException(
f"`service_account_file` must be valid: {str(e)}",
)
else:
try:
self.credentials, _ = default_async(scopes=AUTH_SCOPES)
except google.auth.exceptions.DefaultCredentialsError as e:
raise PushkinSetupException(
f"Failed loading google credentials: {str(e)}",
)

session = None
if proxy_url:
Expand Down

0 comments on commit fccaa44

Please sign in to comment.