• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..23-Jun-2021-

secretservice/H23-Jun-2021-518437

LICENSEH A D23-Jun-20211 KiB2317

README.mdH A D23-Jun-20214 KiB160123

corefoundation.goH A D23-Jun-202110.8 KiB360288

datetime.goH A D23-Jun-20211.9 KiB6938

go.modH A D23-Jun-2021509 1613

go.sumH A D23-Jun-20213.5 KiB3938

ios.goH A D23-Jun-2021995 2312

keychain.goH A D23-Jun-202117.4 KiB572404

macos.goH A D23-Jun-20215.2 KiB185122

util.goH A D23-Jun-2021694 3224

README.md

1# Go Keychain
2
3[![Travis CI](https://travis-ci.org/keybase/go-keychain.svg?branch=master)](https://travis-ci.org/keybase/go-keychain)
4
5A library for accessing the Keychain for macOS, iOS, and Linux in Go (golang).
6
7Requires macOS 10.9 or greater and iOS 8 or greater. On Linux, communicates to
8a provider of the DBUS SecretService spec like gnome-keyring or ksecretservice.
9
10```go
11import "github.com/keybase/go-keychain"
12```
13
14
15## Mac/iOS Usage
16
17The API is meant to mirror the macOS/iOS Keychain API and is not necessarily idiomatic go.
18
19#### Add Item
20
21```go
22item := keychain.NewItem()
23item.SetSecClass(keychain.SecClassGenericPassword)
24item.SetService("MyService")
25item.SetAccount("gabriel")
26item.SetLabel("A label")
27item.SetAccessGroup("A123456789.group.com.mycorp")
28item.SetData([]byte("toomanysecrets"))
29item.SetSynchronizable(keychain.SynchronizableNo)
30item.SetAccessible(keychain.AccessibleWhenUnlocked)
31err := keychain.AddItem(item)
32
33if err == keychain.ErrorDuplicateItem {
34  // Duplicate
35}
36```
37
38#### Query Item
39
40Query for multiple results, returning attributes:
41
42```go
43query := keychain.NewItem()
44query.SetSecClass(keychain.SecClassGenericPassword)
45query.SetService(service)
46query.SetAccount(account)
47query.SetAccessGroup(accessGroup)
48query.SetMatchLimit(keychain.MatchLimitAll)
49query.SetReturnAttributes(true)
50results, err := keychain.QueryItem(query)
51if err != nil {
52  // Error
53} else {
54  for _, r := range results {
55    fmt.Printf("%#v\n", r)
56  }
57}
58```
59
60Query for a single result, returning data:
61
62```go
63query := keychain.NewItem()
64query.SetSecClass(keychain.SecClassGenericPassword)
65query.SetService(service)
66query.SetAccount(account)
67query.SetAccessGroup(accessGroup)
68query.SetMatchLimit(keychain.MatchLimitOne)
69query.SetReturnData(true)
70results, err := keychain.QueryItem(query)
71if err != nil {
72  // Error
73} else if len(results) != 1 {
74  // Not found
75} else {
76  password := string(results[0].Data)
77}
78```
79
80#### Delete Item
81
82Delete a generic password item with service and account:
83
84```go
85item := keychain.NewItem()
86item.SetSecClass(keychain.SecClassGenericPassword)
87item.SetService(service)
88item.SetAccount(account)
89err := keychain.DeleteItem(item)
90```
91
92### Other
93
94There are some convenience methods for generic password:
95
96```go
97// Create generic password item with service, account, label, password, access group
98item := keychain.NewGenericPassword("MyService", "gabriel", "A label", []byte("toomanysecrets"), "A123456789.group.com.mycorp")
99item.SetSynchronizable(keychain.SynchronizableNo)
100item.SetAccessible(keychain.AccessibleWhenUnlocked)
101err := keychain.AddItem(item)
102if err == keychain.ErrorDuplicateItem {
103  // Duplicate
104}
105
106accounts, err := keychain.GetGenericPasswordAccounts("MyService")
107// Should have 1 account == "gabriel"
108
109err := keychain.DeleteGenericPasswordItem("MyService", "gabriel")
110if err == keychain.ErrorNotFound {
111  // Not found
112}
113```
114
115### OS X
116
117Creating a new keychain and add an item to it:
118
119```go
120
121// Add a new key chain into ~/Application Support/Keychains, with the provided password
122k, err := keychain.NewKeychain("mykeychain.keychain", "my keychain password")
123if err != nil {
124  // Error creating
125}
126
127// Create generic password item with service, account, label, password, access group
128item := keychain.NewGenericPassword("MyService", "gabriel", "A label", []byte("toomanysecrets"), "A123456789.group.com.mycorp")
129item.UseKeychain(k)
130err := keychain.AddItem(item)
131if err != nil {
132  // Error creating
133}
134```
135
136Using a Keychain at path:
137
138```go
139k, err := keychain.NewWithPath("mykeychain.keychain")
140```
141
142Set a trusted applications for item (OS X only):
143
144```go
145item := keychain.NewGenericPassword("MyService", "gabriel", "A label", []byte("toomanysecrets"), "A123456789.group.com.mycorp")
146trustedApplications := []string{"/Applications/Mail.app"}
147item.SetAccess(&keychain.Access{Label: "Mail", TrustedApplications: trustedApplications})
148err := keychain.AddItem(item)
149```
150
151## iOS
152
153Bindable package in `bind`. iOS project in `ios`. Run that project to test iOS.
154
155To re-generate framework:
156
157```
158(cd bind && gomobile bind -target=ios -tags=ios -o ../ios/bind.framework)
159```
160