diff --git a/conversion.go b/conversion.go index 850574b..685f90a 100644 --- a/conversion.go +++ b/conversion.go @@ -39,6 +39,11 @@ func utf16ToByte(wstr []uint16) (result []byte) { return } +// utf16FromString creates a UTF16 char array from a string. +func utf16FromString(str string) []uint16 { + return syscall.StringToUTF16(str) +} + // goBytes copies the given C byte array to a Go byte array (see `C.GoBytes`). // This function avoids having cgo as dependency. func goBytes(src uintptr, len uint32) []byte { diff --git a/conversion_unsupported.go b/conversion_unsupported.go new file mode 100644 index 0000000..a1ea720 --- /dev/null +++ b/conversion_unsupported.go @@ -0,0 +1,11 @@ +// +build !windows + +package wincred + +func utf16ToByte(...interface{}) []byte { + return nil +} + +func utf16FromString(...interface{}) []uint16 { + return nil +} diff --git a/examples_test.go b/examples_test.go new file mode 100644 index 0000000..3cf70cc --- /dev/null +++ b/examples_test.go @@ -0,0 +1,32 @@ +package wincred + +import "fmt" + +func ExampleList() { + if creds, err := List(); err == nil { + for _, cred := range creds { + fmt.Println(cred.TargetName) + } + } +} + +func ExampleGetGenericCredential() { + if cred, err := GetGenericCredential("myGoApplication"); err == nil { + fmt.Println(cred.TargetName, string(cred.CredentialBlob)) + } +} + +func ExampleGenericCredential_Delete() { + cred, _ := GetGenericCredential("myGoApplication") + if err := cred.Delete(); err == nil { + fmt.Println("Deleted") + } +} + +func ExampleGenericCredential_Write() { + cred := NewGenericCredential("myGoApplication") + cred.CredentialBlob = []byte("my secret") + if err := cred.Write(); err == nil { + fmt.Println("Created") + } +} diff --git a/sys_unsupported.go b/sys_unsupported.go new file mode 100644 index 0000000..dd9e135 --- /dev/null +++ b/sys_unsupported.go @@ -0,0 +1,32 @@ +// +build !windows + +package wincred + +import "errors" + +const ( + sysCRED_TYPE_GENERIC = 0 + sysCRED_TYPE_DOMAIN_PASSWORD = 0 + sysCRED_TYPE_DOMAIN_CERTIFICATE = 0 + sysCRED_TYPE_DOMAIN_VISIBLE_PASSWORD = 0 + sysCRED_TYPE_GENERIC_CERTIFICATE = 0 + sysCRED_TYPE_DOMAIN_EXTENDED = 0 + + sysERROR_NOT_FOUND = "" +) + +func sysCredRead(...interface{}) (*Credential, error) { + return nil, errors.New("Operation not supported") +} + +func sysCredWrite(...interface{}) error { + return errors.New("Operation not supported") +} + +func sysCredDelete(...interface{}) error { + return errors.New("Operation not supported") +} + +func sysCredEnumerate(...interface{}) ([]*Credential, error) { + return nil, errors.New("Operation not supported") +} diff --git a/wincred.go b/wincred.go index 09fd023..ff79d8e 100644 --- a/wincred.go +++ b/wincred.go @@ -1,5 +1,3 @@ -// +build windows - // Package wincred provides primitives for accessing the Windows Credentials Management API. // This includes functions for retrieval, listing and storage of credentials as well as Go structures for convenient access to the credential data. // @@ -7,10 +5,6 @@ // MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/aa374789(v=vs.85).aspx package wincred -import ( - "syscall" -) - // GetGenericCredential fetches the generic credential with the given name from Windows credential manager. // It returns nil and an error if the credential could not be found or an error occurred. func GetGenericCredential(targetName string) (*GenericCredential, error) { @@ -77,7 +71,7 @@ func (t *DomainPassword) Delete() (err error) { // SetPassword sets the CredentialBlob field of a domain password credential to the given string. func (t *DomainPassword) SetPassword(pw string) { - t.CredentialBlob = utf16ToByte(syscall.StringToUTF16(pw)) + t.CredentialBlob = utf16ToByte(utf16FromString(pw)) } // List retrieves all credentials of the Credentials store. diff --git a/wincred_test.go b/wincred_test.go index 972e286..4badeeb 100644 --- a/wincred_test.go +++ b/wincred_test.go @@ -3,7 +3,6 @@ package wincred import ( - "fmt" "testing" "github.com/stretchr/testify/assert" @@ -89,32 +88,3 @@ func TestGenericCredential_DeleteNotFound(t *testing.T) { // MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681383(v=vs.85).aspx assert.Equal(t, "Element not found.", err.Error()) } - -func ExampleList() { - if creds, err := List(); err == nil { - for _, cred := range creds { - fmt.Println(cred.TargetName) - } - } -} - -func ExampleGetGenericCredential() { - if cred, err := GetGenericCredential("myGoApplication"); err == nil { - fmt.Println(cred.TargetName, string(cred.CredentialBlob)) - } -} - -func ExampleGenericCredential_Delete() { - cred, _ := GetGenericCredential("myGoApplication") - if err := cred.Delete(); err == nil { - fmt.Println("Deleted") - } -} - -func ExampleGenericCredential_Write() { - cred := NewGenericCredential("myGoApplication") - cred.CredentialBlob = []byte("my secret") - if err := cred.Write(); err == nil { - fmt.Println("Created") - } -}