2015-07-29 20:12:01 +02:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2016-09-02 01:46:51 +02:00
|
|
|
"strings"
|
2015-07-29 20:12:01 +02:00
|
|
|
|
2020-08-24 13:18:39 +02:00
|
|
|
"github.com/distribution/distribution/v3/context"
|
|
|
|
"github.com/distribution/distribution/v3/registry/client/auth"
|
|
|
|
"github.com/distribution/distribution/v3/registry/client/auth/challenge"
|
2015-07-29 20:12:01 +02:00
|
|
|
)
|
|
|
|
|
2016-02-11 03:07:28 +01:00
|
|
|
const challengeHeader = "Docker-Distribution-Api-Version"
|
2015-07-29 20:12:01 +02:00
|
|
|
|
|
|
|
type userpass struct {
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
}
|
|
|
|
|
|
|
|
type credentials struct {
|
|
|
|
creds map[string]userpass
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c credentials) Basic(u *url.URL) (string, string) {
|
|
|
|
up := c.creds[u.String()]
|
|
|
|
|
|
|
|
return up.username, up.password
|
|
|
|
}
|
|
|
|
|
2016-03-04 09:34:17 +01:00
|
|
|
func (c credentials) RefreshToken(u *url.URL, service string) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c credentials) SetRefreshToken(u *url.URL, service, token string) {
|
|
|
|
}
|
|
|
|
|
2016-02-17 19:42:34 +01:00
|
|
|
// configureAuth stores credentials for challenge responses
|
2016-09-02 01:46:51 +02:00
|
|
|
func configureAuth(username, password, remoteURL string) (auth.CredentialStore, error) {
|
|
|
|
creds := map[string]userpass{}
|
|
|
|
|
|
|
|
authURLs, err := getAuthURLs(remoteURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, url := range authURLs {
|
|
|
|
context.GetLogger(context.Background()).Infof("Discovered token authentication URL: %s", url)
|
|
|
|
creds[url] = userpass{
|
2015-07-29 20:12:01 +02:00
|
|
|
username: username,
|
|
|
|
password: password,
|
2016-09-02 01:46:51 +02:00
|
|
|
}
|
2015-07-29 20:12:01 +02:00
|
|
|
}
|
2016-09-02 01:46:51 +02:00
|
|
|
|
2015-07-29 20:12:01 +02:00
|
|
|
return credentials{creds: creds}, nil
|
|
|
|
}
|
|
|
|
|
2016-09-02 01:46:51 +02:00
|
|
|
func getAuthURLs(remoteURL string) ([]string, error) {
|
|
|
|
authURLs := []string{}
|
|
|
|
|
|
|
|
resp, err := http.Get(remoteURL + "/v2/")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2016-11-08 02:13:56 +01:00
|
|
|
for _, c := range challenge.ResponseChallenges(resp) {
|
2016-09-02 01:46:51 +02:00
|
|
|
if strings.EqualFold(c.Scheme, "bearer") {
|
|
|
|
authURLs = append(authURLs, c.Parameters["realm"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return authURLs, nil
|
|
|
|
}
|
|
|
|
|
2016-11-08 02:13:56 +01:00
|
|
|
func ping(manager challenge.Manager, endpoint, versionHeader string) error {
|
2015-07-29 20:12:01 +02:00
|
|
|
resp, err := http.Get(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2017-09-29 22:27:49 +02:00
|
|
|
return manager.AddResponse(resp)
|
2015-07-29 20:12:01 +02:00
|
|
|
}
|