Skip to content

Use expirationDurationInSeconds for token refresh window - #243

Merged
kylebrowning merged 1 commit into
mainfrom
refactor/auth-token-window
Jun 26, 2026
Merged

Use expirationDurationInSeconds for token refresh window#243
kylebrowning merged 1 commit into
mainfrom
refactor/auth-token-window

Conversation

@kylebrowning

Copy link
Copy Markdown
Owner

Problem

APNSAuthenticationTokenManager declares expirationDurationInSeconds = .seconds(60 * 55) but never reads it — the 55-minute refresh window is hardcoded inline in nextValidToken. Changing the named constant would silently have no effect, a latent foot-gun.

Separately, testTokenIsRefreshed asserts only that two token strings differ — but ECDSA P256 signatures are randomized, so the strings differ even when the payload is unchanged. The test passes for the wrong reason and proves little about the refresh.

Changes

  • Reference expirationDurationInSeconds in the refresh check so it is the single source of truth.
  • testTokenReusedJustBeforeBoundaryAndRefreshedAtBoundary pins the [0, 55min) reuse window at the exact boundary (reused at 54:59, refreshed at 55:00).
  • testRefreshedTokenIsStructurallyValid decodes the refreshed JWT and asserts the header/claims (alg, kid, iss), instead of relying on string inequality.

Note

I deliberately did not change the JWT iat from DispatchWallTime to the injected Clock: ContinuousClock is monotonic and cannot yield epoch seconds, so wall-clock time is the correct source for iat, while the injected clock correctly measures the refresh interval.

Testing

swift test --filter APNSAuthenticationTokenManagerTests — 5 tests pass.

The 55-minute refresh window was hardcoded inline in `nextValidToken` while the
named `expirationDurationInSeconds` constant sat unused — so changing the
constant silently had no effect. Reference the constant directly so it is the
single source of truth.

Also strengthen the token-manager tests:
- `testTokenReusedJustBeforeBoundaryAndRefreshedAtBoundary` pins the `[0, 55min)`
  reuse window at the exact boundary.
- `testRefreshedTokenIsStructurallyValid` decodes the refreshed JWT and asserts
  the header/claims, rather than relying solely on string inequality (ECDSA
  signatures are randomized, so a different string alone proves little).
@kylebrowning
kylebrowning merged commit 4a412e7 into main Jun 26, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant