Use @vercel/oidc utilities to reduce auth duplication#34
Conversation
Pass teamId and projectId to getVercelOidcToken() to enable token refresh without needing to read from .vercel/project.json. This removes duplication between @vercel/oidc and Sandbox's credential management. Changes: - get-credentials.ts: Pass opts.teamId/projectId when calling getVercelOidcToken() - jwt-expiry.ts: Pass payload.owner_id/project_id when refreshing tokens Benefits: - Token refresh works without .vercel/project.json when credentials are known - Reduces filesystem dependencies in token refresh flow - Maintains backward compatibility - still parses JWT for credential extraction - All 57 tests continue to pass
Replaced Sandbox's custom auth refresh logic with @vercel/oidc's getVercelCliToken() which handles: - Reading auth.json - Checking token expiry - Refreshing with OAuth - Saving updated tokens Removed code: - refreshToken() function (~30 lines) - Imports from @vercel/sandbox/dist/auth (getAuth, updateAuthConfig, OAuth, isOAuthError) Added: - Import getVercelCliToken, NoAuthConfigError, TokenExpiredError, RefreshFailedError from @vercel/oidc - Error handling to trigger login when auth is missing/expired Benefits: - 32 net lines removed (65 deletions, 33 insertions) - Single source of truth for auth refresh logic - Better error handling with specific error types - All tests passing (6/6)
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Follows the rename of NoAuthConfigError to NoAuthError in @vercel/oidc. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Follows the removal of TokenExpiredError in @vercel/oidc, which now only throws NoAuthError or RefreshFailedError. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Follows the rename of NoAuthError to AccessTokenMissingError in @vercel/oidc. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
| refreshed === "invalid refresh token" | ||
| // Try to get CLI token, which handles auth.json reading and refresh | ||
| try { | ||
| return await getVercelCliToken(); |
There was a problem hiding this comment.
Do we need an access token? Or could we also get an OIDC token for Sandbox?
There was a problem hiding this comment.
Handling the access token is good idea. But then it should actually be called "access token". And we should also allow it be specified via an env var, e.g. VERCEL_TOKEN. But IMHO it shouldn't be API'd to CLI specifically.
Follows the rename of RefreshFailedError to RefreshAccessTokenFailedError in @vercel/oidc. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Simplifies token management by using the new bufferMs option in getVercelOidcToken() instead of maintaining a separate JwtExpiry class. This consolidates token expiry logic in the @vercel/oidc package. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Follows the rename of bufferMs to expirationBufferMs in @vercel/oidc. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
3165adc to
76d85ab
Compare
| refreshed === "invalid refresh token" | ||
| // Try to get CLI token, which handles auth.json reading and refresh | ||
| try { | ||
| return await getVercelCliToken(); |
There was a problem hiding this comment.
Handling the access token is good idea. But then it should actually be called "access token". And we should also allow it be specified via an env var, e.g. VERCEL_TOKEN. But IMHO it shouldn't be API'd to CLI specifically.
| "@types/ms": "^2.1.0", | ||
| "@types/node": "^22.15.12", | ||
| "@vercel/oidc": "^3.1.0", | ||
| "@vercel/oidc": "file:../../../vercel/packages/oidc", |
There was a problem hiding this comment.
I'm working on it locally, this is a local install path for local testing. Once the @vercel/oidc PR ships I'll update it.
I'm not sure, access token can mean anything, but here we specifically trying to get the Vercel CLI access token. E.g. gateway has its own access token independent of OIDC and CLI. I think |
Summary
This PR refactors the authentication logic to use utilities from
@vercel/oidc, reducing code duplication and improving maintainability.Changes
@vercel/oidcoptional parameters to reduce duplication in auth flowRelated
Depends on vercel/vercel#14864 which adds the utilities to
@vercel/oidcpackage.Test plan
🤖 Generated with Claude Code