|
| 1 | +package secret |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/godbus/dbus/v5" |
| 7 | + "github.com/rymdport/portal/internal/apis" |
| 8 | + "github.com/rymdport/portal/internal/convert" |
| 9 | + "github.com/rymdport/portal/internal/request" |
| 10 | +) |
| 11 | + |
| 12 | +const retrieveSecretCallName = interfaceName + ".RetrieveSecret" |
| 13 | + |
| 14 | +// RetrieveOptions contains options for the RetrieveSecret function call. |
| 15 | +type RetrieveOptions struct { |
| 16 | + HandleToken string // A string that will be used as the last element of the handle. Must be a valid object path element. |
| 17 | + Token string // An opaque string returned by a previous org.freedesktop.portal.Secret.RetrieveSecret call. |
| 18 | +} |
| 19 | + |
| 20 | +// RetrieveSecret retrieves a master secret for a sandboxed application. |
| 21 | +// The master secret is unique per application and does not change as |
| 22 | +// long as the application is installed (once it has been created). |
| 23 | +// In a typical backend implementation, it is stored in the user’s keyring, |
| 24 | +// under the application ID as a key. |
| 25 | +// The parameter fd is a writable file descriptor for transporting the secret. |
| 26 | +// |
| 27 | +// The portal may return an additional identifier associated with the secret in the results. |
| 28 | +// In the next call of this method, the application shall provide a token element in options. |
| 29 | +func RetrieveSecret(fd uintptr, options *RetrieveOptions) (string, error) { |
| 30 | + data := map[string]dbus.Variant{} |
| 31 | + if options != nil { |
| 32 | + if options.HandleToken != "" { |
| 33 | + data["handle_token"] = convert.FromString(options.HandleToken) |
| 34 | + } |
| 35 | + if options.Token != "" { |
| 36 | + data["token"] = convert.FromString(options.Token) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + result, err := apis.Call(retrieveSecretCallName, dbus.UnixFD(fd), data) |
| 41 | + if err != nil { |
| 42 | + return "", err |
| 43 | + } |
| 44 | + |
| 45 | + path := result.(dbus.ObjectPath) |
| 46 | + status, results, err := request.OnSignalResponse(path) |
| 47 | + if err != nil { |
| 48 | + return "", err |
| 49 | + } else if status > request.Success { |
| 50 | + return "", nil |
| 51 | + } |
| 52 | + |
| 53 | + if token, ok := results["token"]; ok { |
| 54 | + return token.Value().(string), nil |
| 55 | + } else if len(results) != 0 { |
| 56 | + fmt.Println("Please contribute this information to rymdport/portal: ", results) |
| 57 | + } |
| 58 | + |
| 59 | + return "", nil |
| 60 | +} |
0 commit comments