Giter VIP home page Giter VIP logo

dbus.services.secrets's Introduction

DBus.Services.Secrets

High-level .NET bindings for the D-Bus Secret Service API.

These bindings were made using Tmds.DBus.Protocol and Tmds.DBus.SourceGenerator, making them trimmer and AOT friendly.

Requirements

The bindings target .NET 6.0, .NET 7.0 and .NET 8.0.

Basic Usage

To get started, add a reference to the Ace4896.DBus.Services.Secrets NuGet package.

The example below shows how to create and retrieve a secret value in the default collection:

// Connect to the D-Bus Secret Service API
// Sessions can use either plaintext or encrypted transport
SecretService secretService = await SecretService.ConnectAsync(EncryptionType.Dh);  // DH Key Agreement for Encryption

// Items are stored in within collections
// Collections can be retrieved using their alias
// Note that collection retrieval can fail, so this would need to be handled
Collection? defaultCollection = await secretService.GetDefaultCollectionAsync();
if (defaultCollection == null)
{
    // ... handle case where collection is not found
}

// Items are created with the following:
// - Label - The displayed label in e.g. GNOME Keyring, KWallet etc.
// - Lookup Attributes - These are used to search for the item later
// - Secret - The secret value as a byte array
// - Content Type - A content type hint for the secret value
string itemLabel = "MySecretValue";
Dictionary<string, string> lookupAttributes = new()
{
    { "my-lookup-attribute", "my-lookup-attribute-value" }
};

byte[] secretValue = Encoding.UTF8.GetBytes("my secret value");
string contentType = "text/plain; charset=utf8";

// Note that item creation can fail, e.g. if the collection could not be unlocked
Item? createdItem = await defaultCollection.CreateItemAsync(label, lookupAttributes, secretBytes, contentType, true);
if (createdItem == null)
{
    // ... handle case where item creation failed
}

// Later, if we want to retrieve this secret value, we need to search using the same lookup attributes
// Note that it's possible for multiple items to match the provided lookup attributes
Item[] matchedItems = await defaultCollection.SearchItemsAsync(lookupAttributes);
foreach (Item matchedItem in matchedItems)
{
    byte[] matchedSecret = await item.GetSecretAsync();
    string matchedSecretString = Encoding.UTF8.GetString(secret);   // my secret value
}

Documentation

The API documentation is auto-generated using docfx, and can be found here.

To preview the generated documentation locally, run docfx docfx_project/docfx.json --serve, which will serve the website at http://localhost:8080.

License

These bindings are licensed under the MIT License.

dbus.services.secrets's People

Contributors

ace4896 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

dbus.services.secrets's Issues

Collection.GetLabelAsync() fails with System.IndexOutOfRangeException

using DBus.Services.Secrets;
using System;

var service = await SecretService.ConnectAsync(EncryptionType.Dh);
var collection = await service.GetDefaultCollectionAsync();

var label = await collection.GetLabelAsync(); // crashes here
Console.WriteLine($"Label: {label}");

Output:

Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at Tmds.DBus.Protocol.ThrowHelper.ThrowIndexOutOfRange()
   at Tmds.DBus.Protocol.Reader.ReadSpan(Int32 length)
   at Tmds.DBus.Protocol.Reader.ReadSpan()
   at Tmds.DBus.Protocol.Reader.ReadString()
   at Tmds.DBus.SourceGenerator.ReaderExtensions.ReadMessage_s(Message message, Object _)
   at Tmds.DBus.Protocol.DBusConnection.<>c__41`1.<CallMethodAsync>b__41_0(Exception exception, Message message, Object state1, Object state2, Object state3)
--- End of stack trace from previous location ---
   at Tmds.DBus.Protocol.DBusConnection.MyValueTaskSource`1.System.Threading.Tasks.Sources.IValueTaskSource<T>.GetResult(Int16 token)
   at Tmds.DBus.Protocol.DBusConnection.CallMethodAsync[T](MessageBuffer message, MessageValueReader`1 valueReader, Object state)
   at Tmds.DBus.Protocol.Connection.CallMethodAsync[T](MessageBuffer message, MessageValueReader`1 reader, Object readerState)
   at DBus.Services.Secrets.Collection.GetLabelAsync()
   at Program.<Main>$(String[] args) in /home/user/.var/distrobox/fedora/Projects/DBusSecrets/UnlockKeyring/Program.cs:line 7
   at Program.<Main>(String[] args)

Collection.IsLockedAsync() returns true for unlocked collection

Hello! Thanks for this very nice package! It's useful, but unfortunately I encountered some bugs so I'm going to open issues for everything I found :)
First is that Collection.IsLockedAsync() always returns true, even when the collection is unlocked.

using DBus.Services.Secrets;
using System;

var service = await SecretService.ConnectAsync(EncryptionType.Dh);
var collection = await service.GetDefaultCollectionAsync();
collection.UnlockAsync();

Console.WriteLine(await collection.IsLockedAsync());

Collection.GetItemsAsync() fails with System.ArgumentOutOfRangeException

using DBus.Services.Secrets;
using System;

var service = await SecretService.ConnectAsync(EncryptionType.Dh);
var collection = await service.GetDefaultCollectionAsync();

var items = await collection.GetItemsAsync(); // crashes here
Console.WriteLine($"Number of items: {items.Length}");

Output:

Unhandled exception. System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'count')
   at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument)
   at System.Buffers.SequenceReader`1.AdvanceToNextSpan(Int64 count)
   at Tmds.DBus.Protocol.Reader.ReadSpan(Int32 length)
   at Tmds.DBus.Protocol.Reader.ReadSpan()
   at Tmds.DBus.Protocol.Reader.ReadString()
   at Tmds.DBus.Protocol.Reader.ReadObjectPath()
   at Tmds.DBus.SourceGenerator.ReaderExtensions.ReadArray_ao(Reader& reader)
   at Tmds.DBus.SourceGenerator.ReaderExtensions.ReadMessage_ao(Message message, Object _)
   at Tmds.DBus.Protocol.DBusConnection.<>c__41`1.<CallMethodAsync>b__41_0(Exception exception, Message message, Object state1, Object state2, Object state3)
--- End of stack trace from previous location ---
   at Tmds.DBus.Protocol.DBusConnection.MyValueTaskSource`1.System.Threading.Tasks.Sources.IValueTaskSource<T>.GetResult(Int16 token)
   at Tmds.DBus.Protocol.DBusConnection.CallMethodAsync[T](MessageBuffer message, MessageValueReader`1 valueReader, Object state)
   at Tmds.DBus.Protocol.Connection.CallMethodAsync[T](MessageBuffer message, MessageValueReader`1 reader, Object readerState)
   at DBus.Services.Secrets.Collection.GetItemsAsync()
   at Program.<Main>$(String[] args) in /home/user/.var/distrobox/fedora/Projects/DBusSecrets/UnlockKeyring/Program.cs:line 17
   at Program.<Main>(String[] args)

Incorrect timestamp from Collection.GetCreatedAsync()

using DBus.Services.Secrets;
using System;

var service = await SecretService.ConnectAsync(EncryptionType.Dh);
var collection = await service.GetDefaultCollectionAsync();

var creationTime = await collection.GetCreatedAsync();
Console.WriteLine($"Created: {DateTimeOffset.FromUnixTimeSeconds((long)creationTime).ToLocalTime()} ({creationTime})");

Output: Created: 01/01/1970 11:14:57 +03:00 (29697)
The actual time for my default collection 07/31/23 21:17:20 +03:00 (1690827440).
And I get the same incorrect result even for newly created collection.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.