// Copyright 2025 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. import { escapeHtml } from './sanitize.js'; /** * Renders the Google Sign-In button using the GIS library. * @param {string} toolId The ID of the tool. * @param {string} clientId The Google OAuth Client ID. * @param {string} authProfileName The name of the auth service in tools file. */ function renderGoogleSignInButton(toolId, clientId, authProfileName) { const UNIQUE_ID_BASE = `${toolId}-${authProfileName}`; const GIS_CONTAINER_ID = `gisContainer-${UNIQUE_ID_BASE}`; const gisContainer = document.getElementById(GIS_CONTAINER_ID); const setupGisBtn = document.querySelector(`#google-auth-details-${UNIQUE_ID_BASE} .btn--setup-gis`); if (!gisContainer) { console.error('GIS container not found:', GIS_CONTAINER_ID); return; } if (!clientId) { alert('Please enter an OAuth Client ID first.'); return; } // hide the setup button and show the container for the GIS button if (setupGisBtn) setupGisBtn.style.display = 'none'; gisContainer.innerHTML = ''; gisContainer.style.display = 'flex'; if (window.google && window.google.accounts && window.google.accounts.id) { try { const handleResponse = (response) => handleCredentialResponse(response, toolId, authProfileName); window.google.accounts.id.initialize({ client_id: clientId, callback: handleResponse, auto_select: false }); window.google.accounts.id.renderButton( gisContainer, { theme: "outline", size: "large", text: "signin_with" } ); } catch (error) { console.error("Error initializing Google Sign-In:", error); alert("Error initializing Google Sign-In. Check the Client ID and browser console."); gisContainer.innerHTML = '
Error loading Sign-In button.
'; if (setupGisBtn) setupGisBtn.style.display = ''; } } else { console.error("GIS library not fully loaded yet."); alert("Google Identity Services library not ready. Please try again in a moment."); gisContainer.innerHTML = 'GIS library not ready.
'; if (setupGisBtn) setupGisBtn.style.display = ''; } } /** * Handles the credential response from the Google Sign-In library. * @param {!CredentialResponse} response The credential response object from GIS. * @param {string} toolId The ID of the tool. * @param {string} authProfileName The name of the auth service in tools file. */ function handleCredentialResponse(response, toolId, authProfileName) { console.debug("handleCredentialResponse called with:", { response, toolId, authProfileName }); const headersTextarea = document.getElementById(`headers-textarea-${toolId}`); if (!headersTextarea) { console.error('Headers textarea not found for toolId:', toolId); return; } const UNIQUE_ID_BASE = `${toolId}-${authProfileName}`; const setupGisBtn = document.querySelector(`#google-auth-details-${UNIQUE_ID_BASE} .setup-gis-btn`); const gisContainer = document.getElementById(`gisContainer-${UNIQUE_ID_BASE}`); if (response.credential) { const idToken = response.credential; try { let currentHeaders = {}; if (headersTextarea.value) { currentHeaders = JSON.parse(headersTextarea.value); } const HEADER_KEY = `${authProfileName}_token`; currentHeaders[HEADER_KEY] = `${idToken}`; headersTextarea.value = JSON.stringify(currentHeaders, null, 2); if (gisContainer) gisContainer.style.display = 'none'; if (setupGisBtn) setupGisBtn.style.display = ''; } catch (e) { alert('Headers are not valid JSON. Please correct and try again.'); console.error("Header JSON parse error:", e); } } else { console.error("Error: No credential in response", response); alert('Error: No ID Token received. Check console for details.'); if (gisContainer) gisContainer.style.display = 'none'; if (setupGisBtn) setupGisBtn.style.display = ''; } } // creates the Google Auth method dropdown export function createGoogleAuthMethodItem(toolId, authProfileName) { const safeProfileName = escapeHtml(authProfileName); const UNIQUE_ID_BASE = `${toolId}-${authProfileName}`; const item = document.createElement('div'); item.className = 'auth-method-item'; item.innerHTML = `