Skip to content

Commit d47da7e

Browse files
patkasperbusunkim96
authored andcommitted
feat: add to_json method to google.oauth2.credentials.Credentials (#367)
1 parent 6c519c7 commit d47da7e

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

packages/google-auth/google/oauth2/credentials.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,33 @@ def from_authorized_user_file(cls, filename, scopes=None):
223223
with io.open(filename, "r", encoding="utf-8") as json_file:
224224
data = json.load(json_file)
225225
return cls.from_authorized_user_info(data, scopes)
226+
227+
def to_json(self, strip=None):
228+
"""Utility function that creates a JSON representation of a Credentials
229+
object.
230+
231+
Args:
232+
strip (Sequence[str]): Optional list of members to exclude from the
233+
generated JSON.
234+
235+
Returns:
236+
str: A JSON representation of this instance, suitable to pass to
237+
from_json().
238+
"""
239+
prep = {
240+
"token": self.token,
241+
"refresh_token": self.refresh_token,
242+
"token_uri": self.token_uri,
243+
"client_id": self.client_id,
244+
"client_secret": self.client_secret,
245+
"scopes": self.scopes,
246+
}
247+
248+
# Remove empty entries
249+
prep = {k: v for k, v in prep.items() if v is not None}
250+
251+
# Remove entries that explicitely need to be removed
252+
if strip is not None:
253+
prep = {k: v for k, v in prep.items() if k not in strip}
254+
255+
return json.dumps(prep)

packages/google-auth/tests/oauth2/test_credentials.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,27 @@ def test_from_authorized_user_file(self):
331331
assert creds.refresh_token == info["refresh_token"]
332332
assert creds.token_uri == credentials._GOOGLE_OAUTH2_TOKEN_ENDPOINT
333333
assert creds.scopes == scopes
334+
335+
def test_to_json(self):
336+
info = AUTH_USER_INFO.copy()
337+
creds = credentials.Credentials.from_authorized_user_info(info)
338+
339+
# Test with no `strip` arg
340+
json_output = creds.to_json()
341+
json_asdict = json.loads(json_output)
342+
assert json_asdict.get("token") == creds.token
343+
assert json_asdict.get("refresh_token") == creds.refresh_token
344+
assert json_asdict.get("token_uri") == creds.token_uri
345+
assert json_asdict.get("client_id") == creds.client_id
346+
assert json_asdict.get("scopes") == creds.scopes
347+
assert json_asdict.get("client_secret") == creds.client_secret
348+
349+
# Test with a `strip` arg
350+
json_output = creds.to_json(strip=["client_secret"])
351+
json_asdict = json.loads(json_output)
352+
assert json_asdict.get("token") == creds.token
353+
assert json_asdict.get("refresh_token") == creds.refresh_token
354+
assert json_asdict.get("token_uri") == creds.token_uri
355+
assert json_asdict.get("client_id") == creds.client_id
356+
assert json_asdict.get("scopes") == creds.scopes
357+
assert json_asdict.get("client_secret") is None

0 commit comments

Comments
 (0)