Skip to content

Commit

Permalink
Tidy up SecretService.Store
Browse files Browse the repository at this point in the history
Tidy up the `SecretService.Store()` method to move the unlock logic to
the top of the function. This keeps code together - code for unlocking
and code for creating the item.

Reverse the test for prompting at the end of the function so it is the
same as the `Delete()` method - i.e. early return for the non-prompt
case. Keep code that does the same thing looking the same.
  • Loading branch information
camh- committed Sep 5, 2024
1 parent 4784c95 commit b3859cd
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions dbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ func (ss *SecretService) Get(attrs map[string]string) (string, error) {
// the secret could not be created, an error is returned.
func (ss *SecretService) Store(label string, attrs map[string]string, secret string) error {
path := dbus.ObjectPath("/org/freedesktop/secrets/aliases/default")
// Try to unlock the collection first. Will be a no-op if it is not locked
// but if it is locked, we'll prompt the user to unlock it.
if _, err := ss.unlockObject(path); err != nil {
return err
}

collection := ss.conn.Object("org.freedesktop.secrets", path)
props := map[string]dbus.Variant{
"org.freedesktop.Secret.Item.Label": dbus.MakeVariant(label),
Expand All @@ -223,22 +229,17 @@ func (ss *SecretService) Store(label string, attrs map[string]string, secret str
return err
}

// Try to unlock the collection first. Will be a no-op if it is not locked
// but if it is locked, we'll prompt the user to unlock it.
if _, err := ss.unlockObject(path); err != nil {
return err
}

var itemPath, promptPath dbus.ObjectPath
call := collection.Call("org.freedesktop.Secret.Collection.CreateItem", 0, props, &sec, true)
if err := call.Store(&itemPath, &promptPath); err != nil {
return fmt.Errorf("couldn't create secret: %w", err)
}

if promptPath != noPrompt {
return ss.prompt(promptPath)
if promptPath == noPrompt {
return nil
}
return nil

return ss.prompt(promptPath)
}

// Delete removes a secret matching the given attributes. If expectedPassword
Expand Down

0 comments on commit b3859cd

Please sign in to comment.