Skip to content

Commit

Permalink
Update token response handling
Browse files Browse the repository at this point in the history
Registry authorization token is now taken from the response body rather than
the repsonse header.

Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn)
  • Loading branch information
Josh Hawn committed Dec 12, 2014
1 parent a16d068 commit 576cb50
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions registry/token.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package registry

import (
"encoding/json"
"errors"
"fmt"
"net/http"
Expand All @@ -10,6 +11,10 @@ import (
"github.com/docker/docker/utils"
)

type tokenResponse struct {
Token string `json:"token"`
}

func getToken(username, password string, params map[string]string, registryEndpoint *Endpoint, client *http.Client, factory *utils.HTTPRequestFactory) (token string, err error) {
realm, ok := params["realm"]
if !ok {
Expand Down Expand Up @@ -57,14 +62,20 @@ func getToken(username, password string, params map[string]string, registryEndpo
}
defer resp.Body.Close()

if !(resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusNoContent) {
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("token auth attempt for registry %s: %s request failed with status: %d %s", registryEndpoint, req.URL, resp.StatusCode, http.StatusText(resp.StatusCode))
}

token = resp.Header.Get("X-Auth-Token")
if token == "" {
return "", errors.New("token server did not include a token in the response header")
decoder := json.NewDecoder(resp.Body)

tr := new(tokenResponse)
if err = decoder.Decode(tr); err != nil {
return "", fmt.Errorf("unable to decode token response: %s", err)
}

if tr.Token == "" {
return "", errors.New("authorization server did not include a token in the response")
}

return token, nil
return tr.Token, nil
}

0 comments on commit 576cb50

Please sign in to comment.