Skip to content

Commit

Permalink
Make bearerToken.Token private
Browse files Browse the repository at this point in the history
No need to make it a public field now.

Should not change behavior.

Signed-off-by: Miloslav Trmač <[email protected]>
  • Loading branch information
mtrmac committed Jul 9, 2024
1 parent 57d7e83 commit dc3703b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
11 changes: 6 additions & 5 deletions docker/docker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ type extensionSignatureList struct {
Signatures []extensionSignature `json:"signatures"`
}

// bearerToken records a cached token we can use to authenticate.
type bearerToken struct {
Token string
token string
AccessToken string
ExpiresIn int
IssuedAt time.Time
Expand Down Expand Up @@ -172,13 +173,13 @@ func newBearerTokenFromHTTPResponseBody(res *http.Response) (*bearerToken, error
}

bt := &bearerToken{
Token: token.Token,
token: token.Token,
AccessToken: token.AccessToken,
ExpiresIn: token.ExpiresIn,
IssuedAt: token.IssuedAt,
}
if bt.Token == "" {
bt.Token = bt.AccessToken
if bt.token == "" {
bt.token = bt.AccessToken
}
if bt.ExpiresIn < minimumTokenLifetimeSeconds {
bt.ExpiresIn = minimumTokenLifetimeSeconds
Expand Down Expand Up @@ -799,7 +800,7 @@ func (c *dockerClient) setupRequestAuth(req *http.Request, extraScope *authScope
token = *t
c.tokenCache.Store(cacheKey, token)
}
registryToken = token.Token
registryToken = token.token
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", registryToken))
return nil
Expand Down
8 changes: 4 additions & 4 deletions docker/docker_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,23 @@ func TestNewBearerTokenFromHTTPResponseBody(t *testing.T) {
},
{ // "token"
input: `{"token":"IAmAToken","expires_in":100,"issued_at":"2018-01-01T10:00:02+00:00"}`,
expected: &bearerToken{Token: "IAmAToken", ExpiresIn: 100, IssuedAt: time.Unix(1514800802, 0)},
expected: &bearerToken{token: "IAmAToken", ExpiresIn: 100, IssuedAt: time.Unix(1514800802, 0)},
},
{ // "access_token"
input: `{"access_token":"IAmAToken","expires_in":100,"issued_at":"2018-01-01T10:00:02+00:00"}`,
expected: &bearerToken{Token: "IAmAToken", ExpiresIn: 100, IssuedAt: time.Unix(1514800802, 0)},
expected: &bearerToken{token: "IAmAToken", ExpiresIn: 100, IssuedAt: time.Unix(1514800802, 0)},
},
{ // Small expiry
input: `{"token":"IAmAToken","expires_in":1,"issued_at":"2018-01-01T10:00:02+00:00"}`,
expected: &bearerToken{Token: "IAmAToken", ExpiresIn: 60, IssuedAt: time.Unix(1514800802, 0)},
expected: &bearerToken{token: "IAmAToken", ExpiresIn: 60, IssuedAt: time.Unix(1514800802, 0)},
},
} {
token, err := newBearerTokenFromHTTPResponseBody(testTokenHTTPResponse(t, c.input))
if c.expected == nil {
assert.Error(t, err, c.input)
} else {
require.NoError(t, err, c.input)
assert.Equal(t, c.expected.Token, token.Token, c.input)
assert.Equal(t, c.expected.token, token.token, c.input)
assert.Equal(t, c.expected.ExpiresIn, token.ExpiresIn, c.input)
assert.True(t, c.expected.IssuedAt.Equal(token.IssuedAt),
"expected [%s] to equal [%s], it did not", token.IssuedAt, c.expected.IssuedAt)
Expand Down

0 comments on commit dc3703b

Please sign in to comment.