forked from libgit2/git2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
credentials.go
140 lines (121 loc) · 3.5 KB
/
credentials.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package git
/*
#include <git2.h>
#include <git2/sys/cred.h>
git_credtype_t _go_git_cred_credtype(git_cred *cred);
*/
import "C"
import (
"runtime"
"unsafe"
)
type CredType 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
)
type Cred struct {
ptr *C.git_cred
}
func newCred() *Cred {
cred := &Cred{}
runtime.SetFinalizer(cred, (*Cred).Free)
return cred
}
func (o *Cred) HasUsername() bool {
if C.git_cred_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 *Cred) Free() {
C.git_cred_free(o.ptr)
runtime.SetFinalizer(o, nil)
o.ptr = nil
}
func NewCredUserpassPlaintext(username string, password string) (*Cred, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cred := newCred()
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)
if ret != 0 {
cred.Free()
return nil, MakeGitError(ret)
}
return cred, nil
}
// NewCredSshKey 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) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cred := newCred()
cusername := C.CString(username)
defer C.free(unsafe.Pointer(cusername))
cpublickey := C.CString(publicKeyPath)
defer C.free(unsafe.Pointer(cpublickey))
cprivatekey := C.CString(privateKeyPath)
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)
if ret != 0 {
cred.Free()
return nil, MakeGitError(ret)
}
return cred, nil
}
// NewCredSshKeyFromMemory 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) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cred := newCred()
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)
if ret != 0 {
cred.Free()
return nil, MakeGitError(ret)
}
return cred, nil
}
func NewCredSshKeyFromAgent(username string) (*Cred, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cred := newCred()
cusername := C.CString(username)
defer C.free(unsafe.Pointer(cusername))
ret := C.git_cred_ssh_key_from_agent(&cred.ptr, cusername)
if ret != 0 {
cred.Free()
return nil, MakeGitError(ret)
}
return cred, nil
}
func NewCredDefault() (*Cred, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cred := newCred()
ret := C.git_cred_default_new(&cred.ptr)
if ret != 0 {
cred.Free()
return nil, MakeGitError(ret)
}
return cred, nil
}