diff options
author | Christopher Speller <crspeller@gmail.com> | 2016-05-12 23:56:07 -0400 |
---|---|---|
committer | Christopher Speller <crspeller@gmail.com> | 2016-05-12 23:56:07 -0400 |
commit | 38ee83e45b4de7edf89bf9f0ef629eb4c6ad0fa8 (patch) | |
tree | a4fde09672192b97d453ad605b030bd5a10c5a45 /vendor/github.com/mattermost/rsc/keychain | |
parent | 84d2482ddbff9564c9ad75b2d30af66e3ddfd44d (diff) | |
download | chat-38ee83e45b4de7edf89bf9f0ef629eb4c6ad0fa8.tar.gz chat-38ee83e45b4de7edf89bf9f0ef629eb4c6ad0fa8.tar.bz2 chat-38ee83e45b4de7edf89bf9f0ef629eb4c6ad0fa8.zip |
Moving to glide
Diffstat (limited to 'vendor/github.com/mattermost/rsc/keychain')
-rw-r--r-- | vendor/github.com/mattermost/rsc/keychain/doc.go | 28 | ||||
-rw-r--r-- | vendor/github.com/mattermost/rsc/keychain/mac.go | 107 |
2 files changed, 135 insertions, 0 deletions
diff --git a/vendor/github.com/mattermost/rsc/keychain/doc.go b/vendor/github.com/mattermost/rsc/keychain/doc.go new file mode 100644 index 000000000..e7e21fd2e --- /dev/null +++ b/vendor/github.com/mattermost/rsc/keychain/doc.go @@ -0,0 +1,28 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package keychain implements access to the passwords and other keys +// stored in the system-provided keychain. +package keychain + +// BUG(rsc): Package keychain is only implemented on OS X. + +import ( + "fmt" +) + +// UserPasswd returns the user name and password for authenticating +// to the named server. If the user argument is non-empty, UserPasswd +// restricts its search to passwords for the named user. +func UserPasswd(server, preferredUser string) (user, passwd string, err error) { + user, passwd, err = userPasswd(server, preferredUser) + if err != nil { + if preferredUser != "" { + err = fmt.Errorf("loading password for %s@%s: %v", preferredUser, server, err) + } else { + err = fmt.Errorf("loading password for %s: %v", server, err) + } + } + return +} diff --git a/vendor/github.com/mattermost/rsc/keychain/mac.go b/vendor/github.com/mattermost/rsc/keychain/mac.go new file mode 100644 index 000000000..523579169 --- /dev/null +++ b/vendor/github.com/mattermost/rsc/keychain/mac.go @@ -0,0 +1,107 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package keychain + +/* +#include <CoreFoundation/CoreFoundation.h> +#include <Security/Security.h> +#include <CoreServices/CoreServices.h> + +#cgo LDFLAGS: -framework CoreFoundation -framework Security + +static char* +mac2c(CFStringRef s) +{ + char *p; + int n; + + n = CFStringGetLength(s)*8; + p = malloc(n); + CFStringGetCString(s, p, n, kCFStringEncodingUTF8); + return p; +} + +void +keychain_getpasswd(char *user0, char *server, char **user, char **passwd, char **error) +{ + OSStatus st; + UInt32 len; + void *data; + SecKeychainItemRef it; + CFStringRef str; + + *user = NULL; + *passwd = NULL; + *error = NULL; + + st = SecKeychainFindInternetPassword( + NULL, // default keychain + strlen(server), server, + 0, NULL, // security domain + strlen(user0), user0, // account name + 0, NULL, // path + 0, // port + 0, // protocol type + kSecAuthenticationTypeDefault, + &len, + &data, + &it); + if(st != 0) { + str = SecCopyErrorMessageString(st, NULL); + *error = mac2c(str); + CFRelease(str); + return; + } + *passwd = malloc(len+1); + memmove(*passwd, data, len); + (*passwd)[len] = '\0'; + SecKeychainItemFreeContent(NULL, data); + + SecKeychainAttribute attr = {kSecAccountItemAttr, 0, NULL}; + SecKeychainAttributeList attrl = {1, &attr}; + st = SecKeychainItemCopyContent( + it, + NULL, + &attrl, + 0, NULL); + if(st != 0) { + str = SecCopyErrorMessageString(st, NULL); + *error = mac2c(str); + CFRelease(str); + return; + } + data = attr.data; + len = attr.length; + *user = malloc(len+1); + memmove(*user, data, len); + (*user)[len] = '\0'; + SecKeychainItemFreeContent(&attrl, NULL); +} +*/ +import "C" + +import ( + "errors" + "unsafe" +) + +func userPasswd(server, user string) (user1, passwd string, err error) { + cServer := C.CString(server) + cUser := C.CString(user) + defer C.free(unsafe.Pointer(cServer)) + defer C.free(unsafe.Pointer(cUser)) + + var cPasswd, cError *C.char + C.keychain_getpasswd(cUser, cServer, &cUser, &cPasswd, &cError) + defer C.free(unsafe.Pointer(cUser)) + defer C.free(unsafe.Pointer(cPasswd)) + defer C.free(unsafe.Pointer(cError)) + + if cError != nil { + return "", "", errors.New(C.GoString(cError)) + } + + return C.GoString(cUser), C.GoString(cPasswd), nil +} |