Skip to content

Commit

Permalink
Remove unnecessary fields from bearerToken
Browse files Browse the repository at this point in the history
These fields need to exist when parsing JSON; but we can just
record the outcome of processing them.

Should not change behavior.

Signed-off-by: Miloslav Trmač <[email protected]>
  • Loading branch information
mtrmac committed Jul 9, 2024
1 parent 1b7daaf commit 9b43674
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
23 changes: 9 additions & 14 deletions docker/docker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ type extensionSignatureList struct {
// bearerToken records a cached token we can use to authenticate.
type bearerToken struct {
token string
AccessToken string
ExpiresIn int
IssuedAt time.Time
expirationTime time.Time
}

Expand Down Expand Up @@ -173,22 +170,20 @@ func newBearerTokenFromHTTPResponseBody(res *http.Response) (*bearerToken, error
}

bt := &bearerToken{
token: token.Token,
AccessToken: token.AccessToken,
ExpiresIn: token.ExpiresIn,
IssuedAt: token.IssuedAt,
token: token.Token,
}
if bt.token == "" {
bt.token = bt.AccessToken
bt.token = token.AccessToken
}
if bt.ExpiresIn < minimumTokenLifetimeSeconds {
bt.ExpiresIn = minimumTokenLifetimeSeconds
logrus.Debugf("Increasing token expiration to: %d seconds", bt.ExpiresIn)

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

Expand Down
10 changes: 3 additions & 7 deletions docker/docker_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ 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), expirationTime: time.Unix(1514800802+100, 0)},
expected: &bearerToken{token: "IAmAToken", expirationTime: time.Unix(1514800802+100, 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), expirationTime: time.Unix(1514800802+100, 0)},
expected: &bearerToken{token: "IAmAToken", expirationTime: time.Unix(1514800802+100, 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), expirationTime: time.Unix(1514800802+60, 0)},
expected: &bearerToken{token: "IAmAToken", expirationTime: time.Unix(1514800802+60, 0)},
},
} {
token, err := newBearerTokenFromHTTPResponseBody(testTokenHTTPResponse(t, c.input))
Expand All @@ -134,9 +134,6 @@ func TestNewBearerTokenFromHTTPResponseBody(t *testing.T) {
} else {
require.NoError(t, err, 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)
assert.True(t, c.expected.expirationTime.Equal(token.expirationTime),
"expected [%s] to equal [%s], it did not", token.expirationTime, c.expected.expirationTime)
}
Expand All @@ -149,7 +146,6 @@ func TestNewBearerTokenFromHTTPResponseBodyIssuedAtZero(t *testing.T) {
tokenBlob := fmt.Sprintf(`{"token":"IAmAToken","expires_in":100,"issued_at":"%s"}`, zeroTime)
token, err := newBearerTokenFromHTTPResponseBody(testTokenHTTPResponse(t, tokenBlob))
require.NoError(t, err)
assert.False(t, token.IssuedAt.Before(now), "expected [%s] not to be before [%s]", token.IssuedAt, now)
expectedExpiration := now.Add(time.Duration(100) * time.Second)
require.False(t, token.expirationTime.Before(expectedExpiration),
"expected [%s] not to be before [%s]", token.expirationTime, expectedExpiration)
Expand Down

0 comments on commit 9b43674

Please sign in to comment.