diff options
| author | lhchavez <[email protected]> | 2020-12-05 07:23:44 -0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-12-05 07:23:44 -0800 |
| commit | 137c05e802d5e11a5ab54809bc8be8f61ccece21 (patch) | |
| tree | b44997824565f9506744a4259177746b83112e92 /credentials.go | |
| parent | 1fabe95fb7275df980ff6ab03fb85eac91c5849d (diff) | |
Mark some symbols to be deprecated #minor (#698)
This change introduces the file deprecated.go, which contains any
constants, functions, and types that are slated to be deprecated in the
next major release.
These symbols are deprecated because they refer to old spellings in
pre-1.0 libgit2. This also makes the build be done with the
`-DDEPRECATE_HARD` flag to avoid regressions.
This, together with
[gorelease](https://godoc.org/golang.org/x/exp/cmd/gorelease)[1] should
make releases safer going forward.
1: More information about how that works at
https://go.googlesource.com/exp/+/refs/heads/master/apidiff/README.md
Diffstat (limited to 'credentials.go')
| -rw-r--r-- | credentials.go | 136 |
1 files changed, 101 insertions, 35 deletions
diff --git a/credentials.go b/credentials.go index 5469b20..b1051b9 100644 --- a/credentials.go +++ b/credentials.go @@ -2,62 +2,128 @@ package git /* #include <git2.h> -#include <git2/sys/cred.h> +#include <git2/credential.h> -git_credtype_t _go_git_cred_credtype(git_cred *cred); +git_credential_t _go_git_credential_credtype(git_credential *cred); */ import "C" import ( + "fmt" "runtime" + "strings" "unsafe" ) -type CredType uint +// CredentialType is a bitmask of supported credential types. +// +// This represents the various types of authentication methods supported by the +// library. +type CredentialType uint const ( - CredTypeUserpassPlaintext CredType = C.GIT_CREDTYPE_USERPASS_PLAINTEXT - CredTypeSshKey CredType = C.GIT_CREDTYPE_SSH_KEY - CredTypeSshCustom CredType = C.GIT_CREDTYPE_SSH_CUSTOM - CredTypeDefault CredType = C.GIT_CREDTYPE_DEFAULT + CredentialTypeUserpassPlaintext CredentialType = C.GIT_CREDENTIAL_USERPASS_PLAINTEXT + CredentialTypeSSHKey CredentialType = C.GIT_CREDENTIAL_SSH_KEY + CredentialTypeSSHCustom CredentialType = C.GIT_CREDENTIAL_SSH_CUSTOM + CredentialTypeDefault CredentialType = C.GIT_CREDENTIAL_DEFAULT + CredentialTypeSSHInteractive CredentialType = C.GIT_CREDENTIAL_SSH_INTERACTIVE + CredentialTypeUsername CredentialType = C.GIT_CREDENTIAL_USERNAME + CredentialTypeSSHMemory CredentialType = C.GIT_CREDENTIAL_SSH_MEMORY ) -type Cred struct { - ptr *C.git_cred +func (t CredentialType) String() string { + if t == 0 { + return "CredentialType(0)" + } + + var parts []string + + if (t & CredentialTypeUserpassPlaintext) != 0 { + parts = append(parts, "UserpassPlaintext") + t &= ^CredentialTypeUserpassPlaintext + } + if (t & CredentialTypeSSHKey) != 0 { + parts = append(parts, "SSHKey") + t &= ^CredentialTypeSSHKey + } + if (t & CredentialTypeSSHCustom) != 0 { + parts = append(parts, "SSHCustom") + t &= ^CredentialTypeSSHCustom + } + if (t & CredentialTypeDefault) != 0 { + parts = append(parts, "Default") + t &= ^CredentialTypeDefault + } + if (t & CredentialTypeSSHInteractive) != 0 { + parts = append(parts, "SSHInteractive") + t &= ^CredentialTypeSSHInteractive + } + if (t & CredentialTypeUsername) != 0 { + parts = append(parts, "Username") + t &= ^CredentialTypeUsername + } + if (t & CredentialTypeSSHMemory) != 0 { + parts = append(parts, "SSHMemory") + t &= ^CredentialTypeSSHMemory + } + + if t != 0 { + parts = append(parts, fmt.Sprintf("CredentialType(%#x)", t)) + } + + return strings.Join(parts, "|") } -func newCred() *Cred { - cred := &Cred{} - runtime.SetFinalizer(cred, (*Cred).Free) +type Credential struct { + ptr *C.git_credential +} + +func newCredential() *Credential { + cred := &Credential{} + runtime.SetFinalizer(cred, (*Credential).Free) return cred } -func (o *Cred) HasUsername() bool { - if C.git_cred_has_username(o.ptr) == 1 { +func (o *Credential) HasUsername() bool { + if C.git_credential_has_username(o.ptr) == 1 { return true } return false } -func (o *Cred) Type() CredType { - return (CredType)(C._go_git_cred_credtype(o.ptr)) +func (o *Credential) Type() CredentialType { + return (CredentialType)(C._go_git_credential_credtype(o.ptr)) } -func (o *Cred) Free() { - C.git_cred_free(o.ptr) +func (o *Credential) Free() { + C.git_credential_free(o.ptr) runtime.SetFinalizer(o, nil) o.ptr = nil } -func NewCredUserpassPlaintext(username string, password string) (*Cred, error) { +func NewCredentialUsername(username string) (*Credential, error) { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + cred := newCredential() + cusername := C.CString(username) + ret := C.git_credential_username_new(&cred.ptr, cusername) + if ret != 0 { + cred.Free() + return nil, MakeGitError(ret) + } + return cred, nil +} + +func NewCredentialUserpassPlaintext(username string, password string) (*Credential, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() - cred := newCred() + cred := newCredential() cusername := C.CString(username) defer C.free(unsafe.Pointer(cusername)) cpassword := C.CString(password) defer C.free(unsafe.Pointer(cpassword)) - ret := C.git_cred_userpass_plaintext_new(&cred.ptr, cusername, cpassword) + ret := C.git_credential_userpass_plaintext_new(&cred.ptr, cusername, cpassword) if ret != 0 { cred.Free() return nil, MakeGitError(ret) @@ -65,13 +131,13 @@ func NewCredUserpassPlaintext(username string, password string) (*Cred, error) { return cred, nil } -// NewCredSshKey creates new ssh credentials reading the public and private keys +// NewCredentialSSHKey creates new ssh credentials reading the public and private keys // from the file system. -func NewCredSshKey(username string, publicKeyPath string, privateKeyPath string, passphrase string) (*Cred, error) { +func NewCredentialSSHKey(username string, publicKeyPath string, privateKeyPath string, passphrase string) (*Credential, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() - cred := newCred() + cred := newCredential() cusername := C.CString(username) defer C.free(unsafe.Pointer(cusername)) cpublickey := C.CString(publicKeyPath) @@ -80,7 +146,7 @@ func NewCredSshKey(username string, publicKeyPath string, privateKeyPath string, defer C.free(unsafe.Pointer(cprivatekey)) cpassphrase := C.CString(passphrase) defer C.free(unsafe.Pointer(cpassphrase)) - ret := C.git_cred_ssh_key_new(&cred.ptr, cusername, cpublickey, cprivatekey, cpassphrase) + ret := C.git_credential_ssh_key_new(&cred.ptr, cusername, cpublickey, cprivatekey, cpassphrase) if ret != 0 { cred.Free() return nil, MakeGitError(ret) @@ -88,13 +154,13 @@ func NewCredSshKey(username string, publicKeyPath string, privateKeyPath string, return cred, nil } -// NewCredSshKeyFromMemory creates new ssh credentials using the publicKey and privateKey +// NewCredentialSSHKeyFromMemory creates new ssh credentials using the publicKey and privateKey // arguments as the values for the public and private keys. -func NewCredSshKeyFromMemory(username string, publicKey string, privateKey string, passphrase string) (*Cred, error) { +func NewCredentialSSHKeyFromMemory(username string, publicKey string, privateKey string, passphrase string) (*Credential, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() - cred := newCred() + cred := newCredential() cusername := C.CString(username) defer C.free(unsafe.Pointer(cusername)) cpublickey := C.CString(publicKey) @@ -103,7 +169,7 @@ func NewCredSshKeyFromMemory(username string, publicKey string, privateKey strin defer C.free(unsafe.Pointer(cprivatekey)) cpassphrase := C.CString(passphrase) defer C.free(unsafe.Pointer(cpassphrase)) - ret := C.git_cred_ssh_key_memory_new(&cred.ptr, cusername, cpublickey, cprivatekey, cpassphrase) + ret := C.git_credential_ssh_key_memory_new(&cred.ptr, cusername, cpublickey, cprivatekey, cpassphrase) if ret != 0 { cred.Free() return nil, MakeGitError(ret) @@ -111,14 +177,14 @@ func NewCredSshKeyFromMemory(username string, publicKey string, privateKey strin return cred, nil } -func NewCredSshKeyFromAgent(username string) (*Cred, error) { +func NewCredentialSSHKeyFromAgent(username string) (*Credential, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() - cred := newCred() + cred := newCredential() cusername := C.CString(username) defer C.free(unsafe.Pointer(cusername)) - ret := C.git_cred_ssh_key_from_agent(&cred.ptr, cusername) + ret := C.git_credential_ssh_key_from_agent(&cred.ptr, cusername) if ret != 0 { cred.Free() return nil, MakeGitError(ret) @@ -126,12 +192,12 @@ func NewCredSshKeyFromAgent(username string) (*Cred, error) { return cred, nil } -func NewCredDefault() (*Cred, error) { +func NewCredentialDefault() (*Credential, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() - cred := newCred() - ret := C.git_cred_default_new(&cred.ptr) + cred := newCredential() + ret := C.git_credential_default_new(&cred.ptr) if ret != 0 { cred.Free() return nil, MakeGitError(ret) |
