Skip to content

Commit

Permalink
Public interface documentation fixed
Browse files Browse the repository at this point in the history
This removes the build constraint from the main interface file `wincred.go` to have the documentation available again on godoc.org. Stubs for the system call functions have been added for unsupported platforms (everything else than windows). The examples have been extracted so a separate file.
  • Loading branch information
danieljoos committed Aug 5, 2018
1 parent ab1dbd1 commit 1dcb5a2
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 37 deletions.
5 changes: 5 additions & 0 deletions conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions conversion_unsupported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// +build !windows

package wincred

func utf16ToByte(...interface{}) []byte {
return nil
}

func utf16FromString(...interface{}) []uint16 {
return nil
}
32 changes: 32 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
32 changes: 32 additions & 0 deletions sys_unsupported.go
Original file line number Diff line number Diff line change
@@ -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")
}
8 changes: 1 addition & 7 deletions wincred.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
// +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.
//
// A more detailed description of Windows Credentials Management can be found on
// 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) {
Expand Down Expand Up @@ -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.
Expand Down
30 changes: 0 additions & 30 deletions wincred_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package wincred

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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")
}
}

0 comments on commit 1dcb5a2

Please sign in to comment.