oficina / estilo Public

Clone
git clone https://amadan.net/oficina/estilo

Plain git — no account needed to clone.

Download

Download this file

1# Google Drive import
2
3This optional component connects to Google Identity Services, Google Picker
4and the Google Drive API on the import page. It exports one selected native
5Google document or spreadsheet into browser memory and submits it through the
6app's ordinary file upload form. The original Google file is unchanged.
7
8The Google access token is held only in page memory and sent only in the
9Authorization header to Google's fixed API origin. It is never uploaded to
10Oficina, persisted in browser storage, or written to a server database. No
11background synchronization or refresh token is involved. Google's authorization
12grant can outlive the page; people can remove it from their Google account's
13third-party connections. The requested `drive.file` scope grants per-file access
14and can authorize modifications, but this component only calls the export API.
15
16JavaScript is required for the chooser. With scripts disabled, a note explains
17how to download from Google and use the app's ordinary file form. Configuring
18none of the public Google fields hides the component entirely; partial or
19malformed configuration must refuse app startup through `Config.Validate`.
20
21## Google Cloud setup
22
231. Create a Google Cloud project and enable **Google Drive API** and **Google
24 Picker API**.
252. Configure the OAuth application's branding, audience and support information.
26 Request only `https://www.googleapis.com/auth/drive.file`. This scope is
27 non-sensitive. Follow the console's publishing requirements; while in
28 Testing, add the accounts that will exercise consent as test users.
293. Create an OAuth **Web application** client. Add each app's exact HTTPS
30 origin to its authorized JavaScript origins, including the canary used for
31 verification. Local development can use its exact localhost origin and port.
32 Google does not accept a wildcard for arbitrary tenant subdomains; register
33 each deployed origin. This token flow does not use an OAuth redirect endpoint.
344. Create a browser API key in the same project. Restrict it to Google Picker
35 API and Google Drive API. Its website restrictions must include the app's
36 origin (`https://example.test/*`) and `https://docs.google.com/*`, where
37 Picker's iframe runs. The key is public client configuration.
385. Record the project's numeric **project number**, not its project name or ID.
39 It must be the OAuth client's project and is passed to Picker's `setAppId`;
40 mismatches can make valid selections fail to export.
416. Set all three app settings together:
42
43 | Setting | Docs | Sheets |
44 | --- | --- | --- |
45 | OAuth client ID | `DOCS_GOOGLE_CLIENT_ID` | `SHEETS_GOOGLE_CLIENT_ID` |
46 | Public API key | `DOCS_GOOGLE_API_KEY` | `SHEETS_GOOGLE_API_KEY` |
47 | Project number | `DOCS_GOOGLE_APP_ID` | `SHEETS_GOOGLE_APP_ID` |
48
49There is no client secret setting. Leave production Google configuration unset
50until the real consent and import checks below have succeeded on an authorized
51canary origin.
52
53## App integration
54
55Pass `Config` through the app's config and validate at startup. Merge
56`Templates()` with the app templates (glob `templates/*.html`), then render
57`estilo-google-import` with `View{Config, Kind: Docs/Sheets, FormID, InputID,
58Labels: Text(locale)}`. The file input must belong to the named same-origin
59multipart POST form. Existing hidden fields, including creation tokens, remain
60intact. The app owns permissions, CSRF, upload limits, conversion and reports.
61
62Serve `CSS()` as a stylesheet and link it on the page that renders the
63chooser; it spaces the chooser's disclosure, buttons and status on the spacing
64scale. Remove any `[estilo-google-import]` rules of the app's own when you link
65it: a column gap of the app's would add to its margins. It has no hashed name,
66so serve and cache it as the app serves its other estilo stylesheets; the
67immutable caching below is the script's. Serve `JS()` as
68JavaScript at `JSName()` and load it with a module script only
69on configured import pages. The name contains the asset's hash and may be
70cached as immutable. Call `Headers(w, ContentSecurityPolicy)` before rendering
71the opted-in import page, including a refused POST's re-render. Handle errors
72before writing a response. This sets one expanded policy, preserves unrelated
73directives, and sets no-store. It adds no COOP header. Both current apps already
74allow inline styles; this component adds neither inline-script nor eval access.
75
76The scripts come directly from Google as Google requires; no CDN library is
77vendored or bundled. Loading waits until prerendered pages are activated.
78Picker is filtered by native MIME type, and returned shortcuts/other types
79are rejected again. Download uses a fixed URL built from a validated file ID,
80refuses redirects, and checks actual streamed bytes against `MaxExportBytes`
81(10 MiB). Google's export API also applies its own size limit. Files handed to
82the upload form use the chosen name with path separators removed and `.docx`
83or `.xlsx` appended. Never treat that name or MIME type as server validation.
84
85## Verification
86
87`go test ./googleimport` runs config/policy/template checks and the Node tests
88for fixed-origin export, bounded streaming, scope-independent response errors,
89and MIME/ID validation. `go test -tags browser ./googleimport` additionally
90exercises Chromium and WebKit through real headers, using controlled Google
91responses. Point `ESTILO_PLAYWRIGHT` at an installed Playwright package if it
92is not resolvable here. `ESTILO_BROWSERS` defaults to `chromium,webkit`.
93
94Mocked responses prove the integration's behavior, not real account consent.
95Before enabling a production origin, a person must connect a permitted Google
96account on the configured canary, select a real Google Doc and Sheet, verify
97their editable copies and omission reports, and retry after cancelling consent.
98Keep representative exports as regression fixtures only with permission to
99commit their contents. Real Google libraries were separately checked to load
100and create a Picker frame under the proposed CSP; that also is not proof of
101live consent or export with a real project.
102
103Google references: [token model](https://developers.google.com/identity/oauth2/web/guides/use-token-model),
104[Picker setup](https://developers.google.com/workspace/drive/picker/guides/web-picker),
105[exports](https://developers.google.com/workspace/drive/api/reference/rest/v3/files/export).
106