Skip to content

Commit

Permalink
Fix misbehaviour in store_oauth2_credentials
Browse files Browse the repository at this point in the history
The previous implementation was both needlessly complicated, and also had a
misbehaviour.

When writing new credentials to an already existing file, the file would
not get correctly truncated. This caused some previous data to remain if
the new data to be written was shorter than the current file contents.

It would then cause gmvault to fail with the following error upon the
next invocation:

=== Exception traceback ===
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/gmv/credential_utils.py", line 147, in read_oauth2_tok_sec
    oauth_result = json.load(oauth_file)
  File "/usr/lib64/python2.7/json/__init__.py", line 291, in load
    **kw)
  File "/usr/lib64/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.7/json/decoder.py", line 367, in decode
    raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 1 column 225 - line 1 column 237 (char 224 - 236)
  • Loading branch information
tobbez committed Mar 14, 2016
1 parent a759623 commit aa81392
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions src/gmv/credential_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,16 @@ def store_oauth2_credentials(cls, email, access_token, refresh_token, validity,
"""
store oauth_credentials
"""
oauth_file = '%s/%s.oauth2' % (gmvault_utils.get_home_dir_path(), email)

# Open a file
fdesc = os.open(oauth_file, os.O_RDWR|os.O_CREAT )
fobj = os.fdopen(fdesc, "w+")
oauth_file_path = '%s/%s.oauth2' % (gmvault_utils.get_home_dir_path(), email)

the_obj = { "access_token" : access_token,
"refresh_token" : refresh_token,
"validity" : validity,
"access_creation" : gmvault_utils.get_utcnow_epoch(),
"type" : type}

json.dump(the_obj, fobj)

fobj.close()
with open(oauth_file_path, 'w') as oauth_file:
json.dump(the_obj, oauth_file)

@classmethod
def read_oauth2_tok_sec(cls, email):
Expand Down

0 comments on commit aa81392

Please sign in to comment.