diff --git a/firebase_admin/credentials.py b/firebase_admin/credentials.py index 1f207e483..5477e1cf7 100644 --- a/firebase_admin/credentials.py +++ b/firebase_admin/credentials.py @@ -15,6 +15,7 @@ """Firebase credentials module.""" import collections import json +import pathlib import google.auth from google.auth.transport import requests @@ -78,7 +79,7 @@ def __init__(self, cert): ValueError: If the specified certificate is invalid. """ super(Certificate, self).__init__() - if isinstance(cert, str): + if _is_file_path(cert): with open(cert) as json_file: json_data = json.load(json_file) elif isinstance(cert, dict): @@ -179,7 +180,7 @@ def __init__(self, refresh_token): ValueError: If the refresh token configuration is invalid. """ super(RefreshToken, self).__init__() - if isinstance(refresh_token, str): + if _is_file_path(refresh_token): with open(refresh_token) as json_file: json_data = json.load(json_file) elif isinstance(refresh_token, dict): @@ -212,3 +213,11 @@ def get_credential(self): Returns: google.auth.credentials.Credentials: A Google Auth credential instance.""" return self._g_credential + + +def _is_file_path(path): + try: + pathlib.Path(path) + return True + except TypeError: + return False diff --git a/tests/test_credentials.py b/tests/test_credentials.py index d78ef5192..cceb6b6f9 100644 --- a/tests/test_credentials.py +++ b/tests/test_credentials.py @@ -16,6 +16,7 @@ import datetime import json import os +import pathlib import google.auth from google.auth import crypt @@ -47,6 +48,12 @@ def test_init_from_file(self): testutils.resource_filename('service_account.json')) self._verify_credential(credential) + def test_init_from_path_like(self): + path = pathlib.Path(testutils.resource_filename('service_account.json')) + credential = credentials.Certificate(path) + self._verify_credential(credential) + + def test_init_from_dict(self): parsed_json = json.loads(testutils.resource('service_account.json')) credential = credentials.Certificate(parsed_json) @@ -129,6 +136,11 @@ def test_init_from_file(self): testutils.resource_filename('refresh_token.json')) self._verify_credential(credential) + def test_init_from_path_like(self): + path = pathlib.Path(testutils.resource_filename('refresh_token.json')) + credential = credentials.RefreshToken(path) + self._verify_credential(credential) + def test_init_from_dict(self): parsed_json = json.loads(testutils.resource('refresh_token.json')) credential = credentials.RefreshToken(parsed_json)