The only uses of RequestAuthorization and its associated functions were
removed in 19515a7ad859b28c474d81e756ac245afcd968e3 ("Update graph to
use vendored distribution client for the v2 codepath")
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
| ... | ... |
@@ -6,100 +6,11 @@ import ( |
| 6 | 6 |
"io/ioutil" |
| 7 | 7 |
"net/http" |
| 8 | 8 |
"strings" |
| 9 |
- "sync" |
|
| 10 |
- "time" |
|
| 11 | 9 |
|
| 12 | 10 |
"github.com/Sirupsen/logrus" |
| 13 | 11 |
"github.com/docker/docker/cliconfig" |
| 14 | 12 |
) |
| 15 | 13 |
|
| 16 |
-type RequestAuthorization struct {
|
|
| 17 |
- authConfig *cliconfig.AuthConfig |
|
| 18 |
- registryEndpoint *Endpoint |
|
| 19 |
- resource string |
|
| 20 |
- scope string |
|
| 21 |
- actions []string |
|
| 22 |
- |
|
| 23 |
- tokenLock sync.Mutex |
|
| 24 |
- tokenCache string |
|
| 25 |
- tokenExpiration time.Time |
|
| 26 |
-} |
|
| 27 |
- |
|
| 28 |
-func NewRequestAuthorization(authConfig *cliconfig.AuthConfig, registryEndpoint *Endpoint, resource, scope string, actions []string) *RequestAuthorization {
|
|
| 29 |
- return &RequestAuthorization{
|
|
| 30 |
- authConfig: authConfig, |
|
| 31 |
- registryEndpoint: registryEndpoint, |
|
| 32 |
- resource: resource, |
|
| 33 |
- scope: scope, |
|
| 34 |
- actions: actions, |
|
| 35 |
- } |
|
| 36 |
-} |
|
| 37 |
- |
|
| 38 |
-func (auth *RequestAuthorization) getToken() (string, error) {
|
|
| 39 |
- auth.tokenLock.Lock() |
|
| 40 |
- defer auth.tokenLock.Unlock() |
|
| 41 |
- now := time.Now() |
|
| 42 |
- if now.Before(auth.tokenExpiration) {
|
|
| 43 |
- logrus.Debugf("Using cached token for %s", auth.authConfig.Username)
|
|
| 44 |
- return auth.tokenCache, nil |
|
| 45 |
- } |
|
| 46 |
- |
|
| 47 |
- for _, challenge := range auth.registryEndpoint.AuthChallenges {
|
|
| 48 |
- switch strings.ToLower(challenge.Scheme) {
|
|
| 49 |
- case "basic": |
|
| 50 |
- // no token necessary |
|
| 51 |
- case "bearer": |
|
| 52 |
- logrus.Debugf("Getting bearer token with %s for %s", challenge.Parameters, auth.authConfig.Username)
|
|
| 53 |
- params := map[string]string{}
|
|
| 54 |
- for k, v := range challenge.Parameters {
|
|
| 55 |
- params[k] = v |
|
| 56 |
- } |
|
| 57 |
- params["scope"] = fmt.Sprintf("%s:%s:%s", auth.resource, auth.scope, strings.Join(auth.actions, ","))
|
|
| 58 |
- token, err := getToken(auth.authConfig.Username, auth.authConfig.Password, params, auth.registryEndpoint) |
|
| 59 |
- if err != nil {
|
|
| 60 |
- return "", err |
|
| 61 |
- } |
|
| 62 |
- auth.tokenCache = token |
|
| 63 |
- auth.tokenExpiration = now.Add(time.Minute) |
|
| 64 |
- |
|
| 65 |
- return token, nil |
|
| 66 |
- default: |
|
| 67 |
- logrus.Infof("Unsupported auth scheme: %q", challenge.Scheme)
|
|
| 68 |
- } |
|
| 69 |
- } |
|
| 70 |
- |
|
| 71 |
- // Do not expire cache since there are no challenges which use a token |
|
| 72 |
- auth.tokenExpiration = time.Now().Add(time.Hour * 24) |
|
| 73 |
- |
|
| 74 |
- return "", nil |
|
| 75 |
-} |
|
| 76 |
- |
|
| 77 |
-// Checks that requests to the v2 registry can be authorized. |
|
| 78 |
-func (auth *RequestAuthorization) CanAuthorizeV2() bool {
|
|
| 79 |
- if len(auth.registryEndpoint.AuthChallenges) == 0 {
|
|
| 80 |
- return true |
|
| 81 |
- } |
|
| 82 |
- scope := fmt.Sprintf("%s:%s:%s", auth.resource, auth.scope, strings.Join(auth.actions, ","))
|
|
| 83 |
- if _, err := loginV2(auth.authConfig, auth.registryEndpoint, scope); err != nil {
|
|
| 84 |
- logrus.Debugf("Cannot authorize against V2 endpoint: %s", auth.registryEndpoint)
|
|
| 85 |
- return false |
|
| 86 |
- } |
|
| 87 |
- return true |
|
| 88 |
-} |
|
| 89 |
- |
|
| 90 |
-func (auth *RequestAuthorization) Authorize(req *http.Request) error {
|
|
| 91 |
- token, err := auth.getToken() |
|
| 92 |
- if err != nil {
|
|
| 93 |
- return err |
|
| 94 |
- } |
|
| 95 |
- if token != "" {
|
|
| 96 |
- req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
| 97 |
- } else if auth.authConfig.Username != "" && auth.authConfig.Password != "" {
|
|
| 98 |
- req.SetBasicAuth(auth.authConfig.Username, auth.authConfig.Password) |
|
| 99 |
- } |
|
| 100 |
- return nil |
|
| 101 |
-} |
|
| 102 |
- |
|
| 103 | 14 |
// Login tries to register/login to the registry server. |
| 104 | 15 |
func Login(authConfig *cliconfig.AuthConfig, registryEndpoint *Endpoint) (string, error) {
|
| 105 | 16 |
// Separates the v2 registry login logic from the v1 logic. |