...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package transport
16
17 import (
18 "encoding/base64"
19 "fmt"
20 "net/http"
21
22 "github.com/google/go-containerregistry/pkg/authn"
23 )
24
25 type basicTransport struct {
26 inner http.RoundTripper
27 auth authn.Authenticator
28 target string
29 }
30
31 var _ http.RoundTripper = (*basicTransport)(nil)
32
33
34 func (bt *basicTransport) RoundTrip(in *http.Request) (*http.Response, error) {
35 if bt.auth != authn.Anonymous {
36 auth, err := bt.auth.Authorization()
37 if err != nil {
38 return nil, err
39 }
40
41
42
43
44
45
46 if in.Host == bt.target || in.URL.Host == bt.target {
47 if bearer := auth.RegistryToken; bearer != "" {
48 hdr := fmt.Sprintf("Bearer %s", bearer)
49 in.Header.Set("Authorization", hdr)
50 } else if user, pass := auth.Username, auth.Password; user != "" && pass != "" {
51 delimited := fmt.Sprintf("%s:%s", user, pass)
52 encoded := base64.StdEncoding.EncodeToString([]byte(delimited))
53 hdr := fmt.Sprintf("Basic %s", encoded)
54 in.Header.Set("Authorization", hdr)
55 } else if token := auth.Auth; token != "" {
56 hdr := fmt.Sprintf("Basic %s", token)
57 in.Header.Set("Authorization", hdr)
58 }
59 }
60 }
61 return bt.inner.RoundTrip(in)
62 }
63
View as plain text