-
Notifications
You must be signed in to change notification settings - Fork 40
X bubble #133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zhubzy
wants to merge
2
commits into
main
Choose a base branch
from
x-bubble
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+708
−20
Open
X bubble #133
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,8 @@ | ||
| import { OAuth2Client, OAuth2Token } from '@badgateway/oauth2-client'; | ||
| import { | ||
| OAuth2Client, | ||
| OAuth2Token, | ||
| generateCodeVerifier, | ||
| } from '@badgateway/oauth2-client'; | ||
| import { | ||
| CredentialType, | ||
| OAUTH_PROVIDERS, | ||
|
|
@@ -44,6 +48,7 @@ export class OAuthService { | |
| credentialName?: string; | ||
| timestamp: number; | ||
| scopes: string[]; | ||
| codeVerifier?: string; // For PKCE (required by X/Twitter) | ||
| } | ||
| > = new Map(); | ||
|
|
||
|
|
@@ -75,6 +80,25 @@ export class OAuthService { | |
| 'Google OAuth credentials not configured. Set GOOGLE_OAUTH_CLIENT_ID and GOOGLE_OAUTH_CLIENT_SECRET' | ||
| ); | ||
| } | ||
|
|
||
| // X (Twitter) OAuth 2.0 configuration with PKCE | ||
| if (env.X_OAUTH_CLIENT_ID && env.X_OAUTH_CLIENT_SECRET) { | ||
| this.clients.set( | ||
| 'x', | ||
| new OAuth2Client({ | ||
| server: 'https://api.twitter.com', | ||
| clientId: env.X_OAUTH_CLIENT_ID, | ||
| clientSecret: env.X_OAUTH_CLIENT_SECRET, | ||
| authorizationEndpoint: 'https://twitter.com/i/oauth2/authorize', | ||
| tokenEndpoint: '/2/oauth2/token', | ||
| // PKCE is automatically handled by @badgateway/oauth2-client | ||
| }) | ||
| ); | ||
| } else { | ||
| console.warn( | ||
| 'X OAuth credentials not configured. Set X_OAUTH_CLIENT_ID and X_OAUTH_CLIENT_SECRET' | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -105,6 +129,15 @@ export class OAuthService { | |
| const defaultScopes = this.getDefaultScopes(provider, credentialType); | ||
| const requestedScopes = scopes || defaultScopes; | ||
|
|
||
| // For X (Twitter), generate PKCE code_verifier using library's helper | ||
| let codeVerifier: string | undefined; | ||
|
|
||
| if (provider === 'x') { | ||
| // Use the library's built-in PKCE code verifier generator | ||
| codeVerifier = await generateCodeVerifier(); | ||
| console.log(`Generated PKCE code_verifier for X OAuth`); | ||
| } | ||
|
|
||
| // Store state for CSRF protection with requested scopes (expires in 10 minutes) | ||
| this.stateStore.set(state, { | ||
| userId, | ||
|
|
@@ -113,37 +146,67 @@ export class OAuthService { | |
| credentialName, | ||
| timestamp, | ||
| scopes: requestedScopes, | ||
| codeVerifier, // Store for token exchange | ||
| }); | ||
|
|
||
| try { | ||
| // Get provider-specific authorization parameters from centralized config | ||
| const providerConfig = OAUTH_PROVIDERS[provider]; | ||
| const authorizationParams = providerConfig?.authorizationParams || {}; | ||
|
|
||
| const authUrl = await client.authorizationCode.getAuthorizeUri({ | ||
| // Build authorization options - library handles PKCE automatically when codeVerifier is provided | ||
| const authOptions: { | ||
| redirectUri: string; | ||
| scope: string[]; | ||
| state: string; | ||
| codeVerifier?: string; | ||
| [key: string]: string | string[] | undefined; | ||
| } = { | ||
| redirectUri, | ||
| scope: requestedScopes, | ||
| state, | ||
| ...authorizationParams, | ||
| }); | ||
| }; | ||
|
|
||
| // Check if our parameters are actually in the URL and manually add if missing | ||
| const urlObj = new URL(authUrl); | ||
|
|
||
| // If parameters are missing, manually add them | ||
| if ( | ||
| !urlObj.searchParams.has('access_type') && | ||
| authorizationParams.access_type | ||
| ) { | ||
| urlObj.searchParams.set('access_type', authorizationParams.access_type); | ||
| // For X, pass codeVerifier - library automatically generates code_challenge and adds PKCE params | ||
| if (provider === 'x' && codeVerifier) { | ||
| authOptions.codeVerifier = codeVerifier; | ||
| } | ||
| if (!urlObj.searchParams.has('prompt') && authorizationParams.prompt) { | ||
| urlObj.searchParams.set('prompt', authorizationParams.prompt); | ||
|
|
||
| const authUrl = | ||
| await client.authorizationCode.getAuthorizeUri(authOptions); | ||
|
|
||
| // For X (Twitter), ensure scopes are space-separated (X API requirement) | ||
| // Library might use comma-separated, so we fix it if needed | ||
| const urlObj = new URL(authUrl); | ||
| if (provider === 'x') { | ||
| const scopeParam = urlObj.searchParams.get('scope'); | ||
| if (scopeParam && scopeParam.includes(',')) { | ||
| const spaceSeparatedScopes = scopeParam | ||
| .split(',') | ||
| .map((s) => s.trim()) | ||
| .join(' '); | ||
| urlObj.searchParams.set('scope', spaceSeparatedScopes); | ||
| } | ||
| } | ||
|
|
||
| const finalAuthUrl = urlObj.toString(); | ||
| // For Google, ensure access_type and prompt are present if specified | ||
| if (provider === 'google') { | ||
| if ( | ||
| !urlObj.searchParams.has('access_type') && | ||
| authorizationParams.access_type | ||
| ) { | ||
| urlObj.searchParams.set( | ||
| 'access_type', | ||
| authorizationParams.access_type | ||
| ); | ||
| } | ||
| if (!urlObj.searchParams.has('prompt') && authorizationParams.prompt) { | ||
| urlObj.searchParams.set('prompt', authorizationParams.prompt); | ||
| } | ||
| } | ||
|
|
||
| return { authUrl: finalAuthUrl, state }; | ||
| return { authUrl: urlObj.toString(), state }; | ||
| } catch (error) { | ||
| // Clean up state on error | ||
| this.stateStore.delete(state); | ||
|
|
@@ -186,10 +249,22 @@ export class OAuthService { | |
|
|
||
| try { | ||
| // Exchange authorization code for tokens | ||
| const token = await client.authorizationCode.getToken({ | ||
| // Library requires code_verifier for PKCE (X/Twitter) - pass it if we have it | ||
| const tokenOptions: { | ||
| code: string; | ||
| redirectUri: string; | ||
| codeVerifier?: string; | ||
| } = { | ||
| code, | ||
| redirectUri, | ||
| }); | ||
| }; | ||
|
|
||
| // For X (Twitter), include code_verifier for PKCE token exchange | ||
| if (provider === 'x' && stateData.codeVerifier) { | ||
| tokenOptions.codeVerifier = stateData.codeVerifier; | ||
| } | ||
|
|
||
| const token = await client.authorizationCode.getToken(tokenOptions); | ||
|
|
||
| if (!token.refreshToken) { | ||
| console.warn( | ||
|
Comment on lines
269
to
270
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logging the PKCE code_verifier generation could expose sensitive authentication flow information in production logs. This log statement should be removed or converted to a debug-level log that's disabled in production.