-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredential.go
210 lines (183 loc) · 3.77 KB
/
credential.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"bufio"
"fmt"
"io"
"net/url"
"os"
"strconv"
"strings"
)
type Credential struct {
Username string
Password string
Protocol string
Host string
Path string
URL string
Quit int
}
func (c *Credential) ToURL() (*url.URL, error) {
// if we already have the URL defined, just return it
if c.URL != "" {
url, err := url.Parse(c.URL)
if err != nil {
return nil, err
}
return url, nil
}
u := new(url.URL)
// ensure host is defined
if c.Host != "" {
u.Host = c.Host
}
// ensure path is defined
if c.Path != "" {
u.Path = c.Path
}
// ensure protocol is defined
if c.Protocol != "" {
u.Scheme = c.Protocol
}
// if we have a username and a password
if c.Username != "" && c.Password != "" {
u.User = url.UserPassword(c.Username, c.Password)
}
// if we just have a username
if c.Username != "" && c.Password == "" {
u.User = url.User(c.Username)
}
return u, nil
}
func (c *Credential) IsValidToStore() bool {
// ensure we actually have stuff to store
return !(c.Protocol == "" || !(c.Host == "" || c.Path == "") || c.Username == "" || c.Password == "")
}
func (c *Credential) PrintToStdOut() {
fmt.Fprintf(os.Stdout, "username=%s\n", c.Username)
fmt.Fprintf(os.Stdout, "password=%s\n", c.Password)
}
func CredentialsMatch(want *Credential, have *Credential) bool {
if want.Protocol != "" {
if have.Protocol != "" {
if want.Protocol != have.Protocol {
return false
}
}
}
if want.Host != "" {
if have.Host != "" {
if want.Host != have.Host {
return false
}
}
}
if want.Path != "" {
if have.Path != "" {
if want.Path != have.Path {
return false
}
}
}
if want.Username != "" {
if have.Username != "" {
if want.Username != have.Username {
return false
}
}
}
return true
}
func ParseCredentialStdin() (*Credential, error) {
c := new(Credential)
reader := bufio.NewReader(os.Stdin)
for {
// read a line from Stdin
line, isPrefix, err := reader.ReadLine()
if err != nil {
if err == io.EOF {
return c, nil
}
return nil, err
}
// if we got a partial line, we pull the rest until we get the full line
for isPrefix {
var lineFragment []byte
lineFragment, isPrefix, err = reader.ReadLine()
if err != nil {
return nil, err
}
line = append(line, lineFragment...)
}
// if the line is empty, we are done getting data from the git credential service
if len(line) == 0 {
break
}
parts := strings.SplitN(string(line), "=", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid Input String")
}
key := parts[0]
value := parts[1]
switch key {
case "username":
c.Username = value
case "password":
c.Password = value
case "protocol":
c.Protocol = value
case "host":
c.Host = value
case "path":
c.Path = value
case "url":
if err := parseCredentialURL(value, c); err != nil {
return nil, err
}
case "quit":
if err := parseQuit(value, c); err != nil {
return nil, err
}
default:
// do nothing
}
}
return nil, nil
}
func parseCredentialURL(rawurl string, c *Credential) error {
url, err := url.Parse(rawurl)
if err != nil {
return err
}
c.URL = url.String()
if url.Scheme != "" {
c.Protocol = url.Scheme
}
/*
* Match one of:
* (1) proto://<host>/...
* (2) proto://<user>@<host>/...
* (3) proto://<user>:<pass>@<host>/...
*/
if url.User != nil {
username := url.User.Username()
if username != "" {
c.Username = url.User.Username()
}
password, passwordSet := url.User.Password()
if passwordSet {
c.Password = password
}
}
c.Host = url.Host
c.Path = url.Path
return nil
}
func parseQuit(value string, c *Credential) error {
quit, err := strconv.Atoi(value)
if err != nil {
return err
}
c.Quit = quit
return nil
}