GithubHelp home page GithubHelp logo

go-keytar's Introduction

go-keytar

Cross-platform system keychain access library for Go.

This package is largely based on the node-keytar package, though the GNOME Keyring implementation has been modified to work on older GNOME versions that don't provide the simple password storage API.

This package is designed to add, get, replace, and delete passwords in the user's default keychain. On OS X, passwords are managed by the Keychain. On Linux, passwords are managed by GNOME Keyring. On Windows, passwords are managed by Credential Vault.

Status

The module is currently tested1 on the following platforms:

Windows OS X/Linux
Windows OS X
1: Sadly, the gnome-keyring-daemon does not work on Travis CI, so, while the library and tests are built on Linux, the tests are not actually run. If you want to execute the tests, you'll have to build and run them locally ๐Ÿ˜ข. You'll probably have a lot better luck if you do this in a GNOME session.

Dependencies

On each platform, you'll need a Go installation that supports cgo compilation. On Windows, this means that you'll need MinGW-w64, because MinGW doesn't support the Windows Credential Vault API and, even if it did, it doesn't support 64-bit compilation. On other platforms, Go should just use the system compiler for cgo compilation.

All library dependencies are met by the system on Windows and OS X.

On Linux, you need to ensure that the GNOME Keyring development package is installed. On Ubuntu systems, do:

sudo apt-get install libgnome-keyring-dev

On Red Hat systems, do:

sudo yum install gnome-keyring-devel

For all other Linux distributions, consult your package manager.

Usage

The interface to the platform's default keychain is provided by the Keychain interface. To get the appropriate Keychain instance for the current platform, do:

keychain, err := keytar.GetKeychain()
if err != nil {
	// Handle error (most likely ErrUnsupported)
}

Then you can add a password:

// NOTE: AddPassword will not overwrite a password - use
// keytar.ReplacePassword for that
err = keychain.AddPassword("example.org", "George", "$eCr37")
if err != nil {
	// Handle error
}

Query a password:

password, err := keychain.GetPassword("example.org", "George")
if err != nil {
	// Handle error
}
// Use password

Replace a password:

// NOTE: This is a module-level function, not part of the keychain interface
err = keytar.ReplacePassword(
	keychain,
	"example.org",
	"George",
	"M0r3-$eCr37",
)
if err != nil {
	// Handle error
}

Or delete a password:

err = keytar.DeletePassword("example.org", "George")
if err != nil {
	// Handle error (you can probably ignore keytar.ErrNotFound)
}

That's it.

Note that all strings passed to the interface must be UTF-8 encoded without any null bytes. The GetPassword method may return a non-UTF-8 string if the entry was created by another program not enforcing this constraint.

