8000 fix: Accept Path-like objects in credential factory functions (#510) · sssaang/firebase-admin-python@dcd3a86 · GitHub
[go: up one dir, main page]

Skip to content

Commit dcd3a86

Browse files
authored
fix: Accept Path-like objects in credential factory functions (firebase#510)
* fix: Accept Path-like objects in credential factory functions * chore: Trigger CI
1 parent 939375c commit dcd3a86

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

firebase_admin/credentials.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Firebase credentials module."""
1616
import collections
1717
import json
18+
import pathlib
1819

1920
import google.auth
2021
from google.auth.transport import requests
@@ -78,7 +79,7 @@ def __init__(self, cert):
7879
ValueError: If the specified certificate is invalid.
7980
"""
8081
super(Certificate, self).__init__()
81-
if isinstance(cert, str):
82+
if _is_file_path(cert):
8283
with open(cert) as json_file:
8384
json_data = json.load(json_file)
8485
elif isinstance(cert, dict):
@@ -179,7 +180,7 @@ def __init__(self, refresh_token):
179180
ValueError: If the refresh token configuration is invalid.
180181
"""
181182
super(RefreshToken, self).__init__()
182-
if isinstance(refresh_token, str):
183+
if _is_file_path(refresh_token):
183184
with open(refresh_token) as json_file:
184185
json_data = json.load(json_file)
185186
elif isinstance(refresh_token, dict):
@@ -212,3 +213,11 @@ def get_credential(self):
212213
Returns:
213214
google.auth.credentials.Credentials: A Google Auth credential instance."""
214215
return self._g_credential
216+
217+
218+
def _is_file_path(path):
219+
try:
220+
pathlib.Path(path)
221+
return True
222+
except TypeError:
223+
return False

tests/test_credentials.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import datetime
1717
import json
1818
import os
19+
import pathlib
1920

2021
import google.auth
2122
from google.auth import crypt
@@ -47,6 +48,12 @@ def test_init_from_file(self):
4748
testutils.resource_filename('service_account.json'))
4849
self._verify_credential(credential)
4950

51+
def test_init_from_path_like(self):
52+
path = pathlib.Path(testutils.resource_filename('service_account.json'))
53+
credential = credentials.Certificate(path)
54+
self._verify_credential(credential)
55+
56+
5057
def test_init_from_dict(self):
5158
parsed_json = json.loads(testutils.resource('service_account.json'))
5259
credential = credentials.Certificate(parsed_json)
@@ -129,6 +136,11 @@ def test_init_from_file(self):
129136
testutils.resource_filename('refresh_token.json'))
130137
self._verify_credential(credential)
131138

139+
def test_init_from_path_like(self):
140+
path = pathlib.Path(testutils.resource_filename('refresh_token.json'))
141+
credential = credentials.RefreshToken(path)
142+
self._verify_credential(credential)
143+
132144
def test_init_from_dict(self):
133145
parsed_json = json.loads(testutils.resource('refresh_token.json'))
134146
credential = credentials.RefreshToken(parsed_json)

0 commit comments

Comments
 (0)
0