Skip to content

fix: Accept Path-like objects in credential factory functions #510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions firebase_admin/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Firebase credentials module."""
import collections
import json
import pathlib

import google.auth
from google.auth.transport import requests
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
12 changes: 12 additions & 0 deletions tests/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import datetime
import json
import os
import pathlib

import google.auth
from google.auth import crypt
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down