-
Notifications
You must be signed in to change notification settings - Fork 1
Adds Google Drive OAuth support #75
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,8 +61,9 @@ export async function handleConnectionDone(req: Request, res: Response): Promise | |
| // Build multi-provider tokens structure for JWT creation | ||
| const atlassianTokens = providerTokens['atlassian']; | ||
| const figmaTokens = providerTokens['figma']; | ||
| const googleTokens = providerTokens['google']; | ||
|
|
||
| if (!atlassianTokens && !figmaTokens) { | ||
| if (!atlassianTokens && !figmaTokens && !googleTokens) { | ||
| throw new Error('No provider tokens found - please connect at least one service'); | ||
| } | ||
|
|
||
|
|
@@ -93,6 +94,18 @@ export async function handleConnectionDone(req: Request, res: Response): Promise | |
| console.log(' Warning: Figma tokens incomplete (missing access or refresh token)'); | ||
| } | ||
|
|
||
| if (googleTokens && googleTokens.access_token && googleTokens.refresh_token) { | ||
| console.log(' Adding Google credentials to JWT'); | ||
| multiProviderTokens.google = { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similar here, this code is the same for each provider
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as before. This can be added to the next ticket for authentication improvement. |
||
| access_token: googleTokens.access_token, | ||
| refresh_token: googleTokens.refresh_token, | ||
| expires_at: googleTokens.expires_at, | ||
| scope: googleTokens.scope, | ||
| }; | ||
| } else if (googleTokens) { | ||
| console.log(' Warning: Google tokens incomplete (missing access or refresh token)'); | ||
| } | ||
|
|
||
| // Create JWT access token with nested provider structure | ||
| const tokenOptions = { | ||
| resource: req.session.mcpResource || process.env.VITE_AUTH_SERVER_URL, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ import crypto from 'crypto'; | |
|
|
||
| // All providers that must be connected for auto-redirect to /auth/done | ||
| // If only some providers are connected, user can manually click "Done" button | ||
| const REQUIRED_PROVIDERS = ['atlassian', 'figma'] as const; | ||
| const REQUIRED_PROVIDERS = ['atlassian', 'figma', 'google'] as const; | ||
|
|
||
| /** | ||
| * Renders the connection hub UI | ||
|
|
@@ -185,6 +185,15 @@ export function renderConnectionHub(req: Request, res: Response): void { | |
| } | ||
| </div> | ||
|
|
||
| <div class="provider ${connectedProviders.includes('google') ? 'connected' : ''}"> | ||
| <h2>Google Drive</h2> | ||
| <p>Access Google Drive files and user information</p> | ||
|
||
| ${connectedProviders.includes('google') | ||
| ? '<span class="status">✓ Connected</span>' | ||
| : '<button onclick="location.href=\'/auth/connect/google\'">Connect Google Drive</button>' | ||
| } | ||
| </div> | ||
|
|
||
| <div class="done-section"> | ||
| <button class="done-button" onclick="location.href='/auth/done'" ${connectedProviders.length === 0 ? 'disabled' : ''}> | ||
| Done - Create Session | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /** | ||
| * Google Drive API Client Factory | ||
| * | ||
| * Provides API client instances for OAuth authentication. | ||
| * Uses native fetch (no additional dependencies). | ||
| * | ||
| * Authentication Methods: | ||
| * - OAuth: Uses Bearer tokens from OAuth 2.0 flow (for user delegation) | ||
| */ | ||
|
|
||
| import type { DriveAboutResponse } from './types.js'; | ||
|
|
||
| /** | ||
| * Google API client interface | ||
| * | ||
| * Provides methods for making authenticated requests to Google APIs. | ||
| * All methods have the access token pre-configured via closure. | ||
| */ | ||
| export interface GoogleClient { | ||
| /** | ||
| * Make an authenticated fetch request to Google API | ||
| * @param url - The full URL to fetch | ||
| * @param options - Standard fetch options (method, body, etc.) | ||
| * @returns Promise resolving to fetch Response | ||
| */ | ||
| fetch: (url: string, options?: RequestInit) => Promise<Response>; | ||
|
|
||
| /** | ||
| * Authentication type used by this client | ||
| */ | ||
| authType: 'oauth'; | ||
| } | ||
|
|
||
| /** | ||
| * Create a Google API client using OAuth access token | ||
| * @param accessToken - OAuth 2.0 Bearer token | ||
| * @returns API client with Drive operations | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * const client = createGoogleClient(token); | ||
| * | ||
| * // Fetch with auth automatically included | ||
| * const response = await client.fetch( | ||
| * 'https://www.googleapis.com/drive/v3/about?fields=user', | ||
| * { method: 'GET' } | ||
| * ); | ||
| * ``` | ||
| */ | ||
| export function createGoogleClient(accessToken: string): GoogleClient { | ||
| return { | ||
| authType: 'oauth', | ||
|
|
||
| fetch: async (url: string, options: RequestInit = {}) => { | ||
| // Token is captured in this closure! | ||
| return fetch(url, { | ||
| ...options, | ||
| headers: { | ||
| ...options.headers, | ||
| 'Authorization': `Bearer ${accessToken}`, | ||
| 'Accept': 'application/json', | ||
| }, | ||
| }); | ||
| }, | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /** | ||
| * Google Drive API interaction helpers | ||
| * Reusable functions for Google Drive API calls | ||
| */ | ||
|
|
||
| import type { GoogleClient } from './google-api-client.js'; | ||
| import type { DriveAboutResponse } from './types.js'; | ||
|
|
||
| /** | ||
| * Get the authenticated user's Google Drive information | ||
| * @param client - Authenticated Google API client | ||
| * @returns Promise resolving to Drive user information | ||
| * @throws Error if the API request fails | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * const client = createGoogleClient(token); | ||
| * const userData = await getGoogleDriveUser(client); | ||
| * console.log(userData.user.emailAddress); | ||
| * ``` | ||
| */ | ||
| export async function getGoogleDriveUser(client: GoogleClient): Promise<DriveAboutResponse> { | ||
| const response = await client.fetch('https://www.googleapis.com/drive/v3/about?fields=user'); | ||
|
|
||
| if (!response.ok) { | ||
| const errorText = await response.text(); | ||
| throw new Error(`Drive API error (${response.status}): ${errorText}`); | ||
| } | ||
|
|
||
| return response.json() as Promise<DriveAboutResponse>; | ||
| } |
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.
I think we should do some cleanup to make our auth providers more abstract and code like this just loops through them.
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.
I would suggest creating a new ticket to improve the overall authentication code. I would rather not add new code to the current implementation on this MR. Do you mind creating a ticket and assigning it to me?