2014-01-05 14:55:32 -06:00
|
|
|
package git
|
|
|
|
|
|
|
|
/*
|
|
|
|
#include <git2.h>
|
2019-12-10 16:15:32 -06:00
|
|
|
#include <git2/sys/cred.h>
|
2019-12-10 16:33:00 -06:00
|
|
|
|
|
|
|
git_credtype_t _go_git_cred_credtype(git_cred *cred);
|
2014-01-05 14:55:32 -06:00
|
|
|
*/
|
|
|
|
import "C"
|
2019-01-12 14:56:16 -06:00
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"unsafe"
|
|
|
|
)
|
2014-01-05 14:55:32 -06:00
|
|
|
|
|
|
|
type CredType uint
|
2014-03-11 15:19:12 -05:00
|
|
|
|
2014-01-05 14:55:32 -06:00
|
|
|
const (
|
|
|
|
CredTypeUserpassPlaintext CredType = C.GIT_CREDTYPE_USERPASS_PLAINTEXT
|
2014-10-27 09:12:18 -05:00
|
|
|
CredTypeSshKey CredType = C.GIT_CREDTYPE_SSH_KEY
|
|
|
|
CredTypeSshCustom CredType = C.GIT_CREDTYPE_SSH_CUSTOM
|
|
|
|
CredTypeDefault CredType = C.GIT_CREDTYPE_DEFAULT
|
2014-01-05 14:55:32 -06:00
|
|
|
)
|
|
|
|
|
2014-03-11 15:19:12 -05:00
|
|
|
type Cred struct {
|
2014-01-05 14:55:32 -06:00
|
|
|
ptr *C.git_cred
|
|
|
|
}
|
|
|
|
|
2019-01-12 14:56:16 -06:00
|
|
|
func newCred() *Cred {
|
|
|
|
cred := &Cred{}
|
|
|
|
runtime.SetFinalizer(cred, (*Cred).Free)
|
|
|
|
return cred
|
|
|
|
}
|
|
|
|
|
2014-03-11 15:19:12 -05:00
|
|
|
func (o *Cred) HasUsername() bool {
|
2014-01-05 14:55:32 -06:00
|
|
|
if C.git_cred_has_username(o.ptr) == 1 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-03-11 15:19:12 -05:00
|
|
|
func (o *Cred) Type() CredType {
|
2019-12-10 16:33:00 -06:00
|
|
|
return (CredType)(C._go_git_cred_credtype(o.ptr))
|
2014-01-05 14:55:32 -06:00
|
|
|
}
|
|
|
|
|
2019-01-12 14:56:16 -06:00
|
|
|
func (o *Cred) Free() {
|
|
|
|
C.git_cred_free(o.ptr)
|
|
|
|
runtime.SetFinalizer(o, nil)
|
|
|
|
o.ptr = nil
|
2014-01-05 14:55:32 -06:00
|
|
|
}
|
|
|
|
|
2019-01-12 14:56:16 -06:00
|
|
|
func NewCredUserpassPlaintext(username string, password string) (*Cred, error) {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
cred := newCred()
|
2014-01-05 14:55:32 -06:00
|
|
|
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)
|
2019-01-12 14:56:16 -06:00
|
|
|
if ret != 0 {
|
|
|
|
cred.Free()
|
|
|
|
return nil, MakeGitError(ret)
|
|
|
|
}
|
|
|
|
return cred, nil
|
2014-01-05 14:55:32 -06:00
|
|
|
}
|
|
|
|
|
2016-06-22 16:47:53 -05:00
|
|
|
// NewCredSshKey creates new ssh credentials reading the public and private keys
|
|
|
|
// from the file system.
|
2019-01-12 14:56:16 -06:00
|
|
|
func NewCredSshKey(username string, publicKeyPath string, privateKeyPath string, passphrase string) (*Cred, error) {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
cred := newCred()
|
2014-01-05 14:55:32 -06:00
|
|
|
cusername := C.CString(username)
|
|
|
|
defer C.free(unsafe.Pointer(cusername))
|
2016-06-22 16:47:53 -05:00
|
|
|
cpublickey := C.CString(publicKeyPath)
|
2014-01-05 14:55:32 -06:00
|
|
|
defer C.free(unsafe.Pointer(cpublickey))
|
2016-06-22 16:47:53 -05:00
|
|
|
cprivatekey := C.CString(privateKeyPath)
|
2014-01-05 14:55:32 -06:00
|
|
|
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)
|
2019-01-12 14:56:16 -06:00
|
|
|
if ret != 0 {
|
|
|
|
cred.Free()
|
|
|
|
return nil, MakeGitError(ret)
|
|
|
|
}
|
|
|
|
return cred, nil
|
2014-01-05 14:55:32 -06:00
|
|
|
}
|
|
|
|
|
2016-06-22 16:47:53 -05:00
|
|
|
// NewCredSshKeyFromMemory creates new ssh credentials using the publicKey and privateKey
|
|
|
|
// arguments as the values for the public and private keys.
|
2019-01-12 14:56:16 -06:00
|
|
|
func NewCredSshKeyFromMemory(username string, publicKey string, privateKey string, passphrase string) (*Cred, error) {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
cred := newCred()
|
2016-06-22 16:47:53 -05:00
|
|
|
cusername := C.CString(username)
|
|
|
|
defer C.free(unsafe.Pointer(cusername))
|
|
|
|
cpublickey := C.CString(publicKey)
|
|
|
|
defer C.free(unsafe.Pointer(cpublickey))
|
|
|
|
cprivatekey := C.CString(privateKey)
|
|
|
|
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)
|
2019-01-12 14:56:16 -06:00
|
|
|
if ret != 0 {
|
|
|
|
cred.Free()
|
|
|
|
return nil, MakeGitError(ret)
|
|
|
|
}
|
|
|
|
return cred, nil
|
2016-06-22 16:47:53 -05:00
|
|
|
}
|
|
|
|
|
2019-01-12 14:56:16 -06:00
|
|
|
func NewCredSshKeyFromAgent(username string) (*Cred, error) {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
cred := newCred()
|
2014-01-05 14:55:32 -06:00
|
|
|
cusername := C.CString(username)
|
|
|
|
defer C.free(unsafe.Pointer(cusername))
|
|
|
|
ret := C.git_cred_ssh_key_from_agent(&cred.ptr, cusername)
|
2019-01-12 14:56:16 -06:00
|
|
|
if ret != 0 {
|
|
|
|
cred.Free()
|
|
|
|
return nil, MakeGitError(ret)
|
|
|
|
}
|
|
|
|
return cred, nil
|
2014-01-05 14:55:32 -06:00
|
|
|
}
|
|
|
|
|
2019-01-12 14:56:16 -06:00
|
|
|
func NewCredDefault() (*Cred, error) {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
cred := newCred()
|
2014-01-05 14:55:32 -06:00
|
|
|
ret := C.git_cred_default_new(&cred.ptr)
|
2019-01-12 14:56:16 -06:00
|
|
|
if ret != 0 {
|
|
|
|
cred.Free()
|
|
|
|
return nil, MakeGitError(ret)
|
|
|
|
}
|
|
|
|
return cred, nil
|
2014-01-05 14:55:32 -06:00
|
|
|
}
|