Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def create_collection(connection: DBusConnection, label: str, alias: str = '',
session: Optional[Session] = None) -> Collection:
"""Creates a new :class:`Collection` with the given `label` and `alias`
and returns it. This action requires prompting.
:raises: :exc:`~secretstorage.exceptions.PromptDismissedException`
if the prompt is dismissed.
"""
if not session:
session = open_session(connection)
properties = {SS_PREFIX + 'Collection.Label': ('s', label)}
service = DBusAddressWrapper(SS_PATH, SERVICE_IFACE, connection)
collection_path, prompt = service.call('CreateCollection', 'a{sv}s',
properties, alias)
if len(collection_path) > 1:
return Collection(connection, collection_path, session=session)
dismissed, result = exec_prompt(connection, prompt)
if dismissed:
raise PromptDismissedException('Prompt dismissed.')
signature, collection_path = result
assert signature == 'o'
return Collection(connection, collection_path, session=session)
def set_secret(self, secret: bytes,
content_type: str = 'text/plain') -> None:
"""Sets item secret to `secret`. If `content_type` is given,
also sets the content type of the secret (``text/plain`` by
default)."""
self.ensure_not_locked()
if not self.session:
self.session = open_session(self.connection)
_secret = format_secret(self.session, secret, content_type)
self._item.call('SetSecret', '(oayays)', _secret)
def get_secret_content_type(self):
"""Returns content type of item secret (string)."""
self.ensure_not_locked()
if not self.session:
self.session = open_session(self.bus)
secret = self.item_iface.GetSecret(self.session.object_path,
signature='o')
return str(secret[3])
def get_secret(self):
"""Returns item secret (bytestring)."""
self.ensure_not_locked()
if not self.session:
self.session = open_session(self.bus)
secret = self.item_iface.GetSecret(self.session.object_path,
signature='o')
if not self.session.encrypted:
return bytes(bytearray(secret[2]))
aes_iv = bytes(bytearray(secret[1]))
aes = Cipher.AES.new(self.session.aes_key, Cipher.AES.MODE_CBC, aes_iv)
encrypted_secret = bytes(bytearray(secret[2]))
padded_secret = aes.decrypt(encrypted_secret)
padded_secret = bytearray(padded_secret)
return bytes(padded_secret[:-padded_secret[-1]])
def create_item(self, label: str, attributes: Dict[str, str],
secret: bytes, replace: bool = False,
content_type: str = 'text/plain') -> Item:
"""Creates a new :class:`~secretstorage.item.Item` with given
`label` (unicode string), `attributes` (dictionary) and `secret`
(bytestring). If `replace` is :const:`True`, replaces the existing
item with the same attributes. If `content_type` is given, also
sets the content type of the secret (``text/plain`` by default).
Returns the created item."""
self.ensure_not_locked()
if not self.session:
self.session = open_session(self.connection)
_secret = format_secret(self.session, secret, content_type)
properties = {
SS_PREFIX + 'Item.Label': ('s', label),
SS_PREFIX + 'Item.Attributes': ('a{ss}', attributes),
}
new_item, prompt = self._collection.call('CreateItem', 'a{sv}(oayays)b',
properties, _secret, replace)
return Item(self.connection, new_item, self.session)
def create_collection(bus, label, alias='', session=None):
"""Creates a new :class:`Collection` with the given `label` and `alias`
and returns it. This action requires prompting. If prompt is dismissed,
raises :exc:`~secretstorage.exceptions.ItemNotFoundException`. This is
synchronous function, uses loop from GLib API."""
if not session:
session = open_session(bus)
properties = {SS_PREFIX+'Collection.Label': label}
service_obj = bus_get_object(bus, SS_PATH)
service_iface = dbus.Interface(service_obj, SERVICE_IFACE)
collection_path, prompt = service_iface.CreateCollection(properties,
alias, signature='a{sv}s')
if len(collection_path) > 1:
return Collection(bus, collection_path, session=session)
dismissed, unlocked = exec_prompt_glib(bus, prompt)
if dismissed:
raise ItemNotFoundException('Prompt dismissed.')
return Collection(bus, unlocked, session=session)
def set_secret(self, secret, content_type='text/plain'):
"""Sets item secret to `secret`. If `content_type` is given,
also sets the content type of the secret (``text/plain`` by
default)."""
self.ensure_not_locked()
if not self.session:
self.session = open_session(self.bus)
secret = format_secret(self.session, secret, content_type)
self.item_iface.SetSecret(secret, signature='(oayays)')
def get_secret(self) -> bytes:
"""Returns item secret (bytestring)."""
self.ensure_not_locked()
if not self.session:
self.session = open_session(self.connection)
secret, = self._item.call('GetSecret', 'o', self.session.object_path)
if not self.session.encrypted:
return bytes(secret[2])
assert self.session.aes_key is not None
aes = algorithms.AES(self.session.aes_key)
aes_iv = bytes(secret[1])
decryptor = Cipher(aes, modes.CBC(aes_iv), default_backend()).decryptor()
encrypted_secret = secret[2]
padded_secret = decryptor.update(bytes(encrypted_secret)) + decryptor.finalize()
assert isinstance(padded_secret, bytes)
return padded_secret[:-padded_secret[-1]]