Skip to content

Comments

Added a auto-refresh of token after rate limit sleep#20

Open
mwnickerson wants to merge 1 commit intoSpecterOps:mainfrom
mwnickerson:feat/auto-reauth-after-sleep
Open

Added a auto-refresh of token after rate limit sleep#20
mwnickerson wants to merge 1 commit intoSpecterOps:mainfrom
mwnickerson:feat/auto-reauth-after-sleep

Conversation

@mwnickerson
Copy link

Summary

  • Adds automatic refresh of GitHub App installation tokens after extended sleeps (e.g., rate‑limit backoffs or OS sleep) to prevent using expired credentials.
  • Keeps PAT sessions unchanged; only GitHub App sessions refresh.

What Changed

  • New Invoke-OptionalTokenRefresh helper to refresh when available without hard dependency.
  • New Update-GitHubSessionToken to renew installation tokens when expired or expiring within 5 minutes.
  • New Get-GitHubAppInstallationToken to fetch a fresh token using the app’s JWT.
  • New-GitHubJwtSession now persists AppCredentials and TokenExpiry on the session for later refreshes.
  • Wait-GithubRateLimitReached now calls Invoke-OptionalTokenRefresh after sleeping.

Why

  • Long sleeps (rate limits or machine sleep) often outlive installation token TTL, causing subsequent calls to fail with 401/403.
  • Proactively refreshing prevents mid-run failures and reduces manual intervention.

How It Works

  • For sessions created via New-GitHubJwtSession, we store ClientId, PrivateKeyPath, AppId, and TokenExpiry.
  • After a long sleep (rate limit), Invoke-OptionalTokenRefresh runs; if the token is expired or within 5 minutes of expiry, a new token is fetched and headers are updated.
  • No-ops for PAT sessions (no AppCredentials on the session).

Backward Compatibility

  • PAT flows unchanged.
  • GitHub App flows improve resilience; no breaking API surface changes.

Testing

  • App session: run a request, simulate a long sleep beyond token TTL, verify automatic refresh and continued success.
  • Near-expiry: set TokenExpiry to <5 minutes from now and verify refresh triggers.
  • PAT session: verify no refresh call is attempted and behavior remains the same.
  • Rate limit: force rate-limit sleep and confirm refresh occurs post-sleep when appropriate.

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