class EncryptedStorage(Storage): def __init__(self, filename, key): super(EncryptedStorage, self).__init__(filename) self.enc = SaltedAESString() self._key = key # --------------------------------------------------------------------- def locked_get(self): """Original:Retrieve Credential from file. !!! Subclassed and Overrided: Retrieve Credential from ENCRYPTED file !!! Returns: oauth2client.client.Credentials Raises: CredentialsFileSymbolicLinkError if the file is a symbolic link. """ credentials = None self._validate_file() try: f = open(self._filename, 'rb') content = self.enc.decrypt(self._key, f.read()) f.close() except IOError: return credentials try: credentials = Credentials.new_from_json(content) credentials.set_store(self) except ValueError: pass return credentials # --------------------------------------------------------------------- def locked_put(self, credentials): """Original: Write Credentials to file. !!! Subclassed and Overrided: Write Credentials to file with ENCRYPTION. !!! Args: credentials: Credentials, the credentials to store. Raises: CredentialsFileSymbolicLinkError if the file is a symbolic link. """ self._create_file_if_needed() self._validate_file() # this was 'wb' in python2 f = open(self._filename, 'wb') encrypted = self.enc.encrypt(self._key, credentials.to_json()) f.write(encrypted) f.close()
def __init__(self, filename, key): super(EncryptedStorage, self).__init__(filename) self.enc = SaltedAESString() self._key = key