Skip to content

Commit 76ccd68

Browse files
Add getValue askpass function
Add a new `getValue` askpass function that returns the value of a particular credential type by returning the existing value if that credential has already been provided, or by querying the askpass program if that credential has not yet been provided.
1 parent 54fd43a commit 76ccd68

File tree

1 file changed

+55
-29
lines changed

1 file changed

+55
-29
lines changed

lfsapi/creds.go

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ type AskPassCredentialHelper struct {
7878
Program string
7979
}
8080

81+
type credValueType int
82+
83+
const (
84+
credValueTypeUnknown credValueType = iota
85+
credValueTypeUsername
86+
credValueTypePassword
87+
)
88+
8189
// Fill implements fill by running the ASKPASS program and returning its output
8290
// as a password encoded in the Creds type given the key "password".
8391
//
@@ -98,54 +106,72 @@ func (a *AskPassCredentialHelper) Fill(what Creds) (Creds, error) {
98106

99107
creds := make(Creds)
100108

101-
if givenUser, ok := what["username"]; ok {
102-
creds["username"] = givenUser
103-
} else {
104-
username, err := a.getFromProgram("Username", u)
105-
if err != nil {
106-
return nil, err
107-
}
108-
109-
creds["username"] = username
109+
username, err := a.getValue(what, credValueTypeUsername, u)
110+
if err != nil {
111+
return nil, err
110112
}
113+
creds["username"] = username
111114

112-
if len(creds["username"]) > 0 {
115+
if len(username) > 0 {
113116
// If a non-empty username was given, add it to the URL via func
114117
// 'net/url.User()'.
115118
u.User = url.User(creds["username"])
116119
}
117120

118-
if givenPass, ok := what["password"]; ok {
119-
creds["password"] = givenPass
120-
} else {
121-
password, err := a.getFromProgram("Password", u)
122-
if err != nil {
123-
return nil, err
124-
}
125-
126-
creds["password"] = password
121+
password, err := a.getValue(what, credValueTypePassword, u)
122+
if err != nil {
123+
return nil, err
127124
}
128-
129-
creds["username"] = strings.TrimSpace(creds["username"])
130-
creds["password"] = strings.TrimSpace(creds["password"])
125+
creds["password"] = password
131126

132127
return creds, nil
133128
}
134129

135-
func (a *AskPassCredentialHelper) getFromProgram(valueType string, u *url.URL) (string, error) {
130+
func (a *AskPassCredentialHelper) getValue(what Creds, valueType credValueType, u *url.URL) (string, error) {
131+
var valueString string
132+
133+
switch valueType {
134+
case credValueTypeUsername:
135+
valueString = "username"
136+
case credValueTypePassword:
137+
valueString = "password"
138+
default:
139+
return "", errors.Errorf("Invalid Credential type queried from AskPass")
140+
}
141+
142+
// Return the existing credential if it was already provided, otherwise
143+
// query AskPass for it
144+
if given, ok := what[valueString]; ok {
145+
return given, nil
146+
}
147+
return a.getFromProgram(valueType, u)
148+
}
149+
150+
func (a *AskPassCredentialHelper) getFromProgram(valueType credValueType, u *url.URL) (string, error) {
136151
var (
137152
value bytes.Buffer
138153
err bytes.Buffer
154+
155+
valueString string
139156
)
140157

141-
// 'ucmd' will run the GIT_ASKPASS (or core.askpass) command prompting
158+
switch valueType {
159+
case credValueTypeUsername:
160+
valueString = "Username"
161+
case credValueTypePassword:
162+
valueString = "Password"
163+
default:
164+
return "", errors.Errorf("Invalid Credential type queried from AskPass")
165+
}
166+
167+
// 'cmd' will run the GIT_ASKPASS (or core.askpass) command prompting
142168
// for the desired valueType (`Username` or `Password`)
143-
ucmd := exec.Command(a.Program, a.args(fmt.Sprintf("%s for %q", valueType, u))...)
144-
ucmd.Stderr = &err
145-
ucmd.Stdout = &value
169+
cmd := exec.Command(a.Program, a.args(fmt.Sprintf("%s for %q", valueString, u))...)
170+
cmd.Stderr = &err
171+
cmd.Stdout = &value
146172

147-
tracerx.Printf("creds: filling with GIT_ASKPASS: %s", strings.Join(ucmd.Args, " "))
148-
if err := ucmd.Run(); err != nil {
173+
tracerx.Printf("creds: filling with GIT_ASKPASS: %s", strings.Join(cmd.Args, " "))
174+
if err := cmd.Run(); err != nil {
149175
return "", err
150176
}
151177

0 commit comments

Comments
 (0)