-
Notifications
You must be signed in to change notification settings - Fork 98
/
credentials.go
74 lines (60 loc) · 1.52 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
package crproxy
import (
"net/http"
"net/url"
"strings"
"github.com/docker/distribution/registry/client/auth/challenge"
)
type Userpass struct {
Username string
Password string
}
type basicCredentials struct {
credentials map[string]Userpass
}
func newBasicCredentials(cred map[string]Userpass, domainAlias func(string) string, hostScheme func(string) string) (*basicCredentials, error) {
bc := &basicCredentials{
credentials: map[string]Userpass{},
}
for domain, c := range cred {
urls, err := getAuthURLs(hostScheme(domain)+"://"+domain, domainAlias)
if err != nil {
return nil, err
}
for _, u := range urls {
bc.credentials[u] = c
}
}
return bc, nil
}
func (c *basicCredentials) Basic(u *url.URL) (string, string) {
up := c.credentials[u.String()]
return up.Username, up.Password
}
func (c *basicCredentials) RefreshToken(u *url.URL, service string) string {
return ""
}
func (c *basicCredentials) SetRefreshToken(u *url.URL, service, token string) {
}
func getAuthURLs(remoteURL string, domainAlias func(string) string) ([]string, error) {
authURLs := []string{}
u, err := url.Parse(remoteURL)
if err != nil {
return nil, err
}
if domainAlias != nil {
u.Host = domainAlias(u.Host)
}
remoteURL = u.String()
resp, err := http.Get(remoteURL + "/v2/")
if err != nil {
return nil, err
}
defer resp.Body.Close()
for _, c := range challenge.ResponseChallenges(resp) {
if strings.EqualFold(c.Scheme, "bearer") {
authURLs = append(authURLs, c.Parameters["realm"])
}
}
return authURLs, nil
}