22
22
credsDir = ""
23
23
)
24
24
25
+ type credential struct {
26
+ authtype string
27
+ username string
28
+ password string
29
+ credential string
30
+ skip bool
31
+ }
32
+
33
+ func (c * credential ) Serialize (capabilities map [string ]struct {}) map [string ][]string {
34
+ creds := make (map [string ][]string )
35
+ if c .skip {
36
+ // Do nothing.
37
+ } else if _ , ok := capabilities ["authtype" ]; ok && len (c .authtype ) != 0 && len (c .credential ) != 0 {
38
+ creds ["authtype" ] = []string {c .authtype }
39
+ creds ["credential" ] = []string {c .credential }
40
+ } else if len (c .username ) != 0 && len (c .password ) != 0 {
41
+ creds ["username" ] = []string {c .username }
42
+ creds ["password" ] = []string {c .password }
43
+ }
44
+ return creds
45
+ }
46
+
25
47
func init () {
26
48
if len (credsDir ) == 0 {
27
49
credsDir = os .Getenv ("CREDSDIR" )
@@ -70,35 +92,14 @@ func fill() {
70
92
}
71
93
72
94
hostPieces := strings .SplitN (firstEntryForKey (creds , "host" ), ":" , 2 )
73
- authtype , user , cred , err := credsForHostAndPath (hostPieces [0 ], firstEntryForKey (creds , "path" ))
95
+ credentials , err := credsForHostAndPath (hostPieces [0 ], firstEntryForKey (creds , "path" ))
74
96
if err != nil {
75
97
fmt .Fprintln (os .Stderr , err .Error ())
76
98
os .Exit (1 )
77
99
}
78
100
79
101
capas := discoverCapabilities (creds )
80
-
81
- switch authtype {
82
- case "skip" :
83
- case "" :
84
- if _ , ok := creds ["username" ]; ! ok {
85
- creds ["username" ] = []string {user }
86
- }
87
-
88
- if _ , ok := creds ["password" ]; ! ok {
89
- creds ["password" ] = []string {cred }
90
- }
91
- default :
92
- if _ , ok := capas ["authtype" ]; ok {
93
- if _ , ok := creds ["authtype" ]; ! ok {
94
- creds ["authtype" ] = []string {authtype }
95
- }
96
-
97
- if _ , ok := creds ["credential" ]; ! ok {
98
- creds ["credential" ] = []string {cred }
99
- }
100
- }
101
- }
102
+ creds = cred .Serialize (capas )
102
103
103
104
mode := os .Getenv ("LFS_TEST_CREDS_WWWAUTH" )
104
105
wwwauth := firstEntryForKey (creds , "wwwauth[]" )
@@ -145,7 +146,7 @@ func discoverCapabilities(creds map[string][]string) map[string]struct{} {
145
146
return capas
146
147
}
147
148
148
- func credsForHostAndPath (host , path string ) (string , string , string , error ) {
149
+ func credsForHostAndPath (host , path string ) (credential , error ) {
149
150
var hostFilename string
150
151
151
152
// We need hostFilename to end in a slash so that our credentials all
@@ -160,25 +161,31 @@ func credsForHostAndPath(host, path string) (string, string, string, error) {
160
161
161
162
if len (path ) > 0 {
162
163
pathFilename := fmt .Sprintf ("%s--%s" , hostFilename , strings .Replace (path , "/" , "-" , - 1 ))
163
- authtype , u , cred , err := credsFromFilename (pathFilename )
164
+ cred , err := credsFromFilename (pathFilename )
164
165
if err == nil {
165
- return authtype , u , cred , err
166
+ return cred , err
166
167
}
167
168
}
168
169
169
170
return credsFromFilename (hostFilename )
170
171
}
171
172
172
- func credsFromFilename (file string ) (string , string , string , error ) {
173
- credential , err := os .ReadFile (file )
173
+ func credsFromFilename (file string ) (credential , error ) {
174
+ fileContents , err := os .ReadFile (file )
174
175
if err != nil {
175
- return "" , "" , "" , fmt .Errorf ("Error opening %q: %s" , file , err )
176
+ return credential {} , fmt .Errorf ("Error opening %q: %s" , file , err )
176
177
}
177
- credsPieces := strings .SplitN (strings .TrimSpace (string (credential )), ":" , 3 )
178
+ credsPieces := strings .SplitN (strings .TrimSpace (string (fileContents )), ":" , 3 )
178
179
if len (credsPieces ) != 3 {
179
- return "" , "" , "" , fmt .Errorf ("Invalid data %q while reading %q" , string (credential ), file )
180
+ return credential {}, fmt .Errorf ("Invalid data %q while reading %q" , string (fileContents ), file )
181
+ }
182
+ if credsPieces [0 ] == "skip" {
183
+ return credential {skip : true }, nil
184
+ } else if len (credsPieces [0 ]) == 0 {
185
+ return credential {username : credsPieces [1 ], password : credsPieces [2 ]}, nil
186
+ } else {
187
+ return credential {authtype : credsPieces [0 ], credential : credsPieces [2 ]}, nil
180
188
}
181
- return credsPieces [0 ], credsPieces [1 ], credsPieces [2 ], nil
182
189
}
183
190
184
191
func log () {
0 commit comments