Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/99designs/go-keychain
https://github.com/99designs/go-keychain
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/99designs/go-keychain
- Owner: 99designs
- License: mit
- Fork: true (keybase/go-keychain)
- Created: 2016-02-01T05:20:16.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2022-06-29T19:33:37.000Z (over 2 years ago)
- Last Synced: 2024-06-18T17:24:44.967Z (5 months ago)
- Language: Go
- Size: 13.5 MB
- Stars: 11
- Watchers: 29
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-ccamel - 99designs/go-keychain - (Go)
README
# Go Keychain
A library for accessing the Keychain for OSX and iOS in Go (golang).
Requires OS X 10.9 or greater and iOS 8 or greater.
## Usage
The API is meant to mirror the Keychain API and is not necessarily idiomatic go.
#### Add Item
```go
item := keychain.NewItem()
item.SetSecClass(keychain.SecClassGenericPassword)
item.SetService("MyService")
item.SetAccount("gabriel")
item.SetLabel("A label")
item.SetAccessGroup("A123456789.group.com.mycorp")
item.SetData([]byte("toomanysecrets"))
item.SetSynchronizable(keychain.SynchronizableNo)
item.SetAccessible(keychain.AccessibleWhenUnlocked)
err := keychain.AddItem(item)if err == keychain.ErrorDuplicateItem {
// Duplicate
}
```#### Query Item
Query for multiple results, returning attributes:
```go
query := keychain.NewItem()
query.SetSecClass(keychain.SecClassGenericPassword)
query.SetService(service)
query.SetAccount(account)
query.SetAccessGroup(accessGroup)
query.SetMatchLimit(keychain.MatchLimitAll)
query.SetReturnAttributes(true)
results, err := keychain.QueryItem(query)
if err != nil {
// Error
} else {
for _, r := range results {
fmt.Printf("%#v\n", r)
}
}
```Query for a single result, returning data:
```go
query := keychain.NewItem()
query.SetSecClass(keychain.SecClassGenericPassword)
query.SetService(service)
query.SetAccount(account)
query.SetAccessGroup(accessGroup)
query.SetMatchLimit(keychain.MatchLimitOne)
query.SetReturnData(true)
results, err := keychain.QueryItem(query)
if err != nil {
// Error
} else if len(results) != 1 {
// Not found
} else {
password := string(results[0].Data)
}
```#### Delete Item
Delete a generic password item with service and account:
```go
item := keychain.NewItem()
item.SetSecClass(keychain.SecClassGenericPassword)
item.SetService(service)
item.SetAccount(account)
err := keychain.DeleteItem(item)
```### Other
There are some convenience methods for generic password:
```go
// Create generic password item with service, account, label, password, access group
item := keychain.NewGenericPassword("MyService", "gabriel", "A label", []byte("toomanysecrets"), "A123456789.group.com.mycorp")
item.SetSynchronizable(keychain.SynchronizableNo)
item.SetAccessible(keychain.AccessibleWhenUnlocked)
err := keychain.AddItem(item)
if err == keychain.ErrorDuplicateItem {
// Duplicate
}accounts, err := keychain.GetGenericPasswordAccounts("MyService")
// Should have 1 account == "gabriel"err := keychain.DeleteGenericPasswordItem("MyService", "gabriel")
if err == keychain.ErrorNotFound {
// Not found
}
```### OS X
Set a trusted applications for item (OS X only):
```go
item := keychain.NewGenericPassword("MyService", "gabriel", "A label", []byte("toomanysecrets"), "A123456789.group.com.mycorp")
trustedApplications := []string{"/Applications/Mail.app"}
item.SetAccess(&keychain.Access{Label: "Mail", TrustedApplications: trustedApplications})
err := keychain.AddItem(item)
```## iOS
Bindable package in `bind`. iOS project in `ios`. Run that project to test iOS.
To re-generate framework (in bind dir):
```
gomobile bind -target=ios -o ../ios/bind.framework
```