-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for custom keyring names on linux #47
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,25 @@ | ||
package keyring | ||
|
||
import ( | ||
"github.com/danieljoos/wincred" | ||
"syscall" | ||
|
||
"github.com/danieljoos/wincred" | ||
) | ||
|
||
// ignore, just for typecast in keyring.go | ||
type secretServiceProvider struct { | ||
keyringName string | ||
} | ||
|
||
// Set password in keyring for user. | ||
func (s secretServiceProvider) Set(service, user, password string) error { return nil } | ||
|
||
// Get password from keyring given service and user name. | ||
func (s secretServiceProvider) Get(service, user string) (string, error) { return "", nil } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should not this return DefaultKeyringName? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those functions aren't getting called at all. Those are just for the compiler to not throw an error. Otherwise on any other GOOS than linux the compiler would say that there is no such secretServiceProvider to typecast to and it wouldn't compile. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The DefaultKeyring is picked here: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For me it looks like you should refactor this part, such that the empty string check should be dropped and the default should be returned by the new functions created. @mikkeloscar Wdyt? |
||
|
||
// Delete secret from keyring. | ||
func (s secretServiceProvider) Delete(service, user string) error { return nil } | ||
|
||
type windowsKeychain struct{} | ||
|
||
// Get gets a secret from the keyring given a service name and a user. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,10 @@ package ss | |
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"errors" | ||
|
||
"github.com/godbus/dbus" | ||
) | ||
|
||
|
@@ -21,6 +23,11 @@ const ( | |
collectionBasePath = "/org/freedesktop/secrets/collection/" | ||
) | ||
|
||
const ( | ||
// DefaultKeyringName the name of the keyring to use as default | ||
DefaultKeyringName = "login" | ||
) | ||
|
||
// Secret defines a org.freedesk.Secret.Item secret struct. | ||
type Secret struct { | ||
Session dbus.ObjectPath | ||
|
@@ -42,22 +49,31 @@ func NewSecret(session dbus.ObjectPath, secret string) Secret { | |
// SecretService is an interface for the Secret Service dbus API. | ||
type SecretService struct { | ||
*dbus.Conn | ||
object dbus.BusObject | ||
object dbus.BusObject | ||
KeyringName string | ||
} | ||
|
||
// NewSecretService inializes a new SecretService object. | ||
func NewSecretService() (*SecretService, error) { | ||
func NewSecretService(keyringName string) (*SecretService, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A breaking change in libraries is not a great thing to do.
|
||
conn, err := dbus.SessionBus() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &SecretService{ | ||
conn, | ||
conn.Object(serviceName, servicePath), | ||
Conn: conn, | ||
object: conn.Object(serviceName, servicePath), | ||
KeyringName: formatKeyringName(keyringName), | ||
}, nil | ||
} | ||
|
||
// see https://lists.freedesktop.org/archives/systemd-devel/2013-March/009402.html | ||
func formatKeyringName(name string) string { | ||
re := regexp.MustCompile("[^A-Za-z0-9]") | ||
name = re.ReplaceAllString(name, "_5f") | ||
return name | ||
} | ||
|
||
// OpenSession opens a secret service session. | ||
func (s *SecretService) OpenSession() (dbus.BusObject, error) { | ||
var disregard dbus.Variant | ||
|
@@ -87,9 +103,39 @@ func (s *SecretService) CheckCollectionPath(path dbus.ObjectPath) error { | |
return errors.New("path not found") | ||
} | ||
|
||
// GetCollectionForKeyring returns collection from SecretService | ||
func (s *SecretService) GetCollectionForKeyring() (dbus.BusObject, error) { | ||
var collection dbus.BusObject | ||
// Pick requested collection | ||
if len(s.KeyringName) == 0 || s.KeyringName == DefaultKeyringName { | ||
collection = s.GetLoginCollection() | ||
} else { | ||
// Check for customs collections availability | ||
collectionPath := s.GetCollectionPath(s.KeyringName) | ||
err := s.CheckCollectionPath(collectionPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// If available | ||
collection = s.GetCollectionByPath(collectionPath) | ||
} | ||
return collection, nil | ||
} | ||
|
||
// GetCollectionPath get path of collection by its name | ||
func (s *SecretService) GetCollectionPath(name string) dbus.ObjectPath { | ||
return dbus.ObjectPath(collectionBasePath + name) | ||
} | ||
|
||
// GetCollection returns a collection from a name. | ||
func (s *SecretService) GetCollection(name string) dbus.BusObject { | ||
return s.Object(serviceName, dbus.ObjectPath(collectionBasePath+name)) | ||
return s.GetCollectionByPath(s.GetCollectionPath(name)) | ||
} | ||
|
||
// GetCollectionByPath returns a collection from a name. | ||
func (s *SecretService) GetCollectionByPath(path dbus.ObjectPath) dbus.BusObject { | ||
return s.Object(serviceName, path) | ||
} | ||
|
||
// GetLoginCollection decides and returns the dbus collection to be used for login. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should not this return DefaultKeyringName?