Skip to content

Commit bafd763

Browse files
authored
Scoping credentials to Firebase services (#23)
* Scoping credentials to Firebase services * Fixed variable name * Updated test to compare lists
1 parent 54089a6 commit bafd763

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

firebase_admin/credentials.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323

2424

2525
_request = requests.Request()
26-
26+
_scopes = [
27+
'https://www.googleapis.com/auth/firebase',
28+
'https://www.googleapis.com/auth/userinfo.email'
29+
]
2730

2831
AccessTokenInfo = collections.namedtuple(
2932
'AccessTokenInfo', ['access_token', 'expiry'])
@@ -67,7 +70,8 @@ def __init__(self, file_path):
6770
'"type" field set to "{1}".'.format(file_path, self._CREDENTIAL_TYPE))
6871
self._project_id = json_data.get('project_id')
6972
try:
70-
self._g_credential = service_account.Credentials.from_service_account_info(json_data)
73+
self._g_credential = service_account.Credentials.from_service_account_info(
74+
json_data, scopes=_scopes)
7175
except ValueError as error:
7276
raise ValueError('Failed to initialize a certificate credential from file "{0}". '
7377
'Caused by: "{1}"'.format(file_path, error))
@@ -112,7 +116,7 @@ def __init__(self):
112116
credentials cannot be initialized in the current environment.
113117
"""
114118
super(ApplicationDefault, self).__init__()
115-
self._g_credential, self._project_id = google.auth.default()
119+
self._g_credential, self._project_id = google.auth.default(scopes=_scopes)
116120

117121
def get_access_token(self):
118122
"""Fetches a Google OAuth2 access token using this application default credential.
@@ -166,7 +170,7 @@ def __init__(self, file_path):
166170
self._g_credential = credentials.Credentials(
167171
token=None, refresh_token=refresh_token,
168172
token_uri='https://accounts.google.com/o/oauth2/token',
169-
client_id=client_id, client_secret=client_secret)
173+
client_id=client_id, client_secret=client_secret, scopes=_scopes)
170174

171175
@property
172176
def client_id(self):

tests/test_credentials.py

+8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
from tests import testutils
2828

2929

30+
def check_scopes(g_credential):
31+
assert isinstance(g_credential, google.auth.credentials.Scoped)
32+
assert sorted(credentials._scopes) == sorted(g_credential.scopes)
33+
34+
3035
class TestCertificate(object):
3136

3237
invalid_certs = {
@@ -46,6 +51,7 @@ def test_init_from_file(self):
4651
g_credential = credential.get_credential()
4752
assert isinstance(g_credential, service_account.Credentials)
4853
assert g_credential.token is None
54+
check_scopes(g_credential)
4955

5056
mock_response = {'access_token': 'mock_access_token', 'expires_in': 3600}
5157
credentials._request = testutils.MockRequest(200, json.dumps(mock_response))
@@ -82,6 +88,7 @@ def test_init(self, app_default): # pylint: disable=unused-argument
8288
g_credential = credential.get_credential()
8389
assert isinstance(g_credential, google.auth.credentials.Credentials)
8490
assert g_credential.token is None
91+
check_scopes(g_credential)
8592

8693
mock_response = {'access_token': 'mock_access_token', 'expires_in': 3600}
8794
credentials._request = testutils.MockRequest(200, json.dumps(mock_response))
@@ -108,6 +115,7 @@ def test_init_from_file(self):
108115
g_credential = credential.get_credential()
109116
assert isinstance(g_credential, gcredentials.Credentials)
110117
assert g_credential.token is None
118+
check_scopes(g_credential)
111119

112120
mock_response = {
113121
'access_token': 'mock_access_token',

0 commit comments

Comments
 (0)