From fee3d54a6ab28f817d8c70e2e8335c2d5adf77d5 Mon Sep 17 00:00:00 2001 From: Allen Hutchison Date: Fri, 14 Nov 2025 12:51:02 -0800 Subject: [PATCH 1/3] Refactor Cloud Function to use single entry point oauthHandler --- cloud_function/index.js | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/cloud_function/index.js b/cloud_function/index.js index aae88b61..383d1805 100644 --- a/cloud_function/index.js +++ b/cloud_function/index.js @@ -50,9 +50,11 @@ async function getClientSecret() { } /** - * HTTP Cloud Function that handles the OAuth 2.0 redirect. + * Handles the OAuth 2.0 callback. + * @param {Object} req Express request object. + * @param {Object} res Express response object. */ -functions.http('oauthCallback', async (req, res) => { +async function handleCallback(req, res) { const code = req.query.code; const state = req.query.state; // The state is the base64 encoded local redirect URI console.log(`Received request with code: ${code ? 'present' : 'missing'}`); @@ -253,13 +255,15 @@ functions.http('oauthCallback', async (req, res) => { } res.status(500).send('An error occurred during the token exchange. Check function logs for details.'); } -}); +} /** - * HTTP Cloud Function that handles token refresh. + * Handles token refresh. * Accepts a refresh_token and returns a new access_token. + * @param {Object} req Express request object. + * @param {Object} res Express response object. */ -functions.http('refreshToken', async (req, res) => { +async function handleRefreshToken(req, res) { // Only accept POST requests if (req.method !== 'POST') { console.error('Invalid method for refreshToken:', req.method); @@ -310,4 +314,23 @@ functions.http('refreshToken', async (req, res) => { res.status(500).send('An error occurred during token refresh.'); } } +} + +/** + * Main entry point for the Cloud Function. + * Routes requests to either the callback handler or the refresh handler. + */ +functions.http('oauthHandler', async (req, res) => { + // Route to refresh handler if path ends with /refresh or /refreshToken or it's a POST with refresh_token + if (req.path === '/refresh' || req.path === '/refreshToken' || (req.method === 'POST' && req.body && req.body.refresh_token)) { + return handleRefreshToken(req, res); + } + + // Route to callback handler if path ends with /callback or /oauth2callback or has 'code' query param + if (req.path === '/callback' || req.path === '/oauth2callback' || req.query.code) { + return handleCallback(req, res); + } + + // Default/Error case + res.status(400).send('Unknown request type. Expected OAuth callback or token refresh request.'); }); From f0d6bdd2705951fa8cf10fe244f27f172fc26dad Mon Sep 17 00:00:00 2001 From: Allen Hutchison Date: Fri, 14 Nov 2025 12:53:53 -0800 Subject: [PATCH 2/3] Update cloud_function/index.js Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- cloud_function/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloud_function/index.js b/cloud_function/index.js index 383d1805..4cbb91fd 100644 --- a/cloud_function/index.js +++ b/cloud_function/index.js @@ -322,7 +322,7 @@ async function handleRefreshToken(req, res) { */ functions.http('oauthHandler', async (req, res) => { // Route to refresh handler if path ends with /refresh or /refreshToken or it's a POST with refresh_token - if (req.path === '/refresh' || req.path === '/refreshToken' || (req.method === 'POST' && req.body && req.body.refresh_token)) { + if (['/refresh', '/refreshToken'].includes(req.path) || (req.method === 'POST' && req.body?.refresh_token)) { return handleRefreshToken(req, res); } From 4f892223cbb5efc500fd1980a59b0d16065c056c Mon Sep 17 00:00:00 2001 From: Allen Hutchison Date: Fri, 14 Nov 2025 12:54:01 -0800 Subject: [PATCH 3/3] Update cloud_function/index.js Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- cloud_function/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloud_function/index.js b/cloud_function/index.js index 4cbb91fd..22e3ec0e 100644 --- a/cloud_function/index.js +++ b/cloud_function/index.js @@ -327,7 +327,7 @@ functions.http('oauthHandler', async (req, res) => { } // Route to callback handler if path ends with /callback or /oauth2callback or has 'code' query param - if (req.path === '/callback' || req.path === '/oauth2callback' || req.query.code) { + if (['/callback', '/oauth2callback'].includes(req.path) || req.query.code) { return handleCallback(req, res); }