Skip to content

Commit

Permalink
Move bearerToken.readFromJSONBlob closer to its callers
Browse files Browse the repository at this point in the history
Only moves unchanged code, should not change behavior.

Signed-off-by: Miloslav Trmač <[email protected]>
  • Loading branch information
mtrmac committed Jun 1, 2023
1 parent 97bf204 commit 025f73c
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions docker/docker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,35 +141,6 @@ const (
noAuth
)

// readFromJSONBlob sets token data in dest from the provided JSON.
func (bt *bearerToken) readFromJSONBlob(blob []byte) error {
var token struct {
Token string `json:"token"`
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
IssuedAt time.Time `json:"issued_at"`
expirationTime time.Time
}
if err := json.Unmarshal(blob, &token); err != nil {
return err
}

bt.token = token.Token
if bt.token == "" {
bt.token = token.AccessToken
}

if token.ExpiresIn < minimumTokenLifetimeSeconds {
token.ExpiresIn = minimumTokenLifetimeSeconds
logrus.Debugf("Increasing token expiration to: %d seconds", token.ExpiresIn)
}
if token.IssuedAt.IsZero() {
token.IssuedAt = time.Now().UTC()
}
bt.expirationTime = token.IssuedAt.Add(time.Duration(token.ExpiresIn) * time.Second)
return nil
}

// this is cloned from docker/go-connections because upstream docker has changed
// it and make deps here fails otherwise.
// We'll drop this once we upgrade to docker 1.13.x deps.
Expand Down Expand Up @@ -838,6 +809,35 @@ func (c *dockerClient) getBearerToken(ctx context.Context, dest *bearerToken, ch
return dest.readFromJSONBlob(tokenBlob)
}

// readFromJSONBlob sets token data in dest from the provided JSON.
func (bt *bearerToken) readFromJSONBlob(blob []byte) error {
var token struct {
Token string `json:"token"`
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
IssuedAt time.Time `json:"issued_at"`
expirationTime time.Time
}
if err := json.Unmarshal(blob, &token); err != nil {
return err
}

bt.token = token.Token
if bt.token == "" {
bt.token = token.AccessToken
}

if token.ExpiresIn < minimumTokenLifetimeSeconds {
token.ExpiresIn = minimumTokenLifetimeSeconds
logrus.Debugf("Increasing token expiration to: %d seconds", token.ExpiresIn)
}
if token.IssuedAt.IsZero() {
token.IssuedAt = time.Now().UTC()
}
bt.expirationTime = token.IssuedAt.Add(time.Duration(token.ExpiresIn) * time.Second)
return nil
}

// detectPropertiesHelper performs the work of detectProperties which executes
// it at most once.
func (c *dockerClient) detectPropertiesHelper(ctx context.Context) error {
Expand Down

0 comments on commit 025f73c

Please sign in to comment.