Exemple #1
0
    def create(self, pw_hash: str, salt: str, crypt_id: str) -> dict:
        """
        Create a Locker record in storage

        @param pw_hash: Hashed password, for auth, of new locker
        @param salt: Encryption salt for locker
        @param crypt_id: ID of the crypt_impl used by locker
        @return: record for newly persisted locker
        """
        self.store_path = Path(self.store_path)
        if self.locker_id:
            try:
                self.locker_path.mkdir(exist_ok=False)
            except FileExistsError as err:
                raise PhibesExistsError(err)
        else:
            if not can_create(self.locker_path, remove_if_empty=False):
                raise ValueError(f"could not create {self.locker_path}")
        try:
            self.get()
        except PhibesNotFoundError:
            pass
        phibes_file.write(
            pth=self.locker_file,
            salt=salt,
            crypt_id=crypt_id,
            timestamp=str(datetime.now()),
            body=pw_hash,
            overwrite=False
        )
        return self.get()
 def test_forbid_empty(self):
     with pytest.raises(AttributeError):
         phibes_file.write(self.pth,
                           salt=self.test_salt,
                           crypt_id=self.test_crypt_id,
                           timestamp=self.test_timestamp,
                           body="")
Exemple #3
0
 def save_item(
         self, item_id: str, item_rec: dict, replace: bool = False
 ) -> None:
     """
     Saves the item to the locker
     @param item_id: Encrypted item locker_id
     @param item_rec: contents of item
     @param replace: Whether this is replacing an existing item
     @return: None
     """
     try:
         self.get_item(item_id=item_id)
         if not replace:
             raise PhibesExistsError(f"{self.locker_id}:{item_id} exists")
     except PhibesNotFoundError as err:
         if replace:
             raise err
     item_path = self.locker_path / f"{item_id}.{ITEM_FILE_EXT}"
     phibes_file.write(
         pth=item_path,
         salt=item_rec['salt'],
         crypt_id=item_rec['crypt_id'],
         timestamp=item_rec['timestamp'],
         body=item_rec['_ciphertext'],
         overwrite=replace
     )
 def test_forbid_newlines(self):
     with pytest.raises(ValueError):
         phibes_file.write(self.pth,
                           salt=self.test_salt,
                           crypt_id=self.test_crypt_id,
                           timestamp=self.test_timestamp,
                           body=(f"{self.test_body}\n"
                                 f"{self.test_body}"))
Exemple #5
0
 def save(self, pth: Path, overwrite: bool = False):
     phibes_file.write(
         pth=pth,
         salt=self.salt,
         crypt_id=self.crypt_impl.crypt_id,
         timestamp=self.timestamp,
         body=self._ciphertext,
         overwrite=overwrite
     )
     return
 def test_write_new_read(self):
     phibes_file.write(self.pth,
                       salt=self.test_salt,
                       crypt_id=self.test_crypt_id,
                       timestamp=self.test_timestamp,
                       body=self.test_body)
     result = phibes_file.read(self.pth)
     assert result['body'] == self.test_body
     assert result['salt'] == self.test_salt
     assert result['crypt_id'] == self.test_crypt_id
     assert result['timestamp'] == self.test_timestamp
 def test_forbid_overwrite(self):
     phibes_file.write(self.pth,
                       salt=self.test_salt,
                       crypt_id=self.test_crypt_id,
                       timestamp=self.test_timestamp,
                       body=f"{self.test_body}")
     with pytest.raises(FileExistsError):
         phibes_file.write(self.pth,
                           salt=self.test_salt,
                           crypt_id=self.test_crypt_id,
                           timestamp=self.test_timestamp,
                           body=f"{self.test_body}")
 def test_allow_empty(self):
     phibes_file.write(self.pth,
                       salt=self.test_salt,
                       crypt_id=self.test_crypt_id,
                       timestamp=self.test_timestamp,
                       body="",
                       allow_empty=True,
                       overwrite=True)
     result = phibes_file.read(self.pth)
     assert result['salt'] == self.test_salt
     assert result['crypt_id'] == self.test_crypt_id
     assert result['timestamp'] == self.test_timestamp
     assert result['body'] == ''
 def test_allow_overwrite(self):
     phibes_file.write(self.pth,
                       salt=self.test_salt,
                       crypt_id=self.test_crypt_id,
                       timestamp=self.test_timestamp,
                       body=f"{self.test_body}",
                       overwrite=True)
     phibes_file.write(self.pth,
                       salt=self.test_salt,
                       crypt_id=self.test_crypt_id,
                       timestamp=self.test_timestamp,
                       body="replacement",
                       overwrite=True)
     result = phibes_file.read(self.pth)
     assert result['salt'] == self.test_salt
     assert result['crypt_id'] == self.test_crypt_id
     assert result['timestamp'] == self.test_timestamp
     assert result['body'] == 'replacement'