TODO list

  • Create GoDoc entry.
  • Move Linux convenience C code out of the Go source file (it's a bit long), or, even better, switch to a more modern keychain system on Linux, like libsecret.
  • Make APIs try to extract more concise error information from the underlying platform APIs. At the moment, many failures are classified as ErrUnknown, but we could probably figure out the real error and expand our list of error codes.
  • Figure out if Go has a secure fallback that we could use somewhere in its crypto libraries.

Contributors

  • Jacob Howard (@havoc-io)
  • Jeffrey Hulten (@jhulten)

go-keytar's People

Contributors

jhulten avatar xenoscopic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

go-keytar's Issues

Error compiling on Windows (in CI)

Windows CI job via AppVeyor fails with:

Build started
git clone -q https://github.com/havoc-io/go-keytar.git C:\projects\src\github.com\havoc-io\go-keytar
git fetch -q origin +refs/pull/2/merge:
git checkout -qf FETCH_HEAD
set PATH=%GOROOT%\bin;%MINGWROOT%\bin;%PATH%
cd %GOPATH%\src\github.com\havoc-io\go-keytar
go build
set PATH=%GOROOT%\bin;%MINGWROOT%\bin;%PATH%
cd %GOPATH%\src\github.com\havoc-io\go-keytar
go test
--- FAIL: TestNonExistentReplace (0.00s)
panic: runtime error: cgo argument has Go pointer to Go pointer [recovered]
	panic: runtime error: cgo argument has Go pointer to Go pointer
goroutine 21 [running]:
testing.tRunner.func1(0xc0000a8400)
	C:/go/src/testing/testing.go:830 +0x399
panic(0x540be0, 0xc00004c540)
	C:/go/src/runtime/panic.go:522 +0x1c3
github.com/havoc-io/go-keytar.(*keychainWindows).AddPassword.func2(0xc0000b8000, 0xc0000b8000)
	C:/projects/src/github.com/havoc-io/go-keytar/keytar_windows.go:66 +0x96
github.com/havoc-io/go-keytar.(*keychainWindows).AddPassword(0x671658, 0x56cae7, 0x17, 0x56cad0, 0x17, 0x56a618, 0x10, 0x0, 0x0)
	C:/projects/src/github.com/havoc-io/go-keytar/keytar_windows.go:66 +0x28d
github.com/havoc-io/go-keytar.ReplacePassword(0x590d20, 0x671658, 0x56cae7, 0x17, 0x56cad0, 0x17, 0x56a618, 0x10, 0x4c4764, 0x62bac8)
	C:/projects/src/github.com/havoc-io/go-keytar/keytar.go:66 +0xc0
github.com/havoc-io/go-keytar.TestNonExistentReplace(0xc0000a8400)
	C:/projects/src/github.com/havoc-io/go-keytar/keytar_test.go:54 +0xb0
testing.tRunner(0xc0000a8400, 0x572e08)
	C:/go/src/testing/testing.go:865 +0xc7
created by testing.(*T).Run
	C:/go/src/testing/testing.go:916 +0x361
exit status 2
FAIL	github.com/havoc-io/go-keytar	0.028s
Command exited with code 1

Error compiling on MacOSX

Seeing the following error compiling go-keytar on MacOSX Mojave (10.14.5 (18F132))

# github.com/havoc-io/go-keytar
~/go/src/github.com/havoc-io/go-keytar/keytar_darwin.go:36:40: cannot use nil as type _Ctype_SecKeychainRef in assignment
~/go/src/github.com/havoc-io/go-keytar/keytar_darwin.go:73:40: cannot use nil as type _Ctype_CFTypeRef in assignment
~/go/src/github.com/havoc-io/go-keytar/keytar_darwin.go:115:40: cannot use nil as type _Ctype_CFTypeRef in assignment

Traced back to the use of a legacy (eventually deprecated?) function calls SecKeychainAddGenericPassword and SecKeychainFindGenericPassword which are currently passed nil as the first argument to specify the default keychain.

https://golang.org/cmd/cgo/#hdr-Special_cases

This may be fixable with a simple go fix command.

OS-X SecKeyChain functions are deprecated

First of all thanks for this package. Unfortunately the OS-X SecKeyChain functions are deprecated. The alternative seems to be to use SecItemUpdate, etc as described here:

https://developer.apple.com/documentation/security/keychain_services/keychain_items/updating_and_deleting_keychain_items

Could this be updated? Thanks!

Warnings I am getting:

 github.com/havoc-io/go-keytar
cgo-gcc-prolog:75:11: warning: 'SecKeychainAddGenericPassword' is deprecated: first deprecated in macOS 10.10 - SecKeychain is deprecated [-Wdeprecated-declarations]
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Security.framework/Headers/SecKeychain.h:610:10: note: 'SecKeychainAddGenericPassword' has been explicitly marked deprecated here
cgo-gcc-prolog:103:11: warning: 'SecKeychainFindGenericPassword' is deprecated: first deprecated in macOS 10.10 - SecKeychain is deprecated [-Wdeprecated-declarations]
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Security.framework/Headers/SecKeychain.h:628:10: note: 'SecKeychainFindGenericPassword' has been explicitly marked deprecated here
cgo-gcc-prolog:122:11: warning: 'SecKeychainItemDelete' is deprecated: first deprecated in macOS 10.10 - SecKeychain is deprecated [-Wdeprecated-declarations]
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Security.framework/Headers/SecKeychainItem.h:257:10: note: 'SecKeychainItemDelete' has been explicitly marked deprecated here
cgo-gcc-prolog:142:11: warning: 'SecKeychainItemFreeContent' is deprecated: first deprecated in macOS 10.10 - SecKeychain is deprecated [-Wdeprecated-declarations]
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Security.framework/Headers/SecKeychainItem.h:220:10: note: 'SecKeychainItemFreeContent' has been explicitly marked deprecated here

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.