# Google Drive import

This optional component connects to Google Identity Services, Google Picker
and the Google Drive API on the import page. It exports one selected native
Google document or spreadsheet into browser memory and submits it through the
app's ordinary file upload form. The original Google file is unchanged.

The Google access token is held only in page memory and sent only in the
Authorization header to Google's fixed API origin. It is never uploaded to
Oficina, persisted in browser storage, or written to a server database. No
background synchronization or refresh token is involved. Google's authorization
grant can outlive the page; people can remove it from their Google account's
third-party connections. The requested `drive.file` scope grants per-file access
and can authorize modifications, but this component only calls the export API.

JavaScript is required for the chooser. With scripts disabled, a note explains
how to download from Google and use the app's ordinary file form. Configuring
none of the public Google fields hides the component entirely; partial or
malformed configuration must refuse app startup through `Config.Validate`.

## Google Cloud setup

1. Create a Google Cloud project and enable **Google Drive API** and **Google
   Picker API**.
2. Configure the OAuth application's branding, audience and support information.
   Request only `https://www.googleapis.com/auth/drive.file`. This scope is
   non-sensitive. Follow the console's publishing requirements; while in
   Testing, add the accounts that will exercise consent as test users.
3. Create an OAuth **Web application** client. Add each app's exact HTTPS
   origin to its authorized JavaScript origins, including the canary used for
   verification. Local development can use its exact localhost origin and port.
   Google does not accept a wildcard for arbitrary tenant subdomains; register
   each deployed origin. This token flow does not use an OAuth redirect endpoint.
4. Create a browser API key in the same project. Restrict it to Google Picker
   API and Google Drive API. Its website restrictions must include the app's
   origin (`https://example.test/*`) and `https://docs.google.com/*`, where
   Picker's iframe runs. The key is public client configuration.
5. Record the project's numeric **project number**, not its project name or ID.
   It must be the OAuth client's project and is passed to Picker's `setAppId`;
   mismatches can make valid selections fail to export.
6. Set all three app settings together:

   | Setting | Docs | Sheets |
   | --- | --- | --- |
   | OAuth client ID | `DOCS_GOOGLE_CLIENT_ID` | `SHEETS_GOOGLE_CLIENT_ID` |
   | Public API key | `DOCS_GOOGLE_API_KEY` | `SHEETS_GOOGLE_API_KEY` |
   | Project number | `DOCS_GOOGLE_APP_ID` | `SHEETS_GOOGLE_APP_ID` |

There is no client secret setting. Leave production Google configuration unset
until the real consent and import checks below have succeeded on an authorized
canary origin.

## App integration

Pass `Config` through the app's config and validate at startup. Merge
`Templates()` with the app templates (glob `templates/*.html`), then render
`estilo-google-import` with `View{Config, Kind: Docs/Sheets, FormID, InputID,
Labels: Text(locale)}`. The file input must belong to the named same-origin
multipart POST form. Existing hidden fields, including creation tokens, remain
intact. The app owns permissions, CSRF, upload limits, conversion and reports.

Serve `CSS()` as a stylesheet and link it on the page that renders the
chooser; it spaces the chooser's disclosure, buttons and status on the spacing
scale. Remove any `[estilo-google-import]` rules of the app's own when you link
it: a column gap of the app's would add to its margins. It has no hashed name,
so serve and cache it as the app serves its other estilo stylesheets; the
immutable caching below is the script's. Serve `JS()` as
JavaScript at `JSName()` and load it with a module script only
on configured import pages. The name contains the asset's hash and may be
cached as immutable. Call `Headers(w, ContentSecurityPolicy)` before rendering
the opted-in import page, including a refused POST's re-render. Handle errors
before writing a response. This sets one expanded policy, preserves unrelated
directives, and sets no-store. It adds no COOP header. Both current apps already
allow inline styles; this component adds neither inline-script nor eval access.

The scripts come directly from Google as Google requires; no CDN library is
vendored or bundled. Loading waits until prerendered pages are activated.
Picker is filtered by native MIME type, and returned shortcuts/other types
are rejected again. Download uses a fixed URL built from a validated file ID,
refuses redirects, and checks actual streamed bytes against `MaxExportBytes`
(10 MiB). Google's export API also applies its own size limit. Files handed to
the upload form use the chosen name with path separators removed and `.docx`
or `.xlsx` appended. Never treat that name or MIME type as server validation.

## Verification

`go test ./googleimport` runs config/policy/template checks and the Node tests
for fixed-origin export, bounded streaming, scope-independent response errors,
and MIME/ID validation. `go test -tags browser ./googleimport` additionally
exercises Chromium and WebKit through real headers, using controlled Google
responses. Point `ESTILO_PLAYWRIGHT` at an installed Playwright package if it
is not resolvable here. `ESTILO_BROWSERS` defaults to `chromium,webkit`.

Mocked responses prove the integration's behavior, not real account consent.
Before enabling a production origin, a person must connect a permitted Google
account on the configured canary, select a real Google Doc and Sheet, verify
their editable copies and omission reports, and retry after cancelling consent.
Keep representative exports as regression fixtures only with permission to
commit their contents. Real Google libraries were separately checked to load
and create a Picker frame under the proposed CSP; that also is not proof of
live consent or export with a real project.

Google references: [token model](https://developers.google.com/identity/oauth2/web/guides/use-token-model),
[Picker setup](https://developers.google.com/workspace/drive/picker/guides/web-picker),
[exports](https://developers.google.com/workspace/drive/api/reference/rest/v3/files/export).